351 121 915KB
Russian Pages 96 Year 2009
-
Ы
Ы
,
. . WIN32 API
2009
681.3.06(035.5)
. . Win32 API. , 2009. – 90 .
Microsoft Windows
/
C++
.–
:
Win32 API.
,
»
210202.65 « 0900104.65 «
», «
-
».
№5
,
08
2009 .
-
2007-2008 « », , . 2015
,
.
©
,
2
©
, 2009 . ., 2009
Windows
-
.
–
,
,
.
-
–
. Windows (handle).
–
.
. (API – Application Pro-
gramming 32-
Interface) (Win32 API), ( ),
dows.
Microsoft Win−
. windows.h, . Win32
− (
).
,
. (
,
,
−
).
,
,
-
. ,
,
,
. Windows .
• • • •
: (overlapped window); (pop–up window); (child window); (layered window) – ,
,
WS_OVERLAPPEDWINDOW. , « » ,
, . CreateWindowEx() , .
. CreateWindowEx() ,
WS_POPUP , .
-
3
,
, WS_POPUPWINDOW | WS_CAPTION. CreateWindowEx() WS_CHILD .
, ,
,
,
. -
, . . . ,
.
: • SDI (Single Document Interface) – ; • MDI (Multiple Document Interface) – ; • (Based Dialog) − , . Windows ANSI , UNICODE. , 65536 .
– ,
,
UNICODE ANSI -
UNICODE : tchar.h;
1. 2.
TCHAR, LPTSTR
LPCTSTR;
3.
-
4. , .
_TEXT ( _T); , sizeof(szBuffer) – sizeof(szBuffer)/sizeof(TCHAR) – TCHAR, LPTSTR
LPCTSTR
: typedef unsigned short wchar_t; typedef wchar_t WCHAR; #define UNICODE typedef WCHAR TCHAR; //UNICODE #else typedef char TCHAR; //ANSI #endif
4
-
-
typedef WCHAR* LPWSTR; typedef CHAR* LPSTR; #define UNICODE typedef LPWSTR LPTSTR; #else typedef LPSTR LPTSTR; #endif
//UNICODE //ANSI
typedef CONST WCHAR* LPCWSTR; #define UNICODE typedef LPCWSTR LPCTSTR; #else typedef LPCSTR LPCTSTR; #endif
//ANSI
, UNICODE
ANSI
, lstrcat
//UNICODE
,
, :
-
LPTSTR lstrcat ( LPTSTR lpString1, LPTSTR lpString2 );
Win32 ANSI DispatchMessage():
, # ifdef #define #else #define #endif
UNICODE
UNICODE DispatchMessage
DispatchMessageW
DispatchMessage
DispatchMessageA
,
,
,
-
-
ANSI UNICODE , Microsoft Windows XP Professional 2005. 1.
Microsoft Visual Studio
Щ
Windows , , :
WINDOWS -
, WinMain()
1.1.
WndPoc().
WinMain() WinMain() winbase.h
. :
int WINAPI WinMain( HINSTANCE hInstance,
// // HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow
//
// //
// // ,
5
-
// );
WINAPI
, . .
Win32
-
. ,
, . .
, . WinMain(), Windows (
). hInstance
HINSTANCE .
-
Windows Win32 API (hInstance) . , . . , : HINSTANCE hInst;
.
hInstance • • GetClassLong(), •
-
:
hInst=(HINSTANCE)GetClassLong(hWnd,GCL_HMODULE);
GetModuleHandle()
:
hInst= GetModuleHandle(NULL);.
− hPrevInstance −
.
− lpszCmdLine −
,
-
,
. − nCmdShow − , WinMain(), .
. ,
WinMain()
Windows : 1)
;
-
(Windows
,
); 2) ( ); 3)
, .
6
1.2. (window class),
,–
WNDCLASSEX,
.
-
, ,
,
,
.
-
, . WNDCLASSEX RegisterClassEx(), .
winuser.h:
typedef struct tagWNDCLASSEX { UINT cbSize; // UINT style; // WNDPROC lpfnWndProc; // int cbClsExtra; // // int cbWndExtra; // // HINSTANCE hInstance; // // HICON hIcon; // HCURSOR hCursor; // HBRUSH hbrBackground;
// // // //
LPCTSTR lpszMenuName; LPCTSTR lpszClassName; //
// HICON hIconSm; // } WNDCLASSEX;
WNDCLASSEX
32-
. WNDCLASSEX ,
.
-
. Windows, . OPENFILENAME, « », .
, LOGFONT,
memset()
ZeroMemory() , VOID ZeroMemory( PVOID pBuffer,
: // //
,
7
SIZE_T length // ); void *memset( void *dest, int c, size_t count ); : memset(&wc, 0, sizeof(wc));
WNDCLASSEX, . cbSize .
(32
style ). (
WNDCLASSEX . WNDCLASSEX
).
Windows
. • CS_DBLCLKS –
CS_. : (0x00000008),
; • CS_VREDRAW –
-
(0x00000001),
• CS_HREDRAW –
-
; (0x00000002),
• CS_NOCLOSE –
-
; (0x00000200),
-
. «
»
C++
-
(style). WNDCLASSEX: lpfnWndProc – • hInstance – • lpszClassName – • ).
(
); (
); (11-
WNDCLASSEX ( lpfnWndProc) ,
.
− hIcon −
.
–
-
, .
• 16x16 • 32x32 16• 48x48 :
–
,
–
16,
; –
256
8
.
;
HICON LoadIcon( HINSTANCE hInst, LPCTSTR
// // // // //
lpszIcon
,
);
(.exe) –
(hInst),
:
wc.hIcon=LoadIcon(hInst,MAKEINTRESOURCE(IDI_APPICON));
− lpszIcon,
,– .
, , .
MAKEINTRESOURCE, :
-
winuser.h
#define MAKEINTRESOURCE(i) \ (LPSTR) ((DWORD)((WORD)(i)))
,
16
-
. LoadIcon()
hInst -
NULL. ,
IDI_ (ID for icon),
:
wc.hIcon = LoadIcon(hInst, IDI_APLICATION);
. LoadIcon() –
MSDN. – NULL.
, LoadIcon() , . . .
, 16x16
-
, LoadImage(),
:
HANDLE LoadImage( HINSTANCE hInst,
// // LPCTSTR lpszName, // // // UINT uType, // int cxDesired, // // int cyDesired, // // UINT fuLoad // );
, ,
− lpszName
hInst NULL, LR_LOADFROMFILE,
. fuLoad 9
lpszName
, .
-
− uType − : IMAGE_BITMAP; IMAGE_CURSOR; IMAGE_ICON.
− cxDesired, cyDesired − .
-
, .
− fuLoad −
,
• LR_DEFAULTCOLOR –
: ,
• LR_DEFAULTSIZE – sired , ; LR_LOADTRANSPARENT – « • COLOR_WINDOW – ); LR_LOADFROMFILE – • (
); cxDesired
»
cyDe( .
16x16
: wc.hIcon = (HICON)LoadImage( Inst, MAKEINTRESOURCE(IDI_APPICON_SM), IMAGE_ICON,16, 16,0);
: HBITMAP hBitmap (HBITMAP)LoadImage(hInst, FILE_NAME, IMAGE_BITMAP, 100, 100, LR_LOADFROMFILE);
hCursor . 32x32 LoadCursor(),
WNDCLASSEX Windows − .
-
: HCURSOR LoadCursor( HINSTANCE hInst, LPCTSTR lpszCursor );
// // // //
,
, ,
lpszCursor hInst.
(
) NULL,
,
10
IDC_.
, IDC_ARROW. ,
,
,
-
:
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
: IDC_ARROW – IDC_CROSS – IDC_SIZEALL – IDC_SIZENS – IDC_IBEAM – IDC_WAIT –
; ; ; (
–
);
; . SetCursor(),
: HCURSOR SetCursor(HCURSOR hCursor);
SetCursor , .
, -
WM_MOUSMOVE. . ,
WM_CREATE, WM_SIZE hCursor1, WM_MOUSMOVE ( hCursor2, wClient, hClient, xPos yPos static): Case WM_CREATE: //… hCursor1 = LoadCursor(NULL,IDC_SIZEALL); hCursor2 = LoadCursor(NULL, IDC_WAIT); //… case WM_SIZE: // wClient = LOWORD(lParam); hClient = HIWORD(lParam); break; case WM_MOUSMOVE: // xPos = LOWORD(lParam); yPos = HIWORD(lParam); if(yPos < 32) // SetCursor(hCursor1); if(yPos > hClient – 32) // SetCursor(hCursor2); break; //…
, hbrBackground
WNDCLASSEX.
11
(brush) –
, . ,
,
, , Windows GetStockObject()
, HGDIOBJ.
,
-
: HGDIOBJ GetStockObject(int nObjectTyp);
GetStockObject() (HPEN,HBRUSH, HBITMAP, , :
HPALETTE, HFONT, HRGN).
ws.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH).
GetStockBrush,
windowsx.h GetStockFont,
GetStockPen , :
#define GetSto kBrush(i)(HBRUSH)GetStockObject(i))
GetStockObject() : ws.hbrBackground = GetStockBrush(WHITE_BRUSH);
Windows
(
: WHITE_BRUSH – DKGRAY_BRUSH – LTGRAY_BRUSH – GRAY_BRUSH – BLACK_BRUSH – NULL_BRUSH –
)
; -
; ; ; ; . -
CreateSolidBrush(),
:
HBRUSH CreateSolidBrush(COLORREF
crColor – ,
crColor);
RGB
. ,
. (4
RGB,
)
-
:
ws.hbrBackground= CreateSolidBrush(RGB(0,255,255));
RegisterClassEx(),
:
ATOM RegisterClassEx(CONST WNDCLASSEX* lpwc);
lpw
–
WNDCLASSEX. RegisterClassEx() ATOM (WORD – 16 ), . WM_CREATE
12
SetClassLong() :
SetWindowLong(),
DWORD SetClassLong( HWND hWnd, // int nIndex, // // LONG dwNewLong // ); LONG SetWindowLong( HWND hWnd, // int nIndex, // // LONG dwNewLong // );
•
• • • • • •
-
,
,
nIndex SetClassLong() : GCL_HBRBACKGROUND – ; GCL_HCURSOR – ; GCL_HICON – ; GCL – HICONSM – GCL_MENUNAME – ; GCL_STYLE – ; GCL_WNDPROC – . , :
-
;
, -
Case WM_CREATE: SeClassLong(hWnd,GCL_HBRBACKGROUND, (LONG)CreateSolidBrush(RGB(0,250,250))); return TRUE; //…
1.3. , Windows CreateWindowEx(),
,
-
: HWND CreateWindowEx( DWORD dwExStyle,
//
LPCTSTR lpClassName,
LPCTSTR lpWindowName
, // // // // // //
// //
DWORD dwStyle, int x int y,
// // // // //
int nWidth, //
13
dwStyle ,
int nHeight, // HWND hWndParent,// HMENU hMenu, // // HINSTANCE hInstance, LPVOID lpParam
// // // //
);
− dwExStyle −
-
,
, . • WS_EX_MDICHILD – ; • WS_EX_TOOLWINDOW –
:
,
• WS_EX_ACCEPTFILES – . dwExStyle
-
; ,
, dwStyle.
. MSDN. − lpClassName – , RegisterClassEx() . CreateWindowEx() , , , , . . , : , , , – BS_);
• BUTTON – ( • COMBOBOX – LISTBOX • EDIT–
-
( CBS_ – );
EDIT) (
• LISTBOX –
,
, –ES_); ,
(
LBS_OWNERDRAWFIXED )( • MDICLIENT – • RICHEDIT – • RICHEDIT_CLASS – (
( LBS_OWNERDRAW
–LBS_); EDIT − (
; -
– ES_); RICHEDIT – ES_);
14
• SCROLBAR – ( • STATIC –
( – SBS_);
)
−
-
, (
SS_).
−
CreateWindowEx() −
– lpWindowName .
•
,
:
–
•
-
; BUTTON – . dwStyle
. ,
,
,
ИЛИ (
. .
|)
. WS_OVERLAPPEDWINDOW(0x00CF0000). , , , « » , . y– x , . . .
-
-
x . , CW_USEDEFAULT – x y . nWidth nHeight – nWidth nHeight . CW_USEDEFAULT, . hWndParent – ; , , HWND_DESKTOP NULL. CreateWindowEx() hMenu
.
. hMenu , WNDCLASSEX ( hMenu
• NULL;
15
: lpszMenu ).
•
. hMenu , ( WM_COMMAND, ).
hInstance (
hInstance
WinMain). lpParam CreateWindowEx() – , WM_CREATE, NULL. WM_CREATE WM_NCCREATE l Param CREATESTRUCT, CreateWindowEx(). NULL ((void*)0)
, ,
. CreateWindowEx() , . . hWnd ShowWindow(), ShowWindow() BOOL ShowWindow( HWND hWnd, int nCmdShow );
hWnd.
. :
// //
nCmdShow
,
-
.
nCmdShow WM_SIZE
-
, WinMain. ShowWindow() WM_MOVE.
WM_SIZE WM_PAINT.
-
щ
1.4.
. Windows. ( 321
) Windows ( 0x3FF),
. . , . .
.
, 16
:
• WM_MOUSMOUVE – 200h ( • WM_LBUTTONDOWN – 201h ( ); • WM_TIMER – 113h ( • WM_MOVSEACTIVATE–0021h ( . • • • • ,
);
); ).
,
:
WM_GETMINMAXINFO ( WM_ERASEBKGND ( WM_SIZE ( WM_PAINT ( Windows ,
); ); ); ). -
, .
0x3FF. . Win32
, -
. , . : 2while(GetMessage(&Msg, NULL, 0, 0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); }
–
,
, .
–
-
. Windows GetMessage(), , , MSG,
-
. MSG
winuser.h
-
: typedef struct tagMSG{ HWND hWnd; // // UINT message; // //( WPARAM wParam; // LPARAM lParam; // DWORD time; // POINT pt; //
(
) )
17
,
// }Msg; //
tagMSG.
wParam
lParam
-
(message). POINT : typedef struct tagPOINT{ LONG x; LONG y; } POINT;
MSG
,
GetMessage()
. : BOOL GetMessage( LPMSG lpMsg, // HWND hWnd, // UINT uMsgFilterMin, INT uMsgFilterMax );
MSG ,
c
// //
lpMsg MSG,
. hWnd
, GetMessage()
NULL,
.
uMsgFilterMin . WM_KEYFIRST WM_MOUSFIRST .
. uMsgFilterMax
WM_KEYLAST, , WM_MOUSLAST
-
-
. ,
,
. GetMessage() ,
TRUE -
Msg. Windows TranslateMessage() :
DispatchMessage(),
BOOL TranslateMessage(CONST MSG* lpMsg); LRESULT DispatchMessage(CONST MSG* lpMsg);
TranslateMessage() WM_KEYDOWN ( ) . , Message() .
18
WM_KEYUP ) ( WM_CHAR. wParam WM_CHAR GetMessage() Translate. , -
DispatchMessage()
-
,
, Msg ( time pt, .
).
-
, DispatchMessage() while. GetMessage() 0x12),
WM_QUIT (
FALSE, re-
turn Msg.wParam; . . . GetMessage() , « » .
. .
, GetMessage() PeekMessage(),
:
BOOL PeekMessage( LPMSG lpMsg,
// // // HWND hWnd, UINT wMsgFilterMin,
//Min // //Max // //
UINT wMsgFilterMax, UINT wRemoveMsg );
PeekMessage() GetMessage(). wRemoveMsg , :
• PM_NOREMOV – • PM_REMOV – PeekMessage() , , , , , PM_REMOV) ( PM_NOREMOV). PeekMessage() . , , ,
19
,
; . . MSG
(
, . .
PeekMessage()
:
while(1) { if(PeekMessage(&Msg,NULL,0,0, PM_REMOV)) { if(Msg.message==WM_QUIT) break; TranslateMessage(&Msg); DispatchMessage(&Msg); } }
1.5. –
, ,
,
. –
,
. CALLBACK.
, :
LRESULT CALLBACK HWND hWnd, UINT uMsg WPARAM wParam LPARAM lParam
_
( // // //
(
)
,
// // // //
);
, WndProc. switch…case.
:
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(Msg) { case WM_CREATE: //… return TRUE; case WM_KEYDOWN: /* WM_KEYDOWN ( ) */ switch(wParam) { /* Esc */ case VK_ESCAPE:
20
-
SendMessage(hWnd,WM_CLOSE,0,0); break; } break; case WM_DESTROY: /* PostQuitMessage ,
WM_QUIT. , -
Msg.wParam */
PostQuitMessage(0); break; default: /* */ return DefWindowProc(hWnd,msg,wParam,lParam); } return 0L; }
Def-
, WindowProc() (Default Windows Procedure). , « » DefWindowProc, – WM_DESTROY, . , ALT+F4. Windows WM_DESTROY , . Windows.
WM_CREATE , , . .
, ,
,
-
.
PostQuitMessage(0); , – .
, -1.
, -
,
WM_NCCREATE, WM_CREANT,
. :
DispatchMessage() Windows.
щ
1.6.
Windows
: 1)
;
21
2) 3) 4) 5)
; ; ; . WM_MOUSMOVE
.
Msg ( WM_MOUSEMOVE 1. Msg.hWnd – 2. Msg.message – 3. Msg.wParam –
MSG) ; (0x200); ,
(
/
Ctrl
),
Shift ; 4. Msg.lParam – ; 5. Msg.time – 6. Msg.pt – • • • • • .
:
; . :
WM_LBUTTONDOWN (201h) – WM_LBUTTONUP (202h) – WM_RBUTTONDOWN (204h) – WM_RBUTTONUP – WM_LBUTTONDBLCLK (203h) –
; ; ; ; -
MSG
, . . . lParam lParam y.
–
, x, .
, lParam
, lParam,
HIWORD. wParam Shift Ctrl.
-
LOWORD
,
–
.
: MK_LBUTTON – MK_MBUTTON – MK_RBUTTON – MK_SHIFT – MK_CONTROL –
, , .
wParam , ; ; ; Shift Ctrl
; .
22
,
,
Shift WM_LBUTTONDOWM):
( Case WM_LBUTTONDOWM: //… if(wParam & MK_SHIFT) { /* Shift*/ } //…
: WM_LBUTTONDOWN; WM_LBUTTONUP; WM_LBUTTONDBLCLK; WM_LBUTTONUP.
, WM_LBUTTONDBLCLK. C • WM_KEYDOWN –
, (DBLCLK) CS_DBLCLK. :
,
(
• WM_KEYUP – • WM_SYSKEYDOWN –
Alt); ; (
• WM_SYSKEYUP –
Alt); . lParam
: (
1)
0
15
: WM_KEYUP ( 16 23 (24– )–
, 2) 3)
)– ,
;
)–
-
;
Alt
1
, Ctrl
-
; 4) 5) Alt WM_KEYUP, 6)
( 25 (29– ( . . 0); (30–
28 )–
)–
, WM_KEYDOWN
)–
, , 0–
7)
;
;
(31– )– ,1– . wParam
, . .0– , . (
MSDN.): 23
,
.
• • • • •
Shift – Ctrl – Alt – Esc – F1…F12 –
VK_SHIFT ( – 16); VK_CONTROL ( – 17); VK_MENU ( – 18); VK_ESCAPE ( – 27); VK_F1…VK_F12, ( WM_CHAR . TranslateMessage()
WM_CHAR, WM_CHAR GetMessage()
(wParam). , lParam lParam wParam
– 112…123).
. WM_CHAR
-
, .
WM_CHAR
. ,
A
,
: 1. WM_KEYDOWN – 2. WM_CHAR – ANSI 3. WM_KEYUP – WM_COMMAND
A; ‘a’; A. , . . WM_LBUTTONDOWN
-
WM_COMMAND. WM_COMMAND WM_SYSCOMMAND. WM_INITMENU WM_INITMENUPOPUP . , . wParam lParam – : • lParam – ( ), ; • lParam – TRUE, . WM_COMMAND : 1) , , wParam lParam : • wParam (wid); • wParam (wCmd); • lParam . 2) (keyboard accelerator), wParam lParam :
24
,
–
-
-
•
• • 3)
wParam (wid); wParam lParam . ( , wParam lParam wParam (wid); wParam
•
•
•
(wCmd); ,
. .), : , . .
,
-
(wCmd); lParam (hWnd). WM_COMMAND
-
: case WM_COMMAND: wid = LOWORD(wParam); hWnd = (HWND)(UINT)lParam; wCmd = HIWORD(wParam); //…
WM_CLOSE
,
Alt+F4,
. , DefWindowPro() DestroyWindow(), ( , ) WM_DESTROY. DestroyWindow() . « ?», , , . . WM_CLOSE, : case WM_CLOSE: if(MessageBox(hWnd,” Ы ?”, ” ”, MB_YESNO | ICONQUESTION) == IDYES) DestroyWindow(hWnd); break;
WM_SIZE . wParam ( . MSDN): • SIZE_MAXIMIZED – • SIZE_MINIMIZED – • SIZE_RESTORED – SIZE_MINIMIZED. lParam ,
-
; ; ,
SIZE_MAXIMIZED -
–
25
.
WM_MOVE •
lParam
.
-
,
-
: lParam
•
x;
lParam
y.
WM_CREATE . , (
, : ,
),
. . lParam WM_CREATE CREATESTRUCT, CreateWindowEx(). WM_CREATE
WndProc ,
Windows . WM_MOUSMOVE, MSG.
, WM_CREATE dows
,
Win-
. ,
,
, : • WM_INITDIALOG ( • WM_SYSCOMMAND ( • WM_DESTROY ( WM_PAINT
); ); ). , (invalid)
.
-
, WM_PAINT
• • •
, . . -
:
; ; -
•
; . . , WM_PAINT. , ,
. . WM_PAINT
(device context) ,
.
26
-
. ,
, ,
-
, :
,
,
,
.
. WM_PAINT BeginPaint(), : HDC BeginPaint(HWND hWnd, LPPAINSTRUCT lpPaintStruct); hWnd , , . . lpPaintStruct – . PAINTSTRUCT, Windows , WM_PAINT. PAINTSTRUCT winuser.h : typedef struct tagPAINTSTRUCT { HDC hDc; // BOOL fErase; // // RECT rcPaint; // // BOOL fRestor; // BOOL fIncUpdate; // BYTE rgbReserved[32]; // } PAINSTRUCT;
,
fErase
. .
TRUE. Windos
, -
. wc.hbrBackground. rcPaint
, RECT,
. InvalidateRect()
InvalidateRgn().
BeginPaint() , WM_ERASEBKGND.
. ,
-
. DefWindowProc(). BeginPaint()
WM_NCPAINT, . (rectangle),
, :
27
typedef struct tagRECT { LONG left; //x– // LONG top; //y– // LONG right; //x– // LONG bottom //y– // }RECT;
RECT Windows
, . -
WM_PAINT EndPaint(), : BOOL EndPaint(HWND hWnd,CONST PAINTSTRUCT* lpPaintStruct);
WM_PAINT: Case WM_PAINT: hDc=BeginPaint(hWnd, &ps); // //… EndPaint(hWnd, &ps); //…
GDI
.
-
SendMessage(),
-
: LRESULT SendMessage( HWND hWnd, // UINT Msg, // WPARAM wParam, // LPARAM lParam // );
SendMessage() . . SendMessage(),
, Windows .
,
-
,
, SendMessage(). 1.7. 1.1, : WinMain ,
-
WndProc. ,
.
28
,
щ
1.7.1. –
.
,
, . .
:
(CS), WinMain
-
(DS)
(SS), . (
.
1.1): TCHAR szText [80]; USHORT regCS,regDS,regSS; __asm{ mov regCS,CS mov regDS,DS mov regSS,SS } wsprintf(szText, _T(” CS=%X, DS=%X, SS=%X\n WinMain=%X\n szClassName=%X”), regCS, regDS, regSS, WinMain, szClassName); MessageBox(NULL, szText, _T(“ ”), MB_ICONINFORMATION);
–
(
. 1)
.1 wsprintf()
:
int wsprintf(LPSTR lpBuffer, LPCTSTR lpszFormatString, [arguments] );
wsprintf()
.
([arguments]) lpszFormatString ( ) lpBuffer ( ). lpszFormatString – , :
%[-][#][.],
29
[−] –
, ; [#] – – 0X; [] , . . , ; – – , . . , : – , :
–
(szText,”Э
%S,
%#X”,lpStr, lpStr).
[arguments] –
,
-
. wsprintf() . MessageBox()
,
. int MessageBox( HWND hWnd, // LPCTSTR lpText, // LPCTSTR lpCaption, UINT uType );
-
:
// //
hWnd
, . NULL.
lpText , lpCaption – . uType ,
,
, . . , • •
•
• •
,
.
, ,
. :
MB_YESNO – MB_OKCANCEL – cel; MB_YESNOCANCEL – No Cansel. MB_ICONINFORMATION – MB_ICONSTOP – ,
: Yes No; : Ok Can: Yes, ; . ,
-
. MessageBox() , : • IDCANCEL – NO; • IDNO – OK; • IDOK –
30
, Cancel;
-
• IDES –
YES. . MSDN. (CS)
Windows XP (DS, ES) –
0x23.
0x1B, , ,
-
, .
,
. 1.7.2. (
. . 1.7.4)
Text(),
Draw-
:
int DrawText( HDC hDc, // LPCTSTR lpString, // int nLenght, // LPRECT lpRect, // UINT uFormat // );
, nLenght
lpString RECT.
в1,
-
. ( GetClientRect():
) BOOL GetClientRect( HWND hWnd, // LPRECT lpRect // // );
-
RECT
GetClientRect()
, ,
RECT. right .
,
,
left
, bottom
top
-
uFormat, :
,
DT_SINGLELINE | DT_CENTR | DT_VCENTR
DT_SINGLELINE ; DT_CENTR –
, ; DT_VCENTR –
31
-
1.7.3. : 0x00bbggrr, . RGB(R,G,B).
32b –
( ,g –
COLORREF), ,r–
-
(text color) SetTextColor(), COLORREF SetTextColor(HDC hDc, COLORREF crColor);
color), . . COLORREF
(background , :
, SetBkColor(),
SetBkColor(HDC hDc, COLORREF crColor);
hDc –
,
crColor –
. . (background
mode)
mix
SetBkMode,
int SetBkMode(HDC hDc, int uMode);
uMode : OPAQUE –
, . . ; TRANSPARENT – , -
, . . .
, : –
, – OPAQUE.
–
1.7.4.Т
,
Windows Windows
, .
,
, . SetTimer() WM_TIMER.
, UINT SetTimer( HWND hWnd,
// // UINT uTimerID, // UINT uInterval,// // TIMERPROC fnTimerProc
// //
);
32
-
uInterval (hWnd). fnTimerProc ,
-
WM_TIMER,
CALLBACK– .
NULL ,
, fnTimerProc. , DispatchMessage() , . . , CALLBACK– . , CALLBACK– .
, WM_TIMER
-
: VOID CALLBACK TimerProc( HWND hWnd, // UINT uMsg, //WM_TIMER UINT_PTR idEvent, // DWORD dwTime // // );
, Windows
-
, SetTimer() NULL. KillTimer() BOOL KillTimer( HWND hWnd, UINT_PTR idEvent );
.
// //
1.7.5. Windows. /* /* #define UNICODE #ifdef UNICODE #define _UNICODE #endif */ #define STRICT #include #include /* tchar.h ,
*/
, UNICODE,
33
UNICODE ANSI
,
, string.h
*/ #include /* */ LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); /* */ void OutTimeDate(HWND); /* */ TCHAR szCurrentTime[40]; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { /* */ TCHAR szTitle[]=_TEXT(" "); /* */ TCHAR szWindowClass[]=_TEXT("QWERTY"); /* msg
MSG
*/ MSG msg; /* wcex
WNDCLASSEX
*/ WNDCLASSEX wcex; HWND hWnd; // /* , if(hWnd=FindWindow(szWindowClass,NULL)) { /* , if(IsIconic(hWnd)) ShowWindow(hWnd,SW_RESTORE); /* SetForegroundWindow(hWnd); return FALSE; } /* wcex*/ memset(&wcex,0,sizeof(WNDCLASSEX)); /* */ wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; /* wcex.lpfnWndProc = (WNDPROC)WndProc; //wcex.cbClsExtra = 0; //wcex.cbWndExtra = 0; wcex.hInstance = hInstance;// /* , LoadIcon(hInstance, MAKEINTRESOURCE(IDI_?_?))
34
*/ */ */
*/
*/ wcex.hIcon = (HICON)LoadImage(hInstance, IDI_APPLICATION, IMAGE_ICON,32,32,0); /* */ wcex.hCursor = LoadCursor(NULL, IDC_ARROW); /* : wcex.hbrBackground=(HBRUSH)(COLOR_APPWORKSPACE+1); wcex.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH); GetStockBrush(), windowsx.h */ wcex.hbrBackground=GetStockBrush(LTGRAY_BRUSH); /*wcex.lpszMenuName = MAKEINTRESOURCE(IDC_MSG_1);*/ /* */ wcex.lpszClassName /*
= szWindowClass; , wcex.hIconSm, LoadImage()
*/ wcex.hIconSm = NULL; if(!RegisterClassEx(&wcex)) return FALSE; /* */ hWnd = CreateWindowEx(WS_EX_WINDOWEDGE,szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,0,CW_USEDEFAULT,0, NULL, NULL, hInstance, NULL); if (!hWnd) return FALSE; /* . , , */ TCHAR szAsm[80]; USHORT regCS,regDS,regES,regSS; __asm{ mov regCS,CS mov regDS,DS mov regES,ES mov regSS,SS } wsprintf((LPTSTR)szAsm,_T("CS=%X,DS=%X\nES=%X,SS=%X\n WinMain=%X\nszWindowClass=%X"), regCS,regDS,regES,regSS); MessageBox(NULL,(LPCTSTR)szAsm,_T(" "), MB_ICONINFORMATION); /* */ ShowWindow(hWnd, SW_SHOWNORMAL); /* UpdateWindow() WM_PAINT , InvalidateRect() WM_PAINT ,
35
-
*/ UpdateWindow(hWnd); /* */ while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } /* */ return (int)msg.wParam; } /* */ LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { /* */ int xSize=500; /* */ int ySize=300; /* PAINTSTRUCT , BeginPaint */ PAINTSTRUCT ps; /* TEXTMETRIC */ TEXTMETRIC tm; /* LOGFONT */ LOGFONT lf; static HFONT hFont, hOldFont; /* RECT */ RECT rect; LPMINMAXINFO lpmmi; /* */ HDC hDc; UINT width, height; /* */ LPTSTR lpszFace=_TEXT("Times New Roman Cyr"); switch (message) { /* (msg)*/ case WM_CREATE: /* , SetClassLong(hWnd, GCL_HBRBACKGROUND, (LONG)CreateSolidBrush(RGB(200,160,255)); (hInstance) , : hInst = GetModuleHandle(NULL); hInst = (HINSTANCE)GetClassLong(hWnd,GCL_HMODULE); */ /* lf*/ memset(&lf,0,sizeof(lf)); /* */
.
36
lf.lfHeight=30; /* */ lstrcpy(lf.lfFaceName,lpszFace); /* */ hFont = CreateFontIndirect(&lf); /* */ OutTimeDate(hWnd); /* SetTimer 1 */ SetTimer(hWnd,1,1000,(TIMERPROC)NULL); return TRUE; case WM_TIMER: /* OutTimeDate , . */ OutTimeDate(hWnd); break; case WM_KEYDOWN: /* .*/ switch(wParam) { case VK_ESCAPE: /* WM_CLOSE (hWnd), , , SendMessage. */ SendMessage(hWnd,WM_CLOSE,0,0); break; } break; case WM_COMMAND: switch(LOWORD(wParam)) //switch(wParam) { case ID_FILE_TEST: //… break; } break; case WM_PAINT: /* */ hDc = BeginPaint(hWnd, &ps); /* */ GetClientRect(hWnd,&rect); /* */ hOldFont=SelectFont(hDc,hFont); /* */ GetTextMetrics(hDc,&tm); /* SetBkMode . TRANSPARENT , . . . */
37
-
-
SetBkMode(hDc,TRANSPARENT); /* SetTextColor , .
!
*/ SetTextColor(hDc,RGB(0,0,128)); DrawText(hDc,szCurrentTime,-1,&rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER); /* */ EndPaint(hWnd, &ps); break; case WM_CLOSE: /* WM_CLOSE -
-
*/ if(MessageBox(hWnd,_T("
?"), _T("Bad boys"), MB_YESNO | MB_ICONQUESTION)==IDYES) {
/* DestroyWindow , . . WM_DESTROY. PostQuitMessage, */ DestroyWindow(hWnd); } break; case WM_SIZE: /* width height
WM_QUIT
*/ width=LOWORD(lParam); height=HIWORD(lParam); //... break; case WM_LBUTTONDOWN: /* Shift ?*/ if(wParam & MK_SHIFT) { MessageBox(hWnd,_T(" \nShift"), _T(" !"), MB_OK|MB_ICONEXCLAMATION); } break; case WM_GETMINMAXINFO: lpmmi=(LPMINMAXINFO)lParam; lpmmi->ptMinTrackSize.x=xSize; lpmmi->ptMinTrackSize.y=ySize; lpmmi->ptMaxTrackSize.x=xSize; lpmmi->ptMaxTrackSize.y=ySize; break; case WM_DESTROY: /* DeleteObject .
38
, ,
,
,
.
*/ /* DeleteObject(hFont); /* KillTimer KillTimer(hWnd,1); /*PostQuitMessage()
*/ */ WM_QUIT.
-
,
wParam
*/ PostQuitMessage(0); break; default: /*
*/
return DefWindowProc(hWnd, message,wParam, lParam); } return 0L; } /* */ void OutTimeDate(HWND hWnd) { LPTSTR szDay[]={_T(" ."),_T(" ."),_T(" ."), _T(" ."),_T(" ."), _T(" ."),_T(" .") }; LPTSTR szMonth[]={_T(""),_T(" ."),_T(" ."), _T(" "),_T(" ."), _T(" "),_T(" "), _T(" "),_T(" ."), _T(" ."),_T(" ."), _T(" ."),_T(" .") }; TCHAR szT[20]; SYSTEMTIME SystemTime; /* GetLocalTime , , . . SYSTEMTIME . */ GetLocalTime(&SystemTime); /* */ lstrcpy(szCurrentTime, szDay[SystemTime.wDayOfWeek]); /* */ lstrcat((LPTSTR)szCurrentTime,_T(" ")); /* */ lstrcat((LPTSTR)szCurrentTime, szMonth[SystemTime.wMonth]); /* */ lstrcat((LPTSTR)szCurrentTime,_T(" ")); /* */
39
-
wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wDay); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ lstrcat((LPTSTR)szCurrentTime,_T(" ")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wYear); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); lstrcat((LPTSTR)szCurrentTime,_T("---")); /** */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wHour); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ lstrcat((LPTSTR)szCurrentTime,_T(":")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wMinute); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ lstrcat((LPTSTR)szCurrentTime,_T(":")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wSecond); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ InvalidateRect(hWnd,NULL,TRUE); }
. 2.
. 2. 2.
2.1.
,
, ,
,
. .
40
,
-
.RC. , .RES. .OBJ
-
.RES
.EXE. ,
-
. . 2.2. Windows. (menu bar), (main menu) .
. , ,
. ,
-
. (hotkey), . :
1)
-
–
(
)
-
, ; 2) pup menu) • •
(po-
– ,
. :
WM_COMMAND,
– , . .
; -
–
, . . -
( , ). (enabled),
(disabled) . (enabled),
-
(grayed). (disabled) (grayed)
41
,
.
,
(grayed).
EnableMenuItem(),
:
BOOL EnableMenuItem( HMENU hMenu, UINT uIDEnableItem, UINT uEnable
// // // //
);
EnableMenuItem() , . , uIDEnableItem.
, , ,
. в uEnable.
uEnable
. • MF_BUCOMMAND –
:
• MF_BYPOSITION –
uIDEnableItem
-
uIDEnableItem . :
-
; ,
; • MF_ENABLED – ; • MF_DISABLED – . • MF_GRAYED – : MF_BYCOMMAND | MF_GRAYED.
EnableMenuItem() , DrawMenuBar() DrawMenuBar():
.
-
BOOL DrawMenuBar(HWND hWnd);
«
(
»)
.
(check box). . -
. ) : DWORD ChecMenuItem( HMENU hMenu, // UINT uIDCheckItem, // UINT uCheck // // );
42
( CheckMenuItem(),
ChecMenuItem() ,
-
в1.
. , uIDCheckItem.
,
-
, . − uCheck. uCheck
.
• MF_BUCOMMAND –
: uIDCheckItem
• MF_BYPOSITION –
-
; uIDCheckItem .
,
-
• MF_CHECKED – • MF_UNCHECKED – : MF_BYCOMMAND|MF_CHECKED.
: ; . -
MF_BYCOMMAND. dio button),
,
(ra. . .
,
, . -
(
CheckMenuRadioItem(),
)
BOOL CheckMenuRadioItem( HMENU hMenu, // UINT idFirst, // // UINT idLast, // // UINT idCheck, // // UINT uFlags // //idLast );
:
idFirst, idCheck
CheckMenuRadioItem() , . , idCheck.
, idFirst, idLast
. uFlags,
в1.
-
idCheck , -
: 43
• MF_BYCOMMAND –
• MF_BYPOSITION –
; . , resource.h . -
, , .
. ,
.
Windows SetMenuDefaultItem() «
-
»,
: BOOL SetMenuDefaultItem( HMENU hMenu, UINT uItem, UINT fByPos );
(hMenu)
–
GetMenu(), GetSubMenu().
:
HMENU GetMenu(HWND hWnd); HMENU GetSubMenu( HWND hWnd, // int nPos // // );
–
GetMenu()
-
NULL,
hWnd
. nPos GetMenu() •
nPos ,
, NULL.
. -
. : ,
( Project -> Add -
Visual Studio Resourse -> Menu , ); , . . • CreateMenu(), AppendMenu(), CreatePopupMenu() SetMenu(); LoadMenuIndi• rect(), ( – SetMenu()).
44
LoadMenuIndirect(): HMENU LoadMenuIndirect(CONST MENUTEMPLATE * lpMenuTemplate);
, , lpszMenuName , IDR_MENU1 ( ), ,
, -
WNDCLASSEX.
MAKEINTRESOURCE,
:
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
CreateWindowEx() NULL. ,
hMenu . , . .
, -
, : 1) 2)
LoadMenu() , ,
; SetMenu(), (hMenu),
LoadMenu() ( ). LoadMenu() SetMenu()
-
: HMENU LoadMenu(HINSTANCE hInstance,LPCTSTR lpMenuName); BOLL SetMenu(HWND hWnd, HMENU hMenu);
, . . (
2.3). ,
. , popup ( )
, -
. . .
,
: 1) CreateMenu() 2) 3) 4) ; 5) 6) 7)
, , CreatePopupMenu(); AppendMenu() ; , , . 1,2 3 ,
, ; CreateMenu(); -
AppendMenu(); SetMenu(); 45
DrawMenuBar(), , , UpdateWindow(). DrawMenuBar(), CreateMenu() CreatePopupMenu() : 8)
-
BOOL DrawMenuBar(HWND hWnd); HMENU CreateMenu(VOID); HMENU CreatePopupMenu(VOID);
AppendMenu()
. ,
BOOL AppendMenu( HMENU hMenu,
// // UINT uFlags // // UINT idNewItem, // // LPCTSTR lpszNewItem
. ,
// //
(
uFlags)
);
• • • •
uFlags ( MF_POPUP – MFS_CHECKED – MFS_DEFAULT – MFS_GRAYED –
• MFS_HILITE – • MFT_STRING –
MSDN): ; ;
,
; -
; ;
. DestroyMenu() , , , CreateMenu() CreatePopupMenu(). , . , DestroyMenu() , . DestroyMenu(): BOOL DestroyMenu(HMENU hMenu);
2.3. /* resource.h */ // //{{NO_DEPENDENCIES}} // Microsoft Visual C++ generated include file. // Used by 1111.rc 46
-
, -
// #define IDD_DIALOGBAR #define IDC_BUTTON1 #define IDC_STATIC_1
103 1002 1003
// Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE #define _APS_NEXT_COMMAND_VALUE #define _APS_NEXT_CONTROL_VALUE #define _APS_NEXT_SYMED_VALUE #endif #endif
101 40001 1004 101
1111.rc ,
:
//////////// // Microsoft Visual C++ generated resource script. // #include "resource.h" //... /////////////////////////////////////////////////////////// // // Dialog // IDD_DIALOGBAR DIALOGEX 0, 0, 153, 62 STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU CAPTION " " FONT 10, "Lucida Console", 700, 0, 0xCC BEGIN PUSHBUTTON "Ok",IDOK,105,42,41,14,BS_CENTER CTEXT " \n \n2009", IDC_STATIC,44,14,63,26,WS_TABSTOP END /////////////////////////////////////////////////////////// /* 1111.cpp */ /* */ /* #define UNICODE #ifdef UNICODE #define _UNICODE #endif */ #define STRICT #include #include /* tchar.h , UNICODE , UNICODE, ANSI , , string.h */ #include
47
#include "resource.h" /* , resource.h */ #define ID_FILE_TEST 40001 #define ID_FILE_EXIT 40002 #define ID_HELP_ABOUT 40003 /* */ LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); /* */ INT_PTR CALLBACK AboutProc(HWND, UINT, WPARAM, LPARAM); /* */ VOID CALLBACK TimerProc(HWND,UINT,UINT_PTR,DWORD); /* */ void OutTimeDate(HWND); */ /* HMENU hFileMenu,hHelpMenu,hMenu; /* */ HFONT hFont,hOldFont; /* */ TCHAR szCurrentTime[40]; int nTime=5;// 5 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) { /* */ TCHAR szTitle[]=_TEXT(" "); /* */ TCHAR szWindowClass[]=_TEXT("QWERTY"); /* msg
MSG
*/ MSG msg; /* wcex
WNDCLASSEX
*/ WNDCLASSEX wcex; HWND hWnd; // /* , if(hWnd=FindWindow(szWindowClass,NULL)) { /* , if(IsIconic(hWnd)) ShowWindow(hWnd,SW_RESTORE); /* SetForegroundWindow(hWnd); return FALSE;
48
*/ */ */
} /* wcex*/ memset(&wcex,0,sizeof(WNDCLASSEX)); /* */ wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; /* */ wcex.lpfnWndProc = (WNDPROC)WndProc; //wcex.cbClsExtra = 0; //wcex.cbWndExtra = 0; wcex.hInstance = hInstance;// /* , LoadImage() */ wcex.hIcon = (HICON)LoadImage(hInstance, IDI_APPLICATION, IMAGE_ICON,32,32,0); /* */ wcex.hCursor = LoadCursor(NULL, IDC_ARROW); /* : wcex.hbrBackground=(HBRUSH)(COLOR_APPWORKSPACE+1); wcex.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH); GetStockBrush(), windowsx.h */ wcex.hbrBackground=GetStockBrush(LTGRAY_BRUSH); //wcex.lpszMenuName = MAKEINTRESOURCE(IDC_MSG_1); /* */ wcex.lpszClassName = szWindowClass; wcex.hIconSm = NULL; if(!RegisterClassEx(&wcex)) return FALSE; /* */ hWnd = CreateWindowEx(WS_EX_WINDOWEDGE,szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,0,CW_USEDEFAULT,0, NULL, NULL, hInstance, NULL); if (!hWnd) return FALSE; /* . , , */ TCHAR szAsm[80]; USHORT regCS,regDS,regES,regSS; __asm{ mov regCS,CS mov regDS,DS mov regES,ES mov regSS,SS } wsprintf((LPTSTR)szAsm,_T("CS=%X,DS=%X\nES=%X,SS=%X\n WinMain=%X\nszWindowClass=%X"), regCS,regDS,regES,regSS);
49
MessageBox(NULL,(LPCTSTR)szAsm,_T(" "), MB_ICONINFORMATION); /* hFileMenu=CreatePopupMenu() "Test" */ AppendMenu((hFileMenu=CreatePopupMenu()), MF_ENABLED | MFT_STRING, ID_FILE_TEST,_TEXT("&Test")); /* "Exit" */ AppendMenu(hFileMenu,MF_GRAYED | MFT_STRING, ID_FILE_EXIT,_TEXT("&Exit")); /* hHelpMenu=CreatePopupMenu() "About" */ AppendMenu((hHelpMenu=CreatePopupMenu()), MF_ENABLED|MFT_STRING, ID_HELP_ABOUT,_TEXT("&About")); /* hMenu=CreateMenu() "File" " */ AppendMenu((hMenu=CreateMenu()), MF_ENABLED | MF_POPUP, (UINT_PTR)hFileMenu, _TEXT("&File")); /* "Help" */ AppendMenu(hMenu,MF_ENABLED|MF_POPUP, (UINT_PTR)hHelpMenu,_TEXT("&Help")); / * "QUIT" */ AppendMenu(hMenu,MF_GRAYED,(UINT_PTR)11, _TEXT("Quit")); /* */ SetMenu(hWnd,hMenu); /* */ ShowWindow(hWnd, SW_SHOWNORMAL); /* UpdateWindow() WM_PAINT , InvalidateRect() WM_PAINT , */ UpdateWindow(hWnd); /* */ DrawMenuBar(hWnd); /* */ while (GetMessage(&msg, NULL, 0, 0)) {
50
TranslateMessage(&msg); DispatchMessage(&msg); } /* */ return (int)msg.wParam; } /* */ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { /* */ int xSize=500; /* */ int ySize=300; /* PAINTSTRUCT ( BeginPaint) */ PAINTSTRUCT ps; /* TEXTMETRIC */ TEXTMETRIC tm; /* LOGFONT */ LOGFONT lf; /* RECT– , */ RECT rect; LPMINMAXINFO lpmmi; Static HFONT hFont,hOldFont; /* HDC hDc; /* width height
*/
*/ UINT width, height; /* */ LPTSTR lpszFace=_TEXT("Times New Roman Cur"); switch (message) { /* (msg)*/ case WM_CREATE: /* . , SetClassLong(hWnd,GCL_HBRBACKGROUND, (LONG)CreateSolidBrush(RGB(200,160,255));
, : hInst=GetModuleHandle(NULL); hInst=(HINSTANCE)GetClassLong(hWnd,GCL_HMODULE); */ /* lf*/ memset(&lf,0,sizeof(lf)); 51
/* */ lf.lfHeight=30; /* */ lstrcpy(lf.lfFaceName,lpszFace); /* */ hFont=CreateFontIndirect(&lf); /*
*/ OutTimeDate(hWnd);
/* SetTimer 1 */ SetTimer(hWnd,1,1000,(TIMERPROC)NULL); return TRUE; case WM_TIMER: /* OutTimeDate , . */ OutTimeDate(hWnd); break; case WM_KEYDOWN: /* .*/ switch(wParam) { case VK_ESCAPE: /* WM_CLOSE (hWnd), , , SendMessage */ SendMessage(hWnd,WM_CLOSE,0,0); break; } break; case WM_COMMAND: switch(LOWORD(wParam)) //switch(wParam) { case ID_FILE_TEST: /* ID_FILE_EXIT */ EnableMenuItem(hFileMenu,ID_FILE_EXIT, MF_BYCOMMAND|MF_ENABLED); /* ( ) ID_FILE_TEST*/ CheckMenuItem(hFileMenu,ID_FILE_TEST, MF_BYCOMMAND|MF_CHECKED); /* "QUIT"*/ EnableMenuItem(GetMenu(hWnd), (UINT_PTR)11,MF_BYCOMMAND|MF_ENABLED); /* ,
DrawMenuBar
*/ DrawMenuBar(hWnd);
52
/* nTime */ SetTimer(hWnd,2,nTime*1000, (TIMERPROC)TimerProc); break; case ID_FILE_EXIT: /* PostQuitMessage WM_QUIT */ PostQuitMessage(0); /* , . . SendMessage(hWnd,WM_CLOSE,0,0); */ break; case ID_HELP_ABOUT: /* DialogBox IDD_ABOUTBOX, WndProc . */ DialogBox(GetModuleHandle()NULL, MAKEINTRESOURCE(IDD_DIALOGBAR), hWnd, (DLGPROC)AboutProc); break; case (UINT)11: /* PostQuitMessage WM_QUIT */ PostQuitMessage(0); break; } break; case WM_PAINT: /* */ hDc = BeginPaint(hWnd, &ps); /* */ GetClientRect(hWnd,&rect); /* */ hOldFont=SelectFont(hDc,hFont); /* ( )*/ GetTextMetrics(hDc,&tm); /* SetBkMode . TRANSPARENT , . .
-
-
-
*/ SetBkMode(hDc,TRANSPARENT); /* SetTextColor , !
.
*/ SetTextColor(hDc,RGB(0,0,128)); DrawText(hDc,szCurrentTime,-1,&rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER); /* */
53
EndPaint(hWnd, &ps); break; case WM_CLOSE: /* WM_CLOSE -
-
*/ if(MessageBox(hWnd,_T(" ?"), _T(" !"), MB_YESNO | MB_ICONQUESTION)==IDYES) { /* DestroyWindow , . . WM_DESTROY. PostQuitMessage, */ DestroyWindow(hWnd); } break; case WM_SIZE: /* width height
WM_QUIT
*/ width=LOWORD(lParam); height=HIWORD(lParam); //... break; case WM_LBUTTONDOWN: /* Shift ?*/ if(wParam & MK_SHIFT) { MessageBox(hWnd,_T(" \nShift"), _T("Bad boys"), MB_OK|MB_ICONEXCLAMATION); } break; case WM_GETMINMAXINFO: lpmmi=(LPMINMAXINFO)lParam; /* */ lpmmi->ptMinTrackSize.x=xSize; lpmmi->ptMinTrackSize.y=ySize; lpmmi->ptMaxTrackSize.x=xSize; lpmmi->ptMaxTrackSize.y=ySize; break; case WM_DESTROY: /* DeleteObject , , , */ /* */ DeleteObject(hFont); /* KillTimer
54
. ,
*/
-
KillTimer(hWnd,1); /* PostQuitMessage()
WM_QUIT.
-
,
wParam
*/ PostQuitMessage(0); break; default: /* */ return DefWindowProc(hWnd, message, wParam, lParam); } return 0L; } /* */ INT_PTR CALLBACK AboutProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: /* TRUE */ return (INT_PTR)TRUE; case WM_COMMAND: if(LOWORD(wParam)==IDOK || LOWORD(wParam)== IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } /* FALSE */ return (INT_PTR)FALSE; } /* */ void OutTimeDate(HWND hWnd) { LPTSTR szDay[]={_T(" ."),_T(" ."),_T(" ."), _T(" ."),_T(" ."), _T(" ."),_T(" .") }; LPTSTR szMonth[]={_T(""),_T(" _T(" _T(" _T("
55
."),_T(" "),_T(" "),_T(" "),_T("
."), ."), "), ."),
_T(" _T(" }; TCHAR szT[20]; SYSTEMTIME SystemTime; /* GetLocalTime ,
."),_T(" ."),_T("
."), .")
, . . SYSTEMTIME
.
*/ GetLocalTime(&SystemTime); /* */ lstrcpy(szCurrentTime, szDay[SystemTime.wDayOfWeek]); /* */ lstrcat((LPTSTR)szCurrentTime,_T(" ")); /* */ lstrcat((LPTSTR)szCurrentTime, szMonth[SystemTime.wMonth]); /* */ lstrcat((LPTSTR)szCurrentTime,_T(" ")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wDay); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ lstrcat((LPTSTR)szCurrentTime,_T(" ")); */ /* wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wYear); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); lstrcat((LPTSTR)szCurrentTime,_T("---")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wHour); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ lstrcat((LPTSTR)szCurrentTime,_T(":")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wMinute); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ lstrcat((LPTSTR)szCurrentTime,_T(":")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wSecond); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ InvalidateRect(hWnd,NULL,TRUE); } /* */ VOID CALLBACK TimerProc(HWND hWnd,UINT uMsg, UINT_PTR idEvent,DWORD dwTime) { TCHAR szTimer[100]; KillTimer(hWnd,2); wsprintf(szTimer, _T(" \n Test\n %d c!"),
56
nTime); MessageBox(NULL,(LPCTSTR)szTimer, _T(" "),MB_ICONHAND); }
. 3.
. 3.
Щ
3. • Toolbar ( ; • Statusbar (
: )–
-
)–
, . . , .
,
57
InitCommonControlsEx(), . commctrl.h. : #include
, comctl32.lib.
:
BOOL InitCommonControlsEx( LPINITCOMMONCONTROLSEX lpInitCtrls);
lpInitCtrls – INITCOMMONCONTROLSEX,
, .
: typedef struct tagINITCOMMONCONTROLSEX { DWORD dwSize; // DWORD dwICC; // DLL } INITCOMMONCONTROLSEX, *LPINITCOMMONCONTROLSEX;
dwICC ( MSDN): ICC_BAR_CLASSES – toolbar, status bar, slider, tooltip; • • ICC_PROGRESS_CLASS – progress bar; • ICC_TREEVIEW_CLASSES – tree view. Toolbar Statusbar
-
-
WM_CREATE. 3.1. –
,
-
,
. .
-
. , . . . , WM_COMMAND
, . ,
, .
, -
, , .
,
,
(combo box). CreateWindowEx().
58
: 1) 2)
; TBBUTTON,
-
; CreateToolBarEx() .
3)
Project
Visual Studio в> Add Resource в> Toolbar
New. . ,
. , .
, .rc) ( (
-
toolbar1.bmp). IDR_TOOLBAR1.
(1112.rc)
, :
/////////////////////////////////////////////////////////// // // Toolbar // IDR_TOOLBAR1 TOOLBAR 16, 15 BEGIN BUTTON ID_BUTTON40001 BUTTON ID_BUTTON40002 BUTTON ID_BUTTON40003 END /////////////////////////////////////////////////////////// // // Bitmap // IDR_TOOLBAR1
BITMAP
"toolbar1.bmp"
///////////////////////////////////////////////////////////
CreateToolBarEx() TBBUTTON ( TBBUTTON : typedef struct _TBBUTTON{ int iBitmap; // // //( int idCommand; //
− TBBUTTON tbb [Num];). commctrl.h , – ) ,
59
-
BYTE fsState; BYTE fsStyle; DWORD dwData; INT_PTR iString; // } TBBUTTON;
// // // // // //
WM_COMMAND 0L ,
fsState : • TBSTATE_CHECKED –
TBSTYLE_CHECK),
(
• TBSTATE_PRESSED – ; TBSTATE_ENABLED – •
-
; , , . .
-
(
), ,
,
• TBSTATE_HIDDEN – • TBSTATE_INDETERMINATE – ). fsStyle • : TBSTYLE_BUTTON – • ; • TBSTYLE_SEP – ;
(
); ( , , -
; • TBSTYLE_CHECK – (check box); • TBSTYLE_GROUP – ,
, , , . . (radio button);
, • TBSTYLE_CHECKGROUP – TBSTYLE_CHECK TBSTYLE_GROUP. dwData
. .
, TB_GETBUTTON. iString
, ).
( TB_ADDSTRING, lParam). ( ,
-
–
. iString
, . 60
-
CreateToolBarEx() ( InitCommonControlsEx()). CreateToolBarEx(): HWND CreateToolBarEx( HWND hWnd, // DWORD ws, // UINT wID, // int nBitmaps, HINSTANCE hInst,
// // //
UINT wBMID,
// // LPCTBBUTTON lpButtons,
int iNumButtons, int dxButton, int dyButtons, int dxBitmap, // int dyBitmap, // UINT uSructSize // };
// //(TBBUTTON) // // //
ws .
. WS_CHILD. ,
ws
WS_VISIBLE WS_BORDER, − CCS ADJUSTABLE. TBSTYLE_TOOLTIPS . wID . , , resource.h. dxButton, dyButton, dxBitmap dyBitmap NULL, : 16,15,16,15. toolbar WM_CREATE.
ё . . TB_AUTOSIZE -
,
. wParam
WM_SIZE. lParam
, 61
:
SendMessage(hWndTb, TB_AUTOSIZE, 0, 0);
,
.
.
( )
-
BOOL GetWindowRect( HWND hWnd, // // LPRECT lpRect // );
RECT
3.1.1. /* resource.h */ //{{NO_DEPENDENCIES}} // Microsoft Visual C++ generated include file. // Used by 1112.rc // #define IDR_TOOLBAR1 101 #define IDD_DIALOGBAR 103 #define IDC_BUTTON1 1002 #define IDC_STATIC_1 1003 #define ID_BUTTON40001 40001 #define ID_BUTTON40002 40002 #define ID_BUTTON40003 40003 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE #define _APS_NEXT_COMMAND_VALUE #define _APS_NEXT_CONTROL_VALUE #define _APS_NEXT_SYMED_VALUE #endif #endif /* 1112.rc 59*/ /* 1112.cpp */ /*#define UNICODE #ifdef UNICODE #define _UNICODE #endif */ #define STRICT #include #include #include 62
103 40004 1004 101 .
/* tchar.h
, , ,
UNICODE, ,
UNICODE ANSI
string.h */ #include #include "resource.h" /* ,
resource.h */ #define ID_FILE_TEST 40001 #define ID_FILE_EXIT 40002 #define ID_HELP_ABOUT 40003 /* */ #define IDT_TOOLBAR 400 /* */ LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); /* */ INT_PTR CALLBACK AboutProc(HWND, UINT, WPARAM, LPARAM); /* . */ VOID CALLBACK TimerProc(HWND,UINT,UINT_PTR,DWORD); /* . */ void OutTimeDate(HWND); /* */ HMENU hFileMenu, hHelpMenu, hMenu; /* */ HFONT hFont, hOldFont; /* . */ HWND hWndTb; /* */ TCHAR szCurrentTime[40]; /* 5 */ int nTime=5; /* TBBUTTON*/ TBBUTTON tBb[]= { {0,ID_FILE_TEST,TBSTATE_ENABLED,TBSTYLE_BUTTON,0L,0}, {1,ID_FILE_EXIT,TBSTATE_ENABLED,TBSTYLE_BUTTON,0L,0}, {0,0,TBSTATE_ENABLED,TBSTYLE_SEP,0L,1}, {2,ID_HELP_ABOUT,TBSTATE_ENABLED,TBSTYLE_BUTTON,0L,0} };
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) {
63
/* */ TCHAR szWindowClass[]=_TEXT("QWERTY"); /* */ TCHAR szTitle[]=_TEXT(" /* Msg Windows */ MSG msg; /* C wcex
"); MSG
-
WNDCLASSEX
*/ WNDCLASSEX wcex; /* */ HWND hWnd; /* , */ if(hWnd=FindWindow(szWindowClass,NULL)) { /* , */ if(IsIconic(hWnd)) ShowWindow(hWnd,SW_RESTORE); /*
.
.
. */
SetForegroundWindow(hWnd); return FALSE; } /* wcex*/ memset(&wcex,0,sizeof(WNDCLASSEX)); /* */ wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; /* */ wcex.lpfnWndProc= (WNDPROC)WndProc; //wcex.cbClsExtra = 0; //wcex.cbWndExtra = 0; wcex.hInstance = hInstance; // /* , LoadIcon(hInstance, MAKEINTRESOURCE(IDI_?_?)) */ wcex.hIcon = (HICON)LoadImage(hInstance, IDI_APPLICATION, IMAGE_ICON,32,32,0); /* */ wcex.hCursor = LoadCursor(NULL, IDC_ARROW); /* :
64
wcex.hbrBackground=(HBRUSH)(COLOR_APPWORKSPACE+1); wcex.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH); GetStockBrush(), windowsx.h */ wcex.hbrBackground=GetStockBrush(LTGRAY_BRUSH); /*wcex.lpszMenuName = MAKEINTRESOURCE(IDC_MSG_1);*/ /* */ wcex.lpszClassName = szWindowClass; wcex.hIconSm = NULL; /* LoadImage()*/ if(!RegisterClassEx(&wcex)) return FALSE; /* Common Control Library */ INITCOMMONCONTROLSEX iCc; iCc.dwSize=sizeof(INITCOMMONCONTROLSEX); iCc.dwICC=ICC_WIN95_CLASSES; InitCommonControlsEx(&iCc); /* */ hWnd = CreateWindowEx(WS_EX_WINDOWEDGE,szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,0,CW_USEDEFAULT,0, NULL, NULL, hInstance, NULL); if (!hWnd) return FALSE; /* . , , */ TCHAR szAsm[80]; USHORT regCS,regDS,regES,regSS; __asm{ mov regCS,CS mov regDS,DS mov regES,ES mov regSS,SS } wsprintf((LPTSTR)szAsm,_T("CS=%X,DS=%X\nES=%X,SS=%X\n WinMain=%X\nszWindowClass=%X"), regCS,regDS,regES,regSS); MessageBox(NULL,(LPCTSTR)szAsm,_T(" "), MB_ICONINFORMATION); /* hFileMenu=CreatePopupMenu() "Test" */ AppendMenu((hFileMenu=CreatePopupMenu()), MF_ENABLED | MFT_STRING, ID_FILE_TEST,_TEXT("&Test")); /*
65
"Exit" */ AppendMenu(hFileMenu,MF_GRAYED | MFT_STRING, ID_FILE_EXIT,_TEXT("&Exit")); /* hHelpMenu=CreatePopupMenu() "About" */ AppendMenu((hHelpMenu=CreatePopupMenu()), MF_ENABLED|MFT_STRING, ID_HELP_ABOUT,_TEXT("&About")); /* hMenu=CreateMenu() "File" " */ AppendMenu((hMenu=CreateMenu()), MF_ENABLED | MF_POPUP, (UINT_PTR)hFileMenu, _TEXT("&File")); /* "Help" */ AppendMenu(hMenu,MF_ENABLED|MF_POPUP, (UINT_PTR)hHelpMenu,_TEXT("&Help")); / * "QUIT" */ AppendMenu(hMenu,MF_GRAYED,(UINT_PTR)11, _TEXT("Quit")); /* */ SetMenu(hWnd,hMenu); /* */ ShowWindow(hWnd, SW_SHOWNORMAL); /* UpdateWindow() WM_PAINT , InvalidateRect() WM_PAINT , */ UpdateWindow(hWnd); /* */ DrawMenuBar(hWnd); /*
*/
while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } /* */ return (int)msg.wParam; } /* */ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
66
{
/* int xSize=500; /* int ySize=300; /* PAINTSTRUCT ( */ PAINTSTRUCT ps; /* TEXTMETRIC -
*/ */ BeginPaint).
. */ TEXTMETRIC tm; /* LOGFONT . */ LOGFONT lf; Static HFONT hFont,hOldFont; /* RECT– , */ RECT rect, rcTb; HDC hDc; /* HeightTb */ static UINT, HeightTb; /* width height . */ UINT width,height; int aWidth[PartN],SbWidth;
.
LPTSTR lpszFace=_TEXT("Times New Roman Cyr"); switch (message) { case WM_CREATE: /* , SetClassLong(hWnd, GCL_HBRBACKGROUND, (LONG)CreateSolidBrush(RGB(200,160,255)); (hInstance) , : hInst = GetModuleHandle(NULL); hInst = (HINSTANCE)GetClassLong(hWnd,GCL_HMODULE); */ /* lf*/ memset(&lf,0,sizeof(lf)); /* */ lf.lfHeight=30; /* */ lstrcpy(lf.lfFaceName,lpszFace); /* */ hFont=CreateFontIndirect(&lf); /* */ OutTimeDate(hWnd); /* SetTimer
.
67
1 */ SetTimer(hWnd,1,1000,(TIMERPROC)NULL); /* Toolbar*/ hWndTb=CreateToolbarEx(hWnd, WS_CHILD|WS_VISIBLE|WS_BORDER|TBSTYLE_TOOLTIPS, /* */ IDT_TOOLBAR, 3, GetModuleHandle(NULL), /* */ IDR_TOOLBAR1, (LPCTBBUTTON)tBb, 4, 0,0, 0,0, sizeof(TBBUTTON)); if(hWndTb==NULL) return FALSE; /* ToolBar (
).
*/ GetWindowRect(hWndTb,&rcTb); /* ToolBar .*/ HeightTb=rcTb.bottom-rcTb.top; return TRUE; case WM_TIMER: /* OutTimeDate , . */ OutTimeDate(hWnd); break; case WM_KEYDOWN: switch(wParam) /* switch(wParam) { case VK_ESCAPE: /*
-
WM_CLOSE
.*/
(hWnd),
,
, SendMessage
*/ SendMessage(hWnd,WM_CLOSE,0,0); break; } break; case WM_COMMAND: switch(LOWORD(wParam)) //switch(wParam) { case ID_FILE_TEST: /* ID_FILE_EXIT. */
68
EnableMenuItem(hFileMenu,ID_FILE_EXIT, MF_BYCOMMAND|MF_ENABLED); /* ( ) ID_FILE_TEST */ CheckMenuItem(hFileMenu,ID_FILE_TEST, MF_BYCOMMAND|MF_CHECKED); /* "QUIT"*/ EnableMenuItem(GetMenu(hWnd), (UINT_PTR)11,MF_BYCOMMAND|MF_ENABLED); /* ,
DrawMenuBar
*/ DrawMenuBar(hWnd); /* nTime */ SetTimer(hWnd,2,nTime*1000, (TIMERPROC)TimerProc); break; case ID_FILE_EXIT: /* PostQuitMessage WM_QUIT */ PostQuitMessage(0); /* , . . , SendMessage(hWnd,WM_CLOSE,0,0) WM_CLOSE; */ break; case ID_HELP_ABOUT: /* DialogBox IDD_ABOUTBOX, WndProc */ DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOGBAR), hWnd, (DLGPROC)AboutProc); break; case (UINT)11: /* PostQuitMessage WM_QUIT */ PostQuitMessage(0); break; } break; case WM_PAINT: /* */ hDc = BeginPaint(hWnd, &ps); /* */ hOldFont=SelectFont(hDc,hFont); /* ( )*/ GetTextMetrics(hDc,&tm);
69
-
-
/* c */ GetClientRect(hWnd,&rect); rect.top+=HeightTb;
.
/* SetBkMode TRANSPARENT -
. ,
. .
*/ SetBkMode(hDc,TRANSPARENT); /* SetTextColor , !
.
*/ SetTextColor(hDc,RGB(0,0,128)); DrawText(hDc,szCurrentTime,-1,&rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER); /* */ EndPaint(hWnd, &ps); break; case WM_CLOSE: /* WM_CLOSE -
-
*/ if(MessageBox(hWnd,_T(" ?"), _T(" !"), MB_YESNO | MB_ICONQUESTION)==IDYES) { /* DestroyWindow , . . WM_DESTROY. PostQuitMessage, */ DestroyWindow(hWnd); } break; case WM_SIZE: /* width
height
*/ width=LOWORD(lParam); height=HIWORD(lParam); /* Toolbar , SendMessage(hWndTb,WM_SIZE,wParam,lParam); */ SendMessage(hWndTb,TB_AUTOSIZE,0,0); break;
70
WM_QUIT
case WM_LBUTTONDOWN: if(wParam & MK_SHIFT) { MessageBox(hWnd,_T(" \nShift"), _T(" !"), MB_OK | MB_ICONEXCLAMATION); } break; case WM_GETMINMAXINFO: lpmmi=(LPMINMAXINFO)lParam; /* */ lpmmi->ptMinTrackSize.x=xSize; lpmmi->ptMinTrackSize.y=ySize; lpmmi->ptMaxTrackSize.x=xSize; lpmmi->ptMaxTrackSize.y=ySize; break; case WM_DESTROY: /* DeleteObject . , , , , . */ /* */ DeleteObject(hFont); /* KillTimer */ KillTimer(hWnd,1); /* PostQuitMessage() WM_QUIT. , wParam */ PostQuitMessage(0); break; default: /* */ return DefWindowProc(hWnd, message, wParam,lParam); } return 0L; } /* */ INT_PTR CALLBACK AboutProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG: /* TRUE. */ return (INT_PTR)TRUE; case WM_COMMAND: if(LOWORD(wParam)==IDOK || LOWORD(wParam)== IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE;
71
} break; } /* FALSE. */ return (INT_PTR)FALSE; } /* */ void OutTimeDate(HWND hWnd) { LPTSTR szDay[]={_T(" ."),_T(" ."),_T(" ."), _T(" ."),_T(" ."), _T(" ."),_T(" .") }; LPTSTR szMonth[]={_T(""),_T(" ."),_T(" ."), _T(" "),_T(" ."), _T(" "),_T(" "), _T(" "),_T(" ."), _T(" ."),_T(" ."), _T(" ."),_T(" .") }; TCHAR szT[20]; SYSTEMTIME SystemTime; /* GetLocalTime , , . . SYSTEMTIME . */ GetLocalTime(&SystemTime); /* */ lstrcpy(szCurrentTime, szDay[SystemTime.wDayOfWeek]); /* */ lstrcat((LPTSTR)szCurrentTime,_T(" ")); /* */ lstrcat((LPTSTR)szCurrentTime, szMonth[SystemTime.wMonth]); /* */ lstrcat((LPTSTR)szCurrentTime,_T(" ")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wDay); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ lstrcat((LPTSTR)szCurrentTime,_T(" ")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wYear); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); lstrcat((LPTSTR)szCurrentTime,_T("---")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wHour); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT);
72
-
/* */ lstrcat((LPTSTR)szCurrentTime,_T(":")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wMinute); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ lstrcat((LPTSTR)szCurrentTime,_T(":")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wSecond); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ InvalidateRect(hWnd,NULL,TRUE); } /* */ VOID CALLBACK TimerProc(HWND hWnd,UINT uMsg, UINT_PTR idEvent,DWORD dwTime) { TCHAR szTimer[100]; KillTimer(hWnd,2); wsprintf(szTimer, _T(" \n Test\n %d c!"), nTime); MessageBox(NULL,(LPCTSTR)szTimer, _T(" "),MB_ICONHAND); }
. 4.
. 4. 3.2. (status bar) –
, . ,
73
-
,
.
CreateStatusWindow(),
:
HWND CreateStatusWindow( LONG style, // – //WS_CHILD | WS_VISIBLE LPCTSTR lpszText, // status bar HWND hWndParent, // UINT wID // status bar );
CreateStatusWindow() , lpszText. –
– NULL.
, :
1)
–
, ;
2)
–
, . -
SB_SIMPLE. lParam wParam , TRUE – status bar .
: FALSE , – :
SendMessage(hWndSb,SB_SETTEXT,0,
, SB_SETPARTS
-
,
:
SendMessage(hWndSb, SB_SETPARTS, 3, (LPARAM)ptWidth);
wParam
lParam −
,
-
, , ( status bar
status bar. ), , –1. -
. (progress bar). , SB_GETRECT, :
,
SendMessage(hWndSb, SB_GETRECT,2,(LPARAM)&rect);
wParam −
lParam –
,
,
RECT, SB_SETTEXT.
. :
SendMessage(hWndSb,SB_SETTEXT,0,
74
(LPARAM)”\tStatus bar Part 1 ”); SendMessage(hWndSb, SB_SETTEXT,1 | SBT_NOBORDERS, (LPARAM)””);
wParam
,
, • SBT_NOBORDERS – • SBT_POPOUT – • SBT_OWNERDRAW –
:
ИЛИ
-
; ; (
)
WM_DRAWITEM. lParam ,
. .
\t.
,
, ,
–
-
. ,
SB_SETMINHEIGHT,
:
SendMessage(hWndSb, SB_SETMINHEIGHT, minHeight, 0);
wParam (minHeight) − ,
lParam
. , . .
-
WM_SIZE , (WM_SIZE), lParam, :
wParam
-
SendMessage(hWndSb, WM_SIZE, wParam, lParam);
,
. .
3.2.1.
,
, . . . WM_INITDIALOG.
(modal) (modeless).
DialogBox
.
75
Project->Add
Resource->Dialog->New. . . 1113.rc. IDD_DIALOGBAR.
/*
resource.h 3.1.1. 1113.cpp*/
1113.rc
/*#define UNICODE #ifdef UNICODE #define _UNICODE #endif */ #define STRICT #include #include #include /* tchar.h , ,
-
, UNICODE, ,
UNICODE ANSI
string.h */ #include #include "resource.h" /* ,
resource.h */ #define ID_FILE_TEST 40001 #define ID_FILE_EXIT 40002 #define ID_HELP_ABOUT 40003 /* */ #define #define #define /* LRESULT /* INT_PTR /*
IDT_TOOLBAR 400 IDS_STATUSBAR 401 PartN 3 */ CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); */ CALLBACK AboutProc(HWND, UINT, WPARAM, LPARAM);
*/ VOID CALLBACK TimerProc(HWND,UINT,UINT_PTR,DWORD); /* */ void OutTimeDate(HWND); /*
76
*/ HMENU hFileMenu,hHelpMenu,hMenu; /* */ HWND hWndSb,hWndTb; /* */ TCHAR szCurrentTime[40]; /* 5 */ int nTime=5; /* TBBUTTON*/ TBBUTTON tBb[]= { {0,ID_FILE_TEST,TBSTATE_ENABLED,TBSTYLE_BUTTON,0L,0}, {1,ID_FILE_EXIT,TBSTATE_ENABLED,TBSTYLE_BUTTON,0L,0}, {0,0,TBSTATE_ENABLED,TBSTYLE_SEP,0L,1}, {2,ID_HELP_ABOUT,TBSTATE_ENABLED,TBSTYLE_BUTTON,0L,0} };
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { /* */ TCHAR szWindowClass[]=_TEXT("QWERTY"); /* */ TCHAR szTitle[]=_TEXT(" "); /* Msg MSG Windows */ MSG msg; /* C wcex WNDCLASSEX */ WNDCLASSEX wcex; HWND hWnd; // /* , */ if(hWnd=FindWindow(szWindowClass,NULL)) { /* , */ if(IsIconic(hWnd)) ShowWindow(hWnd,SW_RESTORE); /* */
SetForegroundWindow(hWnd); return FALSE; } /* wcex*/ memset(&wcex,0,sizeof(WNDCLASSEX)); /* */
77
-
.
.
wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; /* */ wcex.lpfnWndProc= (WNDPROC)WndProc; //wcex.cbClsExtra = 0; //wcex.cbWndExtra = 0; wcex.hInstance = hInstance; // /* , LoadImage(). */ wcex.hIcon = (HICON)LoadImage(hInstance, IDI_APPLICATION, IMAGE_ICON,32,32,0); /* */ wcex.hCursor = LoadCursor(NULL, IDC_ARROW); /* : wcex.hbrBackground=(HBRUSH)(COLOR_APPWORKSPACE+1); wcex.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH); GetStockBrush(), windowsx.h */ wcex.hbrBackground=GetStockBrush(LTGRAY_BRUSH); //wcex.lpszMenuName = MAKEINTRESOURCE(IDC_MSG_1); /* */ wcex.lpszClassName = szWindowClass; wcex.hIconSm = NULL; /* LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));*/ if(!RegisterClassEx(&wcex)) return FALSE; /* Common Control Library */ INITCOMMONCONTROLSEX iCc; iCc.dwSize=sizeof(INITCOMMONCONTROLSEX); iCc.dwICC=ICC_WIN95_CLASSES; InitCommonControlsEx(&iCc); /* */ hWnd = CreateWindowEx(WS_EX_WINDOWEDGE,szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,0,CW_USEDEFAULT,0, NULL, NULL, hInstance, NULL); if (!hWnd) return FALSE; /* . , , */ TCHAR szAsm[80]; USHORT regCS,regDS,regES,regSS; __asm{ mov regCS,CS mov regDS,DS
78
mov regES,ES mov regSS,SS } wsprintf((LPTSTR)szAsm,_T("CS=%X,DS=%X\nES=%X,SS=%X\n WinMain=%X\nszWindowClass=%X"), regCS,regDS,regES,regSS); MessageBox(NULL,(LPCTSTR)szAsm,_T(" "), MB_ICONINFORMATION); /*
*/
hFileMenu=CreatePopupMenu() "Test" AppendMenu((hFileMenu=CreatePopupMenu()), MF_ENABLED | MFT_STRING, ID_FILE_TEST, _TEXT("&Test"));
/* "Exit" */ AppendMenu(hFileMenu,MF_GRAYED | MFT_STRING, ID_FILE_EXIT,_TEXT("&Exit")); /* hHelpMenu=CreatePopupMenu() "About" */ AppendMenu((hHelpMenu=CreatePopupMenu()), MF_ENABLED|MFT_STRING, ID_HELP_ABOUT,_TEXT("&About")); /* hMenu=CreateMenu() "File" " */ AppendMenu((hMenu=CreateMenu()), MF_ENABLED | MF_POPUP, (UINT_PTR)hFileMenu, _TEXT("&File")); /* "Help" */ AppendMenu(hMenu,MF_ENABLED|MF_POPUP, (UINT_PTR)hHelpMenu,_TEXT("&Help")); / * "QUIT" */ AppendMenu(hMenu,MF_GRAYED,(UINT_PTR)11, _TEXT("Quit")); /* */ SetMenu(hWnd,hMenu); /* */ ShowWindow(hWnd, SW_SHOWNORMAL); /* UpdateWindow() WM_PAINT , InvalidateRect() WM_PAINT , */
79
UpdateWindow(hWnd); /* */ DrawMenuBar(hWnd); /* */ while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } /* */ return (int)msg.wParam; } /* */ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { int xSize=500; int ySize=300; /* PAINTSTRUCT ( BeginPaint) */ PAINTSTRUCT ps; /* TEXTMETRIC . */ TEXTMETRIC tm; /* LOGFONT . */ LOGFONT lf; /* */ Static HFONT hFont,hOldFont; /* RECT– , . */ RECT rect,rcSb,rcTb; HDC hDc; /* HeightTb HeightSb . */ static UINT HeightSb,HeightTb; /* width height . */ UINT width,height; /* StatusBar*/ int aWidth[PartN],; int SbWidth; LPTSTR lpszFace=_TEXT("Times New Roman Cyr"); switch (message) { case WM_CREATE: /*
.
, SetClassLong(hWnd, GCL_HBRBACKGROUND, (LONG)CreateSolidBrush(RGB(200,160,255));
80
(hInstance) , : hInst = GetModuleHandle(NULL); hInst = (HINSTANCE)GetClassLong(hWnd,GCL_HMODULE); */ /* lf*/ memset(&lf,0,sizeof(lf)); /* */ lf.lfHeight=30; /* */ lstrcpy(lf.lfFaceName,lpszFace); /* */ hFont=CreateFontIndirect(&lf); /* */ OutTimeDate(hWnd); /* SetTimer 1 */ SetTimer(hWnd,1,1000,(TIMERPROC)NULL); /* ToolBar*/ hWndTb=CreateToolbarEx(hWnd, WS_CHILD|WS_VISIBLE|WS_BORDER|TBSTYLE_TOOLTIPS, IDT_TOOLBAR, 3, GetModuleHandle(NULL), IDR_TOOLBAR1, (LPCTBBUTTON)tBb, 4, 0,0, 0,0, sizeof(TBBUTTON)); if(hWndTb==NULL) return FALSE; /* ToolBar ( ). */ GetWindowRect(hWndTb,&rcTb); /* ToolBar .*/ HeightTb=rcTb.bottom-rcTb.top; /* Statusbar*/ hWndSb=CreateStatusWindow(WS_CHILD|WS_VISIBLE, (LPTSTR)" ", hWnd, IDS_STATUSBAR); if(hWndSb==NULL) return FALSE; /* StatusBar ( ). */ GetWindowRect(hWndSb,&rcSb); /* StatusBar */ HeightSb=rcSb.bottom-rcSb.top; return TRUE; case WM_TIMER: /* OutTimeDate ,
81
*/ OutTimeDate(hWnd); break; case WM_KEYDOWN: switch(wParam) /* switch(wParam) { case VK_ESCAPE: /*
-
WM_CLOSE
*/
(hWnd),
,
, SendMessage
*/ SendMessage(hWnd,WM_CLOSE,0,0); break; } break; case WM_COMMAND: switch(LOWORD(wParam)) //switch(wParam) { case ID_FILE_TEST: /* ID_FILE_EXIT */ EnableMenuItem(hFileMenu,ID_FILE_EXIT, MF_BYCOMMAND|MF_ENABLED); /* ID_FILE_TEST*/ CheckMenuItem(hFileMenu,ID_FILE_TEST, MF_BYCOMMAND|MF_CHECKED); "QUIT"*/ /* EnableMenuItem(GetMenu(hWnd), (UINT_PTR)11,MF_BYCOMMAND|MF_ENABLED); /* ,
DrawMenuBar
*/ DrawMenuBar(hWnd); /* nTime */ SetTimer(hWnd,2,nTime*1000, (TIMERPROC)TimerProc); break; case ID_FILE_EXIT: /* PostQuitMessage WM_QUIT */ PostQuitMessage(0); /* , . . SendMessage(hWnd,WM_CLOSE,0,0); */ break; case ID_HELP_ABOUT: /* DialogBox IDD_ABOUTBOX,
82
-
WndProc
-
*/ DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOGBAR), hWnd, (DLGPROC)AboutProc); break; case (UINT)11: /* PostQuitMessage WM_QUIT */ PostQuitMessage(0); break; } break; case WM_PAINT: /* */ hDc = BeginPaint(hWnd, &ps); /* */ hOldFont=SelectFont(hDc,hFont); /* */ GetTextMetrics(hDc,&tm); /* c */ GetClientRect(hWnd,&rect); rect.top+=HeightTb; rect.bottom-=HeightSb; /* SetBkMode TRANSPARENT -
. ,
. .
*/ SetBkMode(hDc,TRANSPARENT); /* SetTextColor , !
.
*/ SetTextColor(hDc,RGB(0,0,128)); DrawText(hDc,szCurrentTime,-1,&rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER); /* */ EndPaint(hWnd, &ps); break; case WM_CLOSE: /* WM_CLOSE */ if(MessageBox(hWnd,_T(" ?"), _T(" !"), MB_YESNO | MB_ICONQUESTION)==IDYES) { /*
83
-
DestroyWindow , . . WM_DESTROY. PostQuitMessage, */ DestroyWindow(hWnd); } break;
WM_QUIT
case WM_SIZE: /* width height . */ width=LOWORD(lParam); height=HIWORD(lParam); /* Toolbar , SendMessage(hWndTb,WM_SIZE,wParam,lParam); */ SendMessage(hWndTb,TB_AUTOSIZE,0,0); /* Statusbar . */ SendMessage(hWndSb,WM_SIZE,wParam,lParam); /* Statusbar*/ SbWidth=width/PartN;; aWidth[0]=SbWidth; aWidth[1]=SbWidth*2; aWidth[2]=-1; /* Statusbar*/ SendMessage(hWndSb, SB_SETPARTS,PartN,(LPARAM)aWidth); /* 0Statusbar*/ SendMessage(hWndSb,SB_SETTEXT,0, (LPARAM)_T("\t ")); /* 1Statusbar, */ SendMessage(hWndSb,SB_SETTEXT,1 | SBT_NOBORDERS, (LPARAM)_T("\t ")); /* 2 : SendMessage(hWndSb,SB_GETRECT,2,(LPARAM)&reSb); */ break; case WM_LBUTTONDOWN: if(wParam & MK_SHIFT) { MessageBox(hWnd,_T(" \nShift"), _T(" !"), MB_OK | MB_ICONEXCLAMATION); } break; case WM_GETMINMAXINFO: lpmmi=(LPMINMAXINFO)lParam;
84
/* */ lpmmi->ptMinTrackSize.x=xSize; lpmmi->ptMinTrackSize.y=ySize; lpmmi->ptMaxTrackSize.x=xSize; lpmmi->ptMaxTrackSize.y=ySize; break; case WM_DESTROY: /* DeleteObject , , , . */ /* */ DeleteObject(hFont); /* KillTimer */ KillTimer(hWnd,1); /* PostQuitMessage() WM_QUIT. , */
.
-
,
wParam
PostQuitMessage(0); break; default: /* */ return DefWindowProc(hWnd, message,wParam,lParam); } return 0L; } /* */ INT_PTR CALLBACK AboutProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { static HWND hStatic; /* LOGFONT */ static LOGFONT lf1; static HFONT hFont3; /* IDC_STATIC_1 */ LPTSTR lpszFace1=_TEXT("Times New Roman Cyr"); switch (message) { case WM_INITDIALOG: /* (hDlg) (hDlg), GetDlgItem*/ hStatic=GetDlgItem(hDlg,IDC_STATIC_1); /* */ lf1.lfHeight=23; /* */ lstrcpy(lf1.lfFaceName,lpszFace1); /* */ hFont3=CreateFontIndirect(&lf1);
85
-
/* hStatic*/ SendMessage(hStatic,WM_SETFONT, (WPARAM)hFont3,TRUE); /* TRUE */ return (INT_PTR)TRUE; case WM_COMMAND: if(LOWORD(wParam)==IDOK || LOWORD(wParam)== IDCANCEL) { DeleteObject(hFont3); EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; } /* FALSE. */ return (INT_PTR)FALSE; } / * */ void OutTimeDate(HWND hWnd) { LPTSTR szDay[]={_T(" ."),_T(" ."),_T(" ."), _T(" ."),_T(" ."), _T(" ."),_T(" .") }; LPTSTR szMonth[]={_T(""),_T(" ."),_T(" ."), _T(" "),_T(" ."), _T(" "),_T(" "), _T(" "),_T(" ."), _T(" ."),_T(" ."), _T(" ."),_T(" .") }; TCHAR szT[20]; SYSTEMTIME SystemTime; /* GetLocalTime , , . . SYSTEMTIME . */ GetLocalTime(&SystemTime); /* */ lstrcpy(szCurrentTime, szDay[SystemTime.wDayOfWeek]); /* */ lstrcat((LPTSTR)szCurrentTime,_T(" ")); /* */ lstrcat((LPTSTR)szCurrentTime, szMonth[SystemTime.wMonth]); /* */
86
-
lstrcat((LPTSTR)szCurrentTime,_T(" ")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wDay); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ lstrcat((LPTSTR)szCurrentTime,_T(" ")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wYear); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); lstrcat((LPTSTR)szCurrentTime,_T("---")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wHour); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ lstrcat((LPTSTR)szCurrentTime,_T(":")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wMinute); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ lstrcat((LPTSTR)szCurrentTime,_T(":")); /* */ wsprintf((LPTSTR)szT,_T("%d"), SystemTime.wSecond); lstrcat((LPTSTR)szCurrentTime,(LPTSTR)szT); /* */ InvalidateRect(hWnd,NULL,TRUE); } /* */ VOID CALLBACK TimerProc(HWND hWnd,UINT uMsg, UINT_PTR idEvent,DWORD dwTime) { TCHAR szTimer[100]; KillTimer(hWnd,2); wsprintf(szTimer, _T(" \n Test\n %d c!"), nTime); MessageBox(NULL,(LPCTSTR)szTimer, _T(" "),MB_ICONHAND); }
. 5.
87
. 5.
,
88
Win32 Windows #define
typedef
Win32,
:
#define WINAPI __stdcall, #define CALLBACK __stdcall, #define APIENTRY WINAPI, typedef UINT WPARAM, typedef LONG LPARAM, typedef LONG LRESULT, typedef WORD ATOM.
Win32
, (32-
). : BOOL – int (32 ); BYTE – unsigned char (8 ); CHAR – char (8 ); COLORREF – unsigneg long (32 ); DWORD – unsigned long (32 ); DOUBLE – (double – 64 ) FLOAT – (float – 32 ) HANDLE – ( 32 ); HBITMAP – ( HBRUSH – ( – 32 ); HCURSOR – ( – 32 ); HDC – ( – 32 HICON – ( – 32 ); HFONT – ( – 32 ); HINSTANCE – ( HMENU – ( – 32 ); HPEN – ( – 32 ); HWND – ( – 32 ); WORD – unsigned short (16 ); LONG – signed long (32 ); INT – signed long (32 ); LPARAM – 32( ); WPARAM – 32( ); LRESULT – 32LPCSTR – C; LPCWSTR – Unicode; LPSTR – C; LPCTSTR – ; 89
– – 32
);
);
– 32
);
long;
SHORT – signed short (16 ); USHORT – unsigned short (16 ); UCHAR – unsigned char (8 ) UINT – unsigned long (32 ); #If defined(_WIN64) typedef _int64 INT_PTR #else typedef int INT_PTR #endif
, ,
– -
. ,
, .
,
, .
,
LoadI–
: CreateWindow(), DrawText()
con(), DialogBox(). , , – ; b– BOOL; by – BYTE; ch – char; dw – DWORD; fn – ; h– ; i – INT; l – LONG; lp – ; lpsz – n – unsigned short; p– ; psz – , pv – void; sz – , u – UINT; v – void; w – WORD; pt – x rgb – ,
:
,
;
; ;
y(
32-
); RGB .
90
1. , . ., . . Visual C++ 4. . .: . « » «Channel Trading Ltd.», 1996. 352 . 2. , . Windows 2000 API. : . . .: « », 2002. 1088 . 3. Ф , Л. ., . . WINDOWS 95. . .: , 1996. 288 . ( ; . 22). 4. , . Ю. Windows. 2., . . .: – , 2003. 656 . 5. Щ , Ю. . Win32 API. . .: , 2007. 572 . 6. Ю , . И. Assembler: . 2. .: , 2004. 637 .
91
1. Щ 1.1. 1.2. 1.3. 1.4. 1.5. 1.6. 1.7. 1.7.1. 1.7.2. 1.7.3. 1.7.4. 1.7.5. 2. 2.2. 2.3. 3. 3.1. 3.1.1. 3.2. 3.2.1.
3 WINDOWS 5 WinMain() ............................................................. 5 ..................................................... 7 ....................................................................... 13 щ ....................................................................... 16 .............................................................................. 20 щ Windows .......................................................................... 21 ....................................... 28 ............................................................................ 29 ................................................................. 31 ............................... 32 Windows........................................................................... 32 ..................................................... 33 40 ............................................................... 41 .......................................... 46 Щ 57 ........................................................................ 58 .............. 62 ............................................................................... 73 , ...................................................................................................... 75 . WIN32……………... 89 че ........................................................................ 91
92
-
2007–2008 « », ,
-
. 2015
,
.
(
1945–1966 ).
1945 . .
17
1945 . ,
, ,
,
,
.
-
. . ., .
.
(
1951 .). -
,
1956 . –
(
-
0705). . . . . , 196–1970
. . (
,
. 0705
). .
93
. . (
1970–1988
-
.
. ). .
0648.
–
. .),
: . . .,
. . . 1988–1997 ).
. . .
(
1976
. ( -
(
-
-
2205). .
, , .
2205 1988 . 1992 . . . . . 1997 (
.
. .
, ). ,
210202 « ».
-
, , . , ».
,
-
, 090104 « , .
1996 .
. . .,
. .
. 4264
.
62
7
94
.
Win32 API. .
. . . . . . -
-
,
.
. .
№ 00408 05.11.99 11.12.09
№ 2174 100 .
95
С
ь
-П
197101, С
ы -П
,
,К
., 49
96
-