Understanding the internal mechanism of Windows program operation

Source: Internet
Author: User
Tags textout

Reference Xinxin Sun "VC + + in-depth detailed"


Steps to create a WIN32 program:

1. Write WinMain function

2. Design window class (WNDCLASS)

3. Registration window class

4. Create a window

5. Display and update the window (ShowWindow (hwnd,sw_shownormal); UpdateWindow (HWND);)

6. Message loop (constantly fetching and responding to messages from message queues)

7, window procedure function (processing the message sent to the window)


The test code is as follows (compiled by VS2010):

#include <stdafx.h> #include <windows.h> #include <stdio.h>lresult CALLBACK winproc (HWND hwnd,uint Umsg,wparam wparam,lparam LPARAM); int WINAPI WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpcmdline, int n ShowCmd) {//Design a window class wndclass Wndcls;wndcls.cbclsextra = 0;wndcls.cbwndextra = 0;wndcls.hbrbackground = (HBRUSH) Getstockobject (black_brush); wndcls.hcursor = LoadCursor (null,idc_cross); Wndcls.hicon = LoadIcon (NULL,IDI_ERROR); Wndcls.hinstance = Hinstance;wndcls.lpfnwndproc = Winproc;wndcls.lpszclassname = _t ("window"); wndcls.lpszmenuname = Null;wndcls.style = Cs_hredraw | Cs_vredraw; RegisterClass (&WNDCLS);//Create a window that defines a variable to hold the handle returned after the successful creation of the window hwnd Hwnd;hwnd = CreateWindow (_t ("window"), _t ("window"), ws_overlappedwindow,0,0,600,400,
Null,null,hinstance,null);//Display and Refresh Window ShowWindow (hwnd,sw_shownormal); UpdateWindow (HWND);//define message structure body, start message loop msg msg;while (GetMessage (&msg,null,0,0)) {TranslateMessage (&msg);D Ispatchmessage (&msg);} return Msg.wparam;} Window procedure function lresult CALLBACK winproc (HWND hwnd,uint umsg,wparam wparam,lparam LPARAM) {switch (umsg) {case Wm_char:char szchar[20];sprintf_s (Szchar, "You pressed%d", wParam); MessageBoxA (Hwnd,szchar, "char", 0), Break;case wm_lbuttondown:messagebox (hwnd,_t ("Mouse clicked"), _t ("message"), 0) ; HDC HDC;HDC = GetDC (hwnd); TextOut (hdc,0,100,_t ("wm_lbuttondown message"), strlen ("wm_lbuttondown message")); ReleaseDC (hwnd, HDC); break;case WM_PAINT:HDC Paint_hdc; Paintstruct PS; PAINT_HDC = BeginPaint (HWND,&AMP;PS); TextOut (paint_hdc,0,0,_t ("WM_PAINT message"), strlen ("WM_PAINT message")); EndPaint (HWND,&AMP;PS); Break;case wm_close:if (Idyes = MessageBox (hwnd,_t ("Is it really over? "), _t (" message "), Mb_yesno)) {DestroyWindow (HWND);} Break;case wm_destroy:postquitmessage (0); Break;default:return DefWindowProc (Hwnd,umsg,wparam,lparam);} RetuRN 0;} 


1. For the following statement:

Char szchar[20];
sprintf_s (Szchar, "You pressed%d", wParam);
MessageBox (Hwnd,szchar, "char", 0);

Compile error: Cannot convert parameter 2 from "Char [20]" to "LPCWSTR"

This is because the project uses the Unicode character set, which can be converted using the _t (""), but _t ("") does not work on the variable.

Changes the MessageBox to MessageBoxA, and the argument is a short character.


Run



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.