Cxpbutton class (VC button self-painting)

Source: Internet
Author: User
Posting time: 17:11:00

Xpbutton. h
========================================================== ==============
# If! Defined (afx_xpbutton_h000044cd5b2a_756e_4939_9261_e0034e0f2def0000included _)
# Define afx_xpbutton_h000044cd5b2a_756e_4939_9261_e0034e0f2def0000included _

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

//////////////////////////////////////// /////////////////////////////////////
// Cxpbutton window

Class cxpbutton: Public cbutton
{
// Construction
Public:
Cxpbutton ();

// Attributes
Protected:
// The outer border of the button
Cpen m_boundrypen;
 
// The inside border of the button when the cursor is placed above the button
Cpen m_insideboundrypenleft;
Cpen m_insideboundrypenright;
Cpen m_insideboundrypentop;
Cpen m_insideboundrypenbottom;
 
// The inside border of the button when the button gets the focus
Cpen m_insideboundrypenleftsel;
Cpen m_insideboundrypenrightsel;
Cpen m_insideboundrypentopsel;
Cpen m_insideboundrypenbottomsel;
 
// The base color of the button, including valid and invalid statuses
Cbrush m_fillactive;
Cbrush m_fillinactive;
 
// Button status
Bool m_bover; // when the cursor is over the button, the value is true, and vice versa.
Bool m_btracking; // This value is true when the mouse is not released
Bool m_bselected; // If the button is pressed, the value is true.
Bool m_bfocus; // when the button is the current focus, the value is true.

// Operations
Public:

// Overrides
// Classwizard generated virtual function overrides
// {Afx_virtual (cxpbutton)
Protected:
Virtual void presubclasswindow ();
//} Afx_virtual

// Implementation
Public:
Virtual void drawitem (lpdrawitemstruct );
Virtual void dogradientfill (CDC * PDC, crect * rect );
Virtual void drawinsideborder (CDC * PDC, crect * rect );
Virtual ~ Cxpbutton ();

// Generated message map Functions
Protected:
// {Afx_msg (cxpbutton)
Afx_msg void onmousemove (uint nflags, cpoint point );
Afx_msg lresult onmouseleave (wparam, lparam );
Afx_msg lresult onmousehover (wparam, lparam );
 
//} Afx_msg

Declare_message_map ()
};

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

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

# Endif //! Defined (afx_xpbutton_h000044cd5b2a_756e_4939_9261_e0034e0f2def0000included _)

========================================================== ======================================
Xpbutton. cpp
========================================================== ======================================
// Xpbutton. cpp: implementation file
//

# Include "stdafx. H"
# Include "xpbutton. H"

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

//////////////////////////////////////// /////////////////////////////////////
// Cxpbutton

