VC自绘控件框架

本文介绍了一个自定义按钮控件CYuButton的实现方法,包括按钮的状态绘制和响应事件处理,并展示了如何让CEdit控件中的文本内容垂直居中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是一个按钮自绘的框架,其他控件也类似

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//.h头文件
#pragma once
#include "afxwin.h"
#include "MemDC.h"//封装内存DC类
 
class CYuButton :public CWnd
{
    private:
                BOOL     m_bIsDown;
        CString  m_sCaption;
    public:
        DECLARE_DYNCREATE(CYuButton)
        CYuButton(void);
        virtual ~CYuButton(void);
 
        BOOL Create(LPCTSTR sCpation,DWORD dwStyle,CONST CRect & rt,
                    CWnd * pParendWnd,UINT uId);
         
        afx_msg void OnNcPaint();
        afx_msg BOOL OnEraseBkgnd(CDC* pDC);
        afx_msg void OnPaint();
        afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
        afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
        afx_msg void OnKillFocus(CWnd* pNewWnd);
        DECLARE_MESSAGE_MAP()
 
        //鼠标按下
        void DrawDown(CDC * pDC);
        void DrawNormal(CDC * pDC);
        //鼠标移动
        void DrawMove(CDC * pDC);
                 
        //获取当前程序路径
        CString  GetApplicationPath();
public:
        //修改按钮状态
        void SetNormalStatus();
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//.cpp文件
#include "StdAfx.h"
#include "YuButton.h"
 
CYuButton::CYuButton(void)
{
    //WNDCLASS wd={CS_VREDRAW|CS_HREDRAW,::DefWindowProc};
    //wd.lpszClassName = _T("YUButton"); 
}
 
CYuButton::~CYuButton(void)
{
}
 
BOOL CYuButton::Create(LPCTSTR sCaption,DWORD dwStyle,CONST CRect & rt,
                      CWnd * pParendWnd,UINT uId)
{  
        LPCTSTR lpszClassName = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, 
        AfxGetApp()->LoadStandardCursor(IDC_ARROW),
           (HBRUSH)GetStockObject(LTGRAY_BRUSH), NULL);
     
    m_sCaption = sCaption;
    return CWnd::Create(lpszClassName,sCaption,dwStyle|WS_CHILD|WS_VISIBLE,
               rt,pParendWnd,uId);
}
 
IMPLEMENT_DYNCREATE(CYuButton, CWnd)
BEGIN_MESSAGE_MAP(CYuButton, CWnd)
    ON_WM_ERASEBKGND()
    ON_WM_PAINT()
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONUP()
    ON_WM_KILLFOCUS()
END_MESSAGE_MAP()
 
void CYuButton::OnNcPaint()
{
 
}
 
BOOL CYuButton::OnEraseBkgnd(CDC* pDC)
{
    return true;//CWnd::OnEraseBkgnd(pDC);
}
 
void CYuButton::OnPaint()
{
    CPaintDC dc(this); // device context for painting
 
    if(m_bIsDown)
        DrawDown(&dc);
    else
        DrawNormal(&dc);
 
    //绘按钮上面的文字
    CRect rt;
    GetClientRect(&rt);
    rt.top = 70;
 
    dc.SetTextColor(RGB(255,255,255));
    
    CFont font;
    VERIFY(font.CreatePointFont(115,_T("微软雅黑"), &dc));
 
    dc.SelectObject(&font);
    dc.SetBkMode(TRANSPARENT);
    dc.DrawText(m_sCaption,rt,DT_CENTER|DT_SINGLELINE);
}
 
void CYuButton::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    m_bIsDown = TRUE;
    this->SetFocus();
    this->Invalidate(FALSE);
     
    CWnd * pWnd = this->GetParent();
    if(pWnd)
        pWnd->SendMessage(WM_COMMAND,GetDlgCtrlID(),(LPARAM)this->GetSafeHwnd());
 
    CWnd::OnLButtonDown(nFlags, point);
}
 
void CYuButton::OnLButtonUp(UINT nFlags, CPoint point)
{
    CWnd::OnLButtonUp(nFlags, point);
}
 
void CYuButton::DrawNormal(CDC * pDC)
{
    pDC->BitBlt(0,0,m_focus_MDC.Width(),m_focus_MDC.Height(),
                &m_CompoundDC,0,0,SRCCOPY);
}
 
void CYuButton::DrawDown(CDC * pDC)
{
 
}
 
void CYuButton::OnKillFocus(CWnd* pNewWnd)
{
    CWnd::OnKillFocus(pNewWnd);
    Invalidate(TRUE);
}
 
CString CYuButton::GetApplicationPath()
{
    WCHAR  buff[255]={0};
    ::GetModuleFileName(0,buff,255);
 
    CString strAppFullName;
    strAppFullName.Format(_T("%s"),buff);
 
    CString strAppPath = _T("");
    strAppPath = strAppFullName.Left(strAppFullName.ReverseFind('\\')+1);
    return strAppPath;
}
 
void CYuButton::SetNormalStatus()
{
    m_bIsDown = FALSE;
    Invalidate(FALSE);
}

如果继承自CEdit类,希望文字内容垂直居中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//创建时的风格,改为多行风格
CEdit::Create(WS_VISIBLE| ES_LEFT| WS_CHILD| ES_MULTILINE, rect, pParentWnd, nID);
//通知消息处理如下:
BOOL CYUEdit::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam, 
     LRESULT* pLResult)
{
    if (message != WM_CTLCOLOREDIT)
    {
        return CEdit::OnChildNotify(message, wParam, lParam, pLResult);
    }
 
    HDC hdcChild = (HDC)wParam;
    SetTextColor(hdcChild, m_colFontColor);
    SetBkColor(hdcChild, m_colBackGround);
 
    CRect rt1;
    GetClientRect(&rt1);
     
    rt1.DeflateRect(0, 0, 0, 0);
    SetRectNP(&rt1);
 
    SetFont(&m_font,FALSE);
 
    return TRUE;
    //return CEdit::OnChildNotify(message, wParam, lParam, pLResult);
}















本文转自Chinayu201451CTO博客,原文链接:http://blog.51cto.com/9233403/1981225 ,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值