Hi everyone , I have an edit control and 2 APIs for saving text from and loading text to the edit respectively.

However, when I load text, instead of giving out the exact alphabets in the text, it gives chinese!! Please, I need help to tackle this problem,any psitive help will be greatly appreciated.

#pragma comment(lib, "comctl32.lib")
#include <windows.h>
#include<cstdlib>
#include<cstring>
#include<tchar.h>
#include <commctrl.h>
#include "resource.h"
#define IDC_MAIN_EDIT	101
#define IDC_MAIN_TOOL	102
#define IDC_MAIN_STATUS	103

BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName)
{
	HANDLE hFile;
	BOOL bSuccess = FALSE;

	hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
		OPEN_EXISTING, 0, NULL);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		DWORD dwFileSize;

		dwFileSize = GetFileSize(hFile, NULL);
		if(dwFileSize != 0xFFFFFFFF)
		{
			LPWSTR pszFileText;

			pszFileText = (LPWSTR)GlobalAlloc(GPTR, dwFileSize + 1);
			if(pszFileText != NULL)
			{
				DWORD dwRead;

				if(ReadFile(hFile, (LPVOID)pszFileText, dwFileSize, &dwRead, NULL))
				{
					pszFileText[dwFileSize] = 0; // Add null terminator
					if(SetWindowText(hEdit, pszFileText))
						bSuccess = TRUE; // It worked!
				}
				GlobalFree(pszFileText);
			}
		}
		CloseHandle(hFile);
	}
	return bSuccess;
}

BOOL SaveTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName)
{
	HANDLE hFile;
	BOOL bSuccess = FALSE;

	hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
		CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		DWORD dwTextLength;

		dwTextLength = GetWindowTextLength(hEdit);
		// No need to bother if there's no text.
		if(dwTextLength > 0)
		{
			LPWSTR pszText;
			DWORD dwBufferSize = dwTextLength + 1;

			pszText = (LPWSTR)GlobalAlloc(GPTR, dwBufferSize);
			if(pszText != NULL)
			{
				if(GetWindowText(hEdit, pszText, dwBufferSize))
				{
					DWORD dwWritten;

					if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
						bSuccess = TRUE;
				}
				GlobalFree(pszText);
			}
		}
		CloseHandle(hFile);
	}
	return bSuccess;
}

void DoFileOpen(HWND hwnd)
{
	OPENFILENAME ofn;
	static TCHAR szFileName[MAX_PATH] = _T("");

	ZeroMemory(&ofn, sizeof(ofn));

	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFilter = _T("Text Files (*.txt)\0*.txt\0Tkud Files(*.tkud)\0*.tkud\0All Files (*.*)\0*.*\0");
	ofn.lpstrFile = szFileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
	ofn.lpstrDefExt = _T("txt");

	if(GetOpenFileName(&ofn))
	{
		HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
		if(LoadTextFileToEdit(hEdit, szFileName))
		{
			SendDlgItemMessage(hwnd, IDC_MAIN_STATUS, SB_SETTEXT, 0, (LPARAM)"Opened...");
			SendDlgItemMessage(hwnd, IDC_MAIN_STATUS, SB_SETTEXT, 1, (LPARAM)szFileName);
		}
	}
}

