您的位置:首页 > 其它

截图功能实现(五)自绘控件实现(button combobox)

2013-05-30 20:34 531 查看
截图工具条上有许多不同的按钮,比如矩形,椭圆等如图

这里我采用了自绘Cbutton废话不多说直接上代码

PicButton.h

#pragma once

// PicButton
class PicButton : public CButton
{
DECLARE_DYNAMIC(PicButton)

public:
PicButton();
virtual ~PicButton();
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
BOOL CreateBtn(const CPoint &pt, CWnd* pParentWnd, UINT nID, CBitmap*pBmp, int nbtnStlye = 0);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
void SetbuttonStlye(const BOOL bStlye)
{
if (bStlye)
{
m_nBtnStatus = BTNSTATUS_DOWN;
}
else
{
m_nBtnStatus = BTNSTATUS_NOMAL;
}
Invalidate(FALSE);
}

int GetButtonStatus() const
{
return m_nBtnStatus;
}
enum//按钮三态
{
BTNSTATUS_NOMAL = 0,
BTNSTATUS_OVER,
BTNSTATUS_DOWN
};
enum//按钮类型
{
NOMAL = 0,
INVALID,
SAVEDOWN
};
protected:
DECLARE_MESSAGE_MAP()
private:
int    m_nBtnStatus;
int    m_nBtnStyle;
CImageList m_ImageList;
public:
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};


PicButton.cpp

/********************************************************
/*
/* 文件名称:PicButton.cpp
/* 摘    要:图片按钮实现
/* 当前版本:1.0
/* 作    者:
/* 创建日期:2013年5月7日星期二
*********************************************************/
// PicButton.cpp : 实现文件
//

#include "stdafx.h"
#include "PicButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// PicButton

IMPLEMENT_DYNAMIC(PicButton, CButton)

PicButton::PicButton()
{
m_nBtnStatus = BTNSTATUS_NOMAL;
m_nBtnStyle = NOMAL;
}

PicButton::~PicButton()
{
m_ImageList.DeleteImageList();
}

BEGIN_MESSAGE_MAP(PicButton, CButton)
ON_WM_DRAWITEM()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

void PicButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC BitmapDC;
BitmapDC.Attach(lpDrawItemStruct->hDC);
CDC memDC;
memDC.CreateCompatibleDC(&BitmapDC);
CBitmap bmp;
bmp.LoadBitmap(IDB_BMP_BK);
CBitmap *pOldBitmap = memDC.SelectObject(&bmp);
CRect rtBtn(0,0,0,0);
GetClientRect(&rtBtn);

m_ImageList.Draw(&memDC, 0, rtBtn.TopLeft(), ILD_TRANSPARENT);
if (m_nBtnStyle != 1)
{
if (BTNSTATUS_OVER == m_nBtnStatus)
{
m_ImageList.Draw(&memDC, 1, rtBtn.TopLeft(), ILD_TRANSPARENT);
}
else if (BTNSTATUS_DOWN == m_nBtnStatus)
{
m_ImageList.Draw(&memDC, 2, rtBtn.TopLeft(), ILD_TRANSPARENT);
}
}
BitmapDC.BitBlt(0, 0, rtBtn.Width(), rtBtn.Height(), &memDC, 0,0, SRCCOPY);
memDC.SelectObject(pOldBitmap);
memDC.DeleteDC();
}

BOOL PicButton::CreateBtn(const CPoint &pt, CWnd* pParentWnd, UINT nID, CBitmap*pBmp, int nbtnStlye /* = 0 */)//其中的图片格式是一张图片包含按钮的3个状态
{

BITMAP bm;
pBmp->GetBitmap(&bm);
m_nBtnStyle = nbtnStlye;
m_ImageList.Create(bm.bmWidth / 3, bm.bmHeight, ILC_COLOR24|ILC_MASK, 3, 3);
m_ImageList.Add(pBmp, RGB(0,0,255));
CRect rtBtn(pt, CSize(bm.bmWidth /3, bm.bmHeight));
return Create(_T(""),WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_OWNERDRAW, rtBtn, pParentWnd, nID);
}