Cxpbutton: cxpbutton ()
{
M_boundrypen.createpen (ps_insideframe | ps_solid, 1, #000000 );
M_insideboundrypenleft.createpen (ps_insideframe | ps_solid, 3, # fac458 );
M_insideboundrypenright.createpen (ps_insideframe | ps_solid, 3, # fbca6a );
M_insideboundrypentop.createpen (ps_insideframe | ps_solid, 2, # fcd279 );
M_insideboundrypenbottom.createpen (ps_insideframe | ps_solid, 2, # e59700 );
 
M_fillactive.createsolidbrush (# dfdeec );
M_fillinactive.createsolidbrush (# dedfec );
 
M_insideboundrypenleftsel.createpen (ps_insideframe | ps_solid, 3, # 99c6fc );
M_insideboundrypentopsel.createpen (ps_insideframe | ps_solid, 2, # a2c9ff );
M_insideboundrypenrightsel.createpen (ps_insideframe | ps_solid, 3, # a2bdfc );
M_insideboundrypenbottomsel.createpen (ps_insideframe | ps_solid, 2, # a2c9ff );
 
M_bover = m_bselected = m_btracking = m_bfocus = false;
 
}

Cxpbutton ::~ Cxpbutton ()
{
M_boundrypen.deleteobject ();
M_insideboundrypenleft.deleteobject ();
M_insideboundrypenright.deleteobject ();
M_insideboundrypentop.deleteobject ();
M_insideboundrypenbottom.deleteobject ();
 
M_fillactive.deleteobject ();
M_fillinactive.deleteobject ();
 
M_insideboundrypenleftsel.deleteobject ();
M_insideboundrypentopsel.deleteobject ();
M_insideboundrypenrightsel.deleteobject ();
M_insideboundrypenbottomsel.deleteobject ();
 
}

Begin_message_map (cxpbutton, cbutton)
// {Afx_msg_map (cxpbutton)
On_wm_mousemove ()
On_message (wm_mouseleave, onmouseleave)
On_message (wm_mousehover, onmousehover)
//} Afx_msg_map
End_message_map ()

//////////////////////////////////////// /////////////////////////////////////
// Cxpbutton message handlers

// Add the owner draw attribute
Void cxpbutton: presubclasswindow ()
{
// Todo: add your specialized code here and/or call the base class
 
Cbutton: presubclasswindow ();
Modifystyle (0, bs_ownerdraw );
}

Void cxpbutton: onmousemove (uint nflags, cpoint point)
{
// Todo: add your message handler code here and/or call default
If (! M_btracking)
{
Trackmouseevent tme;
Tme. cbsize = sizeof (TME );
Tme. hwndtrack = m_hwnd;
Tme. dwflags = tme_leave | tme_hover;
Tme. dwhovertime = 1;
M_btracking = _ trackmouseevent (& TME );
}
 
Cbutton: onmousemove (nflags, point );
}

Lresult cxpbutton: onmouseleave (wparam, lparam)
{
M_bover = false;
M_btracking = false;
Invalidaterect (null, false );
Return 0;
}

Lresult cxpbutton: onmousehover (wparam, lparam)
{
M_bover = true;
Invalidaterect (null );
Return 0;
}

Void cxpbutton: drawitem (lpdrawitemstruct)
{
// Obtain control information from lpdrawitemstruct
Crect rect = lpdrawitemstruct-> rcitem;
CDC * PDC = CDC: fromhandle (lpdrawitemstruct-> HDC );
Int nsavedc = PDC-> savedc ();
Uint state = lpdrawitemstruct-> itemstate;
Point pt;
Tchar strtext [max_path + 1];
: Getwindowtext (m_hwnd, strtext, max_path );
 
// Draw the outer border of the button, which is a rounded rectangle with a radius of 5
PT. x = 5;
PT. Y = 5;
Cpen * holdpen = PDC-> SelectObject (& m_boundrypen );
PDC-> roundrect (& rect, pt );
 
// Obtain the button status
If (State & ods_focus)
{
M_bfocus = true;
M_bselected = true;
}
Else
{
M_bfocus = false;
M_bselected = false;
}
 
 
If (State & ods_selected | State & ods_default)
{
M_bfocus = true;
}

 
PDC-> SelectObject (holdpen );
 
Rect. deflaterect (csize (getsystemmetrics (sm_cxedge), getsystemmetrics (sm_cyedge )));
// Rect. deflaterect (1, 1 );
 
// Fill in the background color of the button Based on the button status
Cbrush * poldbrush;
If (m_bover)
{
Poldbrush = PDC-> SelectObject (& m_fillactive );
Dogradientfill (PDC, & rect );
}
Else
{
Poldbrush = PDC-> SelectObject (& m_fillinactive );
Dogradientfill (PDC, & rect );
}
 
// Draw the inner border based on the button status
If (m_bover | m_bselected)
Drawinsideborder (PDC, & rect );

PDC-> SelectObject (poldbrush );
 
// Display the text of the button
If (strtext! = NULL)
{
Cfont * hfont = getfont ();
Cfont * holdfont = PDC-> SelectObject (hfont );
Csize szextent = PDC-> gettextextent (strtext, lstrlen (strtext ));
Cpoint Pt (rect. centerpoint (). X-szextent. CX/2, rect. centerpoint (). Y-szextent. cy/2 );
If (State & ods_selected)
PT. offset (1, 1 );
Int nmode = PDC-> setbkmode (transparent );
If (State & ods_disabled)
PDC-> drawstate (PT, szextent, strtext, dss_disabled, true, 0, (hbrush) null );
Else
PDC-> drawstate (PT, szextent, strtext, dss_normal, true, 0, (hbrush) null );
PDC-> SelectObject (holdfont );
PDC-> setbkmode (nmode );
}
 
 
PDC-> restoredc (nsavedc );
}

// Specifies the base color of the draw button.
Void cxpbutton: dogradientfill (CDC * PDC, crect * rect)
{
Cbrush brbk [64];
Int nwidth = rect-> width ();
Int nheight = rect-> height ();
Crect CTS;
 
For (INT I = 0; I <64; I ++)
{
If (m_bover)
{
If (m_bfocus)
Brbk [I]. createsolidbrush (RGB (255-(I/4), 255-(I/4), 255-(I/3 )));
Else
Brbk [I]. createsolidbrush (RGB (255-(I/4), 255-(I/4), 255-(I/5 )));
}
Else
{
If (m_bfocus)
Brbk [I]. createsolidbrush (RGB (255-(I/3), 255-(I/3), 255-(I/4 )));
Else
Brbk [I]. createsolidbrush (RGB (255-(I/3), 255-(I/3), 255-(I/5 )));
}
}
 
For (INT I = rect-> top; I <= nheight + 2; I ++)
{
RDBMS. setrect (rect-> left, I, nwidth + 2, I + 1 );
PDC-> fillrect (& RDBMS, & brbk [(I * 63)/nheight)]);
}
 
For (INT I = 0; I <64; I ++)
Brbk [I]. deleteobject ();
}

// Draw the button's inner border
Void cxpbutton: drawinsideborder (CDC * PDC, crect * rect)
{
Cpen * pleft, * pright, * pTop, * pbottom;
 
If (m_bselected &&! M_bover)
{
Pleft = & m_insideboundrypenleftsel;
Pright = & m_insideboundrypenrightsel;
PTop = & m_insideboundrypentopsel;
Pbottom = & m_insideboundrypenbottomsel;
}
Else
{
Pleft = & m_insideboundrypenleft;
Pright = & m_insideboundrypenright;
PTop = & m_insideboundrypentop;
Pbottom = & m_insideboundrypenbottom;
}
 
Cpoint oldpoint = PDC-> moveTo (rect-> left, rect-> bottom-1 );
Cpen * poldpen = PDC-> SelectObject (pleft );
PDC-> lineto (rect-> left, rect-> top + 1 );
PDC-> SelectObject (pright );
PDC-> moveTo (rect-> right-1, rect-> bottom-1 );
PDC-> lineto (rect-> right-1, rect-> top );
PDC-> SelectObject (pTop );
PDC-> moveTo (rect-> left-1, rect-> top );
PDC-> lineto (rect-> right-1, rect-> top );
PDC-> SelectObject (pbottom );
PDC-> moveTo (rect-> left, rect-> bottom );
PDC-> lineto (rect-> right-1, rect-> bottom );
PDC-> SelectObject (poldpen );
PDC-> moveTo (oldpoint );

If (m_bselected &&! M_bover)
Drawfocusrect (PDC-> m_hdc, rect );
}

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.