6. В файле MyExp.cpp ввести код, приведенный в листинге 4.2. Листинг 4.2
// Блок 1
#include
#include «MyExp.h»
// Блок 2
const TCHAR szAppName[] = TEXT («MyExp»);
HINSTANCE hInst;
const struct decodeUINT MainMessages[] = {
WM_DESTROY, DoDestroyMain,
WM_CHAR, CharRec,
};
// Блок 3
wchar_t *szStr;
// Блок 4
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow) {
MSG msg;
int rc = 0;
rc = InitApp (hInstance);
if (rc) return rc;
if ((rc = InitInstance (hInstance, lpCmdLine, nCmdShow))!= 0)
return rc;
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return TermInstance (hInstance, msg.wParam);
}
// Блок 5
int InitApp (HINSTANCE hInstance) {
WNDCLASS wc;
HWND hWnd = FindWindow (szAppName, NULL);
if (hWnd) {
SetForegroundWindow ((HWND)(((DWORD)hWnd) | 0x01));
return -1;
}
wc.style = 0;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL,
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szAppName;
if (RegisterClass (&wc) == 0) return 1;
return 0;
}
// Блок 6
int InitInstance (HINSTANCE hInstance, LPWSTR lpCmdLine, int nCmdShow){
HWND hWnd;
hInst = hInstance;
hWnd = CreateWindow (szAppName,
TEXT(«My Experimental Programm»),
WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if ((!hWnd) || (!IsWindow (hWnd))) return 0x10;
ShowWindow (hWnd, nCmdShow);
UpdateWindow (hWnd);
return 0;
}
// Блок 7
int TermInstance (HINSTANCE hInstance, int nDefRC) {
return nDefRC;
}
// Блок 8
LRESULT CALLBACK MainWndProc (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
INT i;
for (i = 0; i < dim(MainMessages); i++) {
if (wMsg == MainMessages[i].Code)
return (*MainMessages[i].Fxn)(hWnd, wMsg, wParam, lParam);
}
return DefWindowProc (hWnd, wMsg, wParam, lParam);
}
// Блок 9
LRESULT DoDestroyMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
PostQuitMessage (0);
return 0;
}
// Блок 10
LRESULT DoCharRecieveMain (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
HDC hdc;
PAINTSTRUCT ps;
RECT rectCli;
GetClientRect (hWnd, &rectCli);
ps.rcPaint = rectCli;
InvalidateRect (hWnd, &rectCli, true);
hdc = BeginPaint (hWnd, &ps);
szStr = L" GiGoGa";
DrawText (hdc, (const unsigned short *)szStr, – 1, &rectCli,
DT_CENTER | DT_SINGLELINE);
EndPaint (hWnd, &ps);
return 0;
}
// Блок 11
LRESULT CharRec (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
switch ((TCHAR)wParam){
case 49: { szStr = L" Нажата клавиша 1 на клавиатуре";}
break;
case 50: { szStr = L" А теперь на клавиатуре нажата клавиша 2";}
break;
}
MyPaint (hWnd, wMsg, wParam, lParam);
return 0;
}
// Блок 12
int MyPaint (HWND hWnd, UINT wMsg, WPARAM wParam,
LPARAM lParam) {
HDC hdc;
PAINTSTRUCT ps;
RECT rectCli;
GetClientRect (hWnd, &rectCli);
ps.rcPaint = rectCli;
InvalidateRect (hWnd, &rectCli, true);
hdc = BeginPaint (hWnd, &ps);
DrawText (hdc, (const unsigned short *)szStr, – 1, &rectCli,
DT_LEFT | DT_WORDBREAK);
EndPaint (hWnd, &ps);
return 0;
}