void DoFileSave(HWND hwnd)
{
	OPENFILENAME ofn;
	static TCHAR szFileName[MAX_PATH] = _T("");

	ZeroMemory(&ofn, sizeof(ofn));

	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFilter = _T("Text Files (*.txt)\0*.txt\0Tkud Files(*.tkud)\0*.tkud\0All Files (*.*)\0*.*\0");
	ofn.lpstrFile = szFileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.lpstrDefExt = _T("txt");
	ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

	if(GetSaveFileName(&ofn))
	{
		HWND hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
		if(SaveTextFileFromEdit(hEdit, szFileName))
		{
			SendDlgItemMessage(hwnd, IDC_MAIN_STATUS, SB_SETTEXT, 0, (LPARAM)"Saved...");
			SendDlgItemMessage(hwnd, IDC_MAIN_STATUS, SB_SETTEXT, 1, (LPARAM)szFileName);
		}
	}
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_CREATE:
		{
			HFONT hfDefault;
			HWND hEdit;

			HWND hTool;
			TBBUTTON tbb[4];
			TBADDBITMAP tbab;

			HWND hStatus;
			int statwidths[] = {100,59, -1};

			// Create Edit Control

			hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, _T("EDIT"), _T(""), 
				WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 
				0, 0, 100, 100, hwnd, (HMENU)IDC_MAIN_EDIT, GetModuleHandle(NULL), NULL);
			if(hEdit == NULL)
				MessageBox(hwnd, _T("Could not create edit box."), _T("Error"), MB_OK | MB_ICONERROR);

			hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
			SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));

			// Create Toolbar

			hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
				hwnd, (HMENU)IDC_MAIN_TOOL, GetModuleHandle(NULL), NULL);
			if(hTool == NULL)
				MessageBox(hwnd, _T("Could not create tool bar."), _T("Error"), MB_OK | MB_ICONERROR);

			// Send the TB_BUTTONSTRUCTSIZE message, which is required for
			// backward compatibility.
			SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
			
			tbab.hInst = HINST_COMMCTRL;
			tbab.nID = IDB_STD_SMALL_COLOR;
			SendMessage(hTool, TB_ADDBITMAP, 0, (LPARAM)&tbab);

			ZeroMemory(tbb, sizeof(tbb));
			tbb[0].iBitmap = STD_FILENEW;
			tbb[0].fsState = TBSTATE_ENABLED;
			tbb[0].fsStyle = TBSTYLE_BUTTON;
			tbb[0].idCommand = ID_FILE_NEW;

			tbb[1].iBitmap = STD_FILEOPEN;
			tbb[1].fsState = TBSTATE_ENABLED;
			tbb[1].fsStyle = TBSTYLE_BUTTON;
			tbb[1].idCommand = ID_FILE_OPEN;

			tbb[2].iBitmap = STD_FILESAVE;
			tbb[2].fsState = TBSTATE_ENABLED;
			tbb[2].fsStyle = TBSTYLE_BUTTON;
			tbb[2].idCommand = ID_FILE_SAVEAS;

			tbb[3].iBitmap= STD_DELETE;
			tbb[3].fsState=TBSTATE_ENABLED;
			tbb[3].fsStyle=TBSTYLE_BUTTON;
			tbb[3].idCommand=ID_FILE_DELETE;

			SendMessage(hTool, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM)&tbb);

			// Create Status bar

			hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
				WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
				hwnd, (HMENU)IDC_MAIN_STATUS, GetModuleHandle(NULL), NULL);

			SendMessage(hStatus, SB_SETPARTS, sizeof(statwidths)/sizeof(int), (LPARAM)statwidths);
			SetWindowText(hStatus,_T("You no dey feel my app?"));
		}
		break;
		case WM_SIZE:
		{
			HWND hTool;
			RECT rcTool;
			int iToolHeight;

			HWND hStatus;
			RECT rcStatus;
			int iStatusHeight;

			HWND hEdit;
			int iEditHeight;
			RECT rcClient;

			// Size toolbar and get height

			hTool = GetDlgItem(hwnd, IDC_MAIN_TOOL);
			SendMessage(hTool, TB_AUTOSIZE, 0, 0);

			GetWindowRect(hTool, &rcTool);
			iToolHeight = rcTool.bottom - rcTool.top;

			// Size status bar and get height

			hStatus = GetDlgItem(hwnd, IDC_MAIN_STATUS);
			SendMessage(hStatus, WM_SIZE, 0, 0);

			GetWindowRect(hStatus, &rcStatus);
			iStatusHeight = rcStatus.bottom - rcStatus.top;

			// Calculate remaining height and size edit

			GetClientRect(hwnd, &rcClient);

			iEditHeight = rcClient.bottom - iToolHeight - iStatusHeight;

			hEdit = GetDlgItem(hwnd, IDC_MAIN_EDIT);
			SetWindowPos(hEdit, NULL, 0, iToolHeight, rcClient.right, iEditHeight, SWP_NOZORDER);
		}
		break;
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case ID_FILE_EXIT:
					PostMessage(hwnd, WM_CLOSE, 0, 0);
				break;
				case ID_FILE_NEW:
					SetDlgItemText(hwnd, IDC_MAIN_EDIT, _T(""));
				break;
				case ID_FILE_OPEN:
					DoFileOpen(hwnd);
				break;
				case ID_FILE_SAVEAS:
					DoFileSave(hwnd);
				break;
			}
		break;
		default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;
    INITCOMMONCONTROLSEX icex;
    icex.dwSize=sizeof(INITCOMMONCONTROLSEX);
	icex.dwICC=ICC_BAR_CLASSES;
   InitCommonControls();

	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = 0;
	wc.lpfnWndProc	 = WndProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MAINMENU);
	wc.lpszClassName = _T("My window class");
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL, _T("Window Registration Failed!"), _T("Error!"),
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(
		0,
		_T("My window class"),
		_T("Tkud's  Application"),
		WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
		CW_USEDEFAULT, CW_USEDEFAULT, 480, 320,
		NULL, NULL, hInstance, NULL);

	if(hwnd == NULL)
	{
		MessageBox(NULL, _T("Window Creation Failed!"), _T("Error!"),
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}

The code seems to have all the functionality within it for opening files. Have you tried opening a file that doesn't contain Chinese?

Just out of curiosity, I would like you to rebuild this code on your machine again with a call to GetTextMetrics().

We can then examine the last attribute of the TEXTMETRIC struct to determine the character set of the font.

if tmCharSet == 136 you are in Chinese.

Edited 6 Years Ago by Clinton Portis: 我需要更多啤酒。

I'm sorry,portis, i'm lost.

You appear fluent in your windows programming.. I'm surprised you haven't messed around with the getting the current text attributes:

HDC hdc;
TEXTMETRIC tm;
string msg = "Current Char Set is: ";

hdc = GetDC(hwnd);

GetTextMetrics(hdc, &tm);

msg += static_cast<char>(tm.tmCharSet+'0');

MessageBox(hwnd, msg.c_str(), "Char Set:", MB_OK);

Edited 6 Years Ago by Clinton Portis: coders be mad trippin' up in herr.

As I have stated before, I am learning by myself, so It's possible that i missed that. Thank you for your compliment and contribution, portis, i really appreciate it,it has renewed my zeal to continue.
However, i still do not know how to implement GetTextMetrics() to suit my needs.
I want to change the output from Chinese to English. How do I do that?

To be honest, I'm not really sure what is going on with your machine; however, I do believe that if we can gather the current state of your font and text settings, maybe we could come up with some ideas.

I would like to determine what charset your machine is using.. maybe it would give us a little insight as to what is going on.

All I am asking is that you compile the above code either by itself or you can also add to your program at various points.

Here is another function we could consider in our efforts to get a peek inside of your machine:

GetFontLanguageInfo() Function

The GetFontLanguageInfo function returns information about the currently selected font for the specified display context. Applications typically use this information and the GetCharacterPlacement function to prepare a character string for display.

Syntax

DWORD GetFontLanguageInfo(
  __in  HDC hdc
);

Parameters
hdc [in]
Handle to a display device context.

Return Value
The return value identifies characteristics of the currently selected font. The function returns 0 if the font is "normalized" and can be treated as a simple Latin font; it returns GCP_ERROR if an error occurs. Otherwise, the function returns a combination of the following values.

Edited 6 Years Ago by Clinton Portis: #include&lt;windows.h&gt;

You guys should please help me, any text i open with my edit control is in Chinese,meanwhile the text is originally in English.
I need someone to really help me, i cannot continue to the next chapter of my bok until i understand the one i just finished.

Here is a whole program implementing Clinton Portis's suggestion of getting the char set out of the TEXTMETRIC struct. Just run the program and post the results here of what was on the screen. Also, it prints the output to a text file in whatever dir you run it from (Output.txt))...

Main.cpp
#include <windows.h>
#include <stdio.h>

typedef struct    WindowsEventArguments               //Package Window Procedure Parameters into structure
{
 HWND             hWnd;                               //Handle of Window
 WPARAM           wParam;                             //Window Parameter
 LPARAM           lParam;                             //Long Parameter
 HINSTANCE        hIns;                               //Instance Handle (Resolves To Process Address)
}WndEventArgs, *lpWndEventArgs;


long fnWndProc_OnCreate(lpWndEventArgs Wea)           //Called One Time During Window Creatiion
{                                                     //This corresponds to Form_Load() in Visual Basic.
 Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;  //lParam is pointer to CREATESTRUCT in WM_CREATE
 return 0;                                            //Handles WM_CREATE message.
}


long fnWndProc_OnPaint(lpWndEventArgs Wea)
{
 char szBuffer[64],szText[8],szCharSet[16];
 PAINTSTRUCT ps;
 TEXTMETRIC tm;
 FILE* fp=NULL;
 HDC hDC;

 fp=fopen("Output.txt","w");
 fprintf(fp,"Entering fnWndProc_OnPaint()\n");
 hDC=BeginPaint(Wea->hWnd,&ps);
 SetBkMode(hDC,TRANSPARENT);
 GetTextMetrics(hDC,&tm);
 strcpy(szBuffer,"tm.tmHeight = ");
 sprintf(szText,"%u",(unsigned int)tm.tmHeight);
 strcat(szBuffer,szText);
 TextOut(hDC,0,0,szBuffer,strlen(szBuffer));
 fprintf(fp,"  tm.tmHeight = %u\n",(unsigned int)tm.tmHeight);
 sprintf(szText,"%u",(unsigned int)tm.tmAveCharWidth);
 strcpy(szBuffer,"tm.tmAveCharWidth = ");
 strcat(szBuffer,szText);
 TextOut(hDC,0,20,szBuffer,strlen(szBuffer));
 fprintf(fp,"  tm.tmAveCharWidth = %u\n",(unsigned int)tm.tmAveCharWidth);
 strcpy(szBuffer,"tm.tmCharSet = ");
 sprintf(szText,"%u",tm.tmCharSet);
 strcat(szBuffer,szText);
 TextOut(hDC,0,40,szBuffer,strlen(szBuffer));
 fprintf(fp,"  tm.tmCharSet = %u\n",(unsigned int)tm.tmCharSet);
 switch(tm.tmCharSet)
 {
  case ANSI_CHARSET:
    strcpy(szCharSet,"ANSI_CHARSET");
    break;
  case DEFAULT_CHARSET:
    strcpy(szCharSet,"DEFAULT_CHARSET");
    break;
  case SYMBOL_CHARSET:
    strcpy(szCharSet,"SYMBOL_CHARSET");
    break;
  case SHIFTJIS_CHARSET:
    strcpy(szCharSet,"SHIFTJIS_CHARSET");
    break;
  case HANGEUL_CHARSET:
    strcpy(szCharSet,"HANGEUL_CHARSET");
    break;
  case CHINESEBIG5_CHARSET:
    strcpy(szCharSet,"CHINESEBIG5_CHARSET");
    break;
  case OEM_CHARSET:
    strcpy(szCharSet,"OEM_CHARSET");
    break;
  case JOHAB_CHARSET:
    strcpy(szCharSet,"JOHAB_CHARSET");
    break;
  case HEBREW_CHARSET:
    strcpy(szCharSet,"HEBREW_CHARSET");
    break;
  case ARABIC_CHARSET:
    strcpy(szCharSet,"ARABIC_CHARSET");
    break;
  case GREEK_CHARSET:
    strcpy(szCharSet,"GREEK_CHARSET");
    break;
  case TURKISH_CHARSET:
    strcpy(szCharSet,"TURKISH_CHARSET");
    break;
  case THAI_CHARSET:
    strcpy(szCharSet,"THAI_CHARSET");
    break;
  case EASTEUROPE_CHARSET:
    strcpy(szCharSet,"EASTEUROPE_CHARSET");
    break;
  case RUSSIAN_CHARSET:
    strcpy(szCharSet,"RUSSIAN_CHARSET");
    break;
  default:
    strcpy(szCharSet,"Unknown");
    break;
 }
 TextOut(hDC,0,60,szCharSet,strlen(szCharSet));
 fprintf(fp,"  tm.tmCharSet = %s\n",szCharSet);
 EndPaint(Wea->hWnd,&ps);
 fprintf(fp,"Leaving fnWndProc_OnPaint()\n");
 fclose(fp);

 return 0;
}


long fnWndProc_OnClose(lpWndEventArgs Wea)            //This function handles the WM_CLOSE message
{                                                     //sent when the 'x' button is clicked.
 if(MessageBox(Wea->hWnd,"Do You Wish To Exit This App?","Exit Check?",MB_YESNO)==IDYES)
 {
  DestroyWindow(Wea->hWnd);
  PostQuitMessage(0);
 }

 return 0;
}


long __stdcall fnWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
 static WndEventArgs wea;

 switch (msg)
 {
  case WM_CREATE:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnCreate(&wea);
  case WM_PAINT:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnPaint(&wea);
  case WM_CLOSE:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnClose(&wea);
 }

 return DefWindowProc(hwnd,msg,wParam,lParam);
}