// PicButton 消息处理程序

void PicButton::OnMouseMove(UINT nFlags, CPoint point)
{
TRACKMOUSEEVENT csTME;
csTME.cbSize = sizeof (csTME);
csTME.dwFlags = TME_LEAVE|TME_HOVER;
csTME.hwndTrack = m_hWnd ;// 指定要 追踪 的窗口
csTME.dwHoverTime = 5;  // 鼠标在按钮上停留超过 5ms ,才认为状态为 HOVER
::_TrackMouseEvent (&csTME); // 开启 Windows 的 WM_MOUSELEAVE , WM_MOUSEHOVER 事件支持

CButton::OnMouseMove(nFlags,point);
}

LRESULT PicButton::OnMouseHover(WPARAM wParam, LPARAM lParam)
{
if (m_nBtnStatus != BTNSTATUS_DOWN)
{
m_nBtnStatus = BTNSTATUS_OVER;
Invalidate(FALSE);
}
return TRUE;
}

LRESULT PicButton::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
if (m_nBtnStatus != BTNSTATUS_DOWN)
{
m_nBtnStatus = BTNSTATUS_NOMAL;
Invalidate(FALSE);
}
return TRUE;
}

void PicButton::OnLButtonDown(UINT nFlags, CPoint point)
{
if (m_nBtnStatus != BTNSTATUS_DOWN)
{
m_nBtnStatus = BTNSTATUS_DOWN;
Invalidate(FALSE);

}
else
{
if (m_nBtnStyle == NOMAL)
{
m_nBtnStatus = BTNSTATUS_OVER;
Invalidate(FALSE);
}
}

CButton::OnLButtonDown(nFlags,point);
}
void PicButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CRect rect(0,0,0,0);
GetClientRect(&rect);
if (!rect.PtInRect(point))
{
m_nBtnStatus = BTNSTATUS_NOMAL;
Invalidate(FALSE);
}

CButton::OnLButtonUp(nFlags, point);
}


图片示范样式

combobox自绘实现如图的combobox

字号大小选中的combobox

OwnerCombobox.h

#pragma once

// COwerCombobox

class COwnerCombobox : public CComboBox
{
DECLARE_DYNAMIC(COwnerCombobox)

public:
COwnerCombobox();
virtual ~COwnerCombobox();
enum
{
NOMAL = 0,
OVER,
DOWN
};

protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
protected:
void DrawShowText(CDC* pDC);
private:
CImageList m_ImageList;
int m_nBmpWidth;
int m_nBmpHight;
CRect m_rtDrop;
CRect m_rt;
int m_nStyle;
BOOL m_bptInRectDrop;
};
// OwerCombobox.cpp : 实现文件
//

#include "stdafx.h"
#include "OwnerCombobox.h"

// COwerCombobox
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_DYNAMIC(COwnerCombobox, CComboBox)

COwnerCombobox::COwnerCombobox()
{
BITMAP bm;
CBitmap bmp;
bmp.LoadBitmap(IDB_BMP_COMBOB);
bmp.GetBitmap(&bm);
m_nBmpWidth = bm.bmWidth / 3;
m_nBmpHight = bm.bmHeight;
m_ImageList.Create(m_nBmpWidth, m_nBmpHight, ILC_COLOR24|ILC_MASK, 3, 3);
m_ImageList.Add(&bmp, RGB(0,0,255));
m_rtDrop.SetRectEmpty();
m_rt.SetRectEmpty();
m_bptInRectDrop = FALSE;
m_nStyle = NOMAL;
}

COwnerCombobox::~COwnerCombobox()
{
}

BEGIN_MESSAGE_MAP(COwnerCombobox, CComboBox)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
END_MESSAGE_MAP()

// COwerCombobox 消息处理程序

