Inherit CWnd self-portrait button

Source: Internet
Author: User

Header file:

Header file #pragma once//clhsbutton#define mywm_btn_click wm_user+3001//Close button Click Response//tab button status enum buttonstate{Btn_stat E_nor = 0, Btn_state_down = 1,};class clhsbutton:public cwnd{declare_dynamic (Clhsbutton) pub    Lic:clhsbutton ();     Virtual ~clhsbutton (); BOOL Create (cwnd* pparent,crect rc,cstring text,dword id = 0,dword style = ws_visible|      WS_CHILD);      Declare_message_map () public:protected:CString szclassname;        BOOL M_ismousehover;        Whether the mouse is suspended bool m_ismouseclicked;        Whether the mouse clicks CString m_strshowtext;            The text to be displayed image* M_pimgnor;            Normal when the picture image* M_pimghot;            Mouse hover when the picture image* M_pimgdown;    Click the picture when pressed void postclickevent ();    afx_msg int OnCreate (lpcreatestruct lpcreatestruct);      afx_msg void OnMouseMove (UINT nflags, CPoint point);      afx_msg void Onmousehover (UINT nflags, CPoint point); Afx_msg void OnMouseLeave ();      afx_msg void OnLButtonDown (UINT nflags, CPoint point);      afx_msg void OnLButtonUp (UINT nflags, CPoint point);      afx_msg BOOL OnEraseBkgnd (cdc* PDC);      afx_msg void OnPaint ();        afx_msg void OnSize (UINT nType, int cx, int cy); Virtual BOOL PreTranslateMessage (msg* pMsg);p ublic:void settabstate (buttonstate state) {m_btnstate = state;    Invalidate ();}    Set tab State ButtonState Gettabstate () {return m_btnstate;}    void Settooltiptext (CString sptext, BOOL bActivate = TRUE);        void Loadbtnimg (LPCTSTR ttype, uint nnorid, uint nhotid, uint ndownid);                            Load button picture Private:buttonstate m_btnstate;    The State of the tab ctooltipctrl* M_ptooltip; CString m_tooltext;public:afx_msg hbrush OnCtlColor (cdc* PDC, cwnd* pWnd, UINT nctlcolor);};/ /source File//CLhsButton.cpp: Implement File//#include "stdafx.h" #include ". /lander_mini.h "#include" memdc.h "#include". /utility.h "#include" LhsButton.h "//Clhsbuttonimplement_dynamic (ClhsbuttOn, CWnd) Clhsbutton::clhsbutton () {m_ismousehover = false;      m_ismouseclicked = false;     Register Control class Szclassname = AfxRegisterWndClass (0);    M_pimgnor = NULL;    M_pimghot = NULL;    M_pimgdown = NULL;    M_btnstate = Btn_state_nor;    M_ptooltip = NULL;    }clhsbutton::~clhsbutton () {safe_release (m_ptooltip);    Safe_release (M_pimgnor);    Safe_release (M_pimghot); Safe_release (M_pimgdown);}       Begin_message_map (Clhsbutton, CWnd) on_wm_mousemove () On_wm_mousehover ()//This message system will not send us on_wm_mouseleave () On_wm_lbuttondown () On_wm_lbuttonup () On_wm_paint () on_wm_size () On_wm_erasebkgnd () On_wm_cre ATE () On_wm_ctlcolor () End_message_map ()//Clhsbutton Message handler bool Clhsbutton::create (cwnd* pparent,crect rc,CString t Ext,dword ID/* = 0 */,dword Style/* = ws_visible|    Ws_child */) {//Dynamically create control BOOL ret = Cwnd::createex (0, Szclassname, text, style, RC, pparent, id); return ret?  True:false; } int Clhsbutton::oncreatE (lpcreatestruct lpcreatestruct) {//::setwindowlong (m_hwnd, Gwl_exstyle, GetWindowLong (m_hWnd, GWL_EXSTYLE) ^ WS_EX        _layered);    ModifyStyle (ws_clipchildren, 0);    ModifyStyle (0, Ws_clipchildren, 0); return 0;}      void Clhsbutton::P ostclickevent () {///This function is used to send a click message to the parent window cwnd* parent = GetParent ();      if (parent) parent->sendmessage (Mywm_btn_click, 0,0);  } void Clhsbutton::onmousehover (UINT nflags, CPoint point) {//mouse enters Invalidate ();          } void Clhsbutton::onmousemove (UINT nflags, CPoint point) {//handles only when the mouse first enters the case if (!m_ismousehover) {          M_ismousehover = true; TrackMouseEvent evt = {sizeof (EVT), tme_leave|          Tme_hover, m_hwnd, 0};          TrackMouseEvent (&EVT);      Onmousehover (0,cpoint ());      }}void Clhsbutton::onmouseleave () {//mouse left m_ismousehover = false;      m_ismouseclicked = false;  Invalidate (); } void Clhsbutton::onlbuttondown (UINT nflags, CPoint point) {//Mouse Press m_ismouseclicked = true;  Invalidate (); } void Clhsbutton::onlbuttonup (UINT nflags, CPoint point) {//Mouse release if (m_ismouseclicked) {m_ism          ouseclicked = false;         Invalidate ();      Postclickevent ();    }} BOOL Clhsbutton::onerasebkgnd (cdc* PDC) {//return CWND::ONERASEBKGND (PDC);    return TRUE;       Prevents the erase background from blinking} void Clhsbutton::onpaint () {CPAINTDC DC (this);      CRect RC;         GetClientRect (&RC);    Dual cache is used to prevent flicker CMemDC MEMDC (&dc,&rc,true);    Graphics graphic (MEMDC);          if (!m_pimgnor | |!m_pimghot | |!m_pimgdown) {//Do not provide button picture to brush under background//brush background COLORREF Bkgnd = RGB (100,0,0);              if (m_ismousehover) {if (m_ismouseclicked) Bkgnd = RGB (250,0,0);          else Bkgnd = RGB (180,0,0); } MEMDC.      Fillsolidrect (&RC,BKGND); } if (m_ismouseclicked | | m_btnstate = = btn_state_down) {//Click on a certain picture click state        Graphic.    DrawImage (m_pimgdown, 0, 0, m_pimgdown->getwidth (), M_pimgdown->getheight ()); } else if (M_ismousehover &&!m_ismouseclicked) {//suspension, but no click graphic.            DrawImage (m_pimghot, 0, 0, m_pimghot->getwidth (), M_pimghot->getheight ()); } else {graphic.        DrawImage (m_pimgnor, 0, 0, m_pimgnor->getwidth (), M_pimgnor->getheight ()); }//Make drawing effective graphic.                RELEASEHDC (MEMDC);         } BOOL Clhsbutton::P retranslatemessage (msg* pMsg) {if (M_ptooltip) {if (:: IsWindow (M_ptooltip->m_hwnd))                {m_ptooltip->relayevent (PMSG); }} return CWnd::P retranslatemessage (PMSG);}    void Clhsbutton::onsize (UINT nType, int cx, int cy) {}void clhsbutton::settooltiptext (CString sptext, BOOL bActivate) {        if (M_ptooltip = = NULL) {m_ptooltip = new CToolTipCtrl;        Create ToolTip Control m_ptooltip->create (this); M_ptooltip->actiVate (TRUE);    } m_tooltext = Sptext;         If there is no ToolTip defined then add it if (m_ptooltip->gettoolcount () = = 0) {CRect rectbtn;        GetClientRect (RECTBTN);    M_ptooltip->addtool (This, M_tooltext, RECTBTN, 1);    }//Set text for ToolTip m_ptooltip->updatetiptext (M_tooltext, this, 1);    M_ptooltip->setdelaytime (2000); M_ptooltip->activate (bActivate);} Hbrush Clhsbutton::onctlcolor (cdc* PDC, cwnd* pWnd, UINT nctlcolor) {hbrush HBR = Cwnd::onctlcolor (PDC, PWnd, nCtlColor    ); TODO: Change any properties of DC here//TODO: If the default is not the desired brush, return another brush return HBR;} Load button picture void Clhsbutton::loadbtnimg (LPCTSTR ttype, uint nnorid, uint nhotid, uint ndownid) {M_pimgnor = Cqyutility::loa    Dimgfromres (_t ("PNG"), Nnorid);    M_pimghot = Cqyutility::loadimgfromres (_t ("PNG"), Nhotid);    M_pimgdown = Cqyutility::loadimgfromres (_t ("PNG"), Ndownid); }