int __stdcall WinMain(HINSTANCE hIns,HINSTANCE hPrevIns,LPSTR lpszArgument,int iShow)
{                                  //The program starts in WinMain().  The WNDCLASSEX structure variable
 char szClassName[]="Form1";       //wc is filled out with general characteristics of a window.  Then
 WNDCLASSEX wc;                    //the class is RegisteredClassEx()'ed.  Directly after that a
 MSG messages;                     //CreateWindow() call instantiates an instance of the class.  Then
 HWND hWnd;                        //the program drops into a message processing loop in which any messages
                                   //destined for this program are DispatchMessage()'ed to the fnWndProc().
 wc.lpszClassName=szClassName;                wc.lpfnWndProc=fnWndProc;
 wc.cbSize=sizeof (WNDCLASSEX);               wc.style=CS_DBLCLKS;
 wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);     wc.hInstance=hIns;
 wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION);  wc.hCursor=LoadCursor(NULL,IDC_ARROW);
 wc.hbrBackground=(HBRUSH)COLOR_BACKGROUND;   wc.cbWndExtra=0;
 wc.lpszMenuName=NULL;                        wc.cbClsExtra=0;
 RegisterClassEx(&wc);
 hWnd=CreateWindow(szClassName,"Form1",WS_OVERLAPPEDWINDOW,200,100,400,350,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
  TranslateMessage(&messages);
  DispatchMessage(&messages);
 }

 return messages.wParam;
}

Edited 6 Years Ago by Frederick2: n/a

I simply failed to note you were using UNICODE and the TCHAR.h macros, so here is a TCHAR version with pre-processor definitions to quiet warnings. For the above program to run in VC9 Express you would need to set the char set to 'Not Set'.

//Project Properties >> Configuration Properties >> General >> "Use Unicode Character Set"
#include <windows.h>  //Paste In Pre-Processor Definitions...
#include <tchar.h>    //;_CRT_SECURE_NO_WARNINGS;_CRT_NON_CONFORMING_SWPRINTFS
#include <stdio.h>

typedef struct    WindowsEventArguments               //Package Window Procedure Parameters into structure
{
 HWND             hWnd;                               //Handle of Window
 WPARAM           wParam;                             //Window Parameter
 LPARAM           lParam;                             //Long Parameter
 HINSTANCE        hIns;                               //Instance Handle (Resolves To Process Address)
}WndEventArgs, *lpWndEventArgs;


