Cstatic control background transparent instance code

Source: Internet
Author: User

Blog transfer: http://vivianke.blog.163.com/blog/static/29715271200781121358317/

 

Note:
1. the background of the cstatic control is transparent. You must first derive a class from cstatic. In this class, afx_msg hbrush ctlcolor (CDC * PDC, uint nctlcolor) is a reflection function that processes messages of = wm_ctlolor, message ing macro: on_wm_ctlcolor_reflect (),
2. afx_msg hbrush onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor); processes wm_ctlcolor messages, used to change the control color, which is a message processing function.
3. afx_msg bool onerasebkgnd (CDC * PDC); process the wm_erasebkgnd message to display the background image. the background must be set to transparent, that is, the afx_msg hbrush ctlcolor (CDC * PDC, uint nctlcolor) reflection function must be called.
// Ctransparentstatic class

# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000
// Transparentstatic. h: header file
//

//////////////////////////////////////// /////////////////////////////////////
// Ctransparentstatic window

Class ctransparentstatic: Public cstatic
{
// Construction
Public:
Ctransparentstatic ();

// Attributes
Public:

// Operations
Public:

// Overrides
// Classwizard generated virtual function overrides
// {Afx_virtual (ctransparentstatic)
//} Afx_virtual

// Implementation
Public:
Virtual ~ Ctransparentstatic ();

// Generated message map Functions
Protected:
// {Afx_msg (ctransparentstatic)
Afx_msg hbrush onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor); // control color processing function
Afx_msg hbrush ctlcolor (CDC * PDC, uint nctlcolor); // reflection function, processing control color, you can set the background Mode
Afx_msg bool onerasebkgnd (CDC * PDC); // remove the background to display the background.
//} Afx_msg

Declare_message_map ()
PRIVATE:
Cbitmap m_bmp;
};

//////////////////////////////////////// /////////////////////////////////////

// {Afx_insert_location }}
// Microsoft Visual C ++ will insert additional declarations immediately before the previous line.

# Endif //! Defined (afx_transparentstatic_h1_8b6d6931_a3de_400f_ba33_f4097632d8eb1_included _)

// Transparentstatic. cpp: implementation file
//

# Include "stdafx. H"
# Include "material_mis.h"
# Include "transparentstatic. H"

# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif

//////////////////////////////////////// /////////////////////////////////////
// Ctransparentstatic

Ctransparentstatic: ctransparentstatic ()
{
}

Ctransparentstatic ::~ Ctransparentstatic ()
{
}

Begin_message_map (ctransparentstatic, cstatic)
// {Afx_msg_map (ctransparentstatic)
On_wm_ctlcolor ()
On_wm_ctlcolor_reflect ()
On_wm_erasebkgnd ()
//} Afx_msg_map
End_message_map ()

//////////////////////////////////////// /////////////////////////////////////
// Ctransparentstatic message handlers

Hbrush ctransparentstatic: onctlcolor (CDC * PDC, cwnd * pwnd, uint nctlcolor)
{
Hbrush HBr = cstatic: onctlcolor (PDC, pwnd, nctlcolor );

If (pwnd-> getdlgctrlid () = idc_static_text)
PDC-> settextcolor (RGB (255,255,255); // you can specify the text color displayed in the control.

Return HBr;
}

Hbrush ctransparentstatic: ctlcolor (CDC * PDC, uint nctlcolor)
{
PDC-> setbkmode (transparent); // The transparent mode of the device background.
Return (hbrush) getstockobject (null_brush );
}

Bool ctransparentstatic: onerasebkgnd (CDC * PDC)
{
If (m_bmp .getsafehandle () = NULL ){
Crect rect;
Getwindowrect (& rect );
Cwnd * pparent = getparent ();
Assert (pparent );
Pparent-> screentoclient (& rect); // convert our your diates to our parents
// Copy what's on the parents at this point
CDC * PDC = pparent-> getdc ();
CDC memdc;
Memdc. createcompatibledc (PDC );
M_bmp .createcompatiblebitmap (PDC, rect. Width (), rect. Height ());
Cbitmap * poldbmp = memdc. SelectObject (& m_bmp );
Memdc. bitblt (0, 0, rect. Width (), rect. Height (), PDC, rect. Left, rect. Top, srccopy );
Memdc. SelectObject (poldbmp );
Pparent-> releasedc (PDC );
}
Else // copy what we copied off the parent the first time back onto the parent
{
Crect rect;
Getclientrect (rect );
CDC memdc;
Memdc. createcompatibledc (PDC );
Cbitmap * poldbmp = memdc. SelectObject (& m_bmp );
PDC-> bitblt (0, 0, rect. Width (), rect. Height (), & memdc, 0, 0, srccopy );
Memdc. SelectObject (poldbmp );
}

Return true;
}

// View class
Ctransparentstatic m_static;

Void cmaterial_misview: createstaticctrl ()
{
Crect rect;
Getclientrect (& rect );
// Set the size of the static text box
Rect. Top = 5;
Rect. Left = rect. Right/2-180/2;
Rect. Bottom = 30;
Rect. Right = rect. Left + 180;
// Create a static text box
M_static.create (m_strcurlist, ws_visible | ss_center, rect, this, idc_static_text );
// Set the font of the Static Control
M_static.setfont (& m_font );
}

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.