Windows interface programming Fourth profiled form

Source: Internet
Author: User
Tags transparent color

Translated from http://blog.csdn.net/morewindows/article/details/8451638

On a "Windows interface programming the third special form of the ordinary version " introduced the special-shaped window (shaped form) of the creation, its main steps are--first by creating a bit drawing brush to do the background brush of the window, and then through SetWindowLong for the form plus WS _ex_layered property, and then uses setlayeredwindowattributes to specify the transparent color of the window to complete the adjustment of the window shape. And in order to enable the special-shaped window to support the mouse drag, in the wm_lbuttondown message has been specially handled.

Then there are very similar to the two shaped form, except that the shape on the left is small and the shape on the right is large. How can this be achieved?

The bitmap is scaled by other software before the program loads it to specify the size of the profiled window. This method can accomplish the task, but after all it is too out.

Thefirst bitmap background and bit drawing brush from the Windows interface programming can be thought of without the use of a bit drawing brush, and using StretchBlt to scale the bitmap to the window size directly when the window background is drawn, so that the window size can be specified.

Because the window cannot be resized dynamically by the mouse after it has been run, the bitmap can be scaled and loaded into a buffered HDC when the window is initialized , and then mapped using BitBlt when the window background is drawn . This approach only needs to scale the bitmap once, in each background drawing only need to copy the bitmap, the efficiency of the program will be improved. The complete source code is given below (: http://download.csdn.net/download/morewindows/4966819)

//profiled window 2 self-mapping in WM_ERASEBKGND messages//by morewindows-(http://blog.csdn.net/MoreWindows)#include <windows.h>Const CharSzappname[] ="profiled window 2 morewindows-(http://blog.csdn.net/MoreWindows)";/** Function Name: getwindowsize * function: Get window width height * hwnd Window Handle * pnwidth window Width * pnheight window height*/voidGetwindowsize (HWND hwnd,int*pnwidth,int*pnheight);/** Function Name: Initbitmapwindow * Function function: Bitmap window initialization * HINSTANCE Process instance * nwidth window Width * nheight window height * ncmdshow display mode-with Showwindo The second parameter of the W function is the same*/BOOL Initbitmapwindow (hinstance hinstance,intNwidth,intNheight,intncmdshow);//Bitmap window message processing functionsLRESULT CALLBACK Bitmapwindowwndprco (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM lparm); Hbitmap G_hbitmap;intapientry WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpCm Dline,intncmdshow) {    //first create a no back paint brush window,//then in wm_create and specify a transparent color, the bitmap is scaled and loaded into s_hdcmem. //Finally, you can use the S_hdcmem map in the WM_ERASEBKGNDG_hbitmap = (hbitmap) loadimage (NULL,"kitty.bmp", Image_bitmap,0,0, Lr_loadfromfile); if(G_hbitmap = =NULL) {MessageBox (NULL,"Bitmap load Failed","Error", Mb_iconerror); return 0; }    //set the size of an irregular windowBITMAP BM; GetObject (G_hbitmap,sizeof(BM), &BM); intNwindowwidth =bm.bmwidth; intNwindowheight = Bm.bmheight + -;//Pull Height 100    if(!Initbitmapwindow (hinstance, Nwindowwidth, Nwindowheight, ncmdshow))return 0;    MSG msg;  while(GetMessage (&msg, NULL,0,0) {translatemessage (&msg); DispatchMessage (&msg);    } DeleteObject (G_hbitmap); returnMsg.wparam;} BOOL Initbitmapwindow (hinstance hinstance,intNwidth,intNheight,intncmdshow)    {HWND hwnd;        Wndclass Wndclass; Wndclass.style= Cs_vredraw |Cs_hredraw; Wndclass.lpfnwndproc=Bitmapwindowwndprco; Wndclass.cbclsextra=0; Wndclass.cbwndextra=0; Wndclass.hinstance=hinstance; Wndclass.hicon=LoadIcon (NULL, idi_application); Wndclass.hcursor=loadcursor (NULL, Idc_arrow); Wndclass.hbrbackground= (Hbrush) getstockobject (Null_brush);//window back painting brush is emptyWndclass.lpszmenuname =NULL; Wndclass.lpszclassname=Szappname; if(! RegisterClass (&wndclass)) {MessageBox (NULL,"Program need Windows nt!","Error", Mb_iconerror); returnFALSE; } hwnd=CreateWindowEx (Ws_ex_topmost, Szappname, Szappname, Ws_popup, Cw_usedefault, Cw_usedefault, nwidth , nheight, NULL, NULL, Hinsta    NCE, NULL); if(hwnd = =NULL)returnFALSE;    ShowWindow (hwnd, ncmdshow);        UpdateWindow (HWND); returnTRUE;} LRESULT CALLBACK Bitmapwindowwndprco (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM lparm) {StaticHDC S_hdcmem;//placing a scaled bitmap        Switch(message) { Casewm_create: {//Set hierarchical PropertiesSetWindowLong (hwnd, Gwl_exstyle, GetWindowLong (hwnd, Gwl_exstyle) |ws_ex_layered); //Set Transparent ColorCOLORREF cltransparent = RGB (0,0,0); SetLayeredWindowAttributes (hwnd, Cltransparent,0, Lwa_colorkey); //Zoom Bitmap//loading bitmaps into hdctempHDC HDC =GetDC (HWND); HDC hdctemp=CreateCompatibleDC (HDC);            SelectObject (Hdctemp, G_hbitmap); //Get window Size            intnwidth, nheight; Getwindowsize (hwnd,&nwidth, &nheight); //create hdc-s_hdcmem that are equal to the window size and can hold bitmapsS_hdcmem =CreateCompatibleDC (HDC); Hbitmap hbmp=CreateCompatibleBitmap (hdc, nwidth, nheight);            SelectObject (S_hdcmem, hbmp); //scale an in-place map to the window sizeBITMAP BM; GetObject (G_hbitmap,sizeof(BM), &BM); StretchBlt (S_hdcmem,0,0, nwidth, nheight, Hdctemp,0,0, Bm.bmwidth, Bm.bmheight, srccopy); //Freeing ResourcesDeleteDC (hdctemp);        ReleaseDC (hwnd, HDC); }        return 0;  CaseWm_keydown:Switch(wParam) { CaseVk_escape://exit when the ESC key is pressedSendMessage (hwnd, Wm_destroy,0,0); returnTRUE; }         Break;  CaseWm_lbuttondown://You can drag the window when you click the left mouse buttonPostMessage (hwnd, WM_SYSCOMMAND, Sc_move | Htcaption,0); returnTRUE;  CaseWM_ERASEBKGND://Direct mapping in the background of a window{HDC hdc=(HDC) WParam; intnwidth, nheight; Getwindowsize (hwnd,&nwidth, &nheight); BITBLT (HDC,0,0, nwidth, nheight, S_hdcmem,0,0, srccopy); returnTRUE; }     CaseWm_destroy:deletedc (S_HDCMEM); PostQuitMessage (0); return 0; }    returnDefWindowProc (hwnd, message, WParam, lparm);}voidGetwindowsize (HWND hwnd,int*pnwidth,int*pnheight)    {RECT rc; GetWindowRect (hwnd,&RC); *pnwidth = Rc.right-Rc.left; *pnheight = Rc.bottom-rc.top;}

Run the program to get the profiled window as shown on the right side of each picture in the article. Finally summarizes the "three elements" of the special-shaped window:

1. Ws_ex_layered Property

2. Take a bitmap as a window background (self-mapping or bit drawing brush)

3. Specify Transparent Color

This package is: http://download.csdn.net/download/morewindows/4966819

When the background of a window is decorated with a color image, other controls, if they are not harmonized with the gray background, theWindows interface programming fifth static control background transparency describes how to set a transparent background for a static box.

Windows interface programming Fourth profiled form (GO)

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.