Программирование приложений Windows. API графического интерфейса пользователя

Изложены методические основы специальных средств программирования графического интерфейса пользователя операционной сист

353 52 2MB

Russian Pages 351 Year 2010

Report DMCA / Copyright

DOWNLOAD PDF FILE

Recommend Papers

Программирование приложений Windows. API графического интерфейса пользователя

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

. .

АINDOАS.

API -

2010

2 65.29 , Ж:

. . .

/ . . .– : СЭЭЩ://ааа.ОХТЛrКrв.rЮ.

:

Д

АТЧНШаs. API Ж+Д , 2010. – 350 . –

IS N 5-89838-495-1

АТЧНШаs, :

.

,

, .

. 230105 « «

»,

,

АТЧНШаs. . 3.

», 010503

. 69.

. 20

.

. . :

IS N 5-89838-495-1

«

;

. .

»

©

, 2010

3

(

)

(API). (

)

,

Windows Application Programming Interface , Windows. Windows API : Kernel ( ), User GDI ( ), , . . , ,

Windows. ,

. . . . C++,

. ,

АТЧНШаs, MFC, Windows FШrЦs . . . , . 1

, ,

,

,

,

. ,

,

2 ,

,

:

Windows ,

4 ;

,

, . 3 ,

,

,

, .

4

,

, .

, .

5

. ,

. 6

,

.

,

,

. ,

. .

«

Windows», .

5 1. АТЧНШаs. ,

.

(

,

), (

(

),

.



.

)

(

)

1.1. Windows

. .

1. . 2. MDI3.

.

, .

4.

. .

,

. .

.

, ,

:

,

(

. 1 , ,

,

. 1).

,

.

,

. 1.2. .

, (

,

6 ),

.

1–

)

. 1. 3– 4–

;2–

(

.

: ;

;5–

1.2.1. WNDCLASS. typedef struct { UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HANDLE hInstance;

:

;

7 HICON HCURSOR HBRUSH LPCTSTR LPCTSTR WNDCLASS;

hIcon; hCursor; hbrBackground; lpszMenuName; lpszClassName; } WNDCLASS:

1) style – , . 1, . 1.1. : wcstyle = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; 2) lpfnWndProc – ; 3) cbClsExtra – ; 4) cbWndExtra – ; 5) hInstance – ; 6) hIcon – ( ), . LoadIcon: ICON LoadIcon( HINSTANCE hInst, LPCTSTR lpIconName); 7) hCursor – . LoadCursor: HCURSOR LoadCursor( HINSTANCE hInst, LPCTSTR lpCursorName); 8) hbrBackground – . . ( . 1, . 1.4.) 1. HBRUSH. , : wc. hbrBackground = (HBRUSH)( COLOR_WINDOW+1); 9) lpszMenuName – . NULL, . 10) lpszClassName – , , : wc.lpszClassName = szName; 1.2.2. : int RegClass( WNDPROC Proc, LPCTSTR szName) {

8 WNDCLASS wc; Wc.style = CS.HREDRAW | CS.VREDRAW; wc.cbClsExtra = wccbWndExtra = 0; wc.lpfnWndProc = Proc; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); wc.lpszMenuName = (LPCTSTR)NULL; wc.lpszClassName = szName; return RegisterClass(&wc); } .

.

,

.



,

.

RegisterClass

. wc.

,

,



.

1.2.3. .



:

;



;



,

,

. .

(

-

)

. .

. –

, .

9 . LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static short ex, cy, left, top; // switch (msg) // { case WM_CREATE: { ...; return 0;} case WM_MOVE:{ left=LOWORD(lParam); top=HIWORD(lParam); return 0; } case WM_SIZE:{ cx= LOWORD(lParam); cy= HIWORD(lParam); return 0; } case WM_COMMAND: // { switch ( LOWORD(wParam)) { case CM_FILE_EXIT: { DestroyWindow(hwnd); return 0; } // } return 0; } caseWM_DESTROY: { ...; PostQuitMessage(0); return 0;} } return DefWindowProc(hwnd, msg, wParam, lParam); } Windows LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) LRESULT signed long. CALLBACK , ( ). UINT, WPARAM, LPARAM :typedef unsigned int UINT; typedef UINT WPARAM; typedef LONG LPARAM;

10 hwnd – lParam

wParam msg

,

, a

.

.

,

. Windows WM_.

WM_CREATE . CreateWindow – 1, WM_SIZE , a WM_MOVE – .

CreateWindow .



WM_MOVE, WM_SIZE. . .

msg,

WM_SIZE, WM_COMMAND

,

– WM_MOVE. wParam .

,

CM_FILE_EXIT #define

CM_FILE_EXIT WM_DESTROY . DestroyWindow,

3001 , .

WM_DESTROY.

. WM_LBUTTONDOWN – WM_RBUTTON DOWN. ( ) WM_LBUTTONUP (WM_RBUTTONUP). WM_MOUSEMOVE. , . hwnd : HWND SetCapture(HWND hwnd); ,

11 , NULL. void ReleaseCapture(void) . lParam . WM_MOVE lParam : left=LOWORD(lParam); // top=HIWORD(lParam); // WM_SIZE

LOWORD(lParam),

lParam :

// //

cx=LOWORD(lParam); cy=HIWORD(lParam);

– HIWORD(lParam). -

, static.

,

,

,

static.

,

DefWindowProc ).

,

,

,

(

.

, ,

.

,

. 1.3. CreateWindow.

, : HWND CreateWindow( LPCTST lpClassName, // LPCTST lpWindowName, //

.

DWORD int int int int HWND

12 // // // // // // // // // // //

dwStyle, x, y, nWidth, nHeight, hWndParent,

HMENU

hMenu,

HANDLE LPVOID )

hInstance, lpParam

-

:



1) lpClassName RegisterClass 2) dwStyle – ; 3) – ,

-

;

. . –

.

WS_VISIBLE Windows

4)



CW_USEDEFAULT, . (0, 0);

, 5) nWidth –

. –

;

. CW_USEDEFAULT. .

nHeight 6) hWndParent 7) hMenu –



.

-

;

, ;

-

13 .

: ;

, CreateWindow, ,

;

(

. hMenu NULL. hMenu ). CreateWindow .

,

WM_CREATE 0, 1.

, Create Window

NULL.

(

. 1,

. WS_

. 1.5). (pop-up)

(overlapped), . ,

-

.

(title bar),

,

(tool bar)

. ).

(child)

(client region), ,

(scroll bar), , (status bar) ( . . 1). WS_OVERLAPPED. WS_OVERLAPPEDWINDOW

