1. Related functions
1. hbitmap loadbitmap (
Hinstance, // handle to application instance
Lpctstr lpbitmapname // name of Bitmap Resource
);
If you want to load a system bitmap, set the first parameter to null. If it is not a system bitmap,
The first parameter is the handle of the instance. The second parameter can be used to convert the bitmap ID using the makeintresource macro. Otherwise, the bitmap path will be directly added.
2, handle LoadImage (
Hinstance hinst, // handle to instance
Lpctstr lpszname, // image to load
Uint utype, // image type
Int cxdesired, // Desired width
Int cydesired, // Desired height
Uint fuload // load options
);
The first parameter is the instance handle. If an OEM image is loaded, it can be 0;
Parameter 2: If an external bitmap is loaded, it is directly a bitmap path. If an OEM image is loaded, you can use the makeintresource macro,
Third parameter: bitmap type
The fourth and fifth parameters are the width and height of the loaded image, in pixels. If this parameter is zero and the fuload value is lr_defaultsize,
The function uses the sm_cxicon or sm_cxcursor System Metric value to set the width. If this parameter is zero and the value lr_defaultsize is not used,
The function uses the current resource width.
Fifth parameter: You can use "|" for combination.
3. Int GetObject (
Hgdiobj, // handle to graphics object
Int cbbuffer, // size of buffer for Object Information
Lpvoid lpvobject // buffer for Object Information
);
Use this function to obtain bitmap information, such as bitmap size,
The first parameter is the image handle, which can be a logical bitmap, a brush, a font, a palette, a pen, or a device-independent bitmap handle created by calling the createdibsection function.
The second parameter is the buffer size,
Third parameter:
:
4. Display bitmap
Bool bitblt
(
HDC hdcdest, // directory device Environment
Int nxdest, int nydest, // The base point of the bitmap displayed by the object device.
Int nwidth, int nheight, // the height and width of the display area
HDC hdcsrc, // Source Device Environment
Int nxsrc, int nysrc, // coordinates of the upper left corner of the bitmap in The Source Device
DWORD dwdrop // display method of Bitmap
)
Last parameter Display Mode
For the above content, refer to msdn. The image is from
II. Implementation
Add a bitmap on "resource", manually draw and add menus, and add "Open resource bitmap" and "open external bitmap" to the menu"
My implementation code ....
// Development tool: vs2008 <br/> // use the Unicode Character Set </P> <p> # include <windows. h> <br/> # include <tchar. h> // _ T or _ text header file <br/> # include "resource. H "</P> <p> // design a window class. <br/> // register the window class. <br/> // create a window; <br/> // display and update window. <Br/> // message loop <br/> // Window Function </P> <p> // global variable <br/> tchar szwindowclass [] = _ T (" demo program "); <br/> tchar szwindowtitle [] = _ T ("Main Window title"); <br/> hinstance hbmpinstance; <br/> hbitmap hbmp; // bitmap handle <br/> bitmap BMP; // bitmap is a struct <br/> HDC, hdcmem; // hdcmen compatible device <br/> pastruintct pS; <br/> rect winrect; <br/> tchar szpath [] = _ T (".. \ showbmp \ show.bmp "); // external bitmap path </P> <p> // defined window procedure function, which is a callback function, this function is not called directly in the Program <br/> // but is sent in a specific event or condition. The event or condition response is called by the Windows system. <br/> lresult callback mywindowproc (<br/> hwnd, // window handle <br/> uint umsg, // message identifier <br/> wparam, // first message parameter <br/> lparam // second message parameter <br/> ); <br/> // program entry function <br/> int winapi winmain (hinstance, // record the handle of the Instance currently running by the Program <br/> hinstance hprevinstance, // It has lost its meaning. It is always null <br/> lpstr lpcmdline, // command line parameter, record the path of the current running program <br/> int nshowcmd // specify How should the program window be displayed? Normally, the value of this parameter is not checked <br/>) <br/>{</P> <p> // obtain the instance <br/> hbmpinstance = hinstance; <br/> // design window class, this window class is not a class in C ++, but a struct representing window features </P> <p> wndclass mywndclass; // wndclass is a struct, the member in the struct is the data of the specified window feature </P> <p> // these two variables allow the user to request extra space within Windows for extra data and window <br/> // instance contact, usually no space is allocated <br/> mywndclass. cbclsextra = NULL; <br/> mywndclass. cbwndextra = NULL; </P> <p> // mywndclass. hbrbackground = (hbrush) getstockobject (white_brush); <br/> MYW Ndclass. hbrbackground = (hbrush) (color_window + 1); <br/> mywndclass. hicon = loadicon (null, idi_winlogo); </P> <p> // The cursor. The use of loadcursor () is the same as that of loadicon (). <br/> mywndclass. hcursor = loadcursor (null, idc_appstarting); </P> <p> mywndclass. hinstance = hinstance; // handle of the current instance <br/> mywndclass. lpfnwndproc = mywindowproc; // window function (message processing function), lpfnwndproc is a function pointer <br/> mywndclass. lpszclassname = szwindowclass; // window class name <br/> // mywnd Class. lpszmenuname = NULL; // menu, which specifies the name of the menu resource. null indicates no menu. <br/> mywndclass. lpszmenuname = makeintresource (idr_menu1); // One, makeintresource () Load menu <br/> mywndclass. style = cs_hredraw | cs_vredraw; // use | connect multiple window styles <br/> // window styles </P> <p> // register the window class, tell windows that the window class has been designed <br/> If (! Registerclass (& mywndclass) <br/>{< br/> return 0; // exit the program upon registration failure <br/>}</P> <p> // creation window, return window handle <br/> hwnd = createwindow (<br/> szwindowclass, // window class name <br/> szwindowtitle, // window title <br/> ws_overlappedwindow, // window style, multiple styles <br/> cw_usedefault, // X coordinate, default <br/> // cw_usedefault is only applicable to ws_overlapped style Windows <br/> cw_usedefault, // y coordinate, default <br/> 400, // width <br/> 300, // height <br/> null, // parent window <br/> null, // Add a menu <br/> hinstance, // window Instance flag <br/> null // Data Pointer passed in when the window is created. <br/> // The clientcreatestruct must be pointed to when multiple documents are created. <br/> ); </P> <p> // if the window creation fails <br/> If (! Hwnd) <br/> return 0; </P> <p> // display and update the window <br/> showwindow (hwnd, sw_show ); <br/> updatewindow (hwnd); </P> <p> // message loop, wm_qiut stops the loop, or the program has been released <br/> MSG; <br/> while (getmessage (& MSG, null, 0, 0) <br/>{< br/> translatemessage (& MSG ); // enable Windows to convert keyboard-related messages <br/> dispatchmessage (& MSG ); // process the message in the process function of dispatching the message to the window <br/>}< br/> return 1; <br/>}</P> <p> // Implementation of window procedure functions (message processing functions) <br/> lresult callback mywindowproc (<br/> hwnd, // window Port handle <br/> uint umsg, // message identifier <br/> wparam, // first message parameter <br/> lparam // second message parameter <br/>) <br/>{</P> <p> switch (umsg) <br/>{< br/> case wm_paint: <br/>{< br/> tchar STR [] = _ T ("this is a Windows SDK program! "); <Br/> HDC = beginpaint (hwnd, & PS); // load device content <br/> hdcmem = createcompatibledc (HDC ); // create compatible devices <br/> settextcolor (HDC, RGB (200,); // set the text color <br/> textout (HDC, STR, 18); // output text <br/>}; <br/> break; <br/> case wm_close: // send this message when you click "X, stop a message cyclically <br/> If (idyes = MessageBox (hwnd, _ T ("do you want to exit? "), _ T (" prompt "), mb_yesno) <br/>{< br/> destroywindow (hwnd); // destroy the window and send the wm_destroy message, note that the program process has not exited <br/>}; <br/> break; <br/> case wm_destroy: <br/> endpaint (hwnd, & PS ); <br/> deletedc (hdcmem); <br/> deleteobject (hbmp); // clear the bitmap <br/> postquitmessage (null); // process ends, completely exit the Program <br/> break; <br/> case wm_command: <br/>{< br/> switch (loword (wparam )) <br/>{< br/> case id_40001: // load the resource view, </P> <p> // two bitmap loading methods <br/> </pbmp = loadbitmap (hbmpinstance, makeintresource (idb_bitmap1); <br/> hbmp = (hbitmap) LoadImage (hbmpinstance, makeintresource (idb_bitmap1), image_bitmap, 300,200, lr_createdibsection ); </P> <p> GetObject (hbmp, sizeof (Bitmap), & BMP); // obtain the bitmap size. <br/> SelectObject (hdcmem, hbmp ); // Add bitmap to the device <br/> bitblt (getdc (hwnd), BMP. bmwidth, BMP. bmheight, hdcmem, srccopy); // display bitmap <br/> textout (HDC, _ T ("resource bitmap"), 4 ); <br/> break; <br/> case id_40002: // external bitmap. On the menu, "enable external bitmap" Message Processing </P> <p> hbmp = (hbitmap) loadImage (hbmpinstance, szpath, image_bitmap, 200,120, lr_loadfromfile | lr_createdibsection); <br/> GetObject (hbmp, sizeof (Bitmap), & BMP); <br/> SelectObject (hdcmem, hbmp); <br/> bitblt (getdc (hwnd), 0,100, BMP. bmwidth, BMP. bmheight, hdcmem, 230,150, srccopy); <br/> textout (HDC, _ T ("external bitmap"), 4); <br/> break; <br/>}< br/>}; <br/> break; <br/> default: <br/> return defwindowproc (hwnd, umsg, wparam, lparam ); // process unprocessed messages <br/> break; <br/>}</P> <p> return 0; </P> <p >}</P> <p> % lt; /textarea> <br/> <p> <br/> </P> <br/> <p> <font size = "3" color = "# ff0000 "> execution result ....... </font> </P> <br/> <p> </P> <br/> </tchar. h> </windows. h>