Win32 programming Basics

Source: Internet
Author: User
Tags drawtext

/*************************************** * ******************************* // Win32 programming Basics *//************************************* ***********************************//*** **************************************** * *************************** // * Comment on the Win32 program, although it is very simple, it is the beginning of Win32 program design, so it is more detailed.
*//************************************* ***********************************//*** **************************************** * *************************** // * Import the file containing windows. h. This file contains other Windows header files * // * windef. H basic type definition * // * winnt. h supports Unicode type definition * // * WINBASE. h kernel functions
* // * Winuser. h user interface function * // * wingdi. h graphical device interface function *//******************************** **************************************** /# include /************************************ * *********************************** // object process processing function * // * lresult: the simple definition is long (long) * // * callback :__ stdcall, which refers to the function call between windows and your applications.
* // * Special Call sequence. * // * Hwnd: Window handle, a 32-bit number. This parameter is the handle of the window for receiving messages. * // * return value of the createwindow function. * // * Uint: Unsigned int 32-bit unsigned integer. * // * this parameter indicates the number of the Message * // * wparam: uint, 32-bit Message Parameter * // * lparam: Long, 32-bit message parameters *//********************************* ***************************************/ lresult
Callback wndproc (hwnd, uint, wparam, lparam ); /*************************************** * ******************************* // main function, there are four parameters, the name is fixed as winmain, and the returned value is int * // * winapi in windef. define in H, # define winapi _ stdcall * // * hinstance: instance handle, number * // * in a 32-bit system, the second parameter of winmain is always null (defined as 0)
* // * The third parameter is used to run the command line of the program, * // * Some Windows applications use this parameter to load files to the memory when the program starts * // * lpstr/pstr: pointer to a string, it is equivalent to Char ** // *. The fourth parameter indicates the initial display mode of the program, which is normal, maximized, and minimized. *//************************************* * *********************************/INT winapi winmain (hinstance, hinstance,
Pstr lpszcmdline, int icmdshow) {/************************************** * ********************************** define the window class * // * tchar: char * // * _ T and text macro, which have the same functions and are usually useless. In Unicode systems, * // * automatically convert the following string to a wide string *//************************* **************************************** *******/
Tchar tcclassname [] = text ("My window "); // window class name string /********************************* ***************************************/ /* wndclass: the window class structure defines the general features of the window. You can create different windows * // * typedef struct * // * {* // * uint style; * // * wndproc lpfnwndproc; * // * int cbclsextra
; * // * Int cbwndextra; * // * hinstance; * // * hicon; * // * hcursor; * // * hbrush hbrbackground; * // * lpctstr lpszmenuname; * // * lpctstr lpszclassname; * // *} * // * wndclass, * pwndclass; *//************************************* ***********************************/
Wndclass WC; // window class attribute description structure WC. lpszclassname = tcclassname; // window class name WC. lpszmenuname = NULL; // name of the window menu resource WC. lpfnwndproc = wndproc; // process processing function of the window object, pointing to the function pointer WC. hinstance = hinstance; // handle of the current process object, which is received by the winmain parameter WC. hbrbackground = (hbrush) getstockobject (black_brush); // specifies a window background brush object.
WC. hicon = loadicon (null, idi_application); // The icon object WC. hcursor = loadcursor (null, idc_arrow); // cursor object WC. cbclsextra = 0; // size of the public data zone of the same type of window object WC. cbwndextra = 0; // size of the private data zone of the current window object WC. style = cs_hredraw | cs_vredraw; // window style. Refresh if (! Registerclass (& WC) // registers the window class. The parameter is a pointer to the wndclass structure.
{/************************************** * *********************************** Window class registration unsuccessful error message * // * MessageBox message box * // parameter 1: window handle. If not, it is null * // parameter 2: The string displayed by the message body * // * parameter 3: parameter 4 of the character string * // * on the title bar of the message box: winuser. A constant combination starting with MB _ defined in H. The message frame style is: button. If the icon * // * is 0, only the OK button * // * returns the value: returns idok (1 ).
* // * You can also return idyes, IDNO, idcancel, idabort, idretry, idignore, etc *//****************** **************************************** * ************/MessageBox (null, text ("registerclasserror! "), Text (" error "), mb_iconerror); Return 0; // if the registration fails, return and terminate the program }/********************************** **************************************/
/* Define window object attributes, specify more information about the window *//****************************** **************************************** **/tchar tcwindowcaptionname [] = text ("Win32 API "); // window object title name createstruct Cs; // window object attribute description structure, defined in winuser. h CS. lpszclass = tcclassname; // window class name CS. lpszname = tcwindowcaptionname;
// The title of the window object, which is displayed in the title bar CS. style = ws_overlappedwindow; // CS. X = 100; // The X coordinate Cs of the window object on the screen. y = 100; // The Y coordinate Cs of the window object on the screen. cx = 400; // The width of the window object CS. cy = 300; // The height of the window object CS. hwndparent = NULL; // The parent window handle Cs of the window object. hmenu = NULL; // The menu handle or subwindow Number of the window object CS. hinstance = hinstance; // instance handle of the current process, winmain Parameter
CS. lpcreateparams = NULL; // create a parameter pointer, you can access the data in the program to be referenced in the future /****************************** **************************************** ** // * create a window object * // * define the window handle hwnd, the value is the return value of the createwindows function. * // * Indicates the handle of the window that is returned after the creation is successful, otherwise, null * // ********************************** is returned *//********************************** **************************************/
Hwnd = createwindow (CS. lpszclass, CS. lpszname, CS. style, CS. x, CS. y, CS. CX, CS. cy, CS. hwndparent, CS. hmenu, CS. hinstance, CS. lpcreateparams); If (hwnd = NULL) // determine whether the creation is successful {/******************************** **************************************** //*
Window object creation failure error message *//***************************** **************************************** * **/MessageBox (null, text ("createwindowerror! "), Text (" error! "), Mb_iconerror); Return 0 ;} /*************************************** *********************************//*
Display window object * // * This window has been created in windows. Memory allocated. * // * Two functions need to be called to display on the monitor. * // * Showwindows (hwnd, icmdshow) * // * The first parameter is the handle of the window just created using createwindow. * // * The second parameter is the icmdshow parameter passed to winmain. Used to determine how a window is initially displayed on the screen. * // * You can also customize the options as follows: * // * sw_shownormal // General * // * sw_showmaximized // maximize
* // * Sw_showminnoactive // display only in the taskbar * // * updatewindow (hwnd) * // *, leading to the drawing of the customer region. Pass the Window Process (wndproc) send a wm_paint message *//******************************** **************************************** /showwindow (hwnd, icmdshow); updatewindow (hwnd ); // refresh the window object immediately /********************************* ***************************************/
/* Message retrieval, message loop * // * MSG: Message structure, which is defined in winuser. H * // * The message loop starts with the getmessage call. It extracts a message from the Message Queue * // * and passes the call to Windows a MSG structure pointer pointing to MSG. * // * If the parameters 2, 3, and 4 are null or 0, the program receives all messages in all the windows it creates. * // * Windows fills in the next message retrieved from the message queue. fields of the MSG structure * // * MSG: message structure: * // * typedef struct tagmsg *//*{*/
/* Hwnd; // handle of the message sending window. * // * Uint message; // message identifier. A value is defined in the window header file * // * wparam; // a 32-bit message parameter, meaning different messages * // * lparam; // same as above * // * DWORD time; // the time when the message is placed in the message queue * // * point pt; // The mouse coordinate when a message is placed in the message queue * // *} * // * MSG, * PMSG; // structure name */
/*************************************** * ********************************/MSG; while (getmessage (& MSG, null, 0, 0 )) {/**************************************; when wm_quit (with a value of 0x0012) is retrieved, exit from the message loop *//******************************** ****************************************/
Translatemessage (& MSG); // transmits the MSG structure to Windows to convert the message on the virtual keyboard into dispatchmessage (& MSG); // send the message, the operating system calls the corresponding Window Process to process the message }/**************************** **************************************** * *** // returns *//**************************** **************************************** ****/
Return MSG. wparam ;} /*************************************** * ******************************** // process of window objects the four parameters of the processing function * // * are the same as those of the first four parameters in the MSG structure. * // * The program does not call the window process directly, but is called by Windows itself. * // * The program can call its own window process through the sendmessage function *//*********************** **************************************** * ********/lresult
Callback wndproc (hwnd, uint imsg, wparam, lparam) {/************************************** * ********************************** // classification Processing * // * winuser. h defines the identifier prefixed with WM for each message * // * generally, Windows program elements are processed using the switch and case structures. At this time, 0 must be returned. * // * Other messages not processed during the window must be passed to the defwindowproc function,
* // * Return the function return value in the window *//*************************** **************************************** * ***/switch (imsg) {/************************************** * ********************************* // draw messages, refresh the window when the customer region is invalid *//******************************* **************************************** */
Case wm_paint: {/************************************** * ******************************** // * paintstruct: drawing Structure, defined in winuser. * // * rect: rectangular structure * // * HDC: handle to the device description table *//********************************* ***************************************/ paintstruct pS; HDC
HDC; rect; /*************************************** * wm_paint Processing almost always starts with a beginpaint function: * // * HDC = beginpaint (hwnd, & PS) * // * the end of an endpaint function * // * endpaint (hwnd, & PS) * // * in the two calls, the first parameter is the window handle of the program. * // * The second parameter is the structure pointer pointing to the paintstruct type.
*//************************************* * *********************************/HDC = beginpaint (hwnd, & PS); // getclientrect (hwnd, & rect); // obtains the setbkmode (HDC, transparent) rectangle of the current window object ); // settextcolor (HDC, RGB (255, 0, 0 )); // set the text color /********************************** **************************************/
/* Draw text * // * drawtext function, the first parameter is the device description table handle returned from beginpaint * // * The second parameter is the text to be output * // * The third parameter is-1, indicates that the text string ends with byte 0. * // * The fourth parameter is the area of the rectangle to be drawn * // * The last parameter is a Series Flag, which is defined in winduser. h, horizontal, vertical center, single Row *//************************************ * **********************************/drawtext (HDC,
Text ("Hello, Win32! "),-1, & rect, dt_center | dt_vcenter | dt_singleline); endpaint (hwnd, & PS); // when the customer area is drawn, return the display device object return 0 ;} case wm_destroy: // receive the wm_deftroy message {/******************************** **************************************** // * Send the wm_quit message, notification thread message retrieval loop, clearing window main program can exit
* // * Postquitmessage (0) the function inserts a wm_quit message in the message queue *//**************************** **************************************** * ***/postquitmessage (0 ); return 0 ;}} /*************************************** * ********************************* // process function processing for default window objects sent by messages to the Window System
*//************************************* * *********************************/Return defwindowproc (hwnd, imsg, wparam, lparam );}

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.