long fnWndProc_OnCreate(lpWndEventArgs Wea)           //Called One Time During Window Creatiion
{                                                     //This corresponds to Form_Load() in Visual Basic.
 Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;  //lParam is pointer to CREATESTRUCT in WM_CREATE
 return 0;                                            //Handles WM_CREATE message.
}


long fnWndProc_OnPaint(lpWndEventArgs Wea)
{
 TCHAR szBuffer[64],szText[8],szCharSet[16];
 PAINTSTRUCT ps;
 TEXTMETRIC tm;
 FILE* fp=NULL;
 HDC hDC;

 fp=_tfopen(_T("Output.txt"),_T("w"));
 _ftprintf(fp,_T("Entering fnWndProc_OnPaint()\n"));
 hDC=BeginPaint(Wea->hWnd,&ps);
 SetBkMode(hDC,TRANSPARENT);
 GetTextMetrics(hDC,&tm);
 _tcscpy(szBuffer,_T("tm.tmHeight = "));
 _stprintf(szText,_T("%u"),(unsigned int)tm.tmHeight);
 _tcscat(szBuffer,szText);
 TextOut(hDC,0,0,szBuffer,_tcslen(szBuffer));
 _ftprintf(fp,_T("  tm.tmHeight = %u\n"),(unsigned int)tm.tmHeight);
 _stprintf(szText,_T("%u"),(unsigned int)tm.tmAveCharWidth);
 _tcscpy(szBuffer,_T("tm.tmAveCharWidth = "));
 _tcscat(szBuffer,szText);
 TextOut(hDC,0,20,szBuffer,_tcslen(szBuffer));
 _ftprintf(fp,_T("  tm.tmAveCharWidth = %u\n"),(unsigned int)tm.tmAveCharWidth);
 _tcscpy(szBuffer,_T("tm.tmCharSet = "));
 _stprintf(szText,_T("%u"),tm.tmCharSet);
 _tcscat(szBuffer,szText);
 TextOut(hDC,0,40,szBuffer,_tcslen(szBuffer));
 _ftprintf(fp,_T("  tm.tmCharSet = %u\n"),(unsigned int)tm.tmCharSet);
 switch(tm.tmCharSet)
 {
  case ANSI_CHARSET:
    _tcscpy(szCharSet,_T("ANSI_CHARSET"));
    break;
  case DEFAULT_CHARSET:
    _tcscpy(szCharSet,_T("DEFAULT_CHARSET"));
    break;
  case SYMBOL_CHARSET:
    _tcscpy(szCharSet,_T("SYMBOL_CHARSET"));
    break;
  case SHIFTJIS_CHARSET:
    _tcscpy(szCharSet,_T("SHIFTJIS_CHARSET"));
    break;
  case HANGEUL_CHARSET:
    _tcscpy(szCharSet,_T("HANGEUL_CHARSET"));
    break;
  case CHINESEBIG5_CHARSET:
    _tcscpy(szCharSet,_T("CHINESEBIG5_CHARSET"));
    break;
  case OEM_CHARSET:
    _tcscpy(szCharSet,_T("OEM_CHARSET"));
    break;
  case JOHAB_CHARSET:
    _tcscpy(szCharSet,_T("JOHAB_CHARSET"));
    break;
  case HEBREW_CHARSET:
    _tcscpy(szCharSet,_T("HEBREW_CHARSET"));
    break;
  case ARABIC_CHARSET:
    _tcscpy(szCharSet,_T("ARABIC_CHARSET"));
    break;
  case GREEK_CHARSET:
    _tcscpy(szCharSet,_T("GREEK_CHARSET"));
    break;
  case TURKISH_CHARSET:
    _tcscpy(szCharSet,_T("TURKISH_CHARSET"));
    break;
  case THAI_CHARSET:
    _tcscpy(szCharSet,_T("THAI_CHARSET"));
    break;
  case EASTEUROPE_CHARSET:
    _tcscpy(szCharSet,_T("EASTEUROPE_CHARSET"));
    break;
  case RUSSIAN_CHARSET:
    _tcscpy(szCharSet,_T("RUSSIAN_CHARSET"));
    break;
  default:
    _tcscpy(szCharSet,_T("Unknown"));
    break;
 }
 TextOut(hDC,0,60,szCharSet,_tcslen(szCharSet));
 _ftprintf(fp,_T("  tm.tmCharSet = %s\n"),szCharSet);
 EndPaint(Wea->hWnd,&ps);
 _ftprintf(fp,_T("Leaving fnWndProc_OnPaint()\n"));
 fclose(fp);

 return 0;
}


long fnWndProc_OnClose(lpWndEventArgs Wea)            //This function handles the WM_CLOSE message
{                                                     //sent when the 'x' button is clicked.
 if(MessageBox(Wea->hWnd,_T("Do You Wish To Exit This App?"),_T("Exit Check?"),MB_YESNO)==IDYES)
 {
    DestroyWindow(Wea->hWnd);
    PostQuitMessage(0);
 }

 return 0;
}


long __stdcall fnWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
 static WndEventArgs wea;

 switch (msg)
 {
  case WM_CREATE:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnCreate(&wea);
  case WM_PAINT:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnPaint(&wea);
  case WM_CLOSE:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnClose(&wea);
 }

 return DefWindowProc(hwnd,msg,wParam,lParam);
}


int __stdcall WinMain(HINSTANCE hIns,HINSTANCE hPrevIns,LPSTR lpszArgument,int iShow)
{                                  //The program starts in WinMain().  The WNDCLASSEX structure variable
 TCHAR szClassName[]=_T("Form1");  //wc is filled out with general characteristics of a window.  Then
 WNDCLASSEX wc;                    //the class is RegisteredClassEx()'ed.  Directly after that a
 MSG messages;                     //CreateWindow() call instantiates an instance of the class.  Then
 HWND hWnd;                        //the program drops into a message processing loop in which any messages
                                   //destined for this program are DispatchMessage()'ed to the fnWndProc().
 wc.lpszClassName=szClassName;                wc.lpfnWndProc=fnWndProc;
 wc.cbSize=sizeof (WNDCLASSEX);               wc.style=CS_DBLCLKS;
 wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);     wc.hInstance=hIns;
 wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION);  wc.hCursor=LoadCursor(NULL,IDC_ARROW);
 wc.hbrBackground=(HBRUSH)COLOR_BACKGROUND;   wc.cbWndExtra=0;
 wc.lpszMenuName=NULL;                        wc.cbClsExtra=0;
 RegisterClassEx(&wc);
 hWnd=CreateWindow(szClassName,_T("Form1"),WS_OVERLAPPEDWINDOW,200,100,400,350,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
  TranslateMessage(&messages);
  DispatchMessage(&messages);
 }

 return messages.wParam;
}

