Overlay the transparent BUTTON and STATIC_TEXT controls on a non-solid background.

Source: Internet
Author: User

For example, we need to stack the cross zonex to a BUTTON control, where the BUTTON is located in a window with a non-solid background. When displayed, the purple-red (RGB 0xFF00FF) area is dug and the background is displayed. Resource images are shown in figure

Implementation:
1. Based on previous experience, if this Button is created by our own createjavaswex, set HBRUSH to GetStockObject (NULL_BRUSH) in the wndclass struct of RegisterClass ), in the WM_PAINT message, set SetBkMode (TRANSPARENT) first. However, since the combination of GWES and RESOURCE files in eVC improves the development speed, we cannot return to the original era of CreateWindow. We should consider dancing in the GWES game rules set by MS.

2. First, decolor the image. In fact, it is very easy to look at the online data. In the past, we had to make a monochrome "hidden color" hdc, and how to deal with it. WINCE has already completed this function and provided the TransparentBlt function.
Now the button I drew is like this:

The problem is obvious: the image itself achieves the goal of fading, but the background of the button is still flushed out with a gray brush, and the button background must be transparent.

2. First, I want to implement SekBkMode IN THE WM_DRAWITEM message, which has no effect. Then I want to change the registered WNDCLASS, so I find the SetClassLong and GetClassLong functions, and the INDEX parameter provides the GCL_HBRBACKGROUND type. Everything looks wonderful. So I wrote this code.
HBRUSH hbr = GetStockObject (NULL_BRUSH );
Int nReturn = SetWindowLong (hWnd, GCL_HBRBACKGROUND, (int) hbr );
The result compilation is not allowed, so there is no such thing as GCL_HBRBACKGROUND. I checked the header file provided by STANDARDSDK_500, which does not have the definition of GCL_HBRBACKGROUND. So I simply pulled out the SDK of VS2005 in XP and found GCL_HBRBACKGROUND = (-10 ).
SetWindowLong (hWnd,-10, (int) hbr)
The result is compiled and nReturn = 0 is running. Sorry. Try again,
HBRUSH hbr = GetWindowLong (hwnd,-10)
Running result hbr = NULL. Sad, cold and cold. Take a closer look at The WINCE product documentation. Although a lot of nindexes are listed in the table, The following small text Remarks writes "the only values supported for The nIndex parameter are GCL_HICON and GCL_STYLE ", A maximum of GCL_HCURSOR can be added. Other parameter types are not supported by WINCE. I am wasting my feelings.

3. Capture the WM_CTLCOLORBTN message In PROC of the parent window, and create a ghost in it.
Case WM_CTLCOLORBTN:
{
HBRUSH hbr = (HBRUSH) GetStockObject (NULL_BRUSH );
SetBkMode (HDC (wParam), TRANSPARENT );
Return (int) hbr;
}
Depressed, the parent window does not draw this area, the BUTTON does not erase the background, and the words on the desktop are revealed. Like this:

Then I drew an hour for InvalidateRect and ValidateRect. I had to wait for half a day for that small place. I still don't want to draw a Child Window area in the parent window. It's like opening a work meeting, it is not the responsibility of the Department. We will never do it, nor will it be killed. This is why the title of the article emphasizes "non-solid background. If it is a solid color background, I can set the HBRUSH color here to solve the problem, and it is troublesome to use NULL_BRUSH for transparency.

4. After dinner, you can easily think of a method. Yesterday, the manager criticized me for not doing things practically and liked to find shortcuts. Hey, this time I copied another small shortcut: I previously kept the hdcOffScreen of the parent window background in the Code. Now I will take the part of the Child Window area and draw it to the Child Window first, then, the cross of the sub-window is superimposed. In fact, the previous thought was about how to remove the background color. The title of this article was also written. In fact, the background color was not removed, but it only covered the background image of the parent window, reverse Thinking.

Case WM_DRAWITEM:
{
LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) lParam;
If (lpDIS-> CtlID = IDC_CROSS)
{
BitBlt (lpDIS-> hDC, // This is the hdc device of the BUTTON window, rather than the background, so we draw from {0, 0}
0,
0,
Button. nWidth, // width and height of the CROSS image
Button. nHeight,
BackGround. hdcOffScreen, // OffScreen hdc of the BackGround image on the parent window, that is, the image drawn in the memory
Button. x, // coordinates of the CROSS button in the upper left corner of the background image. We first draw the content of the area in the background image to the hdcDevice of the BUTTON as the background of the BUTTON window.
Button. y,
SRCCOPY );

TransparentBlt (lpDIS-> hDC,
0,
0,
Button. nWidth,
Button. nHeight,
Button. hdcOffScreen, // This time the CROSS image is drawn out from the hdcOffScreen in the memory and painted to the hdcDevice.
0,
0,
Button. nWidth,
Button. nHeight,
0x00FF00FF );

Break;
}
}

Final Effect

5. Using this method, you can also achieve "Transparent" display of static text controls. However, this is not a response in the WM_DRAWITEM message, but in the WM_CTLCOLORSTATIC message, the reference code is as follows:

Case WM_CTLCOLORSTATIC:
{
HDC hdc = (HDC) wParam;
HWND hWnd = (HWND) lParam;

SetTextColor (hdc, FontColor); // you can specify the text color.
BitBlt (hdc,...) // capture the background image of the parent window with the corresponding coordinates and length and width as our background.
SetBkMode (hdc, TRANSPARENT); // it is necessary to set the background transparency of the text area. In addition to the button background, the text itself has a base
Return (int) GetStockObject (NULL_BRUSH); // return a transparent brush. Otherwise, the background image we use BitBlt will be removed.
}

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.