Homemade extension button)

Source: Internet
Author: User
Tags drawtext
  • In the IDE environment of vc6, a dialog box-based project is generated.
  • Open the properties page of the button in the dialog box resource, and select the "owner draw" attribute of the button on the "style" tab.
  • Introduce the cursor to the application's resources.
  • Use classwizard and cbutton as the base class to derive a new class: clinkbutton.
  • In a derived class, overload the virtual functions of the base class cbutton:
          virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)     

    This function is reloaded because the "owner draw" attribute of the button is selected. When the visible behavior of the button changes, the application framework needs to call this function to redraw the button.

  • Customize the following message processing:
         afx_msg void OnMouseMove(UINT nFlags, CPoint point);     afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);     afx_msg void OnTimer(UINT nIDEvent);     afx_msg void OnLButtonUp(UINT nFlags, CPoint point);     afx_msg void OnLButtonDown(UINT nFlags, CPoint point);     afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);     afx_msg BOOL OnEraseBkgnd(CDC* pDC);
  • Declaration class member variable definition:
    // Define the font variable cfont funderline; // define the cursor variable hcursor hhand; // determine whether to press the bool blbtndown button; // determine whether the mouse is on the bool bhighlight button;

Implementation of the derived class clinkbutton:

1. Reload the function drawitem (lpdrawitemstruct ).

Void clinkbutton: drawitem (lpdrawitemstruct) {// obtain a CDC pointer * PDC = CDC: fromhandle (lpdrawitemstruct-> HDC ); // define the button area and initialize crect rect (lpdrawitemstruct-> rcitem); // set the background mode colorref OC = PDC-> gettextcolor (); int iobk = PDC-> setbkmode (transparent); // initialization button status uint state = lpdrawitemstruct-> itemstate; cfont * poldfont = NULL; int iyoffset = 0, ixoffset = 0; cstring strtext; getwindowtext (strtext); rect. top + = iyoffset; rect. left + = ixoffset; If (State & ods_disabled) {// The button is dimmed (disabled) cbrush graybrush; graybrush. createsolidbrush (getsyscolor (color_graytext); csize SZ = PDC-> gettextextent (strtext); int x = rect. left + (rect. width ()-Sz. CX)/2; int y = rect. top + (rect. height ()-Sz. cy)/2; rect. top + = 2; rect. left + = 2; PDC-> settextcolor (getsyscolor (color_3dhighlight); PDC-> drawtext (strtext, rect, dt_center | dt_vcenter | dt_singleline); rect. top-= 2; rect. left-= 2; PDC-> settextcolor (getsyscolor (color_graytext); PDC-> drawtext (strtext, rect, dt_center | dt_vcenter | dt_singleline);} else {If (bhighlight) // cursor on the button {If (State & ods_selected) // press the button PDC-> draw3drect (rect, getsyscolor (color_3dshadow), getsyscolor (color_3dhilight )); else // unpressed button PDC-> draw3drect (rect, getsyscolor (color_3dhilight), getsyscolor (color_3dshadow); // font color PDC-> settextcolor (RGB (0, 0, 255); // Add a line (Other fonts can also be used) if (funderline. getsafehandle () = NULL) {cfont * pfont = getfont (); Assert (pfont); logfont lf; pfont-> getlogfont (& lf); LF. lfunderline = true; funderline. createfontindirect (& lf);} poldfont = PDC-> SelectObject (& funderline);} else PDC-> settextcolor (getsyscolor (color_btntext); PDC-> drawtext (strtext, rect, dt_center | dt_vcenter | dt_singleline); If (poldfont) PDC-> SelectObject (poldfont );}}

2. Custom message processing functions

Void clinkbutton: onmousemove (uint nflags, cpoint point) {// set a timer settimer (1, 10, null); cbutton: onmousemove (nflags, point );}

When you move the cursor over the button, execute this function. The timer sends a wm_timer message to the message queue. The ontimer (uint nidevent) function processes the message.

void CLinkButton::OnTimer(UINT nIDEvent) {static bool pPainted = false;POINT pt;GetCursorPos(&pt);CRect rect;GetWindowRect (rect);if (bLBtnDown){KillTimer (1);if (pPainted) InvalidateRect (NULL);pPainted = FALSE;return;}if (!rect.PtInRect (pt)){bHighlight = false;KillTimer (1);if (pPainted)InvalidateRect(NULL);pPainted = false;return;}else{bHighlight = true;if (!pPainted){pPainted = true;InvalidateRect(NULL);}}CButton::OnTimer(nIDEvent);}BOOL CLinkButton::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) {if (bHighlight) {::SetCursor(hHand);return true;}return CButton::OnSetCursor(pWnd, nHitTest, message);}int CLinkButton::OnCreate(LPCREATESTRUCT lpCreateStruct) {if (CButton::OnCreate(lpCreateStruct) == -1)return -1;CFont * pFont = GetFont();ASSERT(pFont);LOGFONT lf;pFont->GetLogFont(&lf);lf.lfUnderline = TRUE;fUnderline.CreateFontIndirect(&lf);return 0;}      

This function is automatically called by the framework before the button is displayed. I will initialize the font displayed on the button here.

void CLinkButton::OnLButtonUp(UINT nFlags, CPoint point) {bLBtnDown = false;if (bHighlight){bHighlight = false;InvalidateRect(NULL);}CButton::OnLButtonUp(nFlags, point);}      

Call this function when you press the button again.

void CLinkButton::OnLButtonDown(UINT nFlags, CPoint point) {bLBtnDown = true;CButton::OnLButtonDown(nFlags, point);}      

Call this function when you press the button.

BOOL CLinkButton::OnEraseBkgnd(CDC* pDC) {COLORREF cr = GetSysColor(COLOR_3DFACE);int r = GetRValue(cr);int g = GetGValue(cr);int b = GetBValue(cr);if (r > 1) r -= 2;if (g > 1) g -= 2;if (r < 3 && g < 3 && b < 253) b += 2;COLORREF cr1 = RGB(r,g,b);CRect rc;GetClientRect(rc);pDC->FillSolidRect(rc, cr1);return CButton::OnEraseBkgnd(pDC);}     

The Application Framework calls this function when the button background needs to be re-painted.
Original link http://www.vckbase.com/document/viewdoc? Id = 559

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.