Windows Programming dialog box loading displays BMP images and refreshes

Source: Internet
Author: User

Reference article: http://blog.csdn.net/wangjian8006/article/details/7464431

The loading and display of pictures is also part of the window drawing. So its code is written under the WM_PAINT message of the message function.

(This code picture path is written dead, the picture will not change, the following will say how to refresh the picture)

Handling dialog box messagesINT_PTR CALLBACK Dlgproc (HWND hdlg, UINT msg, WPARAM WPARAM, LPARAM LPARAM) {HDC hdc;      Paintstruct PS;      HDC Hdcmem;   Hbitmap hbmp; A handle to a bitmapBITMAP bmp; WCHAR txt[32]; Switch(msg) {case wm_initdialog:break ; case  wm_paint:hdc = BeginPaint (hdlg,&  PS); hdcmem =  Create COMPATIBLEDC (HDC); hbmp = (hbitmap) loadimage (NULL, _t ("E:\\bmp\\bmp\\2.bmp"), Image_bitmap, 0, 0, lr_loadfromfile|  Lr_createdibsection); GetObject (hbmp, sizeof (BITMAP), &bmp); Get a Bitmap object  SelectObject (Hdcmem, hbmp); BITBLT (hdc, 0, 0, bmp.bmwidth, bmp.bmheight, Hdcmem, 0, 0, srccopy); Display bitmap  DeleteDC (HDCMEM); DeleteObject (hbmp); EndPaint (Hdlg,&  PS); break , Case  wm_command: {switch  (LOWORD (WParam)) {case  idc_ Button1:newinitinstance (Hinst,g_hwnd, 5 ); break ; case  Idc_button2:messagebox (hdlg, L "Hey Hey, hey, hi! Tip ", MB_OK |  mb_iconinformation); Break , default : Break ;}} return 0 ;} return  (INT_PTR) FALSE;}     

HDC (device context handle), paintstruct (the struct contains the information that an application needs to draw the client area of the window it owns) These are prerequisites for the Win32 drawing and are not explained much.


How do I change pictures dynamically? One of the parameters of the LoadImage function is the picture path, which is set to the global variable WCHAR txt[32]; If you change the path value theoretically, the image will change.

Add:
It's not as I thought it would be. WM_PAINT is always called automatically and requires a trigger condition.
So when we're going to change the picture, we need to refresh the window to trigger the WM_PAINT, as well as reset the path value as above.
The UpdateWindow function is required at this point.
However, there is a precondition for UpdateWindow refresh, which is to set an invalid area before UpdateWindow will refresh that area.
Writing:
                Rect        rect;                   SetRect (&rect, 0, 0, +);                 InvalidateRect (Hdlg,&rect,true);                UpdateWindow (HDLG); 

The above code is to refresh a square with a length width of 100. The size is self-adjusting. Match the picture size.

Need to change the picture when 1 to reset the global variable path, 2 is to use the above code to refresh the window.

To modify the code:

Handling dialog box messagesINT_PTR CALLBACK Dlgproc (HWND hdlg, UINT msg, WPARAM WPARAM, LPARAM LPARAM) {HDC hdc;      Paintstruct PS;      HDC Hdcmem;   Hbitmap hbmp; A handle to a bitmapBITMAP bmp; WCHAR txt[32]; The picture path to replace WCHAR *tmp_path = L "E:\\bmp\\bmp\\3.bmp"; Switch(msg) {caseWm_initdialog:break; CaseWM_PAINT:HDC = BeginPaint (hdlg,&PS); Hdcmem = CreateCompatibleDC (hdc); hbmp = (HBITMAP) loadimage (NULL, Pic_path, Image_bitmap, 0, 0, lr_loadfromfile|  Lr_createdibsection); GetObject (hbmp, sizeof (BITMAP), &bmp); Get a Bitmap object  SelectObject (Hdcmem, hbmp); BITBLT (hdc, 0, 0, bmp.bmwidth, bmp.bmheight, Hdcmem, 0, 0, srccopy); Display bitmap  DeleteDC (HDCMEM); DeleteObject (hbmp); EndPaint (Hdlg,&  PS); break , Case  wm_command: {switch  (LOWORD (WParam)) {case  idc_ BUTTON1://New window, demo text box Newinitinstance (Hinst,g_hwnd, 5 ); break ; case  Idc_button2://Change Picture (path) memcpy ( Pic_path, Tmp_path, + ); Local (picture), refresh redraw  rect rect; SetRect (&rect, 0, 0, $ ); InvalidateRect (Hdlg,&rect,true ); UpdateWindow (HDLG); Break , default : Break ;}} return 0 ;} return  (INT_PTR) FALSE;}     

WCHAR pic_path[32]; is a global variable that is used to store the path. Click the button to change the path and refresh the window, so the new picture is displayed.


Operating effect:


The image is a bitmap of the finger vein.

Code uploaded Github:https://github.com/sonnadolf/sonne_desktop_graphical_development/tree/master/win32_demo/sonnedemo

Windows Programming dialog box loading displays BMP images and refreshes

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.