图文按钮

重绘按钮

// ImageTextButtonDlg.cpp : implementation file
//


#include "stdafx.h"
#include "ImageTextButton.h"
#include "ImageTextButtonDlg.h"


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


/
// CAboutDlg dialog used for App About


class CAboutDlg : public CDialog
{
public:
CAboutDlg();


// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA


// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL


// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};


CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/
// CImageTextButtonDlg dialog


CImageTextButtonDlg::CImageTextButtonDlg(CWnd* pParent /*=NULL*/)
: CDialog(CImageTextButtonDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CImageTextButtonDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}


void CImageTextButtonDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CImageTextButtonDlg)
DDX_Control(pDX, IDC_BUTTON2, m_button2);
DDX_Control(pDX, IDC_BUTTON1, m_button1);
//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CImageTextButtonDlg, CDialog)
//{{AFX_MSG_MAP(CImageTextButtonDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/
// CImageTextButtonDlg message handlers


BOOL CImageTextButtonDlg::OnInitDialog()
{
CDialog::OnInitDialog();


// Add "About..." menu item to system menu.


// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);


CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}


// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

//创建图像列表
m_images.Create(24, 24, ILC_COLOR24 | ILC_MASK, 1, 0);
m_images.Add(AfxGetApp()->LoadIcon(IDI_ICON1)); //加载图标
m_images.Add(AfxGetApp()->LoadIcon(IDI_ICON2));

m_button1.SetImageList(&m_images); //设置图像列表
m_button2.SetImageList(&m_images);

//设置按钮显示的图像索引
m_button1.SetImageIndex(0);
m_button2.SetImageIndex(1);
return TRUE;  // return TRUE  unless you set the focus to a control
}


void CImageTextButtonDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}


// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.


void CImageTextButtonDlg::OnPaint() 
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting


SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);


// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;


// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}


// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CImageTextButtonDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

------------------------------------------------------------------------------------

// ImageButton.cpp : implementation file
//


#include "stdafx.h"
#include "ImageButton.h"


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


/
// ImageButton


ImageButton::ImageButton()
{
IsPressed = FALSE;
}


ImageButton::~ImageButton()
{
}




BEGIN_MESSAGE_MAP(ImageButton, CButton)
//{{AFX_MSG_MAP(ImageButton)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/
// ImageButton message handlers


void ImageButton::SetImageIndex(UINT index)
{
m_ImageIndex = index;
}


void ImageButton::SetImageList(CImageList *pImage)
{
m_pImagelist = pImage;
}


void ImageButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc ;
dc.Attach(lpDrawItemStruct->hDC);
if (m_pImagelist)
{
UINT state = lpDrawItemStruct->itemState; //获取状态
UINT action = lpDrawItemStruct->itemAction;
//获取图像列中图像大小
IMAGEINFO imageinfo;
m_pImagelist->GetImageInfo(m_ImageIndex,&imageinfo);
CSize imagesize;
imagesize.cx = imageinfo.rcImage.right-imageinfo.rcImage.left;
imagesize.cy = imageinfo.rcImage.bottom - imageinfo.rcImage.top;
//在按钮垂直方向居中显示位图
CRect rect;
GetClientRect(rect);
CPoint point;
point.x = 5;
point.y = (rect.Height() - imagesize.cy)/2;
m_pImagelist->Draw(&dc,m_ImageIndex,point,ILD_NORMAL|ILD_TRANSPARENT);
//按钮被选中或者获得焦点时
if ((state&ODS_SELECTED)||(state&ODS_FOCUS))
{
CRect focusRect (rect); //焦点矩形
focusRect.DeflateRect(4,4,4,4);
CPen pen(PS_DASHDOTDOT,1,RGB(0,0,0));
CBrush brush;
brush.CreateStockObject(NULL_BRUSH);
dc.SelectObject(&brush);
dc.SelectObject(&pen);
//绘制焦点矩形
dc.DrawFocusRect(focusRect);
//绘制立体效果
dc.DrawEdge(rect,BDR_RAISEDINNER|BDR_RAISEDOUTER,BF_BOTTOMLEFT|BF_TOPRIGHT);
//获得焦点时绘制黑色边框
dc.Draw3dRect(rect,RGB(51,51,51),RGB(0,0,0));
}
else  //默认情况下
{
CRect focusRect (rect);
focusRect.DeflateRect(4,4,4,4);
CPen pen(PS_DOT,1,RGB(192,192,192));
CBrush brush;
brush.CreateStockObject(NULL_BRUSH);
dc.SelectObject(&brush);
dc.SelectObject(&pen);
dc.Rectangle(focusRect);
dc.DrawEdge(rect,BDR_RAISEDINNER|BDR_RAISEDOUTER,BF_BOTTOMLEFT|BF_TOPRIGHT);
}
if (IsPressed) //在按钮被按下时绘制按下效果
{


CRect focusRect1(rect);
focusRect1.DeflateRect(4,4,4,4);
dc.DrawFocusRect(focusRect1);
dc.DrawEdge(rect,BDR_SUNKENINNER |BDR_SUNKENOUTER ,BF_BOTTOMLEFT|BF_TOPRIGHT);


dc.Draw3dRect(rect,RGB(51,51,51),RGB(0,0,0));
//dc.DrawFocusRect(focusRect1);
}
//绘制按钮文本
CString text;
GetWindowText(text);
rect.DeflateRect(point.x+imagesize.cx+2,0,0,0);
dc.SetBkMode(TRANSPARENT);
dc.DrawText(text,rect,DT_LEFT|DT_SINGLELINE|DT_VCENTER);
}
}


void ImageButton::PreSubclassWindow() 
{
// TODO: Add your specialized code here and/or call the base class

CButton::PreSubclassWindow();
ModifyStyle(0,BS_OWNERDRAW);
}


void ImageButton::OnMouseMove(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
CRect rect;
GetClientRect(rect);


if (!rect.PtInRect(point)) //判断当前鼠标点是否在按钮区域内
{
if (IsPressed) //如果鼠标不在按钮的区域内,并且按钮处于按下状态,将按钮设置为正常状态
IsPressed = FALSE;
}
CButton::OnMouseMove(nFlags, point);
}


void ImageButton::OnLButtonDown(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
IsPressed = TRUE; //按钮被按下
CButton::OnLButtonDown(nFlags, point);
}


void ImageButton::OnLButtonUp(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
IsPressed = FALSE; //释放鼠标按钮
CButton::OnLButtonUp(nFlags, point);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值