(

,

, ,

,

.

, . -

14 ,

. . WS_POPUP.

. WS_POPUPWINDOW. WS_POPUPWINDOW



. (

.

,

.

WS_CAPTION.

.

.) WS_CHILD. WS_CHILDWINDOW.

«

,

»

.

-

,

.

1.4.

.

,

,

,

WinMain. ,

WinMain: int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { MSG msg; HWND hwnd; if (!RegClass(WndProc, szClassName)) return FALSE; hwnd = CreateWindow(szClassName, " ", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInstance.NULL); if (!hwnd) return FALSE;

15 ShowWindow( hwnd, SW_SHOWMAXIMIZED); UpdateWindow(hwnd); while (GetMessage(&msg, 0,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } , Windows API. ,

WINAPI .

hInstance hPrevInstance

.

16NULL.

АТЧ32

NULL

.

lpszCmdLine

. nCmdShow . WinMain

NULL.

msg

.

. msg typedef struct{ HWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt; } MSG; //

msg

hwnd. hwnd

MSG: // // // // // //

-

RegClass: if (!RegClass(WndProc, szClassName)) return FALSE; CreateWindow.

16 ,

.

.

WS_VISIBLE

. ShowWindow: ShowWindow( hwnd, SW_SHOWMAXIMIZED); hwnd , . hwnd . hwnd « » : UpdateWindow(hwnd); , : while (GetMessage(&msg, 0,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } GetMessage , TranslateMessage , DispatchMessage . « » . . ShowWindow : BOOL ShowWindow( HWND hwnd, int nCmdShow);

nCmdShow

WinMain. : ShowWindow

SW_HIDE

hwnd

SW_MAXIMIZE SW RESTORE SW_MINIMIZE

hwnd hwnd

17 ShowWindow SW_SHOW SW_SHOWMAXIMIZED SW_SHOWMINIMIZED SW_SHOWMINNOACTIVE SW_SHOWNA SW_SHOWNOACTIVATE SW_SHOWNORMAL

hwnd hwnd hwnd hwnd hwnd hwnd hwnd

GetMessage : BOOL GetMessage( LPMSG lpmsg, HWND hwnd, WORD uMsgFilterMin, WORD uMsgFilterMax); lpmsg MSG, , hwnd – , . hwnd=NULL, . uMsgFilterMin uMsgFilterMax , . , . GetMessage , . WMQUIT , . GetMessage . WinMain wParam . 1.5. : 1) 2) .

, WNDCLASS

WinMain,

18 ,

1.1 .

Л 1.1. . #include // BOOL RegClass(WNDPROC, LPCTSTR, UINT); LRESULT CALLBACK WndProc(HWND,UINT,WPARAM, LPARAM); // HINSTANCE hInst; char szClassName[ ] = "WindowAppClass"; // int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { MSG msg; HWNDhwnd; hInst=hInstance; if (!RegClass( WndProc, szClassName,COLOR_WINDOW)) return FALSE; hwnd = CreateWindow(szClassName, " ", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW+USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInstance, NULL); if (!hwnd) return FALSE; while(GetMessage(&msg, 0, 0, 0)) { DispatchMessage(&msg); } return msg.wParam; } // BOOL RegClass(WNDPROC Proc, LPCTSTR szName, UINT brBackground) { WNDCLASS wc; wc.style = wc.cbClsExtra = wc.cbWndExtra = 0; wc.lpfnWndProc = Proc; wc.hInstance = hInst; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);

19 wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(brBackground + 1); wc.lpszMenuName = NULL; wc.lpszClassName = szName; return (RegisterClass(&wc) != 0);. } // LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch (msg) { case WM_DESTROY: { PostQuitMessage(0); return 0;} } return DefWindowProc(hwnd, msg, wParam, lParam); } , . , . , . . 1. . 2. . 3. ShowWindow UpdateWindow. , WS_VISIBLE. CreateWindow , . 4. 5. .

6. ,

. . WM_DESTROY, . . .

.

20 . 2

.

,

.

. 2.

. WS_OVERLAPPEDWINDOW , .

,

,

,

, . . . .

.

, .

,

,

.

.

,

.

,

. .

21 1.6. Windows API . ,

,

, .

,

1.6.1. ,

. BOOL IsWindow( HWND hwnd). ,

IsWindow: hwnd – .

,

, , IsWindowEnabled: BOOL IsWindowEnabled( HWND hwnd). hwnd , , – .

EnableWindow: BOOL EnableWindow( HWND hwnd, BOOL bEnable). bEnable=TRUE hwnd, . , . , – . hwnd SetFocus: SetFocus(hwnd). , hwnd : BOOL IsIconic(HWND hwnd). FindWindow ( ) lpClassName lpWindowName. : HWND FindWindow( LPCTSTR lpClassName, LPCTSTR lpWindowName). lpWindowName – NULL, . lpClassName – NULL, . . ,

22 ,

– NULL.

1.6.2. hwnd MoveWindow. MoveWindow BOOL MoveWindow( HWND hwnd, int x, // int . // int nWidth, // int nHeight, // BOOL bRepaint // ); bRepaint=TRUE,

:

,

, .

, – SetWindowPos

.

. hwnd :

BOOL SetWindowPos( HWND hwnd, HWND hWndInsertAfter, // int x, int , int ex, int , UINT

uFlags

// // // // //

); .

hWndInsertAfter :

23 HWND_BOTTOM HWND_NOTOPMOST

,

HWND_TOP HWND_TOPMOST

,

HWND_NOTOPMOST,

:

uFlags SWP_DRAWFRAME SWP_FRAMECHANGED

,

SWP_HIDEWINDOW SWP_NOACTIVATE SWP_NOCOPYBITS SWP_NOMOVE SWP_NOOWNERZORDER SWP_NOREPOSITION SWP_NOREDRAW SWP_NOSENDCHANGING SWP_NOSIZE SWP_NOZORDER SWP_SHOWWINDOW

. SetForegroundWindow. , , . : BOOL SetForegroundWindow (HWND hwnd ); ,



.

. .

GetSystemMetrics. –

(

) Windows.

24 .

:

GetSystemMetrics int GetSystemMetrics (int nIndex). nIndex . : SM_CXMIN SM_CYMIN SM_CXSCREEN SM_CYSCREEN SM_CYCAPTION SM_CYMENU

,

: intw=GetSystemMetrics(SM_CXSCREEN). hwnd : BOOL GetWindowRect( HWND hwnd, LPRECT lpRect); lpRect RECT: typedef struct{ LONG left; // LONG top; // LONG right; // LONG bottom; // } RECT; .

.

:

RECT rc; GetWindowRect(hwnd, & ); GetClientRect : BOOL GetClientRect( HWND hwnd, LPRECT lpRect).

,

, . . rc.left=rc.top=0,

right .

bottom

25 1.6.3.

,

.

,

MessageBox

: int WINAPI MessageBox ( HWND hwnd, // LPCTSTR lpText, // LPCTSTR lpCaption, // UINT uType // ); hwnd=NULL, . lpCaption = NULL « ». uType ,

MB_ABORTRETRYIGNORE И MB_OKCANCEL MB_RETRYCANCEL MB_YESNO MB_YESNOCANCEL

« « « «

.

», « ,«

», « »

», « », « » », « »,«

.

MB_DEFBUTTON1 MB_DEFBUTTON2

»

» »

. :

MB_DEFBUTTON3 MB_DEFBUTTON4

: MB_ICONEXCLAMATION, MB_ICONWARNING MB_ICONINFORMATION, MB_ICONASTERISK MB_ICONQUESTION MB_ICONSTOP, MB_ICONERROR, MB_ICONHAND

"i"

:

26 : .

MB_APPLMODAL ,

hwnd

,

hwnd MB_SYSTEMMODAL MB_TASKMODAL

MB_APPLMODAL, (hwnd= NULL), «

MB_HELP MB_RIGHT MB_RTLREADING

»

MB_SETFOREGROUND

,

. MessageBox .

IDABORT IDCANCEL IDIGNORE IDNO

,

,

,

: « « « «

» »

»

»

MessageBox

ID IDRETRY IDYES

« «

«

»,

IDCANCEL

Esc. «

MessageBeep

»

»

Windows ».

« ». BOOL MessageBeep( UINT uType); uType :

,

«

»

:

27 win.ini OxFFFFFFFF -1 MB_ICONASTERISK MB_ICONEXCLAMATION MB_ICONHAND MB_ICONQUESTION И 0

,

,

SystemAsterisk SystemExclamation SystemHand SystemQuestion SystemDefault

– . MessageBeep . ,

,

.

.

FlashWindow, :

BOOL FlashWindow( HWND hwnd, // BOOL blnvert // ); bInvert=TRUE, bInvert=FALSE, , .

. . FlashWindow, ,

.

0. hwnd

.

OwnedHwnd. : case WM_LBUTTONDOWN: { FlashWindow(OwnedHwnd, TRUE); for (int i=0; ihdr.idFrom) { case CM_FILE_NEW: { TTStr->lpszText=" return 0; } default: return 0; } .

";

( \n).

(

4.6) .

Л 5.3 . . #include #include #pragma comment (lib,"comctl32.lib") #define ID_TOOLBAR 100 #define CM_FILE_NEW 1000 #define CM_FILE_OPEN 1001 #define CM_FILE_SAVE 1002 #define CM_FILE_QUIT 1003 #define CM_EDIT_CUT 2000 #define CM_EDIT_PASTE 2001 #define CM_EDIT_COPY 2002 #define CM_EDIT_DEL 2003 #define CM_HELP_HELP 3000 #define CM_HELP_ABOUT 3001 BOOL RegClass(WNDPROC, LPCTSTR, UINT); LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); HACCEL CreateAccelTable(void); HINSTANCE hInstance; char szClass[]="NotifyClass"; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { MSG msg; HWNDhwnd; ::hInstance=hInstance; if (!RegClass(WndProc, szClass, COLOR_WINDOW)) return FALSE; hwnd = CreateWindow(szClass," ",

223 WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0,0,hInstance,NULL); if (!hwnd) return FALSE; HACCEL hAccel=CreateAccelTable(); while(GetMessage(&msg, 0,0,0)) { if (!hAccel || !TranslateAccelerator(hwnd, hAccel, &msg)) {TranslateMessage(&msg); DispatchMessage(&msg); } } DestroyAcceleratorTable(hAccel); return msg.wParam; } BOOL RegClass(WNDPROC Proc, LPCTSTR szName, UINT brBackground) { WNDCLASS wc; wc.style=wc.cbClsExtra=wc.cbWndExtra=0; wc.lpfnWndProc = Proc; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(brBackground+1); wc.lpszMenuName=NULL;wc.lpszClassName=szName; return (RegisterClass(&wc) != 0); } BOOL CreateMenuItem(HMENU hMenu, char *str, UINT ulns, UINT uCom, HMENU hSubMenu, BOOL flag, UINT fType) { MENUITEMINFO mii; mii.cbSize=sizeof(MENUITEMINFO); mii.fMask = MIIM_STATE | MIIMJYPE | MIIM_SUBMENU | MIIM_ID; mii.fType = fType; mii.fState= MFS_ENABLED; mii.dwTypeData = str; mii.cch=sizeof(str); mii.wID=uCom; mii.hSubMenu=hSubMenu; return InsertMenuItem(hMenu, ulns, flag, &mii); } HACCEL CreateAccelTable( void) { //

224 ACCEL Accel[8]; // Accel[0].fVirt= FVIRTKEY | FCONTROL; Accel[0].key = 0x4e; Accel[0].cmd = CM_FILE_NEW // Accel[1].fVirt= FVIRTKEY | FCONTROL; Accel[1].key = 0x4f; Accel[1].cmd = CM_FILE_OPEN // Accel[2].fVirt= FVIRTKEY | FCONTROL; Accel[2].key = 0x53; Accel[2].cmd = CM_FILE_SAVE; // Accel[3].fVirt= FVIRTKEY | FALT; Accel[3].key = 0x73; Accel[3].cmd = CM_FILE_QUIT; // Accel[4].fVirt= FVIRTKEY | FCONTROL; Accel[4].key = 0x58; Accel[4].cmd = CM_EDIT_CUT; // Accel[5].fVirt= FVIRTKEY | FCONTROL; Accel[5].key = 0x56; Accel[5].cmd = CM_EDIT_PASTE; // Accel[6].tVirt= FVIRTKEY | FCONTROL; Accel[6].key = 0x43; Accel[6].cmd = CM_EDIT_COPY; // Accel[7].fVirt= FVIRTKEY; Accel[7].key = 0x2e; Accel[7].cmd = CM_EDIT_DEL; return CreateAcceleratorTable((LPACCEL)Accel,8); } HWND CreateToolBar(HWND hwnd, DWORD dwStyle, UINT uCom) { static TBBUTTON but[10]; but[0].fsStyle=TBSTYLE_SEP; // but[1].iBitmap=STD_FILENEW; but[1].idCommand=CM_FILE_NEW; but[1].fsState=TBSTATE_ENABLED; but[1].fsStyle=TBSTYLE_BUTTON; // but[2].iBitmap=STD_FILEOPEN; but[2].idCommand=CM_FILE_OPEN;

225 but[2].fsState=TBSTATE_ENABLED; but[2].fsStyle=TBSTYLE_BUTTON; // but[3].iBitmap=STD_FILESAVE; but[3].idCommand=CM_FILE_SAVE; but[3].fsState=TBSTATE_ENABLED; but[3].fsStyle=TBSTYLE_BUTTON; // but[4].iBitmap=STD_CUT; but[4].idCommand=CM_EDIT_CUT; but[4].fsState=TBSTATE_ENABLED; but[4].fSStyle=TBSTYLE_BUTTON; // but[5].iBitmap=STD_COPY; but[5].idCommand=CM_EDIT_COPY; but[5].fsState=TBSTATE_ENABLED; but[5].fsStyle=TBSTYLE_BUTTON; // but[6].iBitmap=STD_PASTE; but[6].idCommand=CM_EDIT_PASTE; but[6].fsState=TBSTATE_ENABLED; but[6].fsStyle=TBSTYLE_BUTTON; // but[7].iBitmap=STD_DELETE; but[7].idCommand=CM_EDIT_DEL; but[7].fsState=TBSTATE_ENABLED; but[7].fsStyle=TBSTYLE_BUTTON; // but[8].iBitmap=STD_HELP; but[8].idCommand=CM_HELP_HELP; but[8].fsState=TBSTATE_ENABLED; but[8].fsStyle=TBSTYLE_BUTTON; return CreateToolbarEx(hwnd, dwStyle, uCom, 0, HINST_COMMCTRL,IDB_STD_LARGE_COLOR, but,9, 0,0,0,0, sizeof(TBBUTTON)); } LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HMENU hMainMenu, hPopUpFile, hPopUpEdit,

226 hPopUpHelp; static HWND hToolbar; switch (msg) { caseWM_SIZE: { MoveWindow(hToolbar,0,0,0,0.TRUE); return 0; } case WM_CREATE: { hMainMenu=CreateMenu(); , hPopUpFile=CreatePopupMenu(); int i=0; // CreateMenuItem(СPШЩUЩFТХО,"& \tCtrl+N", i++,CM_FILE_NEW, NULL, FALSE, MFT_STRING); CreateMenuItem(hPopUpFile,"& \t Ctrl+O", i++,CM_FILE_OPEN, NULL, FALSE, MFT_STRING); CreateMenuItem(hPopUpFile,"& \tCtrl+S", i++,CM_FILE_SAVE, NULL, FALSE, MFT_STRING); CreateMenuItem(hPopUpFile,NULL,i++,0, NULL, FALSE, MFT_SEPARATOR); CreateMenuItem(hPopUpFile," \tAlt+F4", i++,CM_FILE_QUIT, NULL, FALSE, MFT_STRING); hPopUpEdit=CreatePopupMenu(); i=0; // CreateMenuItem(hPopUpEdit,"& \tCtri+X", i++,CM_EDIT_CUT, NULL, FALSE, MFT_STRING); CreateMenuItem(hPopUpEdit," \tCtri+V", i++,CM_EDIT_PASTE, NULL, FALSE, MFT_STRING); CreateMenuItem(hPopUpEdit,"& \tCtrl+C", i++, CM_EDIT_COPY, NULL, FALSE, MFT_STRING); CreateMenuItem(hPopUpEdit,"& \t Delete",

227 i++, CM_EDIT_DEL, NULL, FALSE, MFT_STRING); hPopUpHelp=CreatePopupMenu(); i=0; // CreateMenuItem(hPopUpHelp,"& ", i++,CM_HELP_HELP, NULL, FALSE, MFT_STRING); CreateMenuItem(hPopUpHelp,NULL,i++,0, NULL, FALSE, MFT_SEPARATOR); CreateMenuItem(hPopUpHelp,"&0 ", i++,CM_HELP_ABOUT, NULL, FALSE, MFT_STRING); // i=0; // CreateMenuItem(hMainMenu,"& ", i++, 0, hPopUpFile,FALSE, MFT_STRING); CreateMenuItem(hMainMenu,"& ", i++, 0, hPopUpEdit,FALSE, MFT_STRING); CreateMenuItem(hMainMenu,"& ", i++, 0, hPopUpHelp,FALSE, MFT_STRING); SetMenu(hwnd,hMainMenu); DrawMenuBar(hwnd); DWORD dwStyle=WS_CHILD | WS_VISIBLE | TBSTYLE_TOOLTIPS | WS_DLGFRAME; hToolbar=CreateToolBar(hwnd, dwStyle, ID_TOOLBAR); return 0; } case WM_COMMAND: { switch (LOWORD(wParam)) { case CM_FILE_NEW: case CM_FILE_OPEN: case CM_FILE_SAVE: case CM_EDIT_CUT: case CM_EDIT_PASTE: case CM_EDIT_COPY: case CM_EDIT_DEL: case CM_HELP_HELP: caseCM_HELP_ABOUT: {return 0;}

228 case CM_FILE_QUIT: {DestroyWindow(hwnd); return 0;} } } ase WM_NOTIFY: { LPTOOLTIPTEXT TTStr; TTStr=(LPTOOLTIPTEXT)lParam; if (TTSfr->hdr.code!=TTN_NEEDTEXT) return 0; switch (TTStr->hdr.idFrom) { case CM_FILE_NEW: { TTStr->lpszText=" "; return 0; } case CM_FILE_OPEN: {TTStr->lpszText=" "; return 0; } caseCM_FILE_SAVE: {TTStr->lpszText=" "; return 0; } case CM_EDIT_CUT: {TTStr->lpszText=" "; return 0; { case CM_EDIT_PASTE: {TTStr->lpszText=" "; return 0; } case CM_EDIT_COPY: {TTStr->lpszText=" "; return 0; } caseCM_EDIT_DEL: { TTStr->lpszText=" "; return 0; } case CM_HELP_HELP:

229 "

"1. "2.

{ TTStr->lpszText=

:\ \ " \ \ " ";

return 0; } case CM_HELP_ABOUT: { TTStr->lpszText=" "; return 0; } case CM_FILE_QUIT: { TTStr->lpszText=" "; return 0; } default: return 0; } return 0; } case WM_DESTROY: { PostQuitMessage(0); return 0; } } return DefWindowProc(hwnd, msg, wParam, lParam); } .

,

. : #definelD_TOOLBAR 100 . : HWND CreateToolBar(HWND hwnd, DWORD dwStyle, UINT uCom); , . WM_NOTIFY, dwStyle : DWORD dwStyle=WS_CHILD | WS_VISIBLE |

.

230 TBSTYLE_TOOLTIPS | WS_DLGFRAME; TBSTYLETOOLTIPS. dwStyle CreateToolBar : hToolbar=CreateToolBar(hwnd, dwStyle, ID_TOOLBAR); . WM_NOTIFY. 1. lParam. TOOLTIPTEXT: LPTOOLTIPTEXT TTStr; lParam: TTStr=(LPTOOLTIPTEXT)lParam; 2. WM_NOTIFY , : if (TTStr->hdr.code!=TTN_NEEDTEXT) return 0; 3. , WM_NOTIFY, TTStr: switch (TTStr->hdr.idFrom) { case CM_FILE_NEW: { TTStr->lpszText= " "; return 0; } case CM_FILE_QUIT: { TTStr->lpszText=" "; return 0; } default: return 0; } , . . , . , . : , WM_NOTIFY. . WM_NOTIFY , . , TTStr->lpszText= "

:\n\n"

231 "1. "2.

.\n\n" ." 5.2. .

, commctrl.h

, comctl32.dll.

5.2.1. CreateStatusWindow: HWND CreateStatusWindow( LONG style, LPCTSTR lpszText, HWNDhwndParent,UINTwID); hwndParent . : style – . WS_CHILD | WS_VISIBLE; lpszText – , , NULL; hwndParent – ; wID – . wID . .

. .

Л 5.4. . #indude #include #pragma comment (lib,"comctl32.lib") #define ID_STATUS 200 BOOL RegClass(WNDPROC, LPCTSTR, UINT); LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); HINSTANCE hInstance; char szClass[] = "StatusClass"; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { MSG msg; HWND hMainWnd; ::hInstance=hInstance;

232 if (!RegCIass(WndProc, szCiass, COLOR_WINDOW)) return FALSE; hMainWnd = CreateWindow( szCiass, " ", WS_VISIBLE | WS_OVERLAPPEDWINDOW CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0,0, hInstance, NULL); if (!IhMainWnd) return FALSE; while (GetMessage(&msg, 0,0,0)) DispatchMessage(&msg); return msg.wParam; } BOOL RegClass(WNDPROC Proc, LPCTSTR szName, UINT hbrBackground) { WNDCLASS wc; wc.style=wc.cbClsExtra=wc.cbWndExtra = 0; wc.lpfnWndProc = Proc; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)hbrBackground +1); wc.lpszMenuName = NULL; wc.lpszClassName = szName; return (RegisterClass(&wc) != 0); } LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HWND hStatus; static int pParts[3]; static short ex; switch (msg) { case WM_SIZE: { MoveWindow(hStatus, 0,0, 0,0, TRUE); cx=LOWORD(lParam); pParts[0]=cx-200; pParts[1]=cx-100; pParts[2'j=cx; SendMessage(hStatus, SB_SETPARTS, 3, (LPARAM)pParts); return 0; }

233 case WM_CREATE: { hStatus=CreateStatusWindow( WS_CHILD | WS_VISIBLE, " ", hwnd, ID_STATUS); return 0; } caseWM_LBUTTONDOWN: { SendMessage(hStatus,SB_SETTEXT,0, (LONG)" SendMessage(hStatus,SB_SETTEXT,1, (LONG)" 2"); SendMessage(hStatus,SB_SETTEXT,2, (LONG)" 3"); return 0;

");

} caseWM_RBUTTONDOWN: { SendMessage(hStatus,SB_SETTEXT,0, (LONG)" 1"); SendMessage(hStatus,SB_SETTEXT,1, (LONG)""); return 0; } case WM_DESTROY: {PostQuitMessage(0); return 0;} } return DefWindowProc(hwnd, msg, wParam, lParam); }

.

1. hStatus pParts: static HWND hStatus; static int pParts[3]; pParts 3 ( 2. : hStatus=CreateStatusWindow(WS_CHILD | WS_VISIBLE, " ", hwnd, ID_STATUS); « » . NULL. .

).

234 3.

: MoveWindow( hStatus, 0, 0, 0, 0, TRUE); , . .

.

:

cx=LOWORD(lParam); pParts[0]=cx-200; pParts[1]=cx-100; pParts[2]=cx; , -200.

: SendMessage( hStatus, SB_SETPARTS, 3, (LPARAM)pParts); 4.

«

: « », « 3»: SendMessage( hStatus, SB_SETTEXT, 0, (LONG)" "); SendMessage( hStatus, SB_SETTEXT, 1, (LONG) " SendMessage( hStatus, SB_SETTEXT, 2, (LONG) "Coo SendMessage , 0.



2"); 3"); .

5.2.2.

.

, WM_MENUSELECT. wParam

, )

,

(

.

lParam .

.

4.2.3, 4.3,

235 WM_MENUSELECT .

. « «

»,

« », «

«

»

. «

»

«

«

«

»

»

« ,

» «

»,

«

« «

« ».

«

».

,

»

» .

» «

.

»

» »,

« «

»

«

». »

. .

»

.

Л 5.5. #include #include #pragma comment (lib,"comctl32.lib") #define ID_STATUS 200 #define CM_FILE_OPEN 1001 #define CM_FILE_SAVE 1002 #define CM_FILE_QUiT 1003 #define CM_EDIT_FIND 2001 #define CM_EDIT_REPLC 2002 #define CM_VIEW_NORM 3001 #define CM_VIEW_STRC 3002 BOOL RegClass(WNDPROC, LPCTSTR, UINT); LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); HINSTANCE hInstance; char szClass[ ]="SetMenuInfo"; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)

236 {

MSG msg; HWND hwnd; ::hInstance=hInstance; if (!RegClass(WndProc, szClass,COLOR_WINDOW)) return FALSE; hwnd = CreateWindow(szClass, " ", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0,0, hInstance, NULL); if(!hwnd) return FALSE; while(GetMessage(&msg, 0,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg);} return msg.wParam; } BOOL RegClass(WNDPROC Proc, LPCTSTR szName, UINT brBackground) {WNDCLASS wc; wc.style=wc.cbClsExtra=wc.cbWndExtra = 0; wc.lpfnWndProc = Proc; wc.hInstance=hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor=LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(brBackground +1); wc.lpszMenuName = NULL; wclpszClassName = szName; return (RegisterClass(&wc) != 0); } BOOL SetMenuItem(HMENU hMenu, UINT ulns, UINT fState, BOOL flag) { MENUITEMINFO mii; mii.cbSize = sizeof(MENUITEMINFO); mii.fMask = MHM.STATE | MIIMJD; mii.fState = fState; mii.wID = ulns; return SetMenuItemInfo( hMenu, ulns, flag, &mii); } UINT GetMenuItem(HMENU hMenu, UINT ulns, BOOL flag) { MENUITEMINFO mii; mii.cbSize=sizeof(MENUITEMINFO); mii.fMask = MIIM_STATE; GetMenuItemInfo(hMenu, ulns, flag, &mii); return mii.fState; } BOOL CreateMenuItem(HMENU hMenu, char *str, UINT ulns, UINT uCom, HMENU hSubMenu, BOOL flag, UINT fType)

237 {

MENUITEMINFO mii; mii.cbSize=sizeof(MENUITEMINFO); mii.fMask = MIIM_STATE | MIIM_TYPE | MIIM_SUBMENU | MIIM_ID; mii.fType = fType; mii.fState= MFS_ENABLED; mii.dwTypeData = str; mii.cch=sizeof(str); mii.wID=uCom; mii.hSubMenu=hSubMenu; return InsertMenuItem(hMenu, ulns, flag, &mii);

} LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HMENU hMainMenu, hFileMenu, hEditMenu, hViewMenu; static HWNDhStatus; static int pParts[2]; static short ex; switch (msg) { caseWM_SIZE: { MoveWindow(hStatus, 0,0,0,0, TRUE); cx=LOWORD(lParam); pParts[0]=cx/2; pParts[1]=cx; SendMessage(hStatus, SB_SETPARTS,2, (LPARAM)pParts); return 0; } caseWM_CREATE: { hMainMenu=CreateMenu(); // " " hFileMenu=CreatePopupMenu(); int i=0; // hFileMenu CreateMenuItem(hFileMenu,"& ",i++, CM_FILE_OPEN, NULL, FALSE, MFT_STRING); CreateMenuItem (hFileMenu,"& ",i++, CM_FILE_SAVE, NULL, FALSE, MFT_STRING); CreateMenuItem(hFileMenu,NULL,i++,0, NULL, FALSE, MFT_SEPARATOR); CreateMenuItem(hFileMenu,"& ",i++, CM_FILE_QUIT, NULL, FALSE, MFT_STR1NG); // " " hEditMenu=CreatePopupMenu();

238 i=0; // hEditMenu CreateMenuItem(hEditMenu,"& ",i++, CM_EDIT_FIND, NULL, FALSE, MFT_STRING); CreateMenuItem(hEditMenu,"& ",i++, CM_EDIT_REPLC, NULL, FALSE, MFT_STRING); // " " hViewMenu=CreatePopupMenu(); i=0; // hViewMenu CreateMenuItem(hViewMenu,"& ",i++, CM_VIEW_NORM, NULL, FALSE, MFT_RADIOCHECK); CreateMenuItem(hViewMenu,"& ",i++, CM_VIEW_STRC, NULL, FALSE, MFT_RADIOCHECK); // i=0; // hMainMenu CreateMenuItem(hMainMenu,"& ",i++, , hFileMenu,FALSE, MFT_STRING); SetMenu(hwnd,hMainMenu); SetMenuItem(hFileMenu, CM_FILE_SAVE, MFS_GRAYED, FALSE); DrawMenuBar(hwnd); hStatus=CreateStatusWindow(WS_CHILD | WS_VISIBLE, " ", hwnd, ID_STATUS); return 0; } caseWM_COMMAND: { switch (LOWORD(wParam)) { case CM_FILE_OPEN: { CreateMenuItem(hMainMenu, "& ",1,0, hEditMenu, FALSE, MFT_STRING); CreateMenuItem(hMainMenu, "& ",2,0, hViewMenu, FALSE, MFT_STRING); SetMenuItem(hFileMenu, CM_FILE_OPEN, MFS_GRAYED, FALSE); SetMenuItem(hFiIeMenu, CM_FILE_SAVE, MFS_ENABLED, FALSE); DrawMenuBar(hwnd); return 0; }

239 case CM_FILE_SAVE: { RemoveMenu(hMainMenu,1, MF_BYPOSITION); RemoveMenu(hMainMenu,1, MF_BYPOSITION); SetMenuItem(hFileMenu, CM_FILE_SAVE, MFS_GRAYED, FALSE); SetMenuItem(hFileMenu, CM_FILE_OPEN, MFS_ENABLED, FALSE); DrawMenuBar(hwnd); return 0; } case CM_EDIT_FIND: { if (GetMenuItem(hEditMenu, CM_EDIT_FlND, FALSE)==MFS_CHECKED) SetMenuItem(hEditMenu, CM_EDIT_FIND, MFS_UNCHECKED, FALSE); else SetMenuItem(hEditMenu, CM_EDIT_FIND, MFS_CHECKED, FALSE); return 0; } case CM_EDIT_REPLC: { if (GetMenuItem(hEditMenu, CM_EDIT_REPLC,FALSE)==MFS_CHECKED) SetMenuItem(hEditMenu, CM_EDIT_REPLC, MFS_UNCHECKED, FALSE); else SetMenuItem(hEditMenu, CM_EDIT_REPLC, MFS_CHECKED, FALSE); return 0; } case CM_VIEW_NORM: { SetMenuItem(hViewMenu, CM_VIEW_NORM, MFS_CHECKED, FALSE); SetMenuItem(hViewMenu, CM_VIEW_STRC, MFS_UNCHECKED, FALSE); return 0; } case CM_VIEW_STRC: { SetMenuItem(hViewMenu,

240 CM_VIEW_NORM, MFS_UNCHECKED, FALSE); SetMenuItem(hViewMenu, CM_VIEW_STRC, MFS_CHECKED, FALSE); return 0; } case CM_FILE_QUIT: { DestroyWindow(hwnd); return 0; } } return 0; } case WM_MENUSELECT: { if ((HMENU)lParam==hMainMenu) { switch (LOWORD(wParam)) { case 0: SendMessage(hStatus, SB_SETTEXT,0, (LONG)" / "); SendMessage(hStatus, SB_SETTEXT,1, NULL); return 0; case 1: SendMessage(hStatus, SB_SETTEXT,0, (LONG)" / "); SendMessage(hStatus, SB_SETTEXT,1, NULL); return 0; case 2: SendMessage(hStatus, SB_SETTEXT,0, (LONG)" "); SendMessage(hStatus, SB_SETTEXT,1, NULL); return 0; } } switch (LOWORD(wParam)) { case CM_FILE_OPEN: { SendMessage(hStatus,SB_SETTEXT,1, (LONG) " "); return 0; } case CM_FILE_SAVE: { SendMessage{hStatus,SB_SETTEXT, 1, (LONG)" "); return 0; } case CM_EDIT_FIND:

241 { (LONG)"

SendMessage(hStatus,SB_SETTEXT,1, "); return 0;

} case CM_EDIT_REPLC: { SendMessage(hStatus,SB_SETTEXT,1, (LONG)" "); return 0; } case CM_VIEW_NORM: { SendMessage(hStatus,SB_SETTEXT,1, (LONG)" "); return 0; } case CM_VIEW_STRC: { SendMessage(hStatus,SB_SETTEXT,1, (LONG)" "); return 0; } case CM_FILE_QUIT: { SendMessage(hStatus,SB_SETTEXT, 1, (LONG)" "); return 0; } } return 0; } caseWM_DESTROY: { DestroyMenu(hEditMenu); DestroyMenu(hViewMenu); PostQuitMessage(0); return 0; } } return DefWindowProc(hwnd, msg, wParam, lParam); } 4.3. 1. , , 5.4. .

242 2. : 1)

WM_MENUSELECT .

,

lParam

.

LOWORD(wParam), . , ( LOWORD (wParam)=0) « ». « / »: SendMessage( hStatus, SB_SETTEXT, 0, (LONG) " / "); : SendMessage( hStatus, SB_SETTEXT, 1, NULL); .

.

,

. , if ((HMENU)lParam==hMainMenu) { switch (LOWORD(wParam)) { case 0: }}

,

, . . lParam

; ,

2) аPКrКЦ

«

.

.

, »,

«

« »: SendMessaРО(СSЭКЭЮs,SBИSETTEБT,1, (LONG)” "); 0.

3) , «

».

»

; 4.3. .

243 «

« «

»,

.

»,

/

»,

«

,

».

,

. .

,

.

,

. .

,

.

. 1.

.

2. 3.

. .

, 4.

.

5.

,

. 6. 7. . 8. .

. , ,

9.

.

10. .

,

244 . .

11. 12. 13.

.

14.

.

15.

,

,

,

.

16. ,

. 17.

.

18.

.

19. . 20. 21. 22. 23. , 24. 25.

. . . .

4.

.

. .

1. 2. 3.

.

? ,

?

.

245 6. . Windows

.

, Tab. 6.1. .

: 1)

.

,

2)

