Windows Program Design dialog box

Source: Internet
Author: User
//////////////////////////////////////// /// // Windows Programming chapter 4 dialog box code about2 /////////////////////////////////// /// // # include <windows. h> # include "resource. H "lresult callback wndproc (hwnd, uint, wparam, lparam); bool callback (hwnd, uint, wparam, lparam); int icurrentcolor = idc_black, icurrentfigure = idc_rect; int winapi winmain (hinstance H Instance, hinstance hprevinstance, pstr szcmdline, int icmdshow) {static tchar szappname [] = text ("about2"); MSG; hwnd; wndclass. style = cs_vredraw | cs_hredraw; wndclass. lpfnwndproc = wndproc; wndclass. cbclsextra = 0; wndclass. cbwndextra = 0; wndclass. hinstance = hinstance; wndclass. hicon = loadicon (hinstance, szappname); wndclass. hcursor = loadcursor (null, idc_arrow); wndclass. hbrbackground = (hbrush) Get Stockobject (white_brush); wndclass. lpszclassname = szappname; wndclass. lpszmenuname = szappname; If (! Registerclass (& wndclass) {MessageBox (null, text ("this program requires Windows NT! "), Szappname, mb_iconerror); Return 0;} hwnd = createwindow (szappname, text (" about box demo program "), ws_overlappedwindow, cw_usedefault, null, null, hinstance, null); showwindow (hwnd, icmdshow); updatewindow (hwnd); While (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG ); dispatchmessage (& MSG);} return MSG. wparam;} void paintwindow (hwnd, int icolor, int ifigure) {static Co Lorref crcolor [8] = {RGB (255, 0), RGB (0,255, 0,255,255), RGB (, 0), RGB (), RGB (, 0 ), RGB (255, 255,255), RGB (255,255,255, 0), RGB ()}; hbrush; HDC; rect; HDC = getdc (hwnd); getclientrect (hwnd, & rect); hbrush = createsolidbrush (crcolor [iColor-IDC_BLACK]); hbrush = (hbrush) SelectObject (HDC, hbrush); If (ifigure = idc_rect) rectangle (HDC, rect. left, rect. top, rect. right, rect. bottom); elseellipse (HDC, rect. left, Rect. top, rect. right, rect. bottom); deleteobject (SelectObject (HDC, hbrush); releasedc (hwnd, HDC);} // I don't understand this function. The general idea is to use paintwindow before updatewindow, however, this is not the case. The system will get stuck with void painttheblock (hwnd hctrl, int icolor, int ifigure) {// invalidate the subwindow control invalidaterect (hctrl, null, true ); // if the window update area is not empty, the updatewindow function sends a wm_paint message to update the customer area of the specified window. // The function bypasses the message queue of the application and directly sends the wm_paint message to the window of the specified window. If the update area is empty, no message is sent. // This updatewindow does not understand updatewindow (hctrl); paintwindow (hctrl, icolor, ifigure);} lresult callback wndproc (hwnd, uint message, wparam, lparam) {static hinstance; paintstruct pS; Switch (Message) {Case wm_create: // get the handle. Use hinstance = (lpcreatestruct) lparam)-> hinstance; return 0; case wm_command: Switch (loword (wparam) {Case idm_app_about: // create a Modal Dialog Box Based on the template resource in the dialog box // triggered only after the callback is complete. // It is triggered until the specified callback function passes the call You can use the enddialog function to abort a modal dialog box before returning to control. // Call failed, return-1 // call successful, return enddialog parameter if (dialogbox (hinstance, text ("aboutbox"), hwnd, aboutdlgproc )) // refresh the window invalidaterect (hwnd, null, true); Return 0;} break; Case wm_paint: // The two lines of code do not know what is going on, but after removing them, the program cannot execute // make the previous invalid region valid beginpaint (hwnd, & PS); endpaint (hwnd, & PS); // draw a rectangle or circle paintwindow (hwnd, icurrentcolor, icurrentfigure); Return 0; Case wm_destroy: postquitmessage (0); Return 0;} return defwindowproc (hwnd, message, wparam, lparam );} Bool callback aboutdlgproc (hwnd hdlg, uint message, wparam, lparam) {static hwnd hctrlblock; static int icolor, ifigure; Switch (Message) {// initialize case wm_initdialog: icolor = icurrentcolor; ifigure = icurrentfigure; // specify one in each group, handle, start, end, And checkradiobutton (hdlg, idc_black, idc_white, icolor); checkradiobutton (hdlg, idc_rect, idc_ellipse, ifigure); // obtain the control handle hctrlblock = getdlgitem (hdlg, idc_paint) in the display area of the settings Panel; // set the focus s Etfocus (getdlgitem (hdlg, icolor); // This returns true and false, as if it has no effect on the result. For some reason, return false; Case wm_command: // loword (wparam) specifies the control switch (loword (wparam) {Case idok: icurrentcolor = icolor; icurrentfigure = ifigure; // The returned enddialog (hdlg, true) related to dialogbox ); return true; Case idcancel: enddialog (hdlg, false); Return true; Case idc_black: Case idc_red: Case idc_green: Case idc_yellow: Case idc_blue: Case idc_magenta: Case idc_cyan: Case idc_white: // set the color for drawing icolor = loword (wparam); // set the button for displaying checkradiobutton (hdlg, idc_black, idc_white, loword (wparam )); // update painttheblock (hctrlblock, icolor, ifigure); Return true; Case idc_rect: Case idc_ellipse: ifigure = loword (wparam); Digest (hdlg, idc_rect, idc_ellipse, loword (wparam); painttheblock (hctrlblock, icolor, ifigure); Return true;} break; Case wm_paint: painttheblock (hctrlblock, icolor, Ifigure); break;} return false ;} //////////////////////////////////////// /// // window, the calling process is as follows: // painttheblock -----------> updatewindow --------> paintwindow // when switching colors and shapes, the calling process is as follows: // updatewindow ---------> paintwindow // What does updatewindow do? Does it mean that wm_paint will be called, however, when I set a breakpoint, I do not have ////////////////////////////////// ///////////////////////////////////////

After reading several posts, I seem to have understood this question in general.

1. The first parameter passed in painttheblock is not a subwindow, but a text control in the subwindow (for drawing ).

2. The invalidaterect is called to set the region of this subwindow control to be invalid.

3. Calling updatewindow is used to trigger wm_paint. This wm_paint is the wm_paint of the child window control, rather than the Child Window.

4. The above two codes mean to clear the features of the text control, and then draw a picture directly.

I don't know if this is correct. I hope you can give me more comments.

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.