void COwnerCombobox::DrawShowText(CDC* pDC)
{
//获取显示文字
TCHAR	strText[MAX_PATH+1];
GetWindowText(strText, MAX_PATH);

if (strText==NULL)
return;

CFont*	hOldFont = pDC->SelectObject(GetFont());
CSize	szExtent = pDC->GetTextExtent(strText, lstrlen(strText));
int		nMode	 = pDC->SetBkMode(TRANSPARENT);
CPoint	pt(2, 3);
pDC->DrawState(pt, szExtent, strText, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL);
pDC->SelectObject(hOldFont);
pDC->SetBkMode(nMode);
}

void COwnerCombobox::OnPaint()
{
// TODO: 在此处添加消息处理程序代码
CPaintDC dc(this);

if (m_rt.IsRectEmpty())
{
GetClientRect(&m_rt);
m_rtDrop = CRect(CPoint(m_rt.right - m_nBmpWidth,m_rt.top), CSize(m_nBmpWidth, m_nBmpHight));
}

CBrush brush;
brush.CreateSolidBrush(RGB(255,255,255));
dc.FillRect(&m_rt,&brush);

if (m_nStyle == NOMAL)
{
m_ImageList.Draw(&dc, 0,m_rtDrop.TopLeft(), ILD_TRANSPARENT);
}
else if (m_nStyle == OVER)
{
m_ImageList.Draw(&dc, 1,m_rtDrop.TopLeft(), ILD_TRANSPARENT);
}
else if (m_nStyle == DOWN)
{
m_ImageList.Draw(&dc, 2,m_rtDrop.TopLeft(), ILD_TRANSPARENT);
}
brush.DeleteObject();
CPoint pt(0,0);
GetCursorPos(&pt);
ScreenToClient(&pt);
if (m_rt.PtInRect(pt))
{
CPen pen;
pen.CreatePen(PS_SOLID, 1, STLYECOLOR);
CPen*pOldPen = dc.SelectObject(&pen);
dc.SelectStockObject(NULL_BRUSH);
dc.Rectangle(&m_rt);
dc.SelectObject(pOldPen);
}
DrawShowText(&dc);
// 不为绘图消息调用 CComboBox::OnPaint()
}

void COwnerCombobox::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_nStyle = DOWN;
Invalidate(FALSE);
CComboBox::OnLButtonDown(nFlags, point);
}

void COwnerCombobox::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
m_nStyle = NOMAL;
Invalidate(FALSE);
CComboBox::OnLButtonUp(nFlags, point);
}

void COwnerCombobox::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
TRACKMOUSEEVENT csTME;
csTME.cbSize = sizeof (csTME);
csTME.dwFlags = TME_LEAVE|TME_HOVER;
csTME.hwndTrack = m_hWnd ;// 指定要 追踪 的窗口
csTME.dwHoverTime = 5;  // 鼠标在按钮上停留超过 5ms ,才认为状态为 HOVER
::_TrackMouseEvent (&csTME); // 开启 Windows 的 WM_MOUSELEAVE , WM_MOUSEHOVER 事件支持
CComboBox::OnMouseMove(nFlags, point);
}

LRESULT COwnerCombobox::OnMouseHover(WPARAM wParam, LPARAM lParam)
{
CPoint pt(0,0);
GetCursorPos(&pt);
ScreenToClient(&pt);
if (!m_bptInRectDrop &&am
a079
p; m_rt.PtInRect(pt))
{
m_bptInRectDrop = TRUE;
m_nStyle = OVER;
Invalidate(FALSE);
}

return 0;
}
LRESULT COwnerCombobox::OnMouseLeave(WPARAM wParam, LPARAM lParam)
{
m_bptInRectDrop = FALSE;
m_nStyle = NOMAL;
Invalidate(FALSE);
return 0;
}
图片个格式和上面的一样,我还是把图片也放上来吧


注意吧type 设置为DropList

OK2个控件的自绘就是这样很简单的说
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