;

. ;

.

3)

. . .

,

,

. .

,

.

,

,

. .

246 6.1.1. . .

,

. GetDialogBaseUnits: LONG GetDialogBaseUnits(VOID); 32, –

.

.

:

//

,

int cxChar, cyChar; TEXTMETRIC tm; HDC hdc=GetDC(hwnd); GetTextMetrics(hdc,&tm); ReleaseDC(hwnd.hdc); cxChar=tm.tmAveCharWidlh+1 ; cyChar=tm.tmHeight+tm.tmExternalLeading;

{

} // DWORD dlgunit =GetDialogBaseUnits(); Int dlgwunit=LOWORD(dlgunit), dlghunit=HIWORD(dlgunit); // cxChar=cxChar*4/dlgwunit; cyChar=cyChar*8/dlghunit; cxChar . ,



cyChar

. .

247 6.1.2. :

.

1.

(

,

.

)

.

.

2. . 3.

.

.

.

,

. 1-

ShowWindow. DestroyWindow.

2-

EndDialog. WSИ

:

DSИ

DS_3DLOOK

АТЧ32

DS_ABSALIGN

,

DS_CENTER DS CENTERMOUSE DS_CONTEXTHELP

. . WM_HELP. WinHelp, HELP_WM_HELP.

,

248