sults here of what was on the screen. Also, it prints the output to a text file in whatever dir you run it from (Output.txt))...

Main.cpp

#include <windows.h>
#include <stdio.h>

typedef struct    WindowsEventArguments               //Package Window Procedure Parameters into structure
{
 HWND             hWnd;                               //Handle of Window
 WPARAM           wParam;                             //Window Parameter
 LPARAM           lParam;                             //Long Parameter
 HINSTANCE        hIns;                               //Instance Handle (Resolves To Process Address)
}WndEventArgs, *lpWndEventArgs;


long fnWndProc_OnCreate(lpWndEventArgs Wea)           //Called One Time During Window Creatiion
{                                                     //This corresponds to Form_Load() in Visual Basic.
 Wea->hIns=((LPCREATESTRUCT)Wea->lParam)->hInstance;  //lParam is pointer to CREATESTRUCT in WM_CREATE
 return 0;                                            //Handles WM_CREATE message.
}


long fnWndProc_OnPaint(lpWndEventArgs Wea)
{
 char szBuffer[64],szText[8],szCharSet[16];
 PAINTSTRUCT ps;
 TEXTMETRIC tm;
 FILE* fp=NULL;
 HDC hDC;

 fp=fopen("Output.txt","w");
 fprintf(fp,"Entering fnWndProc_OnPaint()\n");
 hDC=BeginPaint(Wea->hWnd,&ps);
 SetBkMode(hDC,TRANSPARENT);
 GetTextMetrics(hDC,&tm);
 strcpy(szBuffer,"tm.tmHeight = ");
 sprintf(szText,"%u",(unsigned int)tm.tmHeight);
 strcat(szBuffer,szText);
 TextOut(hDC,0,0,szBuffer,strlen(szBuffer));
 fprintf(fp,"  tm.tmHeight = %u\n",(unsigned int)tm.tmHeight);
 sprintf(szText,"%u",(unsigned int)tm.tmAveCharWidth);
 strcpy(szBuffer,"tm.tmAveCharWidth = ");
 strcat(szBuffer,szText);
 TextOut(hDC,0,20,szBuffer,strlen(szBuffer));
 fprintf(fp,"  tm.tmAveCharWidth = %u\n",(unsigned int)tm.tmAveCharWidth);
 strcpy(szBuffer,"tm.tmCharSet = ");
 sprintf(szText,"%u",tm.tmCharSet);
 strcat(szBuffer,szText);
 TextOut(hDC,0,40,szBuffer,strlen(szBuffer));
 fprintf(fp,"  tm.tmCharSet = %u\n",(unsigned int)tm.tmCharSet);
 switch(tm.tmCharSet)
 {
  case ANSI_CHARSET:
    strcpy(szCharSet,"ANSI_CHARSET");
    break;
  case DEFAULT_CHARSET:
    strcpy(szCharSet,"DEFAULT_CHARSET");
    break;
  case SYMBOL_CHARSET:
    strcpy(szCharSet,"SYMBOL_CHARSET");
    break;
  case SHIFTJIS_CHARSET:
    strcpy(szCharSet,"SHIFTJIS_CHARSET");
    break;
  case HANGEUL_CHARSET:
    strcpy(szCharSet,"HANGEUL_CHARSET");
    break;
  case CHINESEBIG5_CHARSET:
    strcpy(szCharSet,"CHINESEBIG5_CHARSET");
    break;
  case OEM_CHARSET:
    strcpy(szCharSet,"OEM_CHARSET");
    break;
  case JOHAB_CHARSET:
    strcpy(szCharSet,"JOHAB_CHARSET");
    break;
  case HEBREW_CHARSET:
    strcpy(szCharSet,"HEBREW_CHARSET");
    break;
  case ARABIC_CHARSET:
    strcpy(szCharSet,"ARABIC_CHARSET");
    break;
  case GREEK_CHARSET:
    strcpy(szCharSet,"GREEK_CHARSET");
    break;
  case TURKISH_CHARSET:
    strcpy(szCharSet,"TURKISH_CHARSET");
    break;
  case THAI_CHARSET:
    strcpy(szCharSet,"THAI_CHARSET");
    break;
  case EASTEUROPE_CHARSET:
    strcpy(szCharSet,"EASTEUROPE_CHARSET");
    break;
  case RUSSIAN_CHARSET:
    strcpy(szCharSet,"RUSSIAN_CHARSET");
    break;
  default:
    strcpy(szCharSet,"Unknown");
    break;
 }
 TextOut(hDC,0,60,szCharSet,strlen(szCharSet));
 fprintf(fp,"  tm.tmCharSet = %s\n",szCharSet);
 EndPaint(Wea->hWnd,&ps);
 fprintf(fp,"Leaving fnWndProc_OnPaint()\n");
 fclose(fp);

 return 0;
}


long fnWndProc_OnClose(lpWndEventArgs Wea)            //This function handles the WM_CLOSE message
{                                                     //sent when the 'x' button is clicked.
 if(MessageBox(Wea->hWnd,"Do You Wish To Exit This App?","Exit Check?",MB_YESNO)==IDYES)
 {
  DestroyWindow(Wea->hWnd);
  PostQuitMessage(0);
 }

 return 0;
}


long __stdcall fnWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
 static WndEventArgs wea;

 switch (msg)
 {
  case WM_CREATE:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnCreate(&wea);
  case WM_PAINT:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnPaint(&wea);
  case WM_CLOSE:
    wea.hWnd=hwnd, wea.wParam=wParam, wea.lParam=lParam;
    return fnWndProc_OnClose(&wea);
 }

 return DefWindowProc(hwnd,msg,wParam,lParam);
}


