Sun Xin VC video Study Notes: how Windows program runs internally

Source: Internet
Author: User
Tags textout
These two days I saw Sun Xin's first tutorial video: How the windows program runs internally. Sort the content into notes for future use. The text is as follows:

1. About Computer Systems ,:

Computer systems are roughly divided into three layers: applications, operating systems, and hardware devices. Applications call the APIS provided by the operating system to notify the operating system to complete operations on the underlying hardware device. On the contrary, the operating system encapsulates the perception of the hardware device and sends messages to the message queue, the application then removes the message from the message queue and makes a specific response to the message.

2. New terms.

Handle: the identifier of a resource. The operating system uses the handle to find the corresponding resource. Hicon, hcursor, hwnd, and hinstance)

Some variables in the window class: each position of the variable 1 indicates that it has the corresponding characteristics, and vice versa. The meanings of specific bits in variables are hard to remember, so they are replaced by macros, such as cs_vredraw = 0x0001 and cs_hredraw = 0x0002. If you want to make this variable have several features at the same time, you can perform or (|) operations on the macros corresponding to these features. To remove each feature from this variable, then, the corresponding macro is reversed and then the variable & operation is performed. For example, style = cs_vredraw | cs_hredraw | cs_noclose indicates that the variables set the above three corresponding features at the same time.

Callback Function: When an application receives a message, it needs to call a function to process the message. The function call process is only completed by the operating system, the code of the callback function processing method must be completed by the application. Each type of window has its own dedicated callback function, which is specified by the lpfwnsproc member.

3. The first Windows program.

Four steps to create a complete window program:

  • Design a window class: Fill wndclass class
  • Registration window class: registerclass Function

    ATOM RegisterClass(  CONST WNDCLASS *lpWndClass  // class data);
  • Create window: createwindow Function

    HWND CreateWindow(  LPCTSTR lpClassName,  // registered class name  LPCTSTR lpWindowName, // window name  DWORD dwStyle,        // window style  int x,                // horizontal position of window  int y,                // vertical position of window  int nWidth,           // window width  int nHeight,          // window height  HWND hWndParent,      // handle to parent or owner window  HMENU hMenu,          // menu handle or child identifier  HINSTANCE hInstance,  // handle to application instance  LPVOID lpParam        // window-creation data);
  • Display and update the window: showwindow and upwindow functions. For details, query the msdn

In addition, there are two important processes: Obtaining various messages through the while loop and writing the callback function to process different messages. The Code is as follows:

# Include <windows. h> # include <stdio. h> lresult callback winwanproc (hwnd, // handle to window uint umsg, // message identifier wparam, // first message parameter lparam // second message parameter ); int winapi winmain (hinstance, // handle to current instance hinstance hprevinstance, // handle to previous instance lpstr lpcmdline, // command line int ncmdshow // show stat E) {// 1. 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 = winwanproc; wndcls. lpszclassname = "myfirst"; wndcls. lpszmenuname = NULL; wndcls. style = cs_hredraw | cs_vredraw; // 2. registration window class registerclas S (& wndcls); // 3. create a window class hwnd; hwnd = createwindow ("myfirst", "Windows", ws_overlappedwindow, 600,400, null, null, hinstance, null); // 4. display and update window showwindow (hwnd, sw_shownormal); updatewindow (hwnd); // get message MSG; while (getmessage (& MSG, null )) // obtain the message from the message queue. If a message exists, true {translatemessage (& MSG) is always returned. // convert the message to wm_keyup and wm_keydown to wm_char message dispatchmessage (& MSG ); // route the message to the operating system. The system calls the window callback function} return 0;} // 5. the callback function does not complete different messages. Same response lresult callback winwanproc (hwnd, // handle to windowuint umsg, // message identifierwparam wparam, // first message parameterlparam lparam // second message parameter) {Switch (umsg) {Case wm_char: Char STR [20]; sprintf (STR, "The key_char is % d", wparam); MessageBox (hwnd, STR, "Wang", mb_ OK); break; case wm_lbuttondown: MessageBox (hwnd, "Mouse clicked! "," Wang ", mb_yesno); HDC; // device context, which can block device drivers and facilitate application calls. HDC = getdc (hwnd); textout (HDC, 0, 50, "Yes I do! ", Strlen (" Yes I do! "); Releasedc (hwnd, HDC); break; Case wm_paint: HDC; paintstruct pS; HDC = beginpaint (hwnd, & PS); textout (HDC, 0, 0, "paint message! ", Strlen (" paint message! "); Endpaint (hwnd, & PS); break; Case wm_close: If (idyes = MessageBox (hwnd," Do you want to launch a program? "," Wang ", mb_yesno) {destroywindow (hwnd);} break; Case wm_destroy: postquitmessage (0); break; default: Return defwindowproc (hwnd, umsg, wparam, lparam);} return 0;} instructor Sun Xin's video is a simple introduction. I decided to study VC Programming and thank the teacher for providing such good resources and improving the performance of the strong!
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.