码迷,mamicode.com
首页 > 其他好文 > 详细

编辑框等控件边框美化(继承CEdit,然后覆盖OnMouseLeave, OnSetFocus, OnPaint函数即可。原来的CEdit虽然代码不可见,但它也是有句柄的,照样随便画)

时间:2018-02-21 10:42:12      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:std   tail   alt   gets   draw   bsp   sage   wm_paint   als   

源码说明:美化能获取焦点控件的边框颜色,获取焦点后颜色不同(类似彗星小助手、QQ等软件),支持自定义颜色,支持单独设置各个控件颜色.
实现方法:子类化,在WM_NCPAINT、WM_PAINT等消息自己画出边框
开发环境:VS2005 MFC / 易语言
源码下载:http://download.csdn.net/detail/cometnet/5663673

技术分享图片

技术分享图片

 

[cpp] view plain copy
 
    1. // SkinEdit.cpp : 实现文件  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5. #include "SkinEdit.h"  
    6.   
    7.   
    8. // CSkinEdit  
    9.   
    10. IMPLEMENT_DYNAMIC(CSkinEdit, CEdit)  
    11.   
    12. CSkinEdit::CSkinEdit()  
    13. {  
    14.     m_bTracking=m_bHover=m_bFocus=FALSE;  
    15.     m_ClientBrush=::CreateSolidBrush(GetSysColor(COLOR_BTNFACE));  
    16.     m_hOrderBrush=::CreateSolidBrush(GetSysColor(COLOR_WINDOWFRAME));  
    17.     m_HotOrderBrush1=::CreateSolidBrush(0xFDC860);  
    18.     m_HotOrderBrush2=::CreateSolidBrush(0xE7AC49);  
    19. }  
    20.   
    21. CSkinEdit::~CSkinEdit()  
    22. {  
    23.     DeleteObject(m_ClientBrush);  
    24.     DeleteObject(m_hOrderBrush);  
    25.     DeleteObject(m_HotOrderBrush1);  
    26.     DeleteObject(m_HotOrderBrush2);  
    27. }  
    28.   
    29.   
    30. BEGIN_MESSAGE_MAP(CSkinEdit, CEdit)  
    31.     ON_WM_NCPAINT()  
    32.     ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)  
    33.     ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)  
    34.     ON_WM_MOUSEMOVE()  
    35.     ON_WM_SETFOCUS()  
    36.     ON_WM_KILLFOCUS()  
    37. END_MESSAGE_MAP()  
    38.   
    39.   
    40.   
    41. // CSkinEdit 消息处理程序  
    42. void CSkinEdit::OnMouseMove(UINT nFlags, CPoint point)  
    43. {  
    44.     if (!m_bTracking)  
    45.     {  
    46.         TRACKMOUSEEVENT tme;  
    47.         tme.cbSize = sizeof(tme);  
    48.         tme.hwndTrack = m_hWnd;  
    49.         tme.dwFlags = TME_LEAVE | TME_HOVER;  
    50.         tme.dwHoverTime = 50;  
    51.         m_bTracking = (bool)_TrackMouseEvent(&tme);  
    52.     }  
    53.   
    54.     CEdit::OnMouseMove(nFlags, point);  
    55. }  
    56. LRESULT CSkinEdit::OnMouseLeave(WPARAM wParam, LPARAM lParam)  
    57. {  
    58.     m_bTracking = FALSE;  
    59.     m_bHover=FALSE;  
    60.     DrawBorder();  
    61.     return 0;  
    62. }  
    63.   
    64. LRESULT CSkinEdit::OnMouseHover(WPARAM wParam, LPARAM lParam)  
    65. {  
    66.     m_bHover=TRUE;  
    67.     DrawBorder();  
    68.     return 0;  
    69. }  
    70. void CSkinEdit::OnSetFocus(CWnd* pOldWnd)  
    71. {  
    72.     CEdit::OnSetFocus(pOldWnd);  
    73.     m_bFocus=TRUE;  
    74.     DrawBorder();  
    75. }  
    76.   
    77. void CSkinEdit::OnKillFocus(CWnd* pNewWnd)  
    78. {  
    79.     CEdit::OnKillFocus(pNewWnd);  
    80.     m_bFocus=FALSE;  
    81.     DrawBorder();  
    82. }  
    83. void CSkinEdit::OnNcPaint()  
    84. {  
    85.     CEdit::OnNcPaint();  
    86.     DrawBorder();  
    87. }  
    88. void CSkinEdit::OnPaint()  
    89. {  
    90.     CEdit::OnPaint();  
    91.     DrawBorder();  
    92. }  
    93.   
    94. void CSkinEdit::DrawBorder()  
    95. {  
    96.     HDC hDC=::GetWindowDC(m_hWnd);  
    97.     CRect rc;  
    98.     ::GetWindowRect(m_hWnd,&rc);  
    99.     rc=CRect(0,0,rc.right-rc.left,rc.bottom-rc.top);  
    100.     if(m_bHover){  
    101.         ::FrameRect(hDC,&rc,m_HotOrderBrush1);  
    102.         rc.InflateRect(-1,-1);  
    103.         ::FrameRect(hDC,&rc,m_HotOrderBrush2);  
    104.     }else if(m_bFocus){  
    105.         ::FrameRect(hDC,&rc,m_ClientBrush);  
    106.         rc.InflateRect(-1,-1);  
    107.         ::FrameRect(hDC,&rc,m_HotOrderBrush2);  
    108.     }else{  
    109.         ::FrameRect(hDC,&rc,m_ClientBrush);  
    110.         rc.InflateRect(-1,-1);  
    111.         ::FrameRect(hDC,&rc,m_hOrderBrush);  
    112.     }  
    113.     ::ReleaseDC(m_hWnd,hDC);  
    114. }  

http://blog.csdn.net/CometNet/article/details/9194751

编辑框等控件边框美化(继承CEdit,然后覆盖OnMouseLeave, OnSetFocus, OnPaint函数即可。原来的CEdit虽然代码不可见,但它也是有句柄的,照样随便画)

标签:std   tail   alt   gets   draw   bsp   sage   wm_paint   als   

原文地址:https://www.cnblogs.com/findumars/p/8456143.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!