您的位置:首页 > 其它

自绘制按钮初探一

2008-09-18 18:41 176 查看


1、很多的做的比较cool的界面都回截取WM_MOUSELEAVE和WM_MOUSEHOVER消息,而此消息不是标准的windows消息,我们可以通过mfc类向导添加WM_MOUSEMOVE响应函数,在其中添加以下代码以得到以上两种消息:
void CXPButton::OnMouseMove(UINT nFlags, CPoint point)
{
if (!m_bTracking)
{
TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme);
tme.hwndTrack = m_hWnd;
tme.dwFlags = TME_LEAVE | TME_HOVER;
tme.dwHoverTime = 1;
m_bTracking = _TrackMouseEvent(&tme);
}
CButton::OnMouseMove(nFlags, point);
}
MDSDN对以上函数解释如下:

TrackMouseEvent
The TrackMouseEvent function posts messages when the mouse pointer leaves a window or hovers over a window for a specified amount of time.
为以上两种消息添加消息响应函数 如下:
afx_msg LRESULT OnMouseLeave(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnMouseHover(WPARAM wParam, LPARAM lParam);

ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
ON_MESSAGE(WM_MOUSEHOVER, OnMouseHover)

2、自绘制按钮一般重DrawItem()函数,在调用此函数前应该设置控件的属性为自绘制,通过调用PreSubclassWindow() 函数实现。代码如下:
void CXPButton::PreSubclassWindow()
{ CButton::PreSubclassWindow();
ModifyStyle(0, BS_OWNERDRAW);

}

DrawItem函数在MSDN解释如下:
virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );

Called by the framework when a visual aspect of an owner-drawn button has changed. An owner-drawn button has the BS_OWNERDRAW style set. Override this member function to implement drawing for an owner-drawn CButton object. The application should restore all graphics device interface (GDI) objects selected for the display context supplied in lpDrawItemStruct before the member function terminates.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: