图形按钮?工具栏?

LaoJiang 2000-06-16 09:30:00
那位大虾给一个简单的对话框中使用工具栏或图形按钮的例子?
多谢!!!
...全文
238 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sun2000 2000-06-17
  • 打赏
  • 举报
回复
你可以在对话框中拖入一个按钮(不要创建对象,属性加上OwnerDraw和Bitmap两项),并按钮的Caption为A(例子,当然也可以是其它的),同时加入两个Bitmap资源标识分别为"AU"和"AD"(注意加上双引号“AU”表示UP时的位图,“AD”表示DOWN时的位图,“A”必需与按钮的Caption 相同,也就是"Caption"+"U/D"),在CDialog的.h中加入CBitmapButton m_bitBtn;
在CDialog::OnInitDialog(...)中调用m_bitBtn.AutoLoad(...)(ID号为对话框中按钮的ID号)
dzl 2000-06-17
  • 打赏
  • 举报
回复
贴一篇关于图形按键的文章:

用AutoLoad调用一下就行了。唯一的不同在于四个位图的后缀为N,U,D和X,分别代表normal,pop-up,push-down和disabled。

使用时在编辑资源时要设置BS_OWNERDRAW属性,并且CCoolButton将忽略WS_TABSTOP属性
(按钮不获得输入焦点)。

// CCoolButton is a class derived from CButton.
// Most codes are copied from CBitmapButton.
// The usage is just like CBitmapButton:
// You declare a CCoolButton variable, and use AutoLoad class
// member to load the bitmaps. The only difference is that
// the four bitmaps have postfixes "N"(normal), "U"(Up),
// "D"(Down) and "X"(Disabled).
// You need to set the style OWNERDRAW and had better
// unset the style TABSTOP when you edit the button resource.
//
// Designed by Scott Zhong, March 1998

// CoolButton.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// CCoolButton window

#ifndef _COOLBUTTON_H
#define _COOLBUTTON_H

#define CBS_NORMAL 0x0
#define CBS_POPUP 0x1
#define CBS_PUSHDOWN 0x2
#define CBS_DISABLED 0x4

// CCoolButton - push-button with 1->4 bitmap images
class CCoolButton : public CButton
{
// Construction
DECLARE_DYNAMIC(CCoolButton)
public:
CCoolButton();

BOOL LoadBitmaps(LPCTSTR lpszBitmapResource,
LPCTSTR lpszBitmapResourceUp = NULL,
LPCTSTR lpszBitmapResourceDown = NULL,
LPCTSTR lpszBitmapResourceDisabled = NULL);
BOOL AutoLoad(UINT nID, CWnd* pParent);

// Attributes

protected:
UINT m_state;
void SetBitmapMode(UINT action = 0, UINT mode = 0);

// all bitmaps must be the same size
CBitmap m_bitmap; // normal image (REQUIRED)
CBitmap m_bitmapUp; // pop up image (OPTIONAL)
CBitmap m_bitmapDown; // push down image (OPTIONAL)
CBitmap m_bitmapDisabled; // disabled bitmap (OPTIONAL)

CRect m_clientRect;

public:

// Operations
void SizeToContent();

public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCoolButton)
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
//}}AFX_VIRTUAL

// Implementation
public:
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

virtual ~CCoolButton();

// Generated message map functions
protected:
//{{AFX_MSG(CCoolButton)
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnCancelMode();
//}}AFX_MSG

DECLARE_MESSAGE_MAP()
};

#endif

/////////////////////////////////////////////////////////////////////////////


// CoolButton.cpp : implementation file
//

#include "stdafx.h"
#include "CoolButton.h"

#ifdef AFX_AUX_SEG
#pragma code_seg(AFX_AUX_SEG)
#endif

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

/////////////////////////////////////////////////////////////////////////////
// CCoolButton

CCoolButton::CCoolButton()
{
m_state = CBS_NORMAL;
}

CCoolButton::~CCoolButton()
{
}

