WinAPI uses GDI mapping

xiaoxiao2021-03-06  49

This is the most basic Windows program demonstrated the most basic Windows form and basic message loop. The following code has been detailed, and after copying directly, it can be directly copied under DEV-C and VC6.0 Compile operation.

// # Define Win32_Leannand_mean // # ffdef debug_mode // Printf ("Debug Mode Is Active!"); // # Endif

#include #include "resourse.h" HWND hWnd; LRESULT CALLBACK MsgHandler (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {return (DefWindowProc (hWnd, msg, wParam, lParam));} int WINAPI WinMain (hINSTANCE hInstance, hINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {// declare window class WNDCLASSEX wc; wc.cbSize = sizeof (WNDCLASSEX); wc.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = MsgHandler Wc.cbclsextra = 0; wc.cbWndextra = 0; wc.hinstance = Hinstance; wc.hicon = loading (null, "icon_fooly"); wc.hcursor = loadingcursor (null, idc_arrow); //Wc.hbrbackGround= ( HBRUSH) GetStockObject (BLACK_BRUSH); wc.hbrBackground = (HBRUSH) (BLACK_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = "Sample class"; wc.hIconSm = LoadIcon (NULL, IDI_WINLOGO); // register window class RegisterClassEx (& WC); // Create a window if (! (hWnd = CREATEWINDOWEX (Null, "Sample Class", "Sample Window", WS_POPUP | WS_Visible, 0, 0, 640, 480, NULL, NULL, HINSTANCE, NULL))) {RETURN (0) } Showwindow (hwnd, ncmdshow); UpdateWindow (hwnd); return (0);} // 1. Hinstance is a symbol variable of a handle type Hinstance is an integer for logo program instance. // WINDOW Set this parameter and passes it to your program code. //2. Hinstance HPREVINSTANCE has been abolished. Just serve the ancient WINDOW version service //3. LPSTR LPCMDLINE: is a pointer to string, which only works only in the DOS command window input / / or in the RUN dialog. // 4. Int ncmdshow: The state determines when the window is displayed in initialization. // Windows usually assigns a value to this parameter.

Usually a SW_ headed constant // example: sw_shownormal indicates the default status SW_MAXINIZE or SW_MINMIZE to indicate the maximum and minimum mode // ************************ ********************************************************** / / / / ××××××× Message ×××××××××××××××××××× //// ********************************** ********************************************** //// The programming under Windows has to consider other The program is running; then Windows is connected to the "Message" to connect to the operation "application" and specific operations: accurately said that our instructions // or the program itself issues a "such as mobile window .." application, Windows takes the actual situation based on the actual //, refuses or issues an instruction to make the program (computer) make the corresponding action // ***** ◎ WM_Activate: A new window is activated. // ◎ WM_CLOSE: A window is turned off. // ◎ WM_COMMAND: A menu function is selected. // ◎ WM_CREATE: A window is established. // ◎ WM_LBUTTONDBLCLK: The left button is double click. // ◎ WM_LBUTTONDOWN: The left mouse button is pressed. // ◎ WM_MOUSEMOVE: The mouse is moved. // ◎ WM_MOVE: A window is moved. // ◎ WM_PAINT: Part of the window needs to be heavy. // ◎ WM_RBUTTONDBLCLK: The right button of the mouse is double click. // ◎ WM_RBUTTONDOWN: The right button of the mouse is pressed. // ◎ WM_SIZE: The size of the window is changed. // ◎ WM_USER: Do you want to do it. // ******************************************************** ************************ / / In short, whenever Windows has to control all the messages, and Windows is constantly receiving various messages // ******************************************************** ********************************** / / ××××××××××××××××××××××××××× ×××××××××××× //// ********* Windows is achieved by a "callback function type" ************** **** //// ××××× You only declare a Callback type function to line ×××××××××××××××××× ××××××××××××××××××××××× //// ******* windows window ************ ********************************************************* / / / / ××××××××× ××××××××××××××××××××××××××××××××××××××× {// uint cbsize; / / Specifies the size of the structure in bytes. Implement // uint style; // window style via sizeof (WNDCLASSEX). Symbol constant: // cs_hredraw (the width change of the client area is re-drawing the window) | // CS_VREDRAW (the high change of the client area will re-draw the window) | // cs_owndc (the window in this class assigns unique device context) | / / Cs_dblclks (Double-click the Mouse to send a double-click message) // wndproc lpfnwndproc; // window process pointer. Generally pointing to the callback function (simple understanding of the address of the function is the name of the function) // int cbclsextra; // for the additional information preserved.

Generally set to 0; because most of the programs do not use it // int CbWndextra; // 同 同 // Handle Hinstance; // points to the handle of the window process instance. It is also one of the parameters of the winmain () function // hicon hicon; // points to the handle of the window icon. Usually use the LoadCon (NULL, IDI_WINLOGO) function to set the handle of the hCURSOR HCURSOR; // window cursor. Usually use loadcursor (null, idc_arrow) function to set // Hbrush HbRBackground; // "Handset" of the window. You can use the getStockObject (Black_brush) function to set //, gray_brush, whit_brush, gray_brush, etc. // lpctstr lpszMenuname; // The window with the drop-down menu (must be assigned to this parameter). You can be set to null // lpctstr lpszclassname; // a class of names. Simple a string can // hicon hiconsm; // Point to a small icon's handle in the title bar of the window. To use the loadICON () function //} WNDCLASSEX; // RegesterClassex (& WC); // Create Window // HWND CREATEWINDOWEX (// DWORD DWEXSTYLE, / / ​​Expansion window style. Take ws_ex_ header is usually set to NULL // LPCTSTSTR LPCLASSNAME, // Window class name. // LPCTSTR LPWINDOWNAME, // will appear in the short text of the title bar // DWORD DWSTYLE, // Window style. Use WS_ header // If you have only one The title bar, you can change the size of WS_OVERLAPPED // If you want to choose a maximize, minimize. If you change the size of the window, select WS_OVERLAPPEDWINDOW // If you just choose WS_POPUP; if you just want to display one Black generic box of ws_visible. // int X, // window of the coordinate // int y, // int nwidth, // window is "pixel" // int nHeiGHT, // hwnd HWndParent, / / ​​Point to the parent window. Null means that the Window desktop is the menu handle on the parent window // HMENU HMENU, // window. You can use loadMenu () to call your own menu resource // Hinstance Hinstance, // a handle Point to the unusual use of Windows to WinMain () // LPVOID LPPARAM // Game programming without it. Set to null //); // ***************** *********************************************************** ** /// Track your window and use the GDI // HDC device context is a structure that represents a set of graphics objects and properties, and some output devices set and properties. // HDC allows you to direct graphics directly, don't consider the low details // Windows GDI is a graphics translation system, one // GDI between applications and graphics hardware can output to any compatible device, but often The device used is a video monitors, a graphic hard copy device (printer or plotter), or the metallographic text in memory. // The GDI function can draw straight lines, curves, closed graphics and text. // All Windows functions that access the GDI need a device context handle as a parameter. // Get the context handle of a window.

Use the function // hdc getdc (// hwnd hwnd // handle to a windows (if you pass the HWnd is NULL getting the entire screen) //); // Returns NULL // if the call fails End the use of DCs must release DC: Use the function: // int releasec (// hwnd hwnd, // handle to window // HDC HDC // Handle to device context //); // If success, return value is (1), otherwise (0) // ************************************************* *************** / / ×××××××××××××××××××××× being moved -> Send WM_MOVE Message, the coordinates of the window new location There is a LPARAM (LPARAM's low field storage window client area's coordinate x, high-defined storage coordinate Y) // window of the window -> Send the WM_SIZE message, LPARAM low field storage The width of the client area, the high-grade storage height of LPARAM. // Different from WM_MOVE is that WPARAM parameters also control some important things. // size_maxhide: Other windows are maximized Size_maximized: This window is maximized / size_maxshow: Other windows are restored to pull // size_minimized: This window minimizes // size_restored: window has changed size, neither Minimize, not to maximize // If you write an instance of the window, save the current location and size of the window in several global variables // assume that these global variables are XPOS, YPOS; Xsize, Ysize // Most Key to control WM_MOVE and WM_SIZE these two messages: // if (msg == wm_size) // {// xsize = loword (lparam); // = HiWord (LPARAM); //} // if (msg = = WM_MOVE) // (// xpos = loword (lparam); // YPOS = HiWord (LPARAM); //) // A window is activated | Release activation -> Send WM_ACTIVATE message // By detecting the low field of WPARAM Know whether it is activated or released // wa_clickactive: window is activated // wa_active: window is activated by other things (keyboard, function call, etc.) // wa_insctive: window is deactivated. / / In order to handle this message, I preserved another global variable BFOCUS when it received a message from WM_ACTIVATE, it will do the following change // if (msg == wm_activate) // {// if (loword (WPARAM ) = Wa_inactive) // bfocus = false; // else // bfocus = true; // // Tell Windows We Handled it // Return (0); //} // has two associated messages WM_KILLFOCUS and WM_SETFOCUS When the window receives the input focus, Windows Message WM_SETFOCUS is sent to it, and the WM_KILLFOCUS message is sent when the focus is lost.

