16,472
社区成员
发帖
与我相关
我的任务
分享
class CButton : public CWnd
{
DECLARE_DYNAMIC(CButton)
// Constructors
public:
CButton();
virtual BOOL Create(LPCTSTR lpszCaption, DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd, UINT nID);
// Attributes
UINT GetState() const;
void SetState(BOOL bHighlight);
int GetCheck() const;
void SetCheck(int nCheck);
UINT GetButtonStyle() const;
void SetButtonStyle(UINT nStyle, BOOL bRedraw = TRUE);
HICON SetIcon(HICON hIcon);
HICON GetIcon() const;
HBITMAP SetBitmap(HBITMAP hBitmap);
HBITMAP GetBitmap() const;
HCURSOR SetCursor(HCURSOR hCursor);
HCURSOR GetCursor();
#if (_WIN32_WINNT >= 0x501)
BOOL GetIdealSize(SIZE* psize);
BOOL SetImageList(PBUTTON_IMAGELIST pbuttonImagelist);
BOOL GetImageList(PBUTTON_IMAGELIST pbuttonImagelist);
BOOL SetTextMargin(RECT* pmargin);
BOOL GetTextMargin(RECT* pmargin);
#endif // (_WIN32_WINNT >= 0x501)
// Overridables (for owner draw only)
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
// Implementation
public:
virtual ~CButton();
protected:
virtual BOOL OnChildNotify(UINT, WPARAM, LPARAM, LRESULT*);
};
winctrl1.cpp CButton部分实现
/////////////////////////////////////////////////////////////////////////////
// CButton
BOOL CButton::Create(LPCTSTR lpszCaption, DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd, UINT nID)
{
CWnd* pWnd = this;
return pWnd->Create(_T("BUTTON"), lpszCaption, dwStyle, rect, pParentWnd, nID);
}
CButton::~CButton()
{
DestroyWindow();
}
// Helper for radio buttons
int CWnd::GetCheckedRadioButton(int nIDFirstButton, int nIDLastButton)
{
for (int nID = nIDFirstButton; nID <= nIDLastButton; nID++)
{
if (IsDlgButtonChecked(nID))
return nID; // id that matched
}
return 0; // invalid ID
}
// Derived class is responsible for implementing all of these handlers
// for owner/self draw controls
void CButton::DrawItem(LPDRAWITEMSTRUCT)
{
ASSERT(FALSE);
}
BOOL CButton::OnChildNotify(UINT message, WPARAM wParam, LPARAM lParam,
LRESULT* pResult)
{
if (message != WM_DRAWITEM)
return CWnd::OnChildNotify(message, wParam, lParam, pResult);
ASSERT(pResult == NULL); // no return value expected
UNUSED(pResult); // unused in release builds
DrawItem((LPDRAWITEMSTRUCT)lParam);
return TRUE;
}
afxwin2.inl CButton的内联实现
_AFXWIN_INLINE CButton::CButton()
{ }
_AFXWIN_INLINE UINT CButton::GetState() const
{ ASSERT(::IsWindow(m_hWnd)); return (UINT)::SendMessage(m_hWnd, BM_GETSTATE, 0, 0); }
_AFXWIN_INLINE void CButton::SetState(BOOL bHighlight)
{ ::SendMessage(m_hWnd, BM_SETSTATE, bHighlight, 0); }
_AFXWIN_INLINE int CButton::GetCheck() const
{ ASSERT(::IsWindow(m_hWnd)); return (int)::SendMessage(m_hWnd, BM_GETCHECK, 0, 0); }
_AFXWIN_INLINE void CButton::SetCheck(int nCheck)
{ ASSERT(::IsWindow(m_hWnd)); ::SendMessage(m_hWnd, BM_SETCHECK, nCheck, 0); }
_AFXWIN_INLINE UINT CButton::GetButtonStyle() const
{ ASSERT(::IsWindow(m_hWnd)); return (UINT)GetWindowLong(m_hWnd, GWL_STYLE) & 0xff; }
_AFXWIN_INLINE void CButton::SetButtonStyle(UINT nStyle, BOOL bRedraw)
{ ASSERT(::IsWindow(m_hWnd)); ::SendMessage(m_hWnd, BM_SETSTYLE, nStyle, (LPARAM)bRedraw); }
// Win4
_AFXWIN_INLINE HICON CButton::SetIcon(HICON hIcon)
{ ASSERT(::IsWindow(m_hWnd)); return (HICON)::SendMessage(m_hWnd, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon); }
_AFXWIN_INLINE HICON CButton::GetIcon() const
{ ASSERT(::IsWindow(m_hWnd)); return (HICON)::SendMessage(m_hWnd, BM_GETIMAGE, IMAGE_ICON, 0L); }
_AFXWIN_INLINE HBITMAP CButton::SetBitmap(HBITMAP hBitmap)
{ ASSERT(::IsWindow(m_hWnd)); return (HBITMAP)::SendMessage(m_hWnd, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap); }
_AFXWIN_INLINE HBITMAP CButton::GetBitmap() const
{ ASSERT(::IsWindow(m_hWnd)); return (HBITMAP)::SendMessage(m_hWnd, BM_GETIMAGE, IMAGE_BITMAP, 0L); }
_AFXWIN_INLINE HCURSOR CButton::SetCursor(HCURSOR hCursor)
{ ASSERT(::IsWindow(m_hWnd)); return (HCURSOR)::SendMessage(m_hWnd, BM_SETIMAGE, IMAGE_CURSOR, (LPARAM)hCursor); }
_AFXWIN_INLINE HCURSOR CButton::GetCursor()
{ ASSERT(::IsWindow(m_hWnd)); return (HCURSOR)::SendMessage(m_hWnd, BM_GETIMAGE, IMAGE_CURSOR, 0L); }