// LoadBitmaps will load in one, two, three or all four bitmaps
// returns TRUE if all specified images are loaded
BOOL CCoolButton::LoadBitmaps(LPCTSTR lpszBitmapResource,
LPCTSTR lpszBitmapResourceUp,
LPCTSTR lpszBitmapResourceDown,
LPCTSTR lpszBitmapResourceDisabled)
{
// delete old bitmaps (if present)
m_bitmap.DeleteObject();
m_bitmapUp.DeleteObject();
m_bitmapDown.DeleteObject();
m_bitmapDisabled.DeleteObject();

if (!m_bitmap.LoadBitmap(lpszBitmapResource))
{
TRACE0("Failed to load bitmap for normal image.\n");
return FALSE; // need this one image
}
BOOL bAllLoaded = TRUE;
if (lpszBitmapResourceUp != NULL)
{
if (!m_bitmapUp.LoadBitmap(lpszBitmapResourceUp))
{
TRACE0("Failed to load bitmap for pop-up image.\n");
bAllLoaded = FALSE;
}
}
if (lpszBitmapResourceDown != NULL)
{
if (!m_bitmapDown.LoadBitmap(lpszBitmapResourceDown))
{
TRACE0("Failed to load bitmap for push-down image.\n");
bAllLoaded = FALSE;
}
}
if (lpszBitmapResourceDisabled != NULL)
{
if (!m_bitmapDisabled.LoadBitmap(lpszBitmapResourceDisabled))
bAllLoaded = FALSE;
}

return bAllLoaded;
}

// SizeToContent will resize the button to the size of the bitmap
void CCoolButton::SizeToContent()
{
ASSERT(m_bitmap.m_hObject != NULL);
CSize bitmapSize;
BITMAP bmInfo;
VERIFY(m_bitmap.GetObject(sizeof(bmInfo), &bmInfo) == sizeof(bmInfo));
m_clientRect = CRect(0, 0, bmInfo.bmWidth, bmInfo.bmHeight);
VERIFY(SetWindowPos(NULL, -1, -1, bmInfo.bmWidth, bmInfo.bmHeight,
SWP_NOMOVE|SWP_NOZORDER|SWP_NOREDRAW|SWP_NOACTIVATE));
}

// Autoload will load the bitmap resources based on the text of
// the button
// Using suffices "N", "U", "D" and "X" for normal/up/down/disabled
BOOL CCoolButton::AutoLoad(UINT nID, CWnd* pParent)
{
// first attach the CCoolButton to the dialog control

if (!SubclassDlgItem(nID, pParent))
return FALSE;

ASSERT(GetStyle() & BS_OWNERDRAW);
ModifyStyle(WS_TABSTOP, 0);

CString buttonName;
GetWindowText(buttonName);
ASSERT(!buttonName.IsEmpty()); // must provide a title

LoadBitmaps(buttonName + _T("N"), buttonName + _T("U"),
buttonName + _T("D"), buttonName + _T("X"));

// we need at least the primary
if (m_bitmap.m_hObject == NULL)
return FALSE;

// size to content
SizeToContent();
return TRUE;
}

// Draw the appropriate bitmap
void CCoolButton::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
ASSERT(lpDIS != NULL);
// must have at least the first bitmap loaded before calling DrawItem
ASSERT(m_bitmap.m_hObject != NULL); // required

// use the main bitmap for normal, the selected bitmap for push-down
CBitmap* pBitmap = &m_bitmap;
m_state = CBS_NORMAL;

UINT state = lpDIS->itemState;

if ((state & ODS_SELECTED) && m_bitmapDown.m_hObject != NULL)
{
pBitmap = &m_bitmapDown;
m_state = CBS_PUSHDOWN;
}
else if ((state & ODS_FOCUS) && m_bitmapUp.m_hObject != NULL)
{
pBitmap = &m_bitmapUp; // third image for pop-up
m_state = CBS_POPUP;
}
else if ((state & ODS_DISABLED) && m_bitmapDisabled.m_hObject != NULL)
{
pBitmap = &m_bitmapDisabled; // last image for disabled
m_state = CBS_DISABLED;
}

// draw the whole button
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap* pOld = memDC.SelectObject(pBitmap);
if (pOld == NULL)
return; // destructors will clean up

CRect rect;
rect.CopyRect(&lpDIS->rcItem);
pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
&memDC, 0, 0, SRCCOPY);
memDC.SelectObject(pOld);
}

