Win32 自绘控件按钮类
今天学了控件的自绘,初步偿试了下,蹂躏的不行不行的,查了好多的资料,头都弄大了,
有好多还是没弄明白,只是初步实现一个按钮的基本功能,好难呀,
先看下效果:
按下状态
弹起状态
按钮2按下状态
按钮2弹起状态
因好多不明白,先把WM_PAIN消息中绘图代码先发上来,以后弄好了再说
子类实现了WM_PAIN,WM_LBUTTONDOWN,WM_LBUTTONUP,现在就实现了这三个消息
还有一个修改文本颜色SetTxtColor,和修改背景色SetBkColor,
LRESULT ChildButton::OnPaint(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); //创建新画笔 HPEN hNewPen = CreatePen(PS_SOLID, 2, RGB(0, 0, 0)); //将新画笔选入DC中 HPEN hOldPen = (HPEN)SelectObject(hdc, hNewPen); //设置文字背景为透明模式 SetBkMode(hdc, TRANSPARENT); //设置文本前景色(即文本颜色),m_TxtColr为成员变量,由外界传入 SetTextColor(hdc, m_TxtColor); //按钮上的文字颜色 //获得按钮窗口大小 RECT rect; GetClientRect(hWnd, &rect); //获得按钮窗口上的文本 TCHAR tchar[128] = { 0 }; GetWindowText(hWnd, tchar, 128); //按钮的两种样式,按下与弹起 if (m_IsBtDown == FALSE)//弹起样式 { //获取按钮背景颜色, COLORREF color = m_BkColor; HBRUSH hBrush1 = CreateSolidBrush(color); HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush1); //画按钮边框,弹起样式为下边和左边的边框能看见,另两边就移到窗口外了 //判断是否是圆角按钮, if (m_IsRng) { //是就画圆角矩形 RoundRect(hdc, -2, -2, rect.right - rect.left - 2, rect.bottom - rect.top - 2, 20, 20); } else { //不是就画矩形 Rectangle(hdc, -2, -2, rect.right - rect.left, rect.bottom - rect.top); } int n = DrawText(hdc, tchar, _tcslen(tchar), &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS); SelectObject(hdc, hOldBrush); DeleteObject(hBrush1); } else //按下状态样式 { //创建以背景色+5的画刷 COLORREF color = m_BkColor + 5; HBRUSH hBrush1 = CreateSolidBrush(color); HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush1); //绘制边框,按下样式为上边和右边为看得见的边框,另两边一样也移动窗口外 //判断是否是圆角形状 if (m_IsRng)//是圆角 { //画圆角矩形 RoundRect(hdc, 1, 1, rect.right - rect.left + 2, rect.bottom - rect.top + 2, 20, 20); } else { //不是就画直角矩形 Rectangle(hdc, 1, 1, rect.right - rect.left + 2, rect.bottom - rect.top + 2); } //文字矩形区域偏移,(这是向下偏移2个单位) OffsetRect(&rect, 0, 2); //绘制文本 DrawText(hdc, tchar, _tcslen(tchar), &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE | DT_END_ELLIPSIS); //释放画笔,画刷等资源 SelectObject(hdc, hOldBrush); DeleteObject(hBrush1); }
//释放画笔,画刷等资源
SelectObject(hdc, hOldPen);
DeleteObject(hNewPen);
EndPaint(hWnd, &ps); return LRESULT(); }
基类的声明:
class MyButton { public: MyButton(); public: BOOL CreateEx(const LPCTSTR lpClssName, LPRECT lpRect, HWND hParent, UINT uID); static LRESULT CALLBACK lpButtonProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam); LRESULT ButtonProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); void SetIsDown(BOOL bol); LRESULT DefaulMsg(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
virtual void SetRectRgn(int nWidth,int nHeight); //设置是否为圆角按钮 virtual void SetBkColor(COLORREF cor); //设置按钮背景色 virtual void SetTxtColor(COLORREF cor); //设置文本颜色 public: BOOL m_IsBtDown; BOOL m_IsRng; HWND m_hWnd; WNDPROC m_lpfnOldWndproc; RECT m_Rect; UINT m_ID; COLORREF m_BkColor; COLORREF m_TxtColor; public: //virtual LRESULT OnCreate(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); virtual LRESULT OnPaint(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); virtual LRESULT OnLButtonUp(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); virtual LRESULT OnLButtonDown(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); };
在父窗口中调用,我创建的是对话框为主窗口,所以创建子窗口在WM_INITDIALOG消息中,如是自己创建的窗口类,
就应放在WM_CREATE消息中创建子窗口
INT_PTR OnInitDialog(HWND hWnd, WPARAM wParam, LPARAM lParam) {
//我弄了好久,只能在创建前设置颜色,创建完了怎么也设置不了,
//不知道哪没弄对,
btn = new ChildButton(); btn->SetBkColor(RGB(0, 255, 0)); btn->SetTxtColor(RGB(255, 0, 0)); RECT rect = { 10,10,120,35 }; btn->CreateEx(TEXT("Button1"), &rect, hWnd, 10001); //btn->SetRectRgn(20,20); pBtn2 = new ChildButton(); RECT rect2 = { 10,50,120,75 }; pBtn2->CreateEx(TEXT("Button2"), &rect2, hWnd, 10002); pBtn2->SetRectRgn(20,20); return FALSE; }
签名:GreenLeaf1976