Applications can take these messages to know any change in the input focus. // ☆ WM_Paint Message Wn_Paint Message Notification Program, all or some of the customer window needs to re-draw. // When the user minimizes, overlaps, or adjusts the customer's window area, this message will be generated. //repaint. You need to do two things. First, a pair of functions you want to use to WM_Paint Messages / (the first is the beginpaint () function. The prototype is as follows: // hdc beginpaint (// hwnd hwnd, // Handle to window // lppAintstruct LPPAINT / / POINTER TO STRUCTURE for Paint Information //); // typef struct tagpaintstruct {// hdc hdc; // bool feed; / / Indicate whether the application should erase the background. If the False Description System has been removed // Rect rcpaint; // Rect tells you the rectangle // Bool FRESTORE; // preserved member // preserved member // Byte rgbreserved [32]; // The member //} PaintStruct; // beginpaint () function has made three things: // 1. Eliminate the window until the WM_PAINT message is emitted before // I. If we The background painting brush is defined in the window class (WINDOW CLASS), use this painting brush to redraw the invalid area // three. Returns the handle // typef struct_rect {// long left; // long TOP ; // long @ @ long bottom; //} Rect; // Note that Rect only contains only the upper left corner, does not include the lower right corner: //, for example: Rect myRect = {0, 0, 5, 5}; It only contains pixels (0, 0) and has not arrived (5, 5), so the lower right corner of the rectangle is actually (4, 4) // The second thing is to release the DC. Use the endpaint () function. The prototype of the function is as follows // bool endpaint (// bool hwnd; // Handle to window // const pagetstruct * LPPAINT / / POINTER TO STRUCTURE for Paint Data //); // You can also replace BeginPaint by calling the validateect () function () The function makes the window again. But you have to handle everything. Maybe we really want to use it. So give you its original shape: // Bool ValidateRect (// hwnd hwnd, // handle of window // const * lprectinates //); // Suppose we have defined a global variable HmainWindow as our window handle.

// if (msg == wm_paint) // {// PAINTSTRUCT PS; // Declare a paintstruct for use with this message // hdc hdc; // display device context for graphics calls // hdc = beginpaint (HmainWindow, & PS) ; // Validate the window your painshere! // endpaint (hmainwindow, & ps); // Release The DC Tell Windows We Took Care of It // Return (0); //} // ***** *********************************************************** ***************** / / / // ********************* Window Close Messaging ×××× ××××××××××××× //// WM_CLOSE message Processes a window or application should issue a WM_CLOSE message when it is closed, // When receiving the WM_Close message, if you prefer, ask the user whether it is Really quit. / / You know when you make users confirm or have errors or something that should be noticed, one message box // code is as follows: // if (msg == wm_close) // {// if (MessageBox (HmainWindow, "Are you sute to quit?", "notice", MB_YESNO | MB_ICONEXCLAMATION == iDNO // Return (0); OtherWise, Let the default handler take care of it //} // WM_DESTORY message WM_DESTROY The news is a bit different. It is issued when the window is being closed. // When you get a WM_DESTROY message, the window has been deleted from visual. // A main window is closed, does not mean that the application is over, which will continue to run without the window. // However, when a user turns off the main window, it means he wants to end the application, //, so if you want the application to end, // You must issue a WM_Quit message when you receive the WM_DESTROY message. You can use the postMessage () function, // prototype as follows: void PostquitMessage (int nexitcode); Axes Studio

Ling Xiaoyu

转载请注明原文地址:https://www.9cbs.com/read-115335.html

New Post(0)