Windows Interface Programming Article 5 static control background transparency

Source: Internet
Author: User
Tags transparent color

The last article "Windows Interface Programming third normal edition of Special-shaped forms" and "Windows Interface Programming Fourth Section Special-shaped forms High Handsome edition" introduced the creation of special-shaped windows (Special-shaped forms, and summed up the "three elements" of the special window ":

1. ws_ex_layered attributes

2. Specify the transparent color

3. Use bitmap as the window background

 

This article will introduce how to make the background of static controls transparent in Windows programming, which will further beautify the interface. Next, let's take a look at the running screen of the dialog box program without transparent background of static controls. For the color background of the dialog box, refer to the first bitmap background and bitmap painting in Windows interface programming.

It can be seen that the gray background of the static control is not harmonious in the color image background of the dialog box, so it is necessary to beautify it.

In the first article "Windows interface programming first article bitmap background and bitmap painting brush", we introduced how to set the background of the dialog box through wm_ctlcolordlg messages, and wm_ctlcolorbtn messages similar to wm_ctlcolordlg messages in windows, wm_ctlcoloredit, wm_ctlcolorlistbox, wm_ctlcolorscrollbar, and wm_ctlcolorstatic manage buttons, edit box, list box, scroll bar, and static box respectively. Therefore, we first return an empty image brush in the wm_ctlcolorstatic message to see if the background transparency requirement can be met. In addition, you can set the background transparency of the text area through setbkmode. The function prototype is as follows:

Int setbkmode (

HDC, // handle to DC

Int ibkmode // background Mode

);

When the second parameter of this function is set to transparent, the foreground of the text area can be set to transparent.

 

The complete source code (: http://download.csdn.net/download/morewindows/4966826) is given below ):

// Static control background transparency wm_ctlcolorstatic returns empty painter // By morewindows-(http://blog.csdn.net/MoreWindows) # include <windows. h> # include "resource. H "const char szdlgtitle [] =" static control background transparent morewindows-(http://blog.csdn.net/MoreWindows) "; // dialog box message processing function bool callback dlgproc (hwnd hdlg, uint message, wparam, lparam); int apientry winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) {dialogbox (hinstance, makeintresource (response), null, dlgproc); Return 0 ;} bool callback dlgproc (hwnd hdlg, uint message, wparam, lparam) {rect rcdialog; hbitmap; static bitmap s_bm; static HDC s_hdcmem; Switch (Message) {case when: // set the dialog box title setwindowtext (hdlg, szdlgtitle); // you can adjust the size of the setwindowlong (hdlg, gwl_style, getwindowlong (hdlg, gwl_style) | ws_sizebox ); // load the background image hbitmap = (hbitmap) LoadImage (null, "006.bmp", image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection); If (hbitmap = NULL) {MessageBox (hdlg, "LoadImage failed", "error", mb_iconerror); exit (0);} else {// put the back image into HDC-s_hdcmemhdc HDC; HDC = getdc (hdlg ); s_hdcmem = createcompatibledc (HDC); SelectObject (s_hdcmem, hbitmap); releasedc (hdlg, HDC); // obtain the bitmap information GetObject (hbitmap, sizeof (s_bm), & 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_ctlcolorstatic: setbkmode (HDC) wparam, transparent); Return (bool) (hbrush) getstockobject (null_brush); case when: getclientrect (hdlg, & rcdialog); // you can set the limit to make the stretchblt clearer in the scaled 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 ;}

This code is also added to the Code of the first bitmap background and bitmap painter in Windows interface programming.

Case wm_ctlcolorstatic:

Setbkmode (HDC) wparam, transparent );

Return (bool) (hbrush) getstockobject (null_brush ));

To achieve the effect of transparent background of static controls, the program running effect is as follows:

 

As shown in the figure, although the static text control meets the background transparency requirement, the display of the descriptive text of the group box control is not beautiful.

 

To solve this problem, we can try bitmap painting. In the wm_ctlcolorstatic message, we also return a bitmap painting brush like the wm_ctlcolordlg message.

// Static controls background transparency-in the wm_ctlcolordlg returned window background bitmap painter // By morewindows-(http://blog.csdn.net/MoreWindows) # include <windows. h> # include "resource. H "const char szdlgtitle [] =" static control background transparent morewindows-(http://blog.csdn.net/MoreWindows) "; // dialog box message processing function bool callback dlgproc (hwnd hdlg, uint message, wparam, lparam); int apientry winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) {dialogbox (hinstance, makeintresource (response), null, dlgproc); Return 0 ;} bool callback dlgproc (hwnd hdlg, uint message, wparam, lparam) {static hbrush s_hbitmapbrush; // bitmap painter switch (Message) {Case wm_initdialog: // set the dialog box title setwindowtext (hdlg, szdlgtitle); // you can adjust the size of the setwindowlong (hdlg, gwl_style, getwindowlong (hdlg, gwl_style) | ws_sizebox ); // load the background image hbitmap; hbitmap = (hbitmap) LoadImage (null, "006.bmp", image_bitmap, 0, 0, lr_loadfromfile | lr_createdibsection); If (hbitmap = NULL) {MessageBox (hdlg, "LoadImage failed", "error", mb_iconerror); exit (0) ;}// create a bitmap painter s_hbitmapbrush = createpatternbrush (hbitmap); Return 0; case wm_command: Switch (loword (wparam) {Case idcancel: deleteobject (s_hbitmapbrush); enddialog (hdlg, loword (wparam); Return true;} break; Case wm_ctlcolorstatic: setbkmode (HDC) wparam, transparent); Case wm_ctlcolordlg: Return (bool) s_hbitmapbrush;} return false ;}

Like the previous program, this program is only added to the Code of the first bitmap background and bitmap painter in Windows interface programming.

Case wm_ctlcolorstatic:

Setbkmode (HDC) wparam, transparent );

To set the background transparency of the static control. The program running effect is as follows:

 

The figure shows that the transparency of the static control is very convenient. You only need to complete two steps in the wm_ctlcolorstatic message:

1. Use setbkmode (HDC) wparam, transparent); To set the background transparency of the text area.

2. Return an empty image brush or the same image brush as the parent window.

 

Later also released Windows Interface Programming articles, more windows programming articles, please visit http://blog.csdn.net/MoreWindows

This article supporting procedures: http://download.csdn.net/download/morewindows/4966826

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8470452

Welcome to Weibo: http://weibo.com/MoreWindows

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.