void CCoolButton::SetBitmapMode(UINT action, UINT mode)
{
DRAWITEMSTRUCT DIS;
DIS.CtlType = ODT_BUTTON;
DIS.CtlID = GetDlgCtrlID();
DIS.itemAction = action;
DIS.itemState = mode;
DIS.hwndItem = GetSafeHwnd();
DIS.hDC = GetDC()->GetSafeHdc();
GetClientRect(&(DIS.rcItem));
SendMessage(WM_DRAWITEM, (WPARAM)GetSafeHwnd(), (LPARAM)&DIS);
ReleaseDC(CDC::FromHandle(DIS.hDC));
}

/////////////////////////////////////////////////////////////////////////////
// CCoolButton diagnostics
#ifdef _DEBUG
void CCoolButton::AssertValid() const
{
CButton::AssertValid();

m_bitmap.AssertValid();
m_bitmapUp.AssertValid();
m_bitmapDown.AssertValid();
m_bitmapDisabled.AssertValid();
}

void CCoolButton::Dump(CDumpContext& dc) const
{
CButton::Dump(dc);

dc << "m_bitmap = " << (UINT)m_bitmap.m_hObject;
dc << "\nm_bitmapUp = " << (UINT)m_bitmapUp.m_hObject;
dc << "\nm_bitmapDown = " << (UINT)m_bitmapDown.m_hObject;
dc << "\nm_bitmapDisabled = " << (UINT)m_bitmapDisabled.m_hObject;

dc << "\n";
}
#endif

#ifdef AFX_INIT_SEG
#pragma code_seg(AFX_INIT_SEG)
#endif

IMPLEMENT_DYNAMIC(CCoolButton, CButton)

/////////////////////////////////////////////////////////////////////////////


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

/////////////////////////////////////////////////////////////////////////////
// CCoolButton message handlers

void CCoolButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_clientRect.PtInRect(point))
{
if (m_state == CBS_NORMAL)
{
// sets pop-up state
SetBitmapMode(ODA_FOCUS, ODS_FOCUS);
SetCapture();
}
}
else
{
// if moving out of the button
ReleaseCapture();
SetBitmapMode();
}

CWnd::OnMouseMove(nFlags, point);
}

void CCoolButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if ((m_state == CBS_PUSHDOWN) || (m_clientRect.PtInRect(point)))
// if having been pressed and now released
// emulates a BN_CLICKED message
{
ReleaseCapture();
SetBitmapMode(ODA_SELECT);
GetParent()->SendMessage(WM_COMMAND, MAKELPARAM(GetDlgCtrlID(), BN_CLICKED), (LPARAM)GetSafeHwnd());
}
}

void CCoolButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetBitmapMode(ODA_SELECT, ODS_SELECTED);
}

void CCoolButton::OnCancelMode()
{
CButton::OnCancelMode();

// TODO: Add your message handler code here
// When another window switchs to the foreground
// reset the button state because of losing mouse capture
SetBitmapMode();
}
LaoJiang 2000-06-16
  • 打赏
  • 举报
回复
有图形按钮的例子吗?
j_james@163.net
多谢!
softdoctor 2000-06-16
  • 打赏
  • 举报
回复

BOOL CDataViewDlg::CreateToolBar()
{
TBBUTTON * m_pTBButtons;
m_pTBButtons= new TBBUTTON[5];
memset(m_pTBButtons,0,sizeof(TBBUTTON)*5);

for (int nIndex = 0; nIndex < 5; nIndex++)
{
m_pTBButtons[nIndex].fsState = TBSTATE_ENABLED;
m_pTBButtons[nIndex].fsStyle = TBSTYLE_BUTTON;
m_pTBButtons[nIndex].dwData = 0;
m_pTBButtons[nIndex].iBitmap = nIndex;
m_pTBButtons[nIndex].idCommand = IDS_PRINTDATA+nIndex;
m_pTBButtons[nIndex].iString = nIndex;
}

m_wndToolBar = CreateToolbarEx(m_hWnd,
WS_CHILD|WS_VISIBLE|TBSTYLE_FLAT|TBSTYLE_TOOLTIPS|CCS_NODIVIDER,
120,5,AfxGetInstanceHandle(),IDB_VIEWDATA,m_pTBButtons,5,24,24,24,24,sizeof(TBBUTTON));
::SendMessage(m_wndToolBar, TB_ADDSTRING, 0, (LPARAM)_T("打印\0装载\0保存\0清空\0提交\0\0"));
::SendMessage(m_wndToolBar, TB_AUTOSIZE, 0, 0L);
delete m_pTBButtons;
return TRUE;
}

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