XP style button (transferred from Code Project)

xiaoxiao2021-03-31  218

// XPButton.h: header fileclass CXPButton: public CButton {DECLARE_DYNCREATE (CXPButton) // Constructionpublic: CXPButton (); CPen pBoundryPen; CPen pInsideBoundryPenLeft; CPen pInsideBoundryPenTop; CPen pInsideBoundryPenRight; CPen pInsideBoundryPenBottom;

CPEN PinsideBoundrypenLeftsel; CPEN PinsideBoundrypenrightsel; CPEN PinsideBoundrypenbottomsel

CPEN * POLDPEN; CBRUSH PFILLACTIVE; CBRUSH PFILLINACTIVE; CBRUSH * POLDBRUSH; BOOL M_BOVERCONTROL; BOOL M_BTRACKING; BOOL M_BSELECTED; CSTRING STRCAPTION; // AttributeSpublic:

// OperationsPublic:

// Overrides // ClassWizard generated virtual function overrides // {{AFX_VIRTUAL (CXPButton) public: virtual void DrawItem (LPDRAWITEMSTRUCT lpDrawItemStruct); protected: virtual void PreSubclassWindow (); //}} AFX_VIRTUAL

// Implementationpublic: BOOL Focus; LRESULT afx_msg OnMouseLeave (WPARAM wparam, LPARAM lparam); LRESULT afx_msg OnMouseHover (WPARAM wparam, LPARAM lparam); void afx_msg OnMouseMove (UINT nFlags, CPoint point); void DrawInsideBorder (CDC * pDC, CRect rect) Void Dogradientfill (CDC * PDC, CRECT); Virtual ~ cxpbutton ();

// generated message mapp functionsprotected: // {AFX_MSG (cxpbutton) // Note - The classwizard will address and remove member functions here. //}} AFX_MSG

DECLARE_MESSAGE_MAP ()};

// xpbutton.cpp: importation file //

#include "stdafx.h" #include "xpbutton.h"

#ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF

/// cxpbutton

Implement_dyncreate (cxpbutton, cbutton) //// CMEMDC - Memory DC //////////// CopyRight 1996-1999, Keith Rule /// MayFreely Use or modify This Code Provided this // Copyright Is Included in All Derived Versions.//// History - 10/3/97 Fixed Scrolling Bug.// Added Print Support. - KR / / / 11/3/99 Fixed Most Common Complaint Added // Background Color Fill. - KR //// 11/3/99 Added Support for mapping modes other Than // mm_text as suggested by Lee Sang Hun. - KR / / / t Class Implements A Memory Device Context Which allows // flicker free drawing.class CMemDC: public CDC {private: CBitmap m_bitmap; // Offscreen bitmap CBitmap * m_oldBitmap; // bitmap originally found in CMemDC CDC * m_pDC; // Saves CDC passed in constructor CRect m_rect; // Rectangle Of Drawing Area. Bool M_BMEMDC; // True IF CDC Really Is A Memory DC .public: cmemdc (CDC * PDC, const crect * prect = null): CDC () {assert (PDC! = null);

// some initialization m_pdc = pdc; m_oldbitmap = null; m_bmemdc =! Pdc-> isprinting ();

// Get the Rectangle to DRAW IF (prect == NULL) {PDC-> getClipbox (& m_Rect);} else {m_rect = * prect;}} (m_bmemdc) {// Create A Memory DC CreateCompatiPLEDC (PDC); PDC- > LPTODP (& M_RECT);