int __stdcall WinMain(HINSTANCE hIns,HINSTANCE hPrevIns,LPSTR lpszArgument,int iShow)
{                                  //The program starts in WinMain().  The WNDCLASSEX structure variable
 char szClassName[]="Form1";       //wc is filled out with general characteristics of a window.  Then
 WNDCLASSEX wc;                    //the class is RegisteredClassEx()'ed.  Directly after that a
 MSG messages;                     //CreateWindow() call instantiates an instance of the class.  Then
 HWND hWnd;                        //the program drops into a message processing loop in which any messages
                                   //destined for this program are DispatchMessage()'ed to the fnWndProc().
 wc.lpszClassName=szClassName;                wc.lpfnWndProc=fnWndProc;
 wc.cbSize=sizeof (WNDCLASSEX);               wc.style=CS_DBLCLKS;
 wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);     wc.hInstance=hIns;
 wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION);  wc.hCursor=LoadCursor(NULL,IDC_ARROW);
 wc.hbrBackground=(HBRUSH)COLOR_BACKGROUND;   wc.cbWndExtra=0;
 wc.lpszMenuName=NULL;                        wc.cbClsExtra=0;
 RegisterClassEx(&wc);
 hWnd=CreateWindow(szClassName,"Form1",WS_OVERLAPPEDWINDOW,200,100,400,350,HWND_DESKTOP,0,hIns,0);
 ShowWindow(hWnd,iShow);
 while(GetMessage(&messages,NULL,0,0))
 {
  TranslateMessage(&messages);
  DispatchMessage(&messages);
 }

 return messages.wParam;
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

OK, the code compiled and my tmCharSet ids ANSI_CHARSET.
What do I do next ,please.Thank you for your time,frederick, and I'm sorry for my mistakes.

OK, the code compiled and my tmCharSet ids ANSI_CHARSET.
What do I do next ,please.Thank you for your time,frederick, and I'm sorry for my mistakes.

Since you are obviously compiling with UNICODE #defined, call the ANSI-version of SetWindowText excplicitly when setting the EDIT control's text (naturally do that only when you work with ANSI text).
As of now, your app displays a UNICODE text file (almost) as expected (try saving a file as UNICODE with e.g. Notepad and load that file).

As a general rule, read the documentation about the Windows functions/other stuff really _carefully_, it will pay off sooner than later.

So the fix is the following

pszFileText[dwFileSize] = 0; // Add null terminator
if(SetWindowText[B]A[/B](hEdit, pszFileText))
bSuccess = TRUE; // It worked!

PS. There are more things to fix in the code but I will not go into them now.

Edited 6 Years Ago by mitrmkar: almost

Sorry but I managed to leave out one thing regarding the above change.

// Since the function became an 'ANSI-version',
// use LPSTR instead of LPWSTR		
LPSTR pszFileText;
// and ...
pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);

OK,mitrmkar, the code compiled successfully but it still doesn't give me English, and please my MDI child has failed to create....please any help will be appreciated, and thank you all in advance for your time spent on my problem!
P.S I am using Visual C++ 2008 Express Edition, maybe there is a setting I am to do or what?

Here is the complete code with all the changes you asked me to do...

#include<windows.h>
#include<fstream>
#include<tchar.h>
#include<cstdlib>
#include<cstring>
#include<commctrl.h>
#include"resource.h"
#pragma comment(lib,"comctl32.lib")



static TCHAR g_szClassName[] = _T("myWindowClass");
static TCHAR g_szChildClassName[] = _T("myMDIChildWindowClass");
static TCHAR g_szWindowName[] = _T("MyWindowName");

#define IDC_MAIN_MDI	101
#define IDC_MAIN_TOOL	102
#define IDC_MAIN_STATUS	103

#define IDC_CHILD_EDIT	101

#define ID_MDI_FIRSTCHILD 50000

HWND g_hMDIClient = NULL;
HWND g_hMainWindow = NULL;

BOOL LoadTextFileToEdit(HWND hEdit, LPCTSTR pszFileName)
{
	HANDLE hFile;
	BOOL bSuccess = FALSE;

	hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
		OPEN_EXISTING, 0, NULL);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		DWORD dwFileSize;

		dwFileSize = GetFileSize(hFile, NULL);
		if(dwFileSize != 0xFFFFFFFF)
		{
			LPSTR pszFileText;

			pszFileText = (LPSTR)GlobalAlloc(GPTR, dwFileSize + 1);
			if(pszFileText != NULL)
			{
				DWORD dwRead;

				if(ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
				{
					pszFileText[dwFileSize] = 0; // Add null terminator
					if(SetWindowTextA(hEdit, (LPCSTR)pszFileText))
						bSuccess = TRUE; // It worked!
				}
				GlobalFree(pszFileText);
			}
		}
		CloseHandle(hFile);
	}
	return bSuccess;
}

BOOL SaveTextFileFromEdit(HWND hEdit, LPCTSTR pszFileName)
{
	HANDLE hFile;
	BOOL bSuccess = FALSE;

	hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
		CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		DWORD dwTextLength;

		dwTextLength = GetWindowTextLength(hEdit);
		// No need to bother if there's no text.
		if(dwTextLength > 0)
		{
			LPSTR pszText;
			DWORD dwBufferSize = dwTextLength + 1;

			pszText = (LPSTR)GlobalAlloc(GPTR, dwBufferSize);
			if(pszText != NULL)
			{
				if(GetWindowText(hEdit, (LPWSTR)pszText, dwBufferSize))
				{
					DWORD dwWritten;

					if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
						bSuccess = TRUE;
				}
				GlobalFree(pszText);
			}
		}
		CloseHandle(hFile);
	}
	return bSuccess;
}

void DoFileOpen(HWND hwnd)
{
	OPENFILENAME ofn;
	wchar_t szFileName[MAX_PATH] = _T("");

	ZeroMemory(&ofn, sizeof(ofn));

	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFilter = _T("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0");
	ofn.lpstrFile = szFileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
	ofn.lpstrDefExt = _T("txt");

	if(GetOpenFileName(&ofn))
	{
		HWND hEdit = GetDlgItem(hwnd, IDC_CHILD_EDIT);
		if(LoadTextFileToEdit(hEdit, (LPCTSTR)szFileName))
		{
			SendDlgItemMessage(g_hMainWindow, IDC_MAIN_STATUS, SB_SETTEXT, 0, (LPARAM)"Opened...");
			SendDlgItemMessage(g_hMainWindow, IDC_MAIN_STATUS, SB_SETTEXT, 1, (LPARAM)szFileName);

			SetWindowText(hwnd, (LPCWSTR)szFileName);
		}
	}
}

