Define a class CMyButton :public CButton add a virtual function to it void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: Add your code to draw the specified item HDC hdc=lpDrawItemStruct->hDC; CString strcatpion; this->GetWindowText(strcatpion); DrawText(hdc,strcatpion,strcatpion.GetLength(),&lpDrawItemStruct->rcItem,DT_CENTER); } Define a resource ID : ID_MYBUTTON. Add a CMyButton member to the view class: CMyButton m_button; Add WM_CREATE message handle to the view class int CObuttonView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1;
// TODO: Add your specialized creation code here m_button.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW, CRect(10,10,100,30), this, ID_MYBUTTON); return 0; } Add a line to handle button click into the MessageMap ON_WM_CREATE() ON_BN_CLICKED(ID_MYBUTTON, OnMyButton)//add this line Add a function to the View class afx_msg void OnMyButton(); //implementation example void CObuttonView::OnMyButton() { AfxMessageBox("clicked"); }
那么你有没有给它指定resource ID?
create,show出来不就行了么?
我用spy看了一下,button被自动置为disable
add a virtual function to it
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
HDC hdc=lpDrawItemStruct->hDC;
CString strcatpion;
this->GetWindowText(strcatpion);
DrawText(hdc,strcatpion,strcatpion.GetLength(),&lpDrawItemStruct->rcItem,DT_CENTER);
}
Define a resource ID :
ID_MYBUTTON.
Add a CMyButton member to the view class:
CMyButton m_button;
Add WM_CREATE message handle to the view class
int CObuttonView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here m_button.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW,
CRect(10,10,100,30), this, ID_MYBUTTON); return 0;
}
Add a line to handle button click into the MessageMap
ON_WM_CREATE()
ON_BN_CLICKED(ID_MYBUTTON, OnMyButton)//add this line
Add a function to the View class
afx_msg void OnMyButton();
//implementation example
void CObuttonView::OnMyButton()
{
AfxMessageBox("clicked");
}
因为你是在View中Create,而我必须在派生的dialogbar中创建这个button,但是当在派生的dialogbar中Create Button时,这个button不能响应任何消息。
记得在TOOLBAR中的一个按扭如果没有定义消息响应函数,这个按扭就自动为DISABLE,在DIALOGBAR中可能也是这个问题。给BUTTON的ID定义一个消息响应函数试一下。
add a member to your derived class CMyDialogBar
CMyButton m_button.
为CMyDialog加WM_CREATE message handle
int CMyDialogBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialogBar::OnCreate(lpCreateStruct) == -1)
return -1;
m_button.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW,
CRect(10,10,100,30), this, ID_MYBUTTON);
return 0;
}View菜单Resource symbols dialog中增加一个resource ID: ID_MYBUTTON
加一个成员函数给CMyDialogBar
afx_msg void OnButtonClick();
void CMyDialogBar::OnButtonClick()
{
AfxMessageBox("button of dlgbar clicked");
}
在Message_MAp 中加上button 的message handle
BEGIN_MESSAGE_MAP(CMyDialogBar, CDialogBar)
//{{AFX_MSG_MAP(CMyDialogBar)
ON_WM_CREATE()
ON_BN_CLICKED(ID_MYBUTTON, OnButtonClick)//add this line
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
第二种, 在属性中选上自给,然后还父窗口中处理WM_DRAWITEM
OK,我已经发过去了。
thank you,Email?
THANK YOU VERY MUCH!!!!!!!!!!!!!!!!
函数!!!!!实在不行把你的代码发给我,我给你看看!!!
[email protected]