Self-painted translucent crystal button

Source: Internet
Author: User
Running Effect

Implementation Method

1. Add the bs_ownerdraw style to the button.
2. Reload the drawitem function. Draw the button here.
3. The key is to copy the background of the parent window to the button for visual transparency.
4. At last, use alphablend to achieve translucent.

Source code

// MyButton.h#pragma once// CMyButtonclass CMyButton : public CButton{DECLARE_DYNAMIC(CMyButton)public:CMyButton();virtual ~CMyButton();public:void SetBkColor(COLORREF color);void SetTextColor(COLORREF color);private:bool m_bOver;bool m_bDown;bool m_bDisable;bool m_bTracking;COLORREF m_bkColor;COLORREF m_textColor;protected:DECLARE_MESSAGE_MAP()virtual BOOL PreCreateWindow(CREATESTRUCT& cs);virtual void PreSubclassWindow();public:virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);afx_msg void OnMouseMove(UINT nFlags, CPoint point);afx_msg void OnLButtonDown(UINT nFlags, CPoint point);afx_msg void OnLButtonUp(UINT nFlags, CPoint point);afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);afx_msg void OnEnable(BOOL bEnable);private:void ButtonInit();void DrawButton();void DrawButton(HDC hDestDC);};

// Mybutton. CPP: implementation file // # include "stdafx. H "# include" alphabutton. H "# include" mybutton. H "# include" maindlg. H "// cmybuttonimplement_dynamic (cmybutton, cbutton) cmybutton: cmybutton () {m_bkcolor = 0 xffffff; m_textcolor = 0x000000;} cmybutton ::~ Cmybutton () {} invoke (cmybutton, cbutton) on_wm_mousemove () invoke () on_message (success, onmouseleave) on_message (wm_mousehover, onmousehover) invoke () end_message_map () // cmybutton message processing program void cmybutton: setbkcolor (colorref color) {m_bkcolor = color;} void cmybutton: settextcolor (colorref color) {m_textcolor = color;} bool cmybutton :: precreatewindow (createstruct & CS) {bool Bret = cbutton: precreatewindow (CS); buttoninit (); Return Bret;} void cmybutton: presubclasswindow () {cbutton: presubclasswindow (); buttoninit ();} void cmybutton: buttoninit () {m_btracking = false; m_bover = m_bdown = m_bdisable = false; m_bdisable = iswindowenabled ()? False: true; modifystyle (null, bs_ownerdraw);} void cmybutton: drawitem (lpdrawitemstruct finished) {drawbutton (success-> HDC);} void cmybutton: onmousemove (uint nflags, cpoint point) {If (! M_btracking) {m_bover = true; trackmouseevent tme; tme. cbsize = sizeof (TME); tme. hwndtrack = m_hwnd; tme. dwflags = tme_leave | tme_hover; tme. dwhovertime = 50; m_btracking = (bool) _ trackmouseevent (& TME);} cbutton: onmousemove (nflags, point);} void cmybutton: onlbuttondown (uint nflags, cpoint point) {m_bdown = true; cbutton: onlbuttondown (nflags, point);} void cmybutton: onlbuttonup (uint nflags, cpoint point) {m_ B Down = false; cbutton: onlbuttonup (nflags, point);} lresult cmybutton: onmouseleave (wparam, lparam) {m_bover = false; m_btracking = false; m_bdown = false; invalidaterect (null, false); Return 0;} lresult cmybutton: onmousehover (wparam, lparam) {m_bover = true; invalidaterect (null); Return 0;} void cmybutton:: drawbutton () {HDC =: getdc (m_hwnd); drawbutton (HDC);: releasedc (m_hwnd, HDC );} Void cmybutton: drawbutton (HDC hdestdc) {crect RC; getclientrect (RC); int nwindth = RC. width (); int nheight = RC. height (); HDC = createcompatibledc (hdestdc); // create compatible DC, draw HDC hmaskdc = createcompatibledc (hdestdc) using double buffering; hbitmap = histogram (hdestdc, nwindth, nheight); hbitmap hmaskbitmap = createcompatiblebitmap (hdestdc, nwindth, nheight); hbitmap holdbitmap = (hbitmap) SelectObject (HDC, hbitmap); hbitmap Holdmaskbitmap = (hbitmap) SelectObject (hmaskdc, hmaskbitmap); setbkmode (HDC, transparent); // copy the background image of the parent window to the DC of the button, visual transparency -------------- cmaindlg * pparent = (cmaindlg *) getparent (); cpoint Pt (); mapwindowpoints (pparent, & PT, 1); pparent-> m_bkimage.bitblt (HDC, RC, PT, srccopy); // optional int nalpha = 100; // 0 -- 255int noffset = 0; hbrush HBr = createsolidbrush (m_bkcolor); FI Llrect (hmaskdc, & rc, HBr); deleteobject (HBr); If (m_bdisable) {nalpha = 100;} else if (m_bdown) {nalpha = 180; noffset = 1 ;} else if (m_bover) {nalpha = 150;} else {nalpha = 100;} blendfunction blend; memset (& blend, 0, sizeof (blend); blend. blendop = ac_src_over; blend. sourceconstantalpha = nalpha; // transparency up to 255 hrgn = opacity (, nwindth, nheight,); selectcliprgn (HDC, hrgn); alphablend (HDC, nwindth, nheight, h Maskdc, 0, 0, nwindth, nheight, blend); cstring strtext; getwindowtext (strtext); If (strtext! = _ T ("") {RC. inflaterect (-2,-2); RC. offsetrect (noffset, noffset); hfont = (hfont) sendmessage (wm_getfont); If (! Hfont) hfont = (hfont) getstockobject (default_gui_font); hfont holdfont = (hfont) SelectObject (HDC, hfont);: settextcolor (HDC, m_textcolor);: drawtext (HDC, strtext,-1, & rc, dt_singleline | dt_center | dt_vcenter | dt_word_ellipsis);: SelectObject (HDC, holdfont);} selectcliprgn (HDC, null); deleteobject (hrgn ); // copy to the DC of the control ---------------------- bitblt (hdestdc, nwindth, nheight, HDC, srccopy); // Delete the resource and release the memory ---------------- ------- SelectObject (HDC, holdbitmap); deleteobject (hbitmap); deletedc (HDC); SelectObject (hmaskdc, success); deleteobject (SUCCESS); deletedc (SUCCESS);} void cmybutton :: onenable (bool benable) {cbutton: onenable (benable); m_bdisable = iswindowenabled ()? False: true; drawbutton ();}

Source code download: http://download.csdn.net/detail/cometnet/4955726

Related Articles: color selector for self-painted buttons

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.