本文简单介绍下按钮控件的位置大小和界面设置。
首先,新建一个对话框应用程序,并且在界面上添加几个按钮
然后,设置贴图按钮的属性
添加图片资源
将第一个图片加载到程序资源中,第二个图片放在资源文件夹中
接着,添加按钮相关变量
CButtonST m_btn5;
CBitmapButton m_btn4;
CBitmapButton m_btn3;
然后处理
void CButtonUiDlg::SetButtonUI()
{
//button1 do nothing
//button2 set size and position
RECT clientRect;
clientRect.left = 200;
clientRect.top = 100;
clientRect.right = clientRect.left + 100;
clientRect.bottom = clientRect.top + 100;
GetDlgItem(IDC_BUTTON2)->MoveWindow(&clientRect);
//button3 set background get from resource
m_btn3.LoadBitmaps(IDB_BITMAP1);
//button4 set background get from file picture
CString sCurrentPath = "";
char Path[270];
DWORD len = 0;
DWORD i = 0;
len = GetCurrentDirectory(270,Path);
for(i=0;i<len;i++)
{
sCurrentPath = sCurrentPath + CString(Path[i]);
}
sCurrentPath = sCurrentPath + "/res";
CString sFilePathName = sCurrentPath + "/bg2.bmp";
m_btn4.LoadBitmaps(sFilePathName);
//button5 set background from class
m_btn5.SetBtnCursor();
m_btn5.SetIcon(m_hIcon);
m_btn5.SetTooltipText(&sFilePathName);
}
运行程序,查看效果
其中,按钮5使用的类CButtonST来源于网络,附件如下
头文件
//
// Class: CButtonST
//
// Compiler: Visual C++
// Tested on: Visual C++ 5.0
//
// Version: See GetVersionC() or GetVersionI()
//
// Created: xx/xxxx/1998
// Updated: 19/September/1999
//
// Author: Davide Calabro' davide_calabro@yahoo.com
//
#ifndef _BTNST_H
#define _BTNST_H
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// CBtnST.h : header file
//
// Comment this if you don't want that CButtonST hilights itself
// also when the window is inactive (like happens in Internet Explorer)
//#define ST_LIKEIE
// Comment this if you don't want to use CMemDC class
//#define ST_USE_MEMDC
/
// CButtonST window
class CButtonST : public CButton
{
// Construction
public:
CButtonST();
~CButtonST();
enum {ST_ALIGN_HORIZ, ST_ALIGN_VERT, ST_ALIGN_HORIZ_RIGHT};
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{
{AFX_VIRTUAL(CButtonST)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void PreSubclassWindow();
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
//}}AFX_VIRTUAL
// Implementation
public:
void DrawTransparent(BOOL bRepaint = FALSE);
BOOL GetDefault();
void SetTooltipText(int nId, BOOL bActivate = TRUE);
void SetTooltipText(CString* spText, BOOL bActivate = TRUE);
void ActivateTooltip(BOOL bEnable = TRUE);
BOOL SetBtnCursor(int nCursorId = -1);
void SetFlatFocus(BOOL bDrawFlatFocus, BOOL bRepaint = FALSE);
BOOL GetFlatFocus();
void SetDefaultActiveFgColor(BOOL bRepaint = FALSE);
void SetActiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE);
const COLORREF GetActiveFgColor();
void SetDefaultActiveBgColor(BOOL bRepaint = FALSE);
void SetActiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE);
const COLORREF GetActiveBgColor();
void SetDefaultInactiveFgColor(BOOL bRepaint = FALSE);
void SetInactiveFgColor(COLORREF crNew, BOOL bRepaint = FALSE);
const COLORREF GetInactiveFgColor();
void SetDefaultInactiveBgColor(BOOL bRepaint = FALSE);
void SetInactiveBgColor(COLORREF crNew, BOOL bRepaint = FALSE);
const COLORREF GetInactiveBgColor();
void SetShowText(BOOL bShow = TRUE);
BOOL GetShowText();
void SetAlign(int nAlign);
int GetAlign();
void SetFlat(BOOL bState = TRUE);
BOOL GetFlat();
void DrawBorder(BOOL bEnable = TRUE);
void SetIcon(int nIconInId, int nIconOutId = NULL);
void SetIcon(HICON hIconIn, HICON hIconOut = NULL);
static const short GetVersionI();
static const char* GetVersionC();
protected:
//{
{AFX_MSG(CButtonST)
afx_msg void OnCaptureChanged(CWnd *pWnd);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
afx_msg void OnKillFocus(CWnd* pNewWnd);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnSysColorChange();
//}}AFX_MSG
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
DECLARE_MESSAGE_MAP()
private:
void DrawTheIcon(CDC* pDC, CString* title, RECT* rcItem, CRect* captionRect, BOOL IsPressed, BOOL IsDisabled);
void InitToolTip();
void PaintBk(CDC* pDC);
int m_nAlign;
BOOL m_bShowText;
BOOL m_bDrawBorder;
BOOL m_bIsFlat;
BOOL m_MouseOnButton;
BOOL m_bDrawFlatFocus;
HCURSOR m_hCursor;
CToolTipCtrl m_ToolTip;
HICON m_hIconIn;
HICON m_hI