{"id":35,"date":"2008-01-25T00:59:09","date_gmt":"2008-01-25T04:59:09","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=35"},"modified":"2008-01-27T13:33:24","modified_gmt":"2008-01-27T17:33:24","slug":"windows-programming-basics","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=35","title":{"rendered":"Windows Programming Basics"},"content":{"rendered":"<p>Notations<\/p>\n<ul>\n<li>b: BOOL, int<\/li>\n<li>by: byte<\/li>\n<li>c:char<\/li>\n<li>dw:DWORD, unsigned long<\/li>\n<li>fn:function<\/li>\n<li>h:handle, int<\/li>\n<li>i:int<\/li>\n<li>l:long<\/li>\n<li>lp:long pointer<\/li>\n<li>n:int<\/li>\n<li>p:pointer<\/li>\n<li>s:string<\/li>\n<li>sz:a zero terminated string<\/li>\n<li>w:WORD, unsigned short<\/li>\n<\/ul>\n<p>WinMain()<\/p>\n<ul>\n<li>Function signature: <code lang=\"cpp\"><br \/>\nint WINAPI WinMain (HINSTANCE hInstance,<br \/>\nHINSTANCE hPreInstance, \/\/ Always NULL<br \/>\nLPSTR lpCmdLine,<br \/>\nint nCmdShow \/\/ e.g. SW_SHOWNORMAL, SW_SHOWHIDE );<br \/>\n<\/code><\/li>\n<li>WINAPI specifier\u00a0\n<ul>\n<li>Required by Windows<\/li>\n<\/ul>\n<\/li>\n<li>Need to include <em>window.h<\/em><\/li>\n<\/ul>\n<p>Create a simple Window<\/p>\n<ul>\n<li>Specifying a program window with WNDCLASS struct (or WNDCLASSEX)<br \/>\n<code lang=\"cpp\">struct WNDCLASS{<br \/>\nUINT style; \/\/ Window style<br \/>\nWNDPROC lpfnWndProc; \/\/ Pointer to message processing fxn<br \/>\nint cbClsExtra; \/\/ Extra byte after window class<br \/>\nint cbWndExtra; \/\/ Extra byte after win instance<br \/>\nHINSTANCE hInstance; \/\/ App handle<br \/>\nHICON hIcon; \/\/ App icon when minimized<br \/>\nHCURSOR hCursor; \/\/ Win cursor<br \/>\nHBRUSH hbrBackground; \/\/ Background color<br \/>\nLPCTSTR lpszMenuName; \/\/ Pointer to menu resource name. 0 if none<br \/>\nLPCTSTR lpszClassName; \/\/ Pointer to class name<br \/>\n};<br \/>\n<\/code><\/li>\n<li>Register WNDCLASS<br \/>\n<code lang=\"cpp\">RegisterClass(&amp;WinClass);<br \/>\n\/\/ Or RegisterClassEx(&amp;WindClassEx); <\/code><\/li>\n<li>Create window with CreateWindow():<br \/>\n<code lang=\"cpp\">HWND hWnd; \/\/ Window handle...hWnd = CreateWindow(<br \/>\nszAppName, \/\/ the window class name<br \/>\n\"A Basic Window the Hard Way\", \/\/ The window title<br \/>\nWS_OVERLAPPEDWINDOW, \/\/ Window style as overlapped<br \/>\nCW_USEDEFAULT, \/\/ Default screen position of upper left<br \/>\nCW_USEDEFAULT, \/\/ corner of our window as x,y...<br \/>\nCW_USEDEFAULT, \/\/ Default window size, width...<br \/>\nCW_USEDEFAULT, \/\/ ...and height<br \/>\n0, \/\/ No parent window<br \/>\n0, \/\/ No menu<br \/>\nhInstance, \/\/ Program Instance handle<br \/>\n0 \/\/ No window creation data<br \/>\n);<\/code><\/li>\n<li>Show window:<code lang=\"cpp\">ShowWindow(hWnd, nCmdShow);<\/code><\/li>\n<li>Update window:<code lang=\"cpp\">UpdateWindow(hWnd);<\/code><\/li>\n<li>Handle messages<br \/>\n<code lang=\"cpp\">MSG msg; \/\/ Windows message structure<br \/>\nwhile(GetMessage(&amp;msg, 0, 0, 0) == TRUE) \/\/ Get any messages<br \/>\n{<br \/>\nTranslateMessage(&amp;msg); \/\/ Translate the message<br \/>\nDispatchMessage(&amp;msg); \/\/ Dispatch the message to WindowProc()<br \/>\n}<\/code><\/li>\n<li>Message processing function:<br \/>\n<code lang=\"cpp\">long CALLBACK WindowProc(<br \/>\nHWND hWnd, \/\/ Window handle where message originated fromUINT msg, \/\/ message id<br \/>\nWPARAM wParam, \/\/ 32 bit value with addtional info<br \/>\nLPARAM lParam \/\/ another 32 bit value<br \/>\n);<br \/>\n<\/code><\/p>\n<ul>\n<li>Decoding windows message<br \/>\n<code lang=\"cpp\">switch(message)<br \/>\n{<br \/>\ncase WM_PAINT:<br \/>\n\/\/ Code to deal with drawing the client area<br \/>\nbreak;case WM_LBUTTONDOWN:<br \/>\n\/\/ Code to deal with the left mouse button being pressed<br \/>\nbreak;<br \/>\ncase WM_LBUTTONUP:<br \/>\n\/\/ Code to deal with the left mouse button being released<br \/>\nbreak;<br \/>\ncase WM_DESTROY:<br \/>\n\/\/ Code to deal with a window being destroyed<br \/>\nbreak;<br \/>\ndefault:<br \/>\n\/\/ Code to handle any other messages<br \/>\n}<\/code><\/p>\n<ul>\n<li>Default message processing\u00a0<br \/>\n<code lang=\"cpp\">default:<br \/>\n\u00a0 return DefWindowProc(hWnd, message, wParam, lParam); <\/code><\/li>\n<li>End program <code lang=\"cpp\">case WM_DESTROY: \/\/ Window is being destroyed<\/code><code lang=\"cpp\">\u00a0 PostQuitMessage(0);\u00a0 return 0; <\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Example:<br \/>\n<code lang=\"cpp\"><\/code><code lang=\"cpp\">\/\/ OFWIN.CPP Native windows program to display text in a window<br \/>\n#include <windows.h><\/windows.h><\/code><code lang=\"cpp\">long CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);<\/code><code lang=\"cpp\">\/\/ Listing OFWIN_1<br \/>\nint WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,<br \/>\n\u00a0 LPSTR lpCmdLine, int nCmdShow)<br \/>\n{<br \/>\n\u00a0 WNDCLASS WindowClass; \/\/ Structure to hold our window's attributes<\/code><code lang=\"cpp\">static char szAppName[] = \"OFWin\"; \/\/ Define window class name<br \/>\n\u00a0 HWND hWnd; \/\/ Window handle<br \/>\n\u00a0 MSG msg; \/\/ Windows message structure<\/code><code lang=\"cpp\">\/\/ Redraw the window if the size changes<br \/>\n\u00a0 WindowClass.style = CS_HREDRAW | CS_VREDRAW;<\/code><code lang=\"cpp\">\/\/ Define our procedure for message handling<br \/>\n\u00a0 WindowClass.lpfnWndProc = WindowProc;<\/code><code lang=\"cpp\">WindowClass.cbClsExtra = 0; \/\/ No extra bytes after the window class<br \/>\n\u00a0 WindowClass.cbWndExtra = 0; \/\/ structure or the window instance<\/code><code lang=\"cpp\">WindowClass.hInstance = hInstance; \/\/ Application instance handle<\/p>\n<p>\/\/ Set default application icon<br \/>\n\u00a0 WindowClass.hIcon = LoadIcon(0, IDI_APPLICATION);<\/p>\n<p>\/\/ Set window cursor to be the standard arrow<br \/>\n\u00a0 WindowClass.hCursor = LoadCursor(0, IDC_ARROW);<\/p>\n<p>\/\/ Set gray brush for background color<br \/>\n\u00a0 WindowClass.hbrBackground = static_cast<hbrush><\/hbrush>(GetStockObject(GRAY_BRUSH));<\/p>\n<p>WindowClass.lpszMenuName = 0; \/\/ No menu, so no menu resource name<\/p>\n<p>WindowClass.lpszClassName = szAppName; \/\/ Set class name<\/p>\n<p>\/\/ Now register our window class<br \/>\n\u00a0 RegisterClass(&amp;WindowClass);<\/p>\n<p>\/\/ Now we can create the window<br \/>\n\u00a0 hWnd = CreateWindow(<br \/>\n\u00a0 szAppName, \/\/ the window class name<br \/>\n\u00a0 \"A Basic Window the Hard Way\", \/\/ The window title<br \/>\n\u00a0 WS_OVERLAPPEDWINDOW, \/\/ Window style as overlapped<br \/>\n\u00a0 CW_USEDEFAULT, \/\/ Default screen position of upper left<br \/>\n\u00a0 CW_USEDEFAULT, \/\/ corner of our window as x,y...<br \/>\n\u00a0 CW_USEDEFAULT, \/\/ Default window size<br \/>\n\u00a0 CW_USEDEFAULT, \/\/ ....<br \/>\n\u00a0 0, \/\/ No parent window<br \/>\n\u00a0 0, \/\/ No menu<br \/>\n\u00a0 hInstance, \/\/ Program Instance handle<br \/>\n\u00a0 0 \/\/ No window creation data<br \/>\n\u00a0 );<\/p>\n<p>ShowWindow(hWnd, nCmdShow); \/\/ Display the window<br \/>\n\u00a0 UpdateWindow(hWnd); \/\/ Cause window client area to be drawn<\/p>\n<p>\/\/ The message loop<br \/>\n\u00a0 while(GetMessage(&amp;msg, 0, 0, 0) == TRUE) \/\/ Get any messages<br \/>\n\u00a0 {<br \/>\n\u00a0 TranslateMessage(&amp;msg); \/\/ Translate the message<br \/>\n\u00a0 DispatchMessage(&amp;msg); \/\/ Dispatch the message<br \/>\n\u00a0 }<\/p>\n<p>return msg.wParam; \/\/ End, so return to Windows<br \/>\n}<\/p>\n<p>\/\/ Listing OFWIN_2<br \/>\nlong CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam,<br \/>\n\u00a0 LPARAM lParam)<br \/>\n{<br \/>\n\u00a0 HDC hDC; \/\/ Display context handle<br \/>\n\u00a0 PAINTSTRUCT PaintSt; \/\/ Structure defining area to be drawn<br \/>\n\u00a0 RECT aRect; \/\/ A working rectangle<\/p>\n<p>switch(message) \/\/ Process selected messages<br \/>\n\u00a0 {<br \/>\n\u00a0 case WM_PAINT: \/\/ Message is to redraw the window<br \/>\n\u00a0 hDC = BeginPaint(hWnd, &amp;PaintSt);\/\/ Prepare to draw the window<\/p>\n<p>\/\/ Get upper left and lower right of client area<br \/>\n\u00a0 GetClientRect(hWnd, &amp;aRect);<\/p>\n<p>SetBkMode(hDC, TRANSPARENT); \/\/ Set text background mode<\/p>\n<p>\/\/ Now draw the text in the window client area<br \/>\n\u00a0 DrawText(<br \/>\n\u00a0 hDC, \/\/ Device context handle<br \/>\n\u00a0 \"But, soft! What light through yonder window breaks?\",<br \/>\n\u00a0 -1, \/\/ Indicate null terminated string<br \/>\n\u00a0 &amp;aRect, \/\/ Rectangle in which text is to be drawn<br \/>\n\u00a0 DT_SINGLELINE| \/\/ Text format - single line<br \/>\n\u00a0 DT_CENTER| \/\/ - centered in the line<br \/>\n\u00a0 DT_VCENTER); \/\/ - line centered in aRect<\/p>\n<p>EndPaint(hWnd, &amp;PaintSt); \/\/ Terminate window redraw operation<br \/>\n\u00a0 return 0;<br \/>\n\u00a0 case WM_DESTROY: \/\/ Window is being destroyed<br \/>\n\u00a0 PostQuitMessage(0);<br \/>\n\u00a0 return 0;<\/p>\n<p>default: \/\/ Any other message - we don't<br \/>\n\u00a0 \/\/ want to know, so call<br \/>\n\u00a0 \/\/ default message processing<br \/>\n\u00a0 return DefWindowProc(hWnd, message, wParam, lParam);<br \/>\n\u00a0 }<br \/>\n}<\/p>\n<p>Notes and example from:<br \/>\nBeginning Visual C++ 6<br \/>\nby Ivor Horton<\/p>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Notations b: BOOL, int by: byte c:char dw:DWORD, unsigned long fn:function h:handle, int i:int l:long lp:long pointer n:int p:pointer s:string sz:a zero terminated string w:WORD, unsigned short WinMain() Function signature: int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPreInstance, \/\/ Always NULL &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=35\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[11],"tags":[],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-cpp"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-z","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=35"}],"version-history":[{"count":0,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}