Memory DC

<pre name= "code" class= "CPP" > #ifndef _memdc_h_#define _memdc_h_/////////////////////////////////////////////        Cmemdc-memory Dc//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:void Build (cdc* PDC) {ASSERT (PDC! = NULL);        M_PDC = PDC;        M_oldbitmap = NULL;        M_BMEMDC =!pdc->isprinting ();            if (M_BMEMDC) {CreateCompatibleDC (PDC);            PDC-&GT;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 {m_bprinting = pdc->m_bprinting;            M_HDC = pdc->m_hdc;        M_HATTRIBDC = pdc->m_hattribdc;    } fillsolidrect (M_rect, Pdc->getbkcolor ());         } CMEMDC (cdc* PDC, const crect* prect = NULL, BOOL bBg = FALSE): 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;            } if (M_BMEMDC) {//Create a Memory DC CreateCompatibleDC (PDC);            PDC-&GT;LPTODP (&m_rect); M_bitmap. CreateCompatibleBitmap (PDC, M_rect. Width (), M_rect.            Height ());            M_oldbitmap = SelectObject (&m_bitmap);   Setmapmode (Pdc->getmapmode ());         SetWindowExt (Pdc->getwindowext ());            SetViewportExt (Pdc->getviewportext ());            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 if (bBg) BitBlt (M_rect.left, M_rect.top, M_rect. Width (), M_rect.        Height (), M_PDC, M_rect.left, M_rect.top, srccopy);    else Fillsolidrect (M_rect, Pdc->getbkcolor ());            } ~CMEMDC () {if (M_BMEMDC) {//Copy the offscreen bitmap onto the screen. M_pdc->bitblt (M_rect.left, M_rect.top, M_rect. Width (), M_rect.                                    Height (), this, M_rect.left, M_rect.top, srccopy); Swap back the original bitmap.           SelectObject (M_OLDBITMAP); } else {//All we need to do are replace the DC with a illegal value,//This keeps us from Acciden                        tly Deleting the handles associated with//the CDC is 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* () {return this;       } CMEMDC (hdc hdc): CDC () {cdc::fromhandle (hdc)->getclipbox (&m_rect);   Build (Cdc::fromhandle (HDC)); }}; #endif

Call Method:

Use the example rect rc = {0, 0, +-};    M_btnclose.loadbtnimg (_t ("PNG"), Idb_png_tab_close_nor, idb_png_tab_close_hot,idb_png_tab_close_hot);    M_btnclose.create (this, RC, L "", id_btn_mytab_close);            M_btnclose.settooltiptext (_t ("Off"));         

Source Text Address: http://blog.csdn.net/huasonl88/article/details/43226011

Inherit CWnd self-portrait button

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.