Use MFC to implement the translucent effect of the buttons above to see the content in the parent window, which is above (one with a background image and the other without a background image ).
Controls inherit from the CWnd class (the color part is the background image of the window, the button is a PNG image, and the second icon is the effect when the mouse points ).
Use GDI + to draw a PNG image. I will not talk about it here (to process the WM_PAINT message ):
[Cpp]
1. void PNGButton: OnPaint ()
2 .{
3. CPaintDC (this );
4. Graphics g (dc. m_hDC );
5. if (DrawBorder ){
6. g. DrawImage (hoverBg,); // draws the highlighted background when the mouse points
7 .}
8. g. DrawImage (this-> bg,); // draw button icon
9. g. ReleaseHDC (dc. m_hDC );
10 .}
The key to transparency: Pay attention to the code that calls this method later.
The key lies in the InvalidateRect function: notifies the parent window to re-draw a specific area. After this function is executed, the area where the button is located is overwritten by the content drawn by the parent window. After the parent window is drawn,
The button will also receive the WM_PAINT message and execute the above OnPaint code.
[Cpp]
1. void PNGButton: PaintParent ()
2 .{
3. CRect rect;
4. GetWindowRect (& rect );
5. GetParent ()-> ScreenToClient (& rect );
6. GetParent ()-> InvalidateRect (& rect );
7 .}
Capture mouse pointing or removing events (process WM_MOUSEMOVE, WM_MOUSEOVER, WM_MOUSELEAVE messages ):
[Cpp]
1. void PNGButton: OnMouseHover (UINT nFlags, CPoint point)
2 .{
3. DrawBorder = true;
4. PaintParent (); // notifies the parent window to re-paint a specific area, which will trigger the re-painting of the control.
5 .}
6.
7.
8. void PNGButton: OnMouseLeave ()
9 .{
10. m_is_mouse_over = false;
11. m_is_tracked = false;
12. DrawBorder = false;
13. PaintParent (); // notifies the parent window to re-paint a specific area, which will trigger the re-painting of the control.
14. CWnd: OnMouseLeave ();
15 .}
16.
17.
18. void PNGButton: OnMouseMove (UINT nFlags, CPoint point)
19 .{
20. m_is_mouse_over = true;
21. if (! M_is_tracked)
22 .{
23. TRACKMOUSEEVENT tme;
24. tme. cbSize = sizeof (TRACKMOUSEEVENT );
25. tme. dwFlags = TME_LEAVE | TME_HOVER;
26. tme. hwndTrack = GetSafeHwnd ();
27. tme. dwHoverTime = 80;
28. _ TrackMouseEvent (& tme );
29. m_is_tracked = true;
30 .}
31. CWnd: OnMouseMove (nFlags, point );
32 .}
Appendix:
Load PNG images from resources
Appendix:
Load PNG images from resources
[Cpp]
1. View Code
2. # pragma once
3. # include "stdafx. h"
4. using namespace Gdiplus;
5.
6. static bool ImageFromIDResource (UINT nID, LPCTSTR sTR, Image * & pImg)
7 .{
8. HINSTANCE hInst = AfxGetResourceHandle ();
9. HRSRC hRsrc =: FindResource (hInst, MAKEINTRESOURCE (nID), sTR); // type
10. if (! HRsrc)
11. return FALSE;
12. // load resource into memory
13. DWORD len = SizeofResource (hInst, hRsrc );
14. BYTE * lpRsrc = (BYTE *) LoadResource (hInst, hRsrc );
15. if (! LpRsrc)
16. return FALSE;
17. // Allocate global memory on which to create stream
18. HGLOBAL m_hMem = GlobalAlloc (GMEM_FIXED, len );
19. BYTE * pmem = (BYTE *) GlobalLock (m_hMem );
20. memcpy (pmem, lpRsrc, len );
21. IStream * pstm;
22. CreateStreamOnHGlobal (m_hMem, FALSE, & pstm );
23. // load from stream
24. pImg = Gdiplus: Image: FromStream (pstm );
25. // free/release stuff
26. GlobalUnlock (m_hMem );
27. pstm-> Release ();
28. FreeResource (lpRsrc );
29. return TRUE;
30 .}
Tile Image Code
[Cpp]
1. CPaintDC (this );
2. CRect rect;
3. GetClientRect (rect );
4. CBrush bs (RGB (240,240,240); // specifies the background color of the window.
5. dc. FillRect (& rect, & bs); // window coloring
6. // fill in the background image: tiled
7. Graphics g (dc. m_hDC );
8. if (has_bg) g. DrawImage (this-> bg, 0, 0 );
9. Gdiplus: TextureBrush bbs (this-> img );
10. g. FillRectangle (& bbs, 0, 0, rect. Width (), this-> img-> GetHeight ());
11. g. ReleaseHDC (dc. m_hDC );
12. // TRACE (L "CMainFrame: OnPaint \ r \ n ");
Author: eit520