Windows interface Programming (1) bitmap background and bit drawing brushes

Source: Internet
Author: User
Tags bool function prototype

You can set the background of the dialog box by WM_CTLCOLORDLG messages, as described in MSDN on this message:

The Wm_ctlcolordlg sent to a dialog box before the System draws the dialog box. The dialog box can set its text and background colors using the specified display device co with responding to this message ntext handle.

When the window message response function receives the message, WPARAM represents the handle to the dialog box on the device's upper and lower side, which is hdc,lparam. If the program handles this message, it should return a painting brush. The system will use this brush to redraw the dialog box background.

So we get the size of the dialog box in this Wm_ctlcolordlg message, and StretchBlt the bitmap to the HDC of the dialog box by scaling it and pasting it into the dialogs, and then returning an empty brush to the system so the system doesn't overwrite the bitmap background.

The code is very simple, note that when using the STRETCHBLT function to scale bitmaps, it is best to first use the

The Setstretchbltmode function is used to set the content extension mode of the lower graph, which avoids the distortion of the bitmap after scaling. The Setstretchbltmode function prototype is as follows:

int Setstretchbltmode (

HDCHDC,//Handle to DC

int Istretchmode//bitmap stretching mode

);

The first parameter is the device's upper and lower side, which is HDC.

The second parameter has four kinds of settings:

1. Blackonwhite or Stretch_andscans

If two or more pixels are merged into one pixel, then StretchBlt performs a logical AND operation on the pixel. The result is that only the entire original pixel is white when the image Socai is white, and its actual meaning is that black pixels control the white pixel. This applies to monochrome bitmaps, which are mainly black in the white background.

2. Whiteonblack or Stretch_orscans

If two or more pixels are merged into one pixel, StretchBlt performs a logical OR operation on the pixel. The result is that when all the original pixels are black, the image Socai is black, which means that the color is determined by the white pixel. This applies to monochrome bitmaps, which are mostly white in black backgrounds.

3. Coloroncolor or Stretch_deletescans

Simply eliminate the graph element row or column without any logical combination. This is usually the best way to process a color bitmap.

4. Halftone or Stretch_halftone

Calculates the average color of the destination based on the combination of source colors.

Other technical details can be found in the code comments, complete code below (also can download, download address: http://download.csdn.net/download/morewindows/4947377):

Dialog bitmap background-use StretchBlt map//by morewindows-(http://blog.csdn.net/MoreWindows) #include in Wm_ctlcolordlg <window s.h> #include "resource.h" const char szdlgtitle[] = "Bitmap background uses StretchBlt map morewindows-(http://blog.csdn.ne  
      
T/morewindows) ";  
                
dialog box message processing functions BOOL CALLBACK dlgproc (HWND hdlg, UINT messages, WPARAM WPARAM, LPARAM LPARAM);     int Apientry WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpCmdLine, int ncmdshow) {dialogbox (hinstance, Makeintresource (IDD_DIALOG1), NULL,  
    DLGPROC);  
return 0; BOOL CALLBACK Dlgproc (HWND hdlg, UINT message, WPARAM WPARAM, LPARAM LPARAM) {RECT rcdial  
    og  
    Hbitmap Hbitmap;  
    Static BITMAP S_BM;  
      
    Static HDC S_hdcmem;  
        Switch (message) {case WM_INITDIALOG://Set dialog box title SetWindowText (hdlg, szdlgtitle);Set dialog box size adjustable SetWindowLong (hdlg, Gwl_style, GetWindowLong (hdlg, Gwl_style) |  
      
        Ws_sizebox); 

Load back picture Hbitmap = (hbitmap) loadimage (NULL, "005.bmp", Image_bitmap, 0, 0, lr_loadfromfile |  
        Lr_createdibsection);  
            if (hbitmap = = NULL) {MessageBox (hdlg, "LoadImage failed", "Error", mb_iconerror);  
        Exit (0);  
            else {//the back picture into the Hdc-s_hdcmem HDC HDC;  
            HDC = GetDC (HDLG);  
            S_hdcmem = CreateCompatibleDC (HDC);      
            SelectObject (S_hdcmem, HBITMAP);  
      
            ReleaseDC (HDLG, HDC);  
        Get bitmap Information GetObject (hbitmap, sizeof (S_BM), &AMP;S_BM);  
      
    return 0;  
            Case Wm_command:switch (LoWord (WParam)) {case Idcancel:deletedc (S_HDCMEM);  
            EndDialog (Hdlg, LoWord (WParam)); Return TRUE;  
      
      
    } break;  
        Case Wm_size:invalidaterect (hdlg, NULL, TRUE);  
      
    return TRUE;  
        Case Wm_ctlcolordlg:getclientrect (hdlg, &rcdialog);  
        The Setstretchbltmode setting enables the StretchBlt to be sharper in scaling the image Setstretchbltmode (HDC) WParam, Coloroncolor);     
        StretchBlt ((HDC) wParam, 0, 0, rcdialog.right, Rcdialog.bottom, S_hdcmem, 0, 0, s_bm.bmwidth, S_bm.bmheight, srccopy);  
    Return (BOOL) ((hbrush) Getstockobject (Null_brush));  
return FALSE; }

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.