# Include <windows. h> # include "resource. H "# define id_edit 1 lresult callback wndproc (hwnd, uint, wparam, lparam); tchar szappname [] = text (" poppad2 "); int winapi winmain (hinstance, hinstance hprevinstance, pstr szcmdline, int icmdshow) {haccel; hwnd; MSG; wndclass. style = cs_vredraw | cs_hredraw; wndclass. lpfnwndproc = wndproc; wndclass. cbclsextra = 0; wndclass. cbwndextra = 0; wndclas S. hbrbackground = (hbrush) getstockobject (white_brush); wndclass. hinstance = hinstance; // The first parameter cannot be null. Otherwise, some unexpected results will be generated. hicon = loadicon (hinstance, makeintresource (idi_icon1); wndclass. hcursor = loadcursor (null, idc_arrow); wndclass. lpszclassname = szappname; // This place cannot be null, or the wndclass in the preceding menu will not be available. lpszmenuname = szappname; If (! Registerclass (& wndclass) {MessageBox (null, text ("this program requires Windows NT! "), Szappname, mb_iconerror); Return 0;} // center, half screen size hwnd = createwindow (szappname, szappname, ws_overlappedwindow, getsystemmetrics (sm_cxscreen)/4, getsystemmetrics (sm_cyscreen)/4, getsystemmetrics (sm_cxscreen)/2, getsystemmetrics (sm_cyscreen)/2, null, null, hinstance, null); showwindow (hwnd, icmdshow ); updatewindow (hwnd); // call the acceleration key table haccel = loadaccelerators (hinstance, szappname); While (getmessage (& MSG, null,) {/* transla The teaccelerator function determines whether the message stored in the MSG message structure is a Keyboard Message. If yes, this function searches for the matching value with the haccel handle in the acceleration key table. If a matching value is found, it will call the window process whose handle is hwnd, that is, askconfirmation (hwnd ). If the keyboard accelerator key ID corresponds to a menu item in the System menu, the corresponding message is wm_syscommand; otherwise, the message is wm_command. When the translateaccelerator returns, if the message has been translated (and has been sent to the window process), the return value is a non-zero value; otherwise, the return value is zero */If (! Translateaccelerator (hwnd, haccel, & MSG) {translatemessage (& MSG); dispatchmessage (& MSG) ;}} return MSG. wparam;} int askconfirmation (hwnd) {return MessageBox (hwnd, text ("really want to close poppad2"), szappname, mb_yesno | mb_iconquestion );} lresult callback wndproc (hwnd, uint message, wparam, lparam) {static hwnd hwndedit; int iselect, ienable; Switch (Message) {Case wm_create:/* ws_child subwindow; ws_v Isible display; ws_hscroll and ws_vscroll; ws_border; es_left alignment; es_muliline; es_autovscroll automatic scroll bar */hwndedit = createwindow (text ("edit"), null, ws_child | ws_visible | ws_hscroll | ws_vscroll | ws_border | es_left | es_multiline | average, hwnd, (hmenu) id_edit, (callback) lparam)-> hinstance, null); Return 0; Case wm_setfocus: // you can specify the intersection of setfocus (hwndedit) in the text box. Return 0; Case wm_size :/ /Set the size of movewindow (hwndedit, 0, 0, loword (lparam), hiword (lparam), true); Return 0; // It should be triggered when the drop-down menu changes to active (it can be understood as before the pop-up. Case wm_initmenupopup: // according to the book, when the menu editing is to be displayed, lparam is 1if (lparam = 1) {// sendmessage determines whether the return value can be undone, if yes, return trueenablemenuitem (hmenu) wparam, idm_edit_undo, sendmessage (hwndedit, em_canundo )? Mf_enabled: mf_grayed); // determines whether the clipboard has corresponding data. If yes, the system returns non-0 enablemenuitem (hmenu) wparam, idm_edit_paste, isclipboardformatavailable (cf_text )? Mf_enabled: mf_grayed); // obtain the selected iselect = sendmessage (hwndedit, em_getsel,); // The iselect low position is the first selected position, A high character is the position of the first character next to the selected text. If they are equal, it indicates that no text is selected. // This place first contacts if (hiword (iselect = loword (iselect ))) ienable = enabled; Enabled = mf_enabled; enablemenuitem (hmenu) wparam, enabled, ienable); enablemenuitem (hmenu) wparam, disabled, ienenable); enablemenuitem (hmenu) wparam, idm_edit_clear, ienable); Return 0;} break; Case wm_c Ommand: // determines the place where the wm_command message is generated. For the menu, the lparam value is 0; // subwindow, where the key is 1if (lparam) {// too many characters, if (loword (wparam) = id_edit & (hiword (wparam = en_errspace) MessageBox (hwnd, text ("Edit Control out of space. "), szappname, mb_ OK | mb_iconstop); Return 0;} else switch (loword (wparam) {Case idm_file_new: Case idm_file_open: Case idm_file_save: case when: Case idm_file_print: messagebeep (0); Return 0; Case idm_app_exit: S Endmessage (hwnd, wm_close,); Return 0; Case idm_edit_undo: sendmessage (hwndedit, wm_undo,); Return 0; Case idm_edit_cut: sendmessage (hwndedit, wm_cut ); return 0; Case idm_edit_copy: sendmessage (hwndedit, wm_copy, 0, 0); Return 0; case when: sendmessage (hwndedit, wm_paste, 0, 0); Return 0; Case idm_edit_clear: sendmessage (hwndedit, wm_clear, 0, 0); Return 0; Case idm_edit_select_all: sendmessage (hwndedit, EM _ Setsel, 0,-1); Return 0; Case idm_help_help: MessageBox (hwnd, text ("help not yet implemented! "), Szappname, mb_ OK | mb_iconexclamation); Return 0; Case idm_app_about: MessageBox (hwnd, text (" poppad2 (c) Charles Petzold, 2012 "), szappname, mb_ OK | mb_iconinformation); Return 0;} break; Case wm_close: If (idyes = askconfirmation (hwnd) destroywindow (hwnd); Return 0; Case wm_queryendsession: if (idyes = askconfirmation (hwnd) return 1; elsereturn 0; Case wm_destroy: postquitmessage (0); Return 0;} return defwindowproc (hwnd, message, wparam, lparam );}
This code is all the code. Let's take a look at it later. There are some useful things in it.