我是在View中动态Create的Button,请问如何相应它的OnLButtonDown函数??应在哪里添加,怎么添加??怎么把按键和消息联系起来?如果我创建时候,把属性改为ownerdraw,为什么显示的Button没有文字??如下:
m_pButton->Create(_T ("Caption"), WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,CRect(400,400,500,424),this,IDC_MYBUTTON);
急,请高手指点,只要有帮助的建议都给分。
2.ownerdraw为自画属性,必须为按钮装载状态位图,才有效果。
把这句话加上
LRESULT CALLBACK YourWindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
if (uMsg == WM_LBUTTONDOWN) {
....
}
return CallWindowProc(theOldWindowProc, hwnd, uMsg, wParam, lParam);}// 在按钮创建之后添加如下代码
theOldWindowProc = (WNDPROC)SetWindowLong(GWL_WNDPROC,YourWindowProc);
theOldWindowProc = (WNDPROC)SetWindowLong(pYourButton->m_hWnd, GWL_WNDPROC,YourWindowProc);// theOldWindowProc 是 WNDPROC 类型的全局变量
然后在里面作处理比较方便吧
//{{AFX_MSG_MAP(Cyourview) ON_COMMAND(IDC_MYBUTTON, OnCommandButton) //}}AFX_MSG_MAP
END_MESSAGE_MAP()
........................
Cyourview::OnCommandButton()
{
//do
}自绘需要用CButton::SetWindowText来设置文字
//{{AFX_MSG_MAP(Cyourview) ON_COMMAND(IDC_MYBUTTON, OnCommandButton) //}}AFX_MSG_MAP
END_MESSAGE_MAP()
........................
Cyourview::OnCommandButton()
{
//do
}自绘需要用CButton::SetWindowText来设置文字
afx_msg void OnButtonStart();资源里加一字符串资源
IDC_BUTTON_START父窗口oncreate中:
m_StartButton.Create(NULL, WS_CHILD|WS_VISIBLE|BS_OWNERDRAW,
CRect(0,1,50,22), this, IDC_BUTTON_START);CPP文件的消息映射中:
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
//{{AFX_MSG_MAP(Cyourview) ON_COMMAND(IDC_MYBUTTON, OnCommandButton) //}}AFX_MSG_MAP
END_MESSAGE_MAP()
........................
Cyourview::OnCommandButton()
{
//do
}自绘需要用CButton::SetWindowText来设置文字
当然,还有另外一个方法:从CButton类继承一个CXXXButton类,然后把你的按钮对应这个类,这个不用我教你了吧,然后在classwizard中手动添加CXXXButton类的消息映射
然后在里面作处理比较方便吧
我是在单文档项目中,在view中动态生成Button,谁有这么用过啊?为什么不能单击它?
CMyButton *p_Button;
p_Button = new CMyButton();
p_Button.Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );
------------------------------
将BS_OWNERDRAW换成BS_PUSHBUTTON
------------------------------
子类化CButton
还是很谢谢大家,散分了。