DS_CONTROL

,

DS_FIXEDSYS

.

SYSTEM_FONT SYSTEM_FIXED_FONT . WS_CAPTION WS_SYSMENU

DS_MODALFRAME

,

DS_TOFAILCREATE DS_NOIDLEMSG

WM_ENTERIDLE,

DS_RECURSE DS_SETFONT

,

DS_ SETFOREGROUND DS_SYSMODAL

WS_POPUP, WS_BORDER, WS_SYSMENU, DS_MODALFRAME. , WS_DLGFRAME. DS_SETFONT , .

WS_CAPTION ,

6.1.3. : BOOL CALLBACK DlgProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam); .

. hdlg,

hwnd.

249 ,

DefWindowProc

.

,

– FALSE. WM_INITDIALOG (

TRUE,

WM_CREATE, WM_PAINT

.

).

WM_DESTROY. .

WM_INITDIALOG . ,

.

wParam , WM_TABSTOP.

,

,

TRUE.

lParam

.

. wParam

,

FALSE .

. WM_COMMAND

.

WMCOMMAND

IDOK IDOK, :

1) 2)

IDCANCEL. WM_COMMAND Enter , . WS_DEFPUSHBUTTON; WS_DEFPUSHBUTTON

IDOK. IDCANCEL, IDCANCEL.

WM_COMMAND Esc

250 6.2. –

.

CreateWindow. : 1) 2) 3) 4)

; ; ; , ,

, DialogBoxIndirect

,

.



,

.

,

. . CreateDialogIndirect.

6.2.1. DialogBoxIndirect

2: int DialogBoxIndirect( HINSTANCE hInstance, LPDLGTEMPLATE lpTemplate, HWND hWndParent, DLGPROC lpDialogFunc); , EndDialog. DialogBoxIndirect ( WS_VISIBLE) . : 1) hInstance – ; 2) lpTemplate – ; 3) hWndParent – ;

1-

,

251 4) lpDialogFunc –

. ,

DialogBoxIndirect EndDialog,

nResult, -1. DialogBoxIndirect

EndDialog: BOOL EndDialog( HWND hdlg, int nResult); .

EndDialog .

EndDialog DialogBoxIndirect WM_INITDIALOG. EndDialog ,

.

(nResult) .

. nResult

.

6.2.2. CreateDialogIndirect: HWND CreateDialogIndirect(HINSTANCE hInstance, LPCDLGTEMPLATE lpTemplate, HWND hWndParent, DLGPROC lpDialogFunc); DialogBoxIndirect, .

,

CreateDialogIndirect WS_VISIBLE.

6.2.3. : DLGTEMPLATE

DLGITEMTEMPLATE. .

DLGITEMTEMPLATE. DLGTEMPLATE. .

,

252 DLGTEMPLATE : typedef struct { DWORD style; DWORD dwExtendedStyle; WORD edit; short x; short y; short ex; short cy; } DLGTEMPLATE; DLGTEMPLATE: 1) style . WSИ ( WS_CAPTION WS_ SYSMENU) HDS_ (HDS_MODALFRAME, HDS_CONTROL); 2) dwExtendedStyle . . . edit ; ; ; ; . , , , . DLGTEMPLATE , . DS_SETFONT, 16. , , WORD. : LPWORD lpwAlign(LPWORD lpln) { ULONG ul = (ULONG)lpIn; id +=3; ul >>=2; ul 0) nchar=nCopyAnsiToWideChar(p,TEXT(txt)); else nchar=nCopyAnsiToWideChar(p,TEXT("")); += nchar; *p++ = 0; p=lpwAlign((LPWORD)p); } , CreateDlg.h . 2. #define #define #define #define #define 3.

ID_STATIC ID_BUTTON1 ID_BUTTON2 ID_BUTTON3 ID_HELP

2000 2001 2002 2003 2004

CreateDlg: case WM_LBUTTONDOWN: { CreateDlg(hwnd); return 0; } 4. CrОКЭОDХР – : 1) : char const caption[ ]=" "; char const stattxt[ ]=" :"; char const modeoff[ ]=" "; char const modedat[ ]=" "; char const modepsw[ ]=" ";

264 CreateDlg

. ;

.

2) WORD: WORD *p, *pdlgtemplate;

: pdlgtemplate=p=(PWORD)LocalAlloc(LPTR,2000); 3) .

. 3

.

: int cxChar, cyChar; { TEXTMETRIC tm; HDC hdc=GetDC(hwnd); GetTextMetrics(hdc,&tm); ReleaseDC(hwnd,hdc); cxChar=tm.tmAveCharWidth+1; cyChar=tm.tmHeight+tm.tmExtemalLeading; } 4) : DWORD dlgunit =GetDialogBaseUnits(); int dlgwunit=LOWORD(dlgunit), dlghunit=HIWORD(dlgunit); 5) : cxChar=cxChar*4/dlgwunit;cyChar=cyChar*8/dlghunit; ; 6) :