m_bitmap.CreateCompatibleBitmap (pDC, m_rect.Width (), m_rect.Height ()); m_oldBitmap = SelectObject (& m_bitmap); SetMapMode (pDC-> GetMapMode ()); pDC-> DPtoLP (& m_rect); SetWindowOrg (m_rect.left, m_rect.top);} else {// Make a copy of the relevent parts of the current DC for printing m_bPrinting = pDC-> m_bPrinting; m_hDC = pDC-> m_hDC; m_hAttribDC = pDC-> m_hAttribDC;} // Fill background FillSolidRect (M_Rect, PDC-> getBkcolor ());

~ Cmemdc () {if (m_bmemdc) {// copy the offscreen bitmap ONTO the screen. M_pdc-> bitblt (m_rect.width (), m_rect.width (), m_rect.height (), M_Rect.Left , m_rect.top, SRCCOPY);. // Swap back the original bitmap SelectObject (m_oldBitmap);} else {// All we need to do is replace the DC with an illegal value, // this keeps us from accidently deleting the handles Associated with // the cdc That Was Passed to the constructor. m_hdc = m_hattribdc = null;}} // allow usage as a pointer cmemdc * Operator -> () {Return this;}

// allow usage as a pointer operator cmemdc * () {returnid;}};

CXPButton :: CXPButton () {pBoundryPen.CreatePen (PS_INSIDEFRAME | PS_SOLID, 1, RGB (0,0,0)); pInsideBoundryPenLeft.CreatePen (PS_INSIDEFRAME | PS_SOLID, 3, RGB (250,196,88)); pInsideBoundryPenRight.CreatePen (PS_INSIDEFRAME | PS_SOLID, 3, RGB (251,202,106)); pInsideBoundryPenTop.CreatePen (PS_INSIDEFRAME | PS_SOLID, 2, RGB (252,210,121)); pInsideBoundryPenBottom.CreatePen (PS_INSIDEFRAME | PS_SOLID, 2, RGB (229,151,0)); pFillActive.CreateSolidBrush (RGB (222,223,236)); pFillInactive.CreateSolidBrush (RGB (222,223,236)); pInsideBoundryPenLeftSel.CreatePen (PS_INSIDEFRAME | PS_SOLID, 3, RGB (153,198,252)); pInsideBoundryPenTopSel.CreatePen (PS_INSIDEFRAME | PS_SOLID, 2, RGB (162,201,255)); pInsideBoundryPenRightSel.CreatePen (PS_INSIDEFRAME | PS_SOLID, 3, RGB (162,189,252)); pInsideBoundryPenBottomSel.CreatePen (PS_INSIDEFRAME | PS_SOLID, 2, RGB (162,201,255)); m_bOverControl = m_bTracking = m_bSelected = Focus = 0;} CXPButton :: ~ CXPButton () {// Todo: Cleanup Your Control's Instance Data Here. PBoundrypen.deleteObject (); Pfillactive.Deleteo bject (); pFillInactive.DeleteObject (); pInsideBoundryPenLeft.DeleteObject (); pInsideBoundryPenRight.DeleteObject (); pInsideBoundryPenBottom.DeleteObject (); pInsideBoundryPenTop.DeleteObject (); pInsideBoundryPenLeftSel.DeleteObject (); pInsideBoundryPenTopSel.DeleteObject (); pInsideBoundryPenRightSel.DeleteObject ( PinsideBoundryPenbottomsel.deleteObject ();

BEGIN_MESSAGE_MAP (CXPButton, CButton) // {{AFX_MSG_MAP (CXPButton) // NOTE - the ClassWizard will add and remove mapping macros here.//}}AFX_MSG_MAPON_WM_MOUSEMOVE()ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave) ON_MESSAGE (WM_MOUSEHOVER, OnMouseHover) END_MESSAGE_MAP ()

/// CXPButton Message Handlers

Void CXPButton :: DOGRADIENTFILL (CDC * PDC, CRECT) {CBRUSH PBRUSH [64]; INT NWIDTH = (Rect.right) - (Rect.TOP) - (Rect.Top) CRECT RCT; For (int i = 0; i <64; i ) {if (m_bovercontrol) {if (focus) {PBRUSH [i] .createsolidbrush (RGB (255- (I / 4), 255- (I / 4 ), 255- (I / 3)));} else {pbrush [i] .createsolidbrush (RGB (255- (I / 4), 255- (I / 4), 255- (I / 5))) }}} Else {if (focus) {pbrush [i] .createsolidbrush (RGB (255- (I / 3), 255- (I / 3), 255- (I / 4))))))));} else {PBRUSH [i ] .Createsolidbrush (RGB (255- (I / 3), 255- (I / 3), 255- (I / 5)))));}}} for (i = Ret.top; i FillRect (& RCT, & PBRUSH [(i * 63) / nHEight)]);} for (i = 0; i <64; i ) {pBrush [i] .DeleteObject ();}} void CXPButton :: DrawItem (lPDRAWITEMSTRUCT lpDrawItemStruct) {// TODO: Add your code to draw the specified item CDC * ppDC = CDC :: FromHandle (lpDrawItemStruct- > HDC); CMEMDC PDC (PPDC); CREC T Rect = lpdrawitemstruct-> rcitem; uint state = lpdrawItemstruct-> itemState; cpoint pt; cstract strText; cstract & m_title = strText;

GetWindowText; // Draw the Control EDGES (DrawFrameControl IS Handy!) Pt.x = 10; Pt.y = 10; PoldPen = PDC-> SelectObject (& PBoundrypen); PDC-> RoundRect (Rect, Pt); if (state & ODS_FOCUS) {Focus = TRUE; m_bSelected = TRUE;} else {m_bSelected = FALSE; Focus = FALSE;} if (state & ODS_SELECTED || state & ODS_DEFAULT) {// pDC-> DrawFrameControl (rect, DFC_BUTTON, DFCS_BUTTONPUSH | DFCS_PUSHED); Focus = TRUE;} else {// pDC-> DrawFrameControl (rect, DFC_BUTTON, DFCS_BUTTONPUSH);} pDC-> SelectObject (pOldPen); // Deflate the drawing rect by the size of the button's edges rect.DeflateRect (CSize (GetSystemMetrics (SM_CXEDGE), GetSystemMetrics (SM_CYEDGE))); // Fill the interior color if necessary if (m_bOverControl) {pOldBrush = pDC-> SelectObject (& pFillActive); //pDC->FloodFill(rect.left 5 , Rect.top 5, RGB (0, 0)); // PDC-> FillsolidRect (Rect, RGB (255, 255, 0)); // Yellow DogradientFill (PDC, Rect);} else {Poldbrush = PDC-> SELECTOBJECT (& Pfilli NACTIVE); //pdc->floodfill (Rect.Left 5 ,Rect.top 5, RGB (0, 0)); // PDC-> FillsolidRect (Rect, RGB (0, 0, 255)))) ; // yellow DoGradientFill (pDC, rect);} if (m_bSelected || m_bOverControl) {DrawInsideBorder (pDC, rect);} pDC-> SelectObject (pOldBrush); // Draw the text if (m_title.IsEmpty () /! *! strText.isempty () * /) {csize eXtent = PDC-> getTextExtent (m_title / * strText * /); cpoint pt (rect.centerpoint (). x - extent.cx/2, rect.centerpoint (). Y - extent.cy/2); if (state & ods_selected) pt.offset (1, 1); int nmode = pdc-> setBkmode (transparent);

// pOldPen = pDC-> SelectObject (pBoundryPen); // CFont * pOldFont = SelectStockFont (pDC); if (state & ODS_DISABLED) pDC-> DrawState (pt, Extent, m_title / * strCaption * /, DSS_DISABLED, TRUE, 0 (Hbrush) null; ELSE PDC-> Textout (pt.x, pt.y, m_title / * strcaption * /); // PDC-> SelectObject (PoldPen); // PDC-> SelectObject (PoldFont); PDC -> SetBkMode (nMode);} return;} void CXPButton :: PreSubclassWindow () {// TODO: Add your specialized code here and / or call the base class ModifyStyle (0, BS_OWNERDRAW); CButton :: PreSubclassWindow ();}

void CXPButton :: DrawInsideBorder (CDC * pDC, CRect rect) {CPen * left, * right, * top, * bottom; if (m_bSelected && m_bOverControl!) {left = & pInsideBoundryPenLeftSel; top = & pInsideBoundryPenTopSel; right = & pInsideBoundryPenRightSel; bottom = & pInsideBoundryPenBottomSel } Else {left = & pinSideBoundrypenLeft; TOP = & PinsideBoundrypenTop; Right = & PinsideBoundrypenright; bottom = & pinsideboundrypenbottom;

Cpoint OldPoint = PDC-> MoveTo (Rect.Left, Rect.Bottom-3); Poldpen = PDC-> SelectObject (left); PDC-> Lineto (Rect.Left, Rect.top 2); PDC-> SelectObject Right); PDC-> Moveto (Rect.right-1, Rect.Bottom-3); PDC-> Lineto (Rect.right-1, Rect.top 2); PDC-> SelectObject (TOP); PDC-> MoveTo (Rect.Left 2, Rect.top); PDC-> Lineto (Rect.right-2, Rect.top); PDC-> SelectObject (Bottom); PDC-> MoveTo (Rect.Left 2, RECT. Bottom); PDC-> Lineto (Rect.right-2, Rect.Bottom); PDC-> SelectObject (PoldPen); PDC-> MoveTo (OldPoint);

void CXPButton :: OnMouseMove (UINT nFlags, CPoint point) {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 :: OnMouseHover (wPARAM wparam, lPARAM lparam) {m_bOverControl = TRUE; Invalidate (); return 1;}

Lresult CXPButton :: OnMouseLeave (WPARAM WPARAM, LPARAM LPARAM) {m_btracking = false; m_boverControl = false; invalidate (false); return 0;}

转载请注明原文地址:https://www.9cbs.com/read-130845.html

New Post(0)