void DoFileSave(HWND hwnd)
{
	OPENFILENAME ofn;
	const char szFileName[MAX_PATH] = "";

	ZeroMemory(&ofn, sizeof(ofn));

	ofn.lStructSize = sizeof(ofn);
	ofn.hwndOwner = hwnd;
	ofn.lpstrFilter = _T("Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0");
	ofn.lpstrFile = (LPWSTR)szFileName;
	ofn.nMaxFile = MAX_PATH;
	ofn.lpstrDefExt = _T("txt");
	ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

	if(GetSaveFileName(&ofn))
	{
		HWND hEdit = GetDlgItem(hwnd, IDC_CHILD_EDIT);
		if(SaveTextFileFromEdit(hEdit, (LPCTSTR)szFileName))
		{
			SendDlgItemMessage(g_hMainWindow, IDC_MAIN_STATUS, SB_SETTEXT, 0, (LPARAM)"Saved...");
			SendDlgItemMessage(g_hMainWindow, IDC_MAIN_STATUS, SB_SETTEXT, 1, (LPARAM)szFileName);

			SetWindowText(hwnd, (LPCWSTR)szFileName);
		}
	}
}

HWND CreateNewMDIChild(HWND hMDIClient)
{
	MDICREATESTRUCT mcs;
	HWND hChild;

	mcs.szTitle = _T("Untitled");
	mcs.szClass = _T("MDI class");
	mcs.hOwner  = GetModuleHandle(NULL);
	mcs.x = mcs.cx = CW_USEDEFAULT;
	mcs.y = mcs.cy = CW_USEDEFAULT;
	mcs.style = MDIS_ALLCHILDSTYLES;

	hChild = (HWND)SendMessage(hMDIClient, WM_MDICREATE, 0, (LONG)&mcs);
	if(!hChild)
	{
		MessageBox(hMDIClient, _T("MDI Child creation failed."), _T("Oh Oh..."),
			MB_ICONEXCLAMATION | MB_OK);
	}
	return hChild;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_CREATE:
		{
			HWND hTool;
			TBBUTTON tbb[3];
			TBADDBITMAP tbab;

			HWND hStatus;
			int statwidths[] = {100, -1};

			CLIENTCREATESTRUCT ccs;

			// Create MDI Client

			// Find window menu where children will be listed
			ccs.hWindowMenu  = GetSubMenu(GetMenu(hwnd), 2);
			ccs.idFirstChild = ID_MDI_FIRSTCHILD;

			g_hMDIClient = CreateWindowEx(WS_EX_CLIENTEDGE, _T("MDICLIENT"), NULL,
				WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
				CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
				hwnd, (HMENU)IDC_MAIN_MDI, GetModuleHandle(NULL), (LPVOID)&ccs);

			if(g_hMDIClient == NULL)
				MessageBox(NULL, _T("Could not create MDI client."), _T("Error"), MB_OK | MB_ICONERROR);

			// Create Toolbar

			hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
				hwnd, (HMENU)IDC_MAIN_TOOL, GetModuleHandle(NULL), NULL);
			if(hTool == NULL)
				MessageBox(hwnd, _T("Could not create tool bar."), _T("Error"), MB_OK | MB_ICONERROR);

			// Send the TB_BUTTONSTRUCTSIZE message, which is required for
			// backward compatibility.
			SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
			
			tbab.hInst = HINST_COMMCTRL;
			tbab.nID = IDB_STD_SMALL_COLOR;
			SendMessage(hTool, TB_ADDBITMAP, 0, (LPARAM)&tbab);

			ZeroMemory(tbb, sizeof(tbb));
			tbb[0].iBitmap = STD_FILENEW;
			tbb[0].fsState = TBSTATE_ENABLED;
			tbb[0].fsStyle = TBSTYLE_BUTTON;
			tbb[0].idCommand = ID_FILE_NEW;

			tbb[1].iBitmap = STD_FILEOPEN;
			tbb[1].fsState = TBSTATE_ENABLED;
			tbb[1].fsStyle = TBSTYLE_BUTTON;
			tbb[1].idCommand = ID_FILE_OPEN;

			tbb[2].iBitmap = STD_FILESAVE;
			tbb[2].fsState = TBSTATE_ENABLED;
			tbb[2].fsStyle = TBSTYLE_BUTTON;
			tbb[2].idCommand = ID_FILE_SAVEAS;

			SendMessage(hTool, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM)&tbb);

			// Create Status bar

			hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
				WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
				hwnd, (HMENU)IDC_MAIN_STATUS, GetModuleHandle(NULL), NULL);

			SendMessage(hStatus, SB_SETPARTS, sizeof(statwidths)/sizeof(int), (LPARAM)statwidths);
			SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Feel Tkud :)");
		}
		break;
		case WM_SIZE:
		{
			HWND hTool;
			RECT rcTool;
			int iToolHeight;

			HWND hStatus;
			RECT rcStatus;
			int iStatusHeight;

			HWND hMDI;
			int iMDIHeight;
			RECT rcClient;

			// Size toolbar and get height

			hTool = GetDlgItem(hwnd, IDC_MAIN_TOOL);
			SendMessage(hTool, TB_AUTOSIZE, 0, 0);

			GetWindowRect(hTool, &rcTool);
			iToolHeight = rcTool.bottom - rcTool.top;

			// Size status bar and get height

			hStatus = GetDlgItem(hwnd, IDC_MAIN_STATUS);
			SendMessage(hStatus, WM_SIZE, 0, 0);

			GetWindowRect(hStatus, &rcStatus);
			iStatusHeight = rcStatus.bottom - rcStatus.top;

			// Calculate remaining height and size edit

			GetClientRect(hwnd, &rcClient);

			iMDIHeight = rcClient.bottom - iToolHeight - iStatusHeight;

			hMDI = GetDlgItem(hwnd, IDC_MAIN_MDI);
			SetWindowPos(hMDI, NULL, 0, iToolHeight, rcClient.right, iMDIHeight, SWP_NOZORDER);
		}
		break;
		case WM_CLOSE:
			DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case ID_FILE_EXIT:
					PostMessage(hwnd, WM_CLOSE, 0, 0);
				break;
				case ID_FILE_NEW:
					CreateNewMDIChild(g_hMDIClient);
				break;
				case ID_FILE_OPEN:
				{
					HWND hChild = CreateNewMDIChild(g_hMDIClient);
					if(hChild)
					{
						DoFileOpen(hChild);	
					}
				}
				break;
				case ID_FILE_CLOSE:
				{
					HWND hChild = (HWND)SendMessage(g_hMDIClient, WM_MDIGETACTIVE,0,0);
					if(hChild)
					{
						SendMessage(hChild, WM_CLOSE, 0, 0);
					}
				}
				break;
				case ID_WINDOW_TILE:
					SendMessage(g_hMDIClient, WM_MDITILE, 0, 0);
				break;
				case ID_WINDOW_CASCADE:
					SendMessage(g_hMDIClient, WM_MDICASCADE, 0, 0);
				break;
				default:
				{
					if(LOWORD(wParam) >= ID_MDI_FIRSTCHILD)
					{
						DefFrameProc(hwnd, g_hMDIClient, WM_COMMAND, wParam, lParam);
					}
					else 
					{
						HWND hChild = (HWND)SendMessage(g_hMDIClient, WM_MDIGETACTIVE,0,0);
						if(hChild)
						{
							SendMessage(hChild, WM_COMMAND, wParam, lParam);
						}
					}
				}
			}
		break;
		default:
			return DefFrameProc(hwnd, g_hMDIClient, msg, wParam, lParam);
	}
	return 0;
}

LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_CREATE:
		{
			HFONT hfDefault;
			HWND hEdit;

			// Create Edit Control

			hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, _T("EDIT"), _T(""), 
				WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL, 
				0, 0, 100, 100, hwnd, (HMENU)IDC_CHILD_EDIT, GetModuleHandle(NULL), NULL);
			if(hEdit == NULL)
				MessageBox(hwnd, _T("Could not create edit box."), _T("Error"), MB_OK | MB_ICONERROR);

			hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
			SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
		}
		break;
		case WM_MDIACTIVATE:
		{
			HMENU hMenu, hFileMenu;
			UINT EnableFlag;

			hMenu = GetMenu(g_hMainWindow);
			if(hwnd == (HWND)lParam)
			{	   //being activated, enable the menus
				EnableFlag = MF_ENABLED;
			}
			else
			{						   //being de-activated, gray the menus
				EnableFlag = MF_GRAYED;
			}

			EnableMenuItem(hMenu, 1, MF_BYPOSITION | EnableFlag);
			EnableMenuItem(hMenu, 2, MF_BYPOSITION | EnableFlag);

			hFileMenu = GetSubMenu(hMenu, 0);
			EnableMenuItem(hFileMenu, ID_FILE_SAVEAS, MF_BYCOMMAND | EnableFlag);

			EnableMenuItem(hFileMenu, ID_FILE_CLOSE, MF_BYCOMMAND | EnableFlag);
			EnableMenuItem(hFileMenu, ID_FILE_CLOSEALL, MF_BYCOMMAND | EnableFlag);

			DrawMenuBar(g_hMainWindow);
		}
		break;
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case ID_FILE_OPEN:
					DoFileOpen(hwnd);
				break;
				case ID_FILE_SAVEAS:
					DoFileSave(hwnd);
				break;
				case ID_EDIT_CUT:
					SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_CUT, 0, 0);
				break;
				case ID_EDIT_COPY:
					SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_COPY, 0, 0);
				break;
				case ID_EDIT_PASTE:
					SendDlgItemMessage(hwnd, IDC_CHILD_EDIT, WM_PASTE, 0, 0);
				break;
			}
		break;
		case WM_SIZE:
		{
			HWND hEdit;
			RECT rcClient;

			// Calculate remaining height and size edit

			GetClientRect(hwnd, &rcClient);

			hEdit = GetDlgItem(hwnd, IDC_CHILD_EDIT);
			SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom, SWP_NOZORDER);
		}
		return DefMDIChildProc(hwnd, msg, wParam, lParam);
		default:
			return DefMDIChildProc(hwnd, msg, wParam, lParam);
	
	}
	return 0;
}

BOOL SetUpMDIChildWindowClass(HINSTANCE hInstance)
{
	WNDCLASSEX wc;

	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style		 = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc	 = MDIChildWndProc;
	wc.cbClsExtra	 = 0;
	wc.cbWndExtra	 = 0;
	wc.hInstance	 = hInstance;
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = g_szChildClassName;
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&wc))
	{
		MessageBox(0, _T("Could Not Register Child Window"), _T("Oh Oh..."),
			MB_ICONEXCLAMATION | MB_OK);
		return FALSE;
	}
	else
		return TRUE;
}





int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
	MSG msg;
	HWND hwnd;
	WNDCLASSEX wcex;
	InitCommonControls();
	wcex.cbClsExtra=0;
	wcex.cbSize=sizeof(WNDCLASSEX);
	wcex.cbWndExtra=0;
	wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW + 1);
	wcex.hCursor=LoadCursor(NULL,IDC_ARROW);
	wcex.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wcex.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
	wcex.hInstance=GetModuleHandle(NULL);
	wcex.lpfnWndProc=WndProc;
	wcex.lpszClassName=g_szClassName;
	wcex.lpszMenuName=MAKEINTRESOURCE(IDR_MAINMENU);
	wcex.style=0;

	if(!RegisterClassEx(&wcex)){
		MessageBox(NULL,_T("Unable to register your class"),_T("Error"),MB_OK|MB_ICONERROR);
	}
	hwnd=CreateWindowEx(WS_EX_CLIENTEDGE,g_szClassName,g_szWindowName,WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN,
		CW_USEDEFAULT,CW_USEDEFAULT,500,500,NULL,NULL,hInstance,NULL);
	if(hwnd == NULL)
	{
		MessageBox(NULL, _T("Window Creation Failed!"), _T("Error!"),
			MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	g_hMainWindow = hwnd;

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&msg, NULL, 0, 0) > 0)
	{
		if (!TranslateMDISysAccel(g_hMDIClient, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return msg.wParam;
}

Edited 6 Years Ago by tkud: n/a

OK,mitrmkar, the code compiled successfully but it still doesn't give me English

I suggest that
- open Notepad
- type some text and save a file as ANSI
- save it also as UNICODE

Open both the files with your app, and tell us what happens ...

FYI, for me your LoadTextFileToEdit() loads and displays ANSI text as it should.

Wow!! I saved the same text with two different names, one in ANSI and the other in Unicode.
Then, when I used my edit to open the 2 files, the ANSI file still came out Chinese, but the Unicode came out in English!!
Is that the problem? If yes, what should I do to make it keep coming out in English, maybe I should start saving my files as Unicode?

the ANSI file still came out Chinese, but the Unicode came out in English!

Now there is some kind of mismatch here, maybe you could zip the project (*.c*, *.h, *.rc) and post it here.

what should I do to make it keep coming out in English, maybe I should start saving my files as Unicode?

Probably it would be easier to stick with the ANSI and master the UNICODE later. First of all you have to understand their difference though and how that impacts your programming (that is actually one thing that you have just stumbled upon).

We already have the main source code; all that would be needed would be the h and rc file. I'd give a shot at compiling it if I had those. However, I'm wondering if some setting isn't messed up in the project file.

This article has been dead for over six months. Start a new discussion instead.