: DWORD IStyle = DS_CENTER | DS_MODALFRAME | WS_POPUPWINDOW | WS_CAPTION; DS_CENTER . , ( , caption) wDlg=lstrlen(caption)*cxChar;

;

265 ; : hDlg=cyChar*10; ; DlgTemplate: DlgTemplate(p, IStyle, 7,0, 0, wDIg, hDIg, (LPSTR)caption); 7) – : hItem=cyChar; top=left=hItem/2; hItem+=left; wItem=(wDlg-left); IStyle = WS_CHILD | WS_VISIBLE | BS_GROUPBOX | WS_TABSTOP; DlgItemTemplate(p, (Style, left, top, wItem, 4*hItem+left, ID_STATIC, (LPSTR)"button",(LPSTR)stattxt); wItem=lstrlen(modeoff)*cxChar+10; top+=hItem; IStyle = BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP; DlgItemTemplate(p, IStyle, hItem, top, wItem, hItem, ID_BUTTON1, (LPSTR)"button", (LPSTR)modeoff); DlgItemTemplate(p, IStyle, left+wItem+hItem+wltern+hltern, top, wItem, hItem, ID_HELP, (LPSTR)"button",(LPSTR)" "); 8) DialogBoxIndirect: DialogBoxIndirect(hInstance, (LPDLGTEMPLATE)pdlgtemplate,hwnd, (DLGPROC)DlgProc); ; 9)

,

CreateDlg

: LocalFree(LocalHandle(pdlgtemplate)); 5. : LRESULT CALLBACK DlgProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_COMMAND: { switch (LOWORD(wParam)) { case IDOK: case IDCANCEL:

266 {

EndDialog(hdlg,TRUE); return TRUE;

} } } } return FALSE; } «

«

,

»

»,

Esc. ,

.

CreateDlg.

6.2.4.

.

«



», «

6.1

. « » « » «

«

« ».

».

» »,

Л 6.2. . #include #include "CreateDIg.h" #define ID_STATIC 2000 #define ID_BUTTON1 2001 #define ID_BUTTON2 2002 #define ID_BUTTON3 2003 #define ID_HELP 2004 BOOL RegClass(WNDPROC, LPCTSTR, UINT); LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); LRESULT CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM); HWND CreateDlg(HWND, WORD*); HINSTANCE hInstance; char szClass[ ]="WindowAppClass";

» 3

267 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow) { MSG msg; HWND hwnd; ::hInstance=hInstance; if (!RegClass(WndProc,szClass,COLOR_DESKTOP)) return FALSE; int wScreen=GetSystemMetrics(SM_CXSCREEN); int hScreen=GetSystemMetrics(SM_CYSCREEN); hwnd = CreateWindow(szClass, " ", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0,0, wScreen, hScreen, 0,0, hInstance, NULL); if (!hwnd) return FALSE; while(GetMessage(&msg, 0,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg);} return msg.wParam; } BOOL RegClass(WNDPROC Proc, LPCTSTR szName, UINT brBackground) { WNDCLASS wc; wc.style=wc.cbClsExtra=wc.cbWndExtra=0; wc.lpfnWndProc=Proc; wc.hInstance = hInstance; wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(brBackground + 1); wc.lpszMenuName = (LPCTSTR)NULL; wc.lpszClassName=szName; return (RegisterClass(&wc) != 0); { LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static WORD *pdlgtemplate; static HWND hdlg; switch (msg) { case WM_CREATE: { pdlgtemplate=(PWORD)LocalAlloc(LPTR,2000); hdlg=CreateDlg(hwnd, pdlgtemplate); return 0; } case WM_LBUTTONDOWN: { ShowWindow(hdlg,SW_SHOWNORMAL); return 0;

268 } case WM_RBUTTONDOWN: { ShowWindow(hdlg,SW_HIDE); return 0; } case WM_DESTROY: { DestroyWindow(hdlg); LocalFree(LocaIHandle(pdlgtemplate)); PostQuitMessage(0); return 0; } } return DefWindowProc(hwnd, msg, wParam, lParam); } HWND CreateDlg( HWND hwnd, WORD *pdlgtemplate) { char const captionД Ж=" "; char const stattxtД Ж=" :"; char const modeoff[ ]=" "; char const modedatД Ж=" "; char const modepsw[ ]=" "; WORD *p; p=pdlgtemplate; int cxChar, cyChar; { TEXTMETRIC tin; HDC hdc=GetDC(hwnd); GetTextMetrics(hdc,&tm); ReleaseDC(hwnd.hdc); cxChar=tm.tmAveCharWidth+1; cyChar=tm.tmHeight+tm.tmExternalLeading; . } DWORD dlgunit =GetDialogBaseUnits(); int dlgwunit=LOWORD(dlgunit), dlghunit=HIWORD(dlgunit); cxChaFCxChar*4/dlgwunit;cyChar=cyChar*8/dlghunit; int wDIg, hDIg, wItem, hItem, left, top; DWORD IStyle; // IStyle = DS_CENTER | DS_MODALFRAME | WS_POPUPWINDOW | WS_CAPTION; wDlg=lstrien(caption)*cxChar; hDlg=cyChar*10; DlgTemplate(p,

269 IStyie, 7,0,0, wDIg, hDIg, (LPSTR)caption); // //1 hItem=cyChar; top=left=hItem/2; hItem+=left; wItem=(wDlg-left-left); IStyle = WS_CHILD | WS_VISIBLE | BS_GROUPBOX | WS_TABSTOP; DlgItemTemplate(p, IStyle, left, top, wItem, 4*hItem+left, ID_STATIC,(LPSTR) "button",(LPSTR)stattxt); //2 wItem=lstrlen(rnodeoff)*cxChar+10; top+=hItem; IStyle = BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP; DlgItemTemplate(p, IStyle, hItem, top, wItem, hItem, ID_BUTTON1, (LPSTR)"button", (LPSTR)modeoff); //3 wItem=lstrlen(modedat)*cxChar+10; top+=hItem; IStyle = BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE; DlgItemTemplate(p, IStyle, hItem, top, wItem, hItem, ID_BUTTON2, "button", (LPSTR)modedat); //4 wItem=lstrlen(modepsw)*cxChar+10; top+=hItem; IStyle = BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE; DlgItemTemplate(p, IStyle, hItem, top, wItem, hItem, ID_BUTT0N3, (LPSTR)"button", (LPSTR)modepsw); //5 wItem=(wDlg-left-left-hItem-hItem)/3; top+=hItem+hItem/2+left; IStyle = BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP; DlgItemTemplate(p, IStyle, left, top, wItem, hItem, IDOK,(LPSTR)"button",(LPSTR)"fla"); //6 IStyle = BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP; DlgItemTemplate(p, IStyle, left+wItem+hItem, top, wItem, hItem, IDCANCEL,(LPSTR)"button",(LPSTR)" "); //7 DlgItemTemplate(p, IStyle, left+wItem+hItem+wItem+hItem, top, wItem, hItem, ID_HELP, (LPSTR)"button",(LPSTR)" );

270 // HWND hdlg=CreateDialogIndirect(hInstance, (LPDLGTEMPLATE)pdlgtemplate,hwnd, (DLGPROC)DlgProc); return hdlg; } LRESULT CALLBACK DlgProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_COMMAND: { switch (LOWORD(wParam)) { case IDOK: case IDCANCEL: { EndDialog(hdlg,TRUE); return TRUE;} } } } return FALSE; } . CreateDlg. , . ShowWindow. 1. ( ): 1) WM_CREATE : pdlgtemplate=(PWORD)LocalAlloc(LPTR,2000); CreateDlg: hdlg=CreateDlg(hwnd, pdlgtempiate); . , CreateDlg. CreateDlg ; 2) CreateDlg , . . , .

271 ,

;

3) : caseWM_DESTROY: { DestroyWindow(hdlg); LocalFree(LocalHandle(pdlgtemplate)); PostQuitMessage(0); return 0; } 2. : 1) , : case WM_LBUTTONDOWN: { ShowWindow(hdlg,SW_SHOWNORMAL); return 0; } 2) : caseWM_RBUTTONDOWN: { ShowWindow(hdlg,SW_HIDE); return 0; }

,

,

.

. .

, ,

.

6.3. .

WM_COMMAND . .

,

,

. . GetDlgItem

,

, SendMessage

,

272 hdlg: HWND GetDlgItem( HWND hdlg, int nIDDIgItem); nIDDIgItem

,

.

,

. ,

– NULL.

GetDlgItem , . . .

, .

,

,

. .

,

ID ИSWITCH,

HWND hSwitch = GetDlgItem( hdlg, IDC_SWITCH ); SendMessage( hSwitch, BM_SETCHECK, TRUE, OL); // He : SendMessage( GetDlgItem( hdlg, IDC_SWITCH ), BM_SETCHECK, TRUE, 0L); , . .

.

,

.

,

. .

-

,

. .

, -

,

273 . .

,

IsDialogMessage. , hdlg: BOOL IsDialogMessage( HWND hdlg, LPMSG lpMsg); lpMsg . IsDialogMessage hdlg. , IsDialogMessage , . IsDialogMessage TranslateMessage DispatchMessage. , . IsDialogMessage Tab

,

,

.

.

, : while (GetMessage(&msg, 0, 0, 0)) { if (!hdlg || !IsDialogMessage(hdlg, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } hdlg . , hdlg ,

hdlg;

, .

: static HWND

274 IsDialogMessage. hdlg. TranslateMessage

, DispatchMessage. . « » « »

«

» ,

»

»

«

, –

«

», «

.

.

«

».

. »

«

. «

«

».

» « .

.

.

« «

»

»

Л 6.3. . #include #include "CreateDlg.h" #define CM_FILE_OPEN 1001 #define CM_FILE_SAVE 1002 #define CM_FILE_QUIT 1003 #define ID_STATIC 2000 #define ID_LSTBOX 2001 BOOL RegClass(WNDPROC, LPCTSTR, UINT); LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); LRESULT CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM); HWND CreateDlg(HWND, WORD*); HACCEL CreateAccelTable( void ); HINSTANCE hInstance; char szClass[ ]="WindowAppClass"; static HWND hdlg; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow)

»

.

275 {

MSG msg; HWND hwnd; ::hInstance=hInstance; if (!RegClass(WndProc,szClass,COLOR_DESKTOP)) return FALSE; int wScreen=GetSystemMetrics(SM_CXSCREEN); int hScreen=GetSystemMetrics(SM_CYSCREEN); hwnd = CreateWindow(szClass, " ", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0,0, wScreen, hScreen, 0,0, hinstance, NULL); if (!hwnd) return FALSE; HACCEL hAccel=CreateAccelTable(); while (GetMessage(&msg, 0,0,0)) { if (!hdlg || !lsDialogMessage(hdlg, &msg)) if (!hAccel || !TranslateAcceierator(hwnd, hAccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } DestroyAcceleratorTable(hAccel); return msg.wParam;

} BOOL RegClassfWNDPRQC Proc, LPCTSTR szName, UINT brBackground) { WNDCLASS wc; wc.style=wc.cbClsExtra=wc.cbWndExtra=0; wc.lpfnWndProc=Proc; wchInstance = hinstance; wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wc.hCursor = LoadCursorJNULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(brBackground +1); wc.lpszMenuName = (LPCTSTR)NULL; wc.lpszClassName=szName; return (RegisterClass(&wc) != 0); } BOOL CreateMenuItem(HMENU hMenu, char *str, UINT ulns, UINT uCom, HMENU hSubMenu, BOOL flag, UINT fType) { MENUITEMINFO mii; mii.cbSize=sizeof(MENUITEMINFO); mii.fMask = MIIM.STATE | MIIM_TYPE | MIIM_SUBMENU | MIIM_ID;

276 mii.fType = fType; mii.fState= MFS.ENABLED; mii.dwTypeData = str; mii.cch=sizeof(str); mii.wID=uCom; mii.hSubMenu=hSubMenu; return InsertMenuItem(hMenu, ulns, flag, &mii); } HACCEL CreateAccelTable( void) { static ACCEL Accel[3]; Accel[0].Mrt= FVIRTKEY | FCONTROL; Accel[0].key = 0x4f; Accel[0].cmd = CM_FILE_0PEN; Accel[1].fVirt= FVIRTKEY | FCONTROL; Accel[1].key = 0x53; Accel[1].cmd = CM_FILE_SAVE; Accel[2].fVirt= FVIRTKEY | FALT; Accel[2].key = 0x73; Accel[2].cmd = CM_FILE_QUIT; return CreateAcceleratorTable((LPACCEL)Accel,8); } LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static WORD *pdlgtemplate; static HMENU hMainMenu, hFileMenu; switch (msg) { caseWM_CREATE: { hMainMenu=CreateMenu(); hFileMenu=CreatePopupMenu(); int i=0; CreateMenuItem(hFileMenu,"& ",i++, CM_FILE_OPEN, NULL, FALSE, MFT_STRING); CreateMenuItem(hFileMenu,"& ",i++, CM_FILE_SAVE, NULL, FALSE, MFT_STRING); CreateMenuItem(hFileMenu,NULL,i++,0, NULL, FALSE, MFT_SEPARATOR); CreateMenuItem(hFileMenu,"& ",i++, CM_FILE_QUIT, NULL, FALSE, MFT_STRING); CreateMenu!tem(hMainMenu,"& ", 0,0, hFileMenu,FALSE, MFT_STRING);

277 SetMenu(hwnd,hMainMenu);DrawMenuBar(hwnd); pdlgtemplate=(PWORD)LocaiAlloc(LPTR,2000); hdlg=CreateDlg(hwnd, pdlgtemplate); return 0; } case WM_COMMAND: { switch (LOWORD(wParam)) { case CM_FILE_OPEN: { ShowWindow(hdlg, SW_SHOWNORMAL); return 0; } case CM_FILE_SAVE: { ShowWindow(hdlg,SW_HIDE); return 0; } case CM_FILE_QUIT: { DestroyWindow(hwnd); return 0; } } return 0; } caseWM_DESTROY: { DestroyWindow(hdlg); LocalFree(LocalHandle(pdlgtemplate)); PostQuitMessage(O); return 0; } } return DefWindowProc(hwnd, msg, wParam, lParam); } HWND CreateDlg( HWND hwnd, WORD *pdlgtemplate) { char const caption[ ]=" "; char const stattxt[ ]=" -"; WORD *p; p=pdlgtemplate; // cxChar cyChar int cxChar, cyChar;

278 {

TEXTMETRIC tm; HDC hdc=GetDC(hwnd); GetTextMetrics(hdc,&tm); ReleaseDC(hwnd,hdc); cxChar=tm.tmAveCharWidth+1; cyChar=tm.tmHeight+tm.tmExtemalLeading;

} // DWORD dlgunit =GetDialogBaseUnits(); int dlgwunit=LOWORD(dlgunit), dlghunit=HIWORD(dlgunit); // cxChar=cxChar*4/dlgwunit; cyChar=cyChar*8/dlghunit; int wDIg, hDIg, wItem, hItem, left, top; DWORD IStyle; // IStyle = DS_CENTER | DS.MODALFRAME | WS_POPUPWINDOW | WS_CAPTION; wDlg=lstrlen(caption)*cxChar; hDlg=cyChar*9; DlgTemplate(p, IStyle, 5,0,0, wDIg, hDIg, (LPSTR)caption); // //1 hItem=cyChar; top=left=hItem/2; hItem+=left; wItem=lstrlen(stattxt)*cxChar; IStyle = SS_LEFT | WS_CHILD | WS_VISIBLE; DlgItemTemplate(p, IStyle, left, top, wItem, hItem, -1, (LPSTR)"static",(LPSTR)stattxt); //2 DlgItemTemplate(p, IStyle, left+wItem, top, wItem, hItem, ID_STATIC,(LPSTR)"static",(LPSTR)""); //3 top+=hItem; IStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | LBS.STANDARD | LBS.DISABLENOSCROLL; DlgItemTemplate(p, IStyle, left, top, wItem, hItemM, ID LISTBOX, (LPSTR)"listbox",(LPSTR)"\0"); //4 wItem=wDlg-wItem-hItem;

279 left+=left+lstrlen(stattxt)*cxChar; IStyle = BS_DEFPUSHBUnON | WS_VISIBLE | WS_CHILD | WS_TABSTOP; DlgItemTemplate(p, IStyle, left, top, wItem, hItem, IDOK,(LPSTR)"button",(LPSTR) " "); //5 top += hItem+hItem; IStyle = BS.PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP; DlgItemTemplate(p, IStyle, left, top, wItem, hItem, IDCANCEL,(LPSTR)"button",(LPSTR)" "); // HWNDhdlg=CreateDialogIndirectfhInstance, (LPDLGTEMPLATE)pdlgtemplate,hwnd, (DLGPROC)DlgProc); return hdlg; } LRESULT CALLBACK DlgProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam) { static HMENU hMainMenu, hFileMenu; switch (msg) { case W_INITDIALOG: { hMainMenu=CreateMenu(); hFileMenu=CreatePopupMenu(); CreateMenuItem(hFileMenu,"& ", 0, CM_FILE_OPEN, NULL, FALSE, MFT_STRING); CreateMenuItem(hFileMenu,"& ", 1, CM_FILE_SAVE, NULL, FALSE, MFT_STRING); CreateMenuItem(hFileMenu,NULL,2,0, NULL, FALSE, MFT.SEPARATOR); CreateMenuItem(hFileMenu,"& ",3, CM_FILE_QUIT, NULL, FALSE, MFT_STRING); CreateMenuItem(hMainMenu,"& ", 0,0, hFileMenu,FALSE, MFT_STRING); SetMenu(hdlg,hMainMenu); DrawMenuBar(hdlg); return TRUE; } case WM_COMMAND:

280 {

{

switch (LOWORD(wParam)) { case IDOK: { int uSelectedItem; char Buffer[256]; // HWNDhListBox=GetDlgItem(hdlg,ID_LI STBOX); // uSelectedItem=(int)SendMessage(hListBo x, LB_GETCURSEL,0,0L); // if(uSelectedItem != LB_ERR) { SendMessage(hListBox, LB_GETTEXT, uSelectedItem,(LPARAM)Buffer); SetWindowText(GetDlgItem( hdlg,ID_STATIC),(LPCTSTR)Buffer); } return TRUE; } case CM_FILE_OPEN: HWND hListBox= GetDlgItem(hdlg,ID_LISTBOX); // SendMessage(hListBox, WM_SETREDRAW, FALSE, OL); // SendMessage(hListBox,LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox,LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, LB.ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, LB_ADDSTRING,

281 0, (LPARAMKLPSTR)" "); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox,LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); // SendMessage(hListBox, WM_SETREDRAW, TRUE, 0L); // InvalidateRect(hListBox, NULL, TRUE); return TRUE; } case IDCANCEL: { EndDialog(hdlg.TRUE); return TRUE; } } } } return FALSE; }

«

«

.

»

«

«

»

« «

».

.

. »

. »,

« ,

«

.

», «

»

(

»,

,

, »,

.

, «

« » «

,

.

» «

»

«

».

».

«

«

»

,

», « , «

», « ») «

», .

282 »

«

«

», .

. » ,

. ,

, .

: while (GetMessage(&msg, 0,0, 0)) { if (!hdlg || NsDialogMessage(hdlg, &msg)) if (!hAccel || !TranslateAccelerator(hwnd, hAccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } . , CreateDlg DlgProc. CreateDlg . DlgProc : 1. WM INITDIALOG. : hMainMenu=CreateMenu(); hFileMenu=CreatePopupMenu(); CreateMenuItem(hFileMenu,"& ", 0, CM_FILE;_OPEN, NULL, FALSE, MFT_STRING); CreateMenuItem(hFileMenu,"& ",3, CM_FILE_QUIT, NULL, FALSE, MFT_STRING); CreateMenuItem(hMainMenu,"& ", 0, 0, hFileMenu, FALSE, MFT_STRING); SetMenu(hdlg, hMainMenu); DrawMenuBar(hdlg); , . 2. « » :case CM_FILE_OPEN: { HWND hListBox=GetDlgItem(hdlg,ID_LISTBOX); SendMessage(hListBox, WM_SETREDRAW, FALSE, OL); SendMessage(hListBox,LB_ADDSTRING, 0, (LPARAM)(LPSTR) " "); SendMessage(hListBox, LB_ADDSTRING,

283 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, WM_SETREDRAW, TRUE, OL); InvalidateRect(hListBox, NULL, TRUE); return TRUE; } DlgProc: 1) : HWNDhListBox=GetDlgItem(hdlg,ID_LISTBOX); 2) : SendMessage(hListBox, WM_SETREDRAW, FALSE, 0L); , ; 3) : SendMessage(hListBox,LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); 4) : SendMessage(hl_istBox, WM_SETREDRAW, TRUE, OL); 5) : InvalidateRect(hListBox, NULL, TRUE). 3. : 1) « » : uSelectedItem=(int)SendMessage(hListBox,LB_GETCURSEL,0,OL); , : SendMessage(hListBox,LB_GEnEXT,uSelectedItem,(LPARAM)Buffer); ID_STATIC: SetWindowText( GetDlgItem( hdlg, ID_STATIC), (LPCTSTR)Buffer); 2) « » :EndDialog(hdlgJRUE). 6.4. ,

. PropertySheet: LPCPROPSHEETHEADER lppsph);

int

PropertySheet(

PROPSHEETHEADER lppsph. ,

. -1.

284 PROPSHEETHEADER .

:

typedef struct { DWORD dwSize; DWORD dwFlags; HWND hwndParent; HINSTANCE hInstance; union { HICON hIcon; LPCTSTR pszIcon; }; LPCTSTR pszCaption; UINT nPages union { UINT nStartPage LPCTSTR pStartPage; }; union \ { LPCPROPSHEETPAGE ppsp; HPROPSHEETPAGE FAR *phpage; }; PFNPROPSHEETCALLBACK pfnCallback; } PROPSHEETHEADER; PROPSHEETHEADER: 1) dwSize – PROPSHEETHEADER 2) dwFlags ,

PSH_DEFAULT PSH_MODELESS PSH NOAPPLYNOW PSH_PROPSHEETPAGE

« ppsp

. :

» phpage ,

PSH_PROPTITLE « PSH_USECALLBACK

pszCaption,

:» ,

PSH_USEHICON

pfnCallback ,

hIcon

PSH_USEPSTARTPAGE pStartPage

3) hwndParent –

nStartPage

-

;

.

285 4) hinstance –

, ;

5) hIcon – dwFlags ; 6) pszIcon – .

PSH_USEHICON,

dwFlags ; 7) pszCaption – ; 8) nPages – 9) nStartPage – 10) pStartPage – 11) ppsp – PROPSHEETPAGE, dwFlags ; 12) phpage – . CreatePropertySheetPage. PSHPROPSHEETPAGE, 13) pfnCalIback –

PSH_USEICONID,

phpage; ; ; . PSHPROPSHEETPAGE,

dwFlags ;

dwFlags ;

PSP_USECALLBACK,

dwFlags PropertySheet : ID_PSREBOOTSYSTEM

ID_PSRESTARTWINDOWS

.

PSH_MODELESS, ; PropertySheet

PSM_REBOOTSYSTEM –

PSM_RESTARTWINDOWS –

, .

286

PropSheetlsDialogMessage: PropSheet_lsDialogMessage( hdlg, &msg);

hdlg –

.

, msg – – TRUE. PROPSHEETPAGE:

typedef struct { DWORD wSize; DWORD dwFlags; HINSTANCE hInstance; union { LPCTSTR pszTempiate; LPCDLGTEMPLATE pResource; }; union { HICON hIcon; LPCTSTR pszIcon; } LPCTSTR pszTitle; DLGPROC pfnDlgProc; LPARAM lParam; LPFNPSPCALLBACK pfnCallback; UINTFAR *pcRefParent; } PROPSHEETPAGE; PROPSHEETPAGE: 1) dwSize , ; 2) dwFIags .

PSP_DEFAULT PSP_DLGINDIRECT

: .

pResource, pszTempiate PSP_HASHELP

,

287

,

PSP_USECALLBACK . pfnCallback PSP_USEHICON PSP_USEICONID

pszIcon

PSP_USEREFPARENT pcRefParent'Q , ,

PSP_USETITLE

,

pszTitle

3) hInstance – 4) pszTempIate –

,

,

;

.

, dwFlags

PSP_DLGINDIRECT; 5) pResource – dwFlags pResource 6) hIcon – dwFlags hIcon ; 7) pszIcon – . PSPUSEICONID, 8) pszTitle – PSPUSETITLE, 9) pfnDIgProc – . 10) lParam – 11) pfnCallback – dwFlags ; 12) 12pcRefParent – dwFlags

. PSPDLGINDIRECT, ;

. PSP_USEHICON,

dwFlags pszIcon .

;

.

,

dwFlags ; EndDialog. ;

.

PSPUSECALLBACK, . PSPUSERREFPARENT,

288 ; 1.

.

,

:

2. , static PROPSHEETPAGE Д3Ж. 3. , pPage[0].dwSize = sizeof(PROPSHEETPAGE); pPage[0].pfnDlgProc = DlgProcO; pPage[0].pszTitle = " 0"; 4. : static PROPSHEETHEADER pHeader; 5. , : pHeader.dwSize = sizeof(PROPSHEETHEADER); pHeader.pszCaption = " "; pHeader.nPages=sizeof(pPage) / sizeof(PROPSHEETPAGE). 6. :PropertySheet(&pHeader). . 6.2, 6.3 . Л 6.4. . #include #include #include "CreateDlg.h" #define ID_STATICO 1000 «НОПine ID_BUTTON1 1001 «НОПТЧО ID_BUTTON2 1002 «НОПТЧО ID_BUTTON3 1003 «НОПТЧО ID.HELP 1004 «НОПТЧО ID_STATIC1 2003 «НОПТЧО IDJ.ISTBOX 2004 «НОПТЧО ID.CREATEDLG 100 BOOL RegClass(WNDPROC, LPCTSTR, UINT); LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); LRESULT CALLBACK WndForDlg(HWND,UINT,WPARAM,LPARAM); LRESULT CALLBACK DlgProcO(HWND,UINT,WPARAM,LPARAM); LRESULT CALLBACK DIgProcI (HWND,UINT,WPARAM,LPARAM);

289 WORD* CreateDlgO(HWND); WORD* CreateDlgl(HWND); HINSTANCE hInstance; char szClass[ ]="WindowAppClass"; char szForDlgj ]="PopupWindow"; in wScreen, hScreen; static HWND hForDIg; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpszCmdLine, int nCmdShow) { MSG msg; HWND hwnd; ::hInstance=hInstance; if (!RegClass(WndProc,szClass,COLOR_DESKTOP)) return FALSE; if (!RegClass(WndForDlg,szForDlg,0)) return FALSE; wScreen=GetSystemMetrics(SM_CXSCREEN); hScreen=GetSystemMetrics(SM_CYSCREEN); hwnd = CreateWindow(szClass, " ", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0,0, wScreen, hScreen, 0,0, hInstance, NULL); if (!hwnd) return FALSE; hForDlg=CreateWindow(szForDlg, NULL, WS_POPUP, 0, 0,10,10, hwnd, 0, hInstance, NULL); while(GetMessage(&msg, 0,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } BOOL RegClass(WNDPROC Proc, LPCTSTR szName, UINT brBackground) { WNDCLASS wc; wc.style=wc.cbClsExtra=wc.cbWndExtra=0; wc.lpfnWndProc=Proc; wc.hInstance = hInstance; wc.hIcon=LoadIcon(NULL,IDI_APPLICATION); wchCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(brBackground +1); wc.lpszMenuName = (LPCTSTR)NULL;

290 wc.lpszClassName=szName; return (RegisterClass(&wc) != 0); } LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { caseWM_LBUTTONDOWN: { SendMessage(hForDlg, WM_COMMAND, ID_CREATEDLG, 0L); return 0; } case WM_DESTROY: {PostQuitMessage(O); return 0;} } return DefWindowProc(hwnd, msg, wParam, lParam); } int CALLBACK PSCallback( HWND hdlg, UINT uMsg, LPARAM lParam) { if (uMsg==PSCB_INITIALIZED) { RECT rc; GetWindowRect(hdlg, &rc); int l=rc.left, t=rc.top, w=rc.right-l, h=rc.bottorn-t; MoveWindow(hForDlg, (wScreen-w)/2, (hScreen-h)/2, w, h, TRUE); } return 0; } LRESULT CALLBACK WndForDlg( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { caseWM_COMMAND: switch (LOWORD(wParam)) { case ID_CREATEDLG: { static PROPSHEETPAGE pPage[2]; static PROPSHEETHEADER pHeader; pPage[0].dwSize = sizeof(PROPSHEETPAGE); pPage[0].dwFlags = PSP_USETITLE | PSP.DLGINDIRECT; pPage[0].pResource =(DLGTEMPLATE*)CreateDlgO(hwnd); pPage[0].pfnDlgProc = (DLGPROC)DlgProcO; pPage[0].pszTitle = " "; pPage[1].dwSize = sizeof(PROPSHEETPAGE);

291 pPage[1].dwFlags = PSP_USETITLE | PSP_DLGINDIRECT; pPage[1].pResource = (DLGTEMPLATE*)CreateDlg1(hwnd); pPage[1].pfnDlgProc = (DLGPROC)DlgProd; pPage[1].pszTitle = " "; pHeader.dwSize = sizeof(PRQPSHEETHEADER); pHeader.dwFlags =PSH_PROPSHEETPAGE | PSH_USECALLBACK; pHeader.hwndParertt = hwnd; pHeader.pszCaption = " "; pHeader.nPages = sizeof(pPage)/sizeof(PROPSHEETPAGE); pHeader.ppsp = (LPCPROPSHEETPAGE)pPage; pHeader.pfnCallback=(PFNPROPSHEETCALLBACK)PSCallback; PropertySheet(&pHeader); LocalFree(LocalHandle(pPage[0].pResource)); LocalFree(LocaIHandle(pPage[1].pResource)); return 0; } return 0; } } return DefWindowProc(hwnd, msg, wParam, lParam); } WORD* CreateDlgO( HWND hwnd) { char const caption! ]=" "; char const stattxtД Ж=" :"; char const modeoffД Ж=" "; char const modedat[ ]=" "; char const modepswД Ж=" "; // WORD *p, *pdlgtemplate; pdlgtemplate=p=(PWORD)LocalAlloc(LPTR,4000); int cxChar, cyChar; { TEXTMETRIC tm; HDC hdc=GetDC(hwnd); GetTextMetrics(hdc,&trn); ReleaseDC(hwnd.hdc); cxChar=tm.tmAveCharWidth+1 ; cyChar=tm.tmHeight+tm.tmExternalLeading;

292 } DWORD dlgunit =GetDialogBaseUnits(); int dlgwunit=LOWORD(dlgunit), dlghunit=HIWORD(dlgunit); cxChar=cxChar*4/dlgwunit;cyChar=cyChar*8/dlghunit; int wDIg, hDIg, wItem, hItem, left, top; DWORD IStyle; // IStyle = DS.MODALFRAME | WS_POPUPWINDOW | WS_CAPTION; wDlg=lstrlen(caption)*cxChar; hDlg=cyChar*8; DlgTemplate(p, IStyle, 7,0,0/wDlg, hDIg, (LPSTR)caption); // //1 hItem=cyChar; top=left=hItem/2; hItem+=left; wItem=(wDlg-left-left); IStyle = WS_CHILD | WS_VISIBLE | BS.GROUPBOX | WS_TABSTOP; DlgItemTemplate(p, IStyle, left, top, wItem, 4*hItem+left, ID_STATICO,(LPSTR)"button",(LPSTR)stattxt); //2 wItem=lstrlen(modeoff)*cxChar+10; top+=hItem; IStyle = BS_AUTORADIOBUTTON | WS_CHILDfWS_VISIBLE | WS_TABSTOP; DlgItemTemplate(p, IStyle, hItem, top, wItem, hItem, ID_BUTTON1, (LPSTR)"button", (LPSTR)modeoff); //3 wItem=lstrlen(modedat)*cxChar+10; top+=hItem; IStyle = BS_AUTORADIOBUTTON | WS_CHILD | WSJ/ISIBLE; DlgItemTemplate(p, IStyle, hItem, top, wItem, hItem, ID_BUTTON2, "button", (LPSTR)modedat); //4 wItem=lstrlen(modepsw)*cxChar+10; top+=hItem; IStyle = BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE; DlgItemTemplate(p, IStyle, hItem, top, wItem, hItem, ID_BUTTON3, (LPSTR)"button", (LPSTR)modepsw);

293 //5 wItem=(wDlg-left-left-hItem-hItem)/3; top+=h!tem+hItem/2+left; IStyle = BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP; DlgItemTemplate(p, IStyle, left, top, wItem, hItem, IDOK,(LPSTR)"button",(LPSTR)" "); //6 IStyle = BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP; DlgItemTemplate(p, IStyle, left+wItem+hItem, top, wItem, hItem, IDCANCEL,(LPSTR)"button",(LPSTR)" "); //7 DlgItemTemplate(p, IStyle, left+wItem+hItem+wItem+hItem, top, wItem, hItem, ID_HELP, (LPSTR)"button",(LPSTR)" "); return pdlgtemplate; } WORD* CreateDlg1( HWND hwnd) { char const caption[ ]=" "; char const stattxt[ ]=" -"; WORD *p, *pdlgtemplate; pdlgtemplate=p=(PWORD)LocalAlloc(LPTR,4000); int cxChar, cyChar; { TEXTMETRIC tm; HDC hdc=GetDC(hwnd); GetTextMetrics(hdc,&tm); ReleaseDC(hwnd,hdc); cxChar=tm.tmAveCharWidth+1; cyChar=tm.tmHeight+tm.tmExternalLeading; } DWORD dlgunit =GetDialogBaseUnits(); int dlgwunit=LOWORD(dlgunit), dlghunit=HIWORD(dlgunit); cxChar=cxChar*4/dlgwunit; cyChar=cyChar*8/dlghunit; int wDlg, hDIg, wItem, hItem, left, top; DWORD IStyle; // IStyle = DS_MODALFRAME | WS_POPUPWINDOW |

294 WS_CAPTION; wDlg=lstrien(caption)*cxChar; hDlg=cyChar*9; DlgTemplate(p, IStyle, 5,0,0, wDlg, hDIg, (LPSTR)caption); // //1 hItem=cyChar; top=left=hItem/2; hItem+=left; wItem=lstrlen(stattxt)*cxChar; IStyle = SS_LEFT | WS_CHILD | WS_VISIBLE; DlgItemTemplate(p, IStyle, left, top, wItem, hItem, -1, (LPSTR)"static",(LPSTR)stattxt); //2 DlgItemTemplate(p, IStyle, left+wItem, top, wItem, hItem, ID_STATIC1, (LPSTR)"static",(LPSTR)""); //3 top+=hItem; IStyle = WS_CHILD | WS_VISIBLE | WS_TABSTOP | LBS_STANDARD | LBS_DISABLENOSCROLL; DlgItemTemplate(p, IStyle, left, top, wItem, hItem*4, ID_LISTBOX,(LPSTR)"listbox",(LPSTR)"\0"); //4 wItem=wDlg-wItem-h Item; left+=left+lstrlen(stattxt)*cxChar; IStyle = BS_DEFPUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP; DlgItemTemplate(p, IStyle, left, top, wItem, hItem, IDOK, (LPSTR)"button",(LPSTR) " "); //5 top += hItem+hItem; IStyle = BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD | WS_TABSTOP; DlgItemTemplate(p, IStyle, left, top, wItem, hItem, IDCANCEL, (LPSTR)"button",(LPSTR)" "); return pdlgtemplate; } LRESULT CALLBACK DlgProcO(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { caseWM_COMMAND: { switch (LOWORD(wParam))

295 {

case IDOK: case IDCANCEL: { return TRUE;}

} return FALSE; }

} return FALSE;

} LRESULT CALLBACK DlgProc1(HWND hdig, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { caseWMJNITDIALOG: { HWND hListBox=GetDlgItem(hdlg,ID_LISTBOX); SendMessage(hListBox, WM_SETREDRAW, FALSE, OL); SendMessage(hListBox, LB_ADDSTRlNG, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox,WM_SETREDRAW,TRUE,0L); InvalidateRect(hListBox, NULL, TRUE); return TRUE; } case WM_NOTIFY: { LPNMHDR pn=(LPNMHDR)lParam; switch (pn->code) { casePSN_SETACTIVE: { MessageBoxtfidlg, "PSN SETACTIVE", "WM_NOTIFY",MB_OK); return FALSE;

296 } case PSN_APPLY: { MessageBox(hdlg,"PSN_APPLY", "WM_NOTIFY",MB_OK); return FALSE; } case PSN_QUERYCANCEL: { MessageBox(hdlg, "PSN_QUERYCANCEL", "WM_NOTIFY",MB_OK); return FALSE; } case PSN_KILLACTIVE: { MessageBox(hdlg, "PSN_KILLACTIVE", "WM_NOTIFY",MB_OK); return FALSE; } }return FALSE; } caseWM_COMMAND: { switch (LOWORD(wParam)) { case IDOK: { int uSelectedItem; char Buffer[256]; HWND hListBox=GetDlgItem(hdlg, ID_LISTBOX); uSelectedItem=(int)SendMessage( hListBox,LB_GETCURSEL,0,0L); if(uSelectedItem != LB_ERR) { SendMessage(hListBox, LB_GETTEXT, uSelectedItem,(LPARAM)Buffer); SetWindowText(GetDlgItem(hdlg, ID_STATIC1), (LPCTSTR)Buffer); } SendMessage(GetParent(hdlg), PSM_CHANGED, (WPARAM)(HWND)hdlg, 0L); return TRUE; } return FALSE;

297 } return FALSE; } } return FALSE; } .

,

. 1. 2. #include

3. WORD* WORD*

comctl32.1ib. prsht.h: \ , АТЧ32.

:

CreateDlgO(HWND); CreateDlgl(HWND); .

:

4. static HWNDhForDIg; WndForDlg

,

.

:

hForDlg

1)

.

,

hForDlg ;

. WMCOMMAND

2) LOWORD(wParam)=lD_CREATEDLG ; 3) , . . .

. .

hForDlg , hForDlg, :

,

298 int CALLBACK PSCallback(HWND hdlg.UINT uMsg, LPARAM lParam) { if(uMsg==PSCB_INITIALIZED) { RECT re; GetWindowRect(hdlg, &rc); int l=rc.left, t=rc.top, w=rc.right-l, h=rc.bottom-t; MoveWindow(hForDlg, (wScreen-w)/2, (hScreen-h)/2, w, h, TRUE); } return 0; } hdlg hForDlg , . . , w h 1: MoveWindow(hForDlg, (wScreen-w)/2, (hScreen-h)/2,1,1, TRUE); 5. СFШrDХР АMИCOMMAND IDИCREATEDLG: SendMessage(hForDlg, WM_COMMAND, ID_CREATEDLG, OL); 6. hForDlg . . ( 6.3). , . , . . , – , . , . – . , . 7. . . : 1) : SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)(LPSTR)" "); ! ; 2)

299 «

.

»

.

, .

.

, : SendMessage(GetParent(hdlg), PSM_CHANGED, (WPARAM)(HWND)hdlg, 0L); GetParent , . . hdlg . , PSMCHANGED, , « ». , EndDialog. , ; 3) . WM_NOTIFY . lParam NMHDR, : LPNMHDR pn=(LPNMHDR)lParam; .

,

PSNSETACTIVE, PSNKILLACTIVE. PSNAPPLY, PSNQUERYCANCEL. . , PSNKJLLACTIVE,

«



PSNAPPLY.

6.5. АТЧ32 API 6.5.1.

GetOpenFileName GetSaveFileName. GetOpenFileName

«

.

» »

300 ,

. .

GetOpenFileName : BOOL GetOpenFileName( LPOPENFILENAME lpofn ); lpofn OPENFILENAME, . GetOpenFileName . , . , lpstrFile , – . GetSaveFiIeName . , . : BOOL GetSaveFiIeName( LPOPENFILENAME lpofn); lpofn OPENFILENAME, . GetSaveFiIeName . ., . , lpstrFile , . – . OPENFILENAME : typedef struct { DWORD IStructSize; HWND hwndOwner; HINSTANC hInstance; E LPCTSTR lpstrFilter; LPTSTR lpstrCusromFilter; DWORD nMaxCustFilter; DWORD nFilterlndex; LPTSTR lpstrFile; DWORD nMaxFile; LPTSTR lpstrFileTitle;

301 DWORD nMaxFileTitle; LPCTSTR lpstrlnitialDir; LPCTSTR lpstrTitle; DWORD Flags; WORD nFileOffset; WORD nFileExtension; LPCTSTR lpstrDefExt; DWORD ICustData; LPOFNHO lpfnHook; OKPROC LPCTSTR lpTemplateName; } OPENFILENAME; OPENFILENAME: 1) IStructSize OPENFILENAME ; 2) hwndOwner – ( ); 3) hInstance . , OFNENABLETEMPLATE OFN_ ENABLETEMPLATEHANDLE. OFNEXPLORER, Flags Explorer, ; 4) lpstrFilter – , . , « \0*.txt\0». '\0' . ( « ») , ( «*.txt») . , ( , «*.txt; *.doc; *.bak»). lpstrFilter=NULL, lpstrCustomFilter; 5) lpstrCustomFilter – , . 40 ; 6) nMaxCustFilter lpstrCustomFilter; 7) nFilterlndex lpstrFilter . nFilterlndex ; 8) lpstrFile – , . , , '\0',

302 , 9) nMaxFile , 10) lpstrFileTitle

'\0';

'\0'.

lpstrFile;

, ,

. ;

11) nMaxFileTitle , lpstrFileTitle=NULL; 12) lpstrInitialDir , ; 13) lpstrTitle , ; 14) nFileOffset lpstrFile. , lpstrFile «c:\dirl\dir2\file.ext», nFileOffset file.ext , 13; 15) nFileExtension lpstrFile. lpstrFile «c:\dir1\dir2\file.ext», ext , 16) lpstrDefExt , . , 17) ICustData lParam; 18) Flags

, IS;

;

: OFN_ALLOWMULTl'SELECT OFN_CREATEPROMPT

, ,

OFN_ENABLEHOOK lpfnHook

.

. ,

303

OFN_EXPLORER

OFN_EXTENSIONDIFFERENT

OFN ENABLEHOOK OFN ALLOWMULTISELECT , ,

OFN_FILEMUSTEXIST

. OFN_PATHMUSTEXIST « »

OFN_HIDEREADONLY OFN_NOCHANGEDIR OFN_NONETWORKBUTTON OFN_NOREADONLYRETURN

lpstrDefExt

Network «

» ,

OFN_OVERWRITEPROMPT

OFN_PATHMUSTEXIST «

OFN_READONLY OFN_SHAREAWARE

»

«

OFN_SHOWHELP

»

,

19) lpfhHook ; 20) lpTemplatename ,

,

.

Flags

«

OFN_ ENABLETEMPLATE. . « », « » » . « » . Л

6.5 .

«

«

».

»

. .

. .

304 #include #define CM_FILE_OPEN 1001 #define CM_FILE_SAVE 1002 #define CM_FILE_QUIT 1003 BOOL RegClass(WNDPROC, LPCTSTR, UINT); LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); HiNSTANCE hInstance; char szClass[] ="OpenSaveFile"; char szTitleД Ж =" "; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { MSG msg; HWND hwnd; ::hInstance=hInstance; if (!RegClass(WndProc, szClass, COLOR_WINDOW)) return FALSE; if (!(hwnd = CreateWindow(szCiass, szTitle, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInstance, NULL))) return FALSE; while (GetMessage(&msg, 0,0,0)) DispatchMessage(&msg); return msg.wParam; } BOOL RegClass(WNDPROC Proc, LPCTSTR szName, UINT brBackground) { WNDCLASS wc; wc.style=wc.cbClsExtra=wc.cbWndExtra=0; wc.lpfnWndProc = Proc; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(brBackground + 1); wc.lpszMenuName=(LPCTSTR)NULL; wc.lpszClassName=szName; return (RegisterClass(&wc) != 0); } BOOL CreateMenuItem(HMENU hMenu, char*str, UINT ulns, UINT uCom, HMENU hSubMenu, BOOL flag, UINT fType) { static MENUITEMINFO mii;

305 mii.cbSize=sizeof(MENUITEMINFO); mii.fMask = MIIM_STATE | MIIM_TYPE | MIIM_SUBMENU | MIIMID; mii.fType = fType; mii.fState= MFS_ENABLED; mii.dwTypeData = str; mii.cch=sizeof(str); mii.wID=uCom; mii.hSubMenu=hSubMenu; return InsertMenuItem(hMenu, ulns, flag, &mii); } LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HMENU hMainMenu, hFileMenu; static OPENFILENAME ofn; static char szFile[256]; static char szFileTitle[256]; static char CustomFilter[256]; switch (msg) { case WM_CREATE: { hMainMenu=CreateMenu(); hFileMenu=CreatePopupMenu(); int i=0; CreateMenuItem( hFileMenu, "& ", i++, CM_FILE_OPEN, NULL, FALSE, MFT_STRING); CreateMenuItem( hFileMenu, "& ", i++, CM_FILE_SAVE, NULL, FALSE, MFT_STRING); CreateMenuItem( hFileMenu, NULL, i++, 0, NULL, FALSE, MFT_SEPARATOR); CreateMenuItem( hFileMenu, "& ", i++, CM_FILE_QUIT, NULL, FALSE, MFT_STRING); i=0; CreateMenuItem(hMainMenu,"& ",i++, 0, hFileMenu,FALSE, MFT_STRING); SetMenu(hwnd,hMainMenu); DrawMenuBar(hwnd); ofn.lStructSize = sizeof(OPENFILENAME); ofn.hwndOwner = hwnd; ofn.nFilterlndex =1; ofn.lpstrFile = szFile; ofn.nMaxFile = sizeof(szFile); ofn.lpstrFileTitle = szFileTitle;

306 ofn.nMaxFileTitle = sizeof(szFileTitle); ofn.lpstrCustomFilter =CustomFilter; return 0; } case WM_COMMAND: { switch (LOWORD(wParam)) { case CM_FILE_OPEN: { ofn.Flags = OFN.EXPLORER | OFN_CREATEPROMPT | OFN_ALLOWMULTISELECT; ofn.lpstrTitle= " "; charszFilter[256] = " C++ \0*. ;*. \0" " \0 1;*. \0" " \0*.*\0"; ofn.lpstrFilter=szFilter; szFileTitle[0]='\0'; szFile[0]='\0'; if (GetOpenFileName(&ofn)) { charstr[512]; strcpy(str," :\t); for (int i=0; i