[MFC]no subclass, no derivation, just redirect one function of CButton, is it possible?
-
As shown below, there are a few functions in CButton, but i want to change its behaviour by dealing with
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
. Yes, just this one and only one functon. It would generate more code than expected if I derive a class from CButton. I wonder if it possible to process this function without Subclass, and not use derivation. If so, how? Remarks: I must use MFC, it's so hard to use pure Win32 API.
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)
AFX_ANSI_DEPRECATED BOOL GetIdealSize(_Out_ LPSIZE psize) const;
AFX_ANSI_DEPRECATED BOOL SetImageList(_In_ PBUTTON_IMAGELIST pbuttonImagelist);
AFX_ANSI_DEPRECATED BOOL GetImageList(_In_ PBUTTON_IMAGELIST pbuttonImagelist) const;
AFX_ANSI_DEPRECATED BOOL SetTextMargin(_In_ LPRECT pmargin);
AFX_ANSI_DEPRECATED BOOL GetTextMargin(_Out_ LPRECT pmargin) const;
#endif // (_WIN32_WINNT >= 0x501)#if ( _WIN32_WINNT >= 0x0600 ) && defined(UNICODE)
CString GetNote() const;
_Check_return_ BOOL GetNote(_Out_z_cap_(*pcchNote) LPTSTR lpszNote, _Inout_ UINT* pcchNote) const;
BOOL SetNote(_In_z_ LPCTSTR lpszNote);
UINT GetNoteLength() const;
BOOL GetSplitInfo(_Out_ PBUTTON_SPLITINFO pInfo) const;
BOOL SetSplitInfo(_In_ PBUTTON_SPLITINFO pInfo);
UINT GetSplitStyle() const;
BOOL SetSplitStyle(_In_ UINT nStyle);
BOOL GetSplitSize(_Out_ LPSIZE pSize) const;
BOOL SetSplitSize(_In_ LPSIZE pSize);
CImageList* GetSplitImageList() const;
BOOL SetSplitImageList(_In_ CImageList* pSplitImageList);
TCHAR GetSplitGlyph() const;
BOOL SetSplitGlyph(_In_ TCHAR chGlyph);
BOOL SetDropDownState(_In_ BOOL fDropDown);// Sets whether the action associated with the button requires elevated permissions. // If elevated permissions are required then the button should display
-
As shown below, there are a few functions in CButton, but i want to change its behaviour by dealing with
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
. Yes, just this one and only one functon. It would generate more code than expected if I derive a class from CButton. I wonder if it possible to process this function without Subclass, and not use derivation. If so, how? Remarks: I must use MFC, it's so hard to use pure Win32 API.
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)
AFX_ANSI_DEPRECATED BOOL GetIdealSize(_Out_ LPSIZE psize) const;
AFX_ANSI_DEPRECATED BOOL SetImageList(_In_ PBUTTON_IMAGELIST pbuttonImagelist);
AFX_ANSI_DEPRECATED BOOL GetImageList(_In_ PBUTTON_IMAGELIST pbuttonImagelist) const;
AFX_ANSI_DEPRECATED BOOL SetTextMargin(_In_ LPRECT pmargin);
AFX_ANSI_DEPRECATED BOOL GetTextMargin(_Out_ LPRECT pmargin) const;
#endif // (_WIN32_WINNT >= 0x501)#if ( _WIN32_WINNT >= 0x0600 ) && defined(UNICODE)
CString GetNote() const;
_Check_return_ BOOL GetNote(_Out_z_cap_(*pcchNote) LPTSTR lpszNote, _Inout_ UINT* pcchNote) const;
BOOL SetNote(_In_z_ LPCTSTR lpszNote);
UINT GetNoteLength() const;
BOOL GetSplitInfo(_Out_ PBUTTON_SPLITINFO pInfo) const;
BOOL SetSplitInfo(_In_ PBUTTON_SPLITINFO pInfo);
UINT GetSplitStyle() const;
BOOL SetSplitStyle(_In_ UINT nStyle);
BOOL GetSplitSize(_Out_ LPSIZE pSize) const;
BOOL SetSplitSize(_In_ LPSIZE pSize);
CImageList* GetSplitImageList() const;
BOOL SetSplitImageList(_In_ CImageList* pSplitImageList);
TCHAR GetSplitGlyph() const;
BOOL SetSplitGlyph(_In_ TCHAR chGlyph);
BOOL SetDropDownState(_In_ BOOL fDropDown);// Sets whether the action associated with the button requires elevated permissions. // If elevated permissions are required then the button should display
I'm not sure I understand exactly what you want to do, but I do not think there is any way to override a single method unless you derive your own class from
CButton
. The documentation at http://msdn.microsoft.com/en-us/library/y0k9f0a4.aspx[^] shows an example. -
I'm not sure I understand exactly what you want to do, but I do not think there is any way to override a single method unless you derive your own class from
CButton
. The documentation at http://msdn.microsoft.com/en-us/library/y0k9f0a4.aspx[^] shows an example.then, how about Hook?
-
As shown below, there are a few functions in CButton, but i want to change its behaviour by dealing with
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
. Yes, just this one and only one functon. It would generate more code than expected if I derive a class from CButton. I wonder if it possible to process this function without Subclass, and not use derivation. If so, how? Remarks: I must use MFC, it's so hard to use pure Win32 API.
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)
AFX_ANSI_DEPRECATED BOOL GetIdealSize(_Out_ LPSIZE psize) const;
AFX_ANSI_DEPRECATED BOOL SetImageList(_In_ PBUTTON_IMAGELIST pbuttonImagelist);
AFX_ANSI_DEPRECATED BOOL GetImageList(_In_ PBUTTON_IMAGELIST pbuttonImagelist) const;
AFX_ANSI_DEPRECATED BOOL SetTextMargin(_In_ LPRECT pmargin);
AFX_ANSI_DEPRECATED BOOL GetTextMargin(_Out_ LPRECT pmargin) const;
#endif // (_WIN32_WINNT >= 0x501)#if ( _WIN32_WINNT >= 0x0600 ) && defined(UNICODE)
CString GetNote() const;
_Check_return_ BOOL GetNote(_Out_z_cap_(*pcchNote) LPTSTR lpszNote, _Inout_ UINT* pcchNote) const;
BOOL SetNote(_In_z_ LPCTSTR lpszNote);
UINT GetNoteLength() const;
BOOL GetSplitInfo(_Out_ PBUTTON_SPLITINFO pInfo) const;
BOOL SetSplitInfo(_In_ PBUTTON_SPLITINFO pInfo);
UINT GetSplitStyle() const;
BOOL SetSplitStyle(_In_ UINT nStyle);
BOOL GetSplitSize(_Out_ LPSIZE pSize) const;
BOOL SetSplitSize(_In_ LPSIZE pSize);
CImageList* GetSplitImageList() const;
BOOL SetSplitImageList(_In_ CImageList* pSplitImageList);
TCHAR GetSplitGlyph() const;
BOOL SetSplitGlyph(_In_ TCHAR chGlyph);
BOOL SetDropDownState(_In_ BOOL fDropDown);// Sets whether the action associated with the button requires elevated permissions. // If elevated permissions are required then the button should display
-
then, how about Hook?
Falconapollo wrote:
how about Hook?
What does that have to do with drawing a
CButton
? Hooks are system wide so you would need to capture every drawing message and then decide whether it was for your control or not. That's like using a big hammer to crack a small nut. -
As shown below, there are a few functions in CButton, but i want to change its behaviour by dealing with
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
. Yes, just this one and only one functon. It would generate more code than expected if I derive a class from CButton. I wonder if it possible to process this function without Subclass, and not use derivation. If so, how? Remarks: I must use MFC, it's so hard to use pure Win32 API.
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)
AFX_ANSI_DEPRECATED BOOL GetIdealSize(_Out_ LPSIZE psize) const;
AFX_ANSI_DEPRECATED BOOL SetImageList(_In_ PBUTTON_IMAGELIST pbuttonImagelist);
AFX_ANSI_DEPRECATED BOOL GetImageList(_In_ PBUTTON_IMAGELIST pbuttonImagelist) const;
AFX_ANSI_DEPRECATED BOOL SetTextMargin(_In_ LPRECT pmargin);
AFX_ANSI_DEPRECATED BOOL GetTextMargin(_Out_ LPRECT pmargin) const;
#endif // (_WIN32_WINNT >= 0x501)#if ( _WIN32_WINNT >= 0x0600 ) && defined(UNICODE)
CString GetNote() const;
_Check_return_ BOOL GetNote(_Out_z_cap_(*pcchNote) LPTSTR lpszNote, _Inout_ UINT* pcchNote) const;
BOOL SetNote(_In_z_ LPCTSTR lpszNote);
UINT GetNoteLength() const;
BOOL GetSplitInfo(_Out_ PBUTTON_SPLITINFO pInfo) const;
BOOL SetSplitInfo(_In_ PBUTTON_SPLITINFO pInfo);
UINT GetSplitStyle() const;
BOOL SetSplitStyle(_In_ UINT nStyle);
BOOL GetSplitSize(_Out_ LPSIZE pSize) const;
BOOL SetSplitSize(_In_ LPSIZE pSize);
CImageList* GetSplitImageList() const;
BOOL SetSplitImageList(_In_ CImageList* pSplitImageList);
TCHAR GetSplitGlyph() const;
BOOL SetSplitGlyph(_In_ TCHAR chGlyph);
BOOL SetDropDownState(_In_ BOOL fDropDown);// Sets whether the action associated with the button requires elevated permissions. // If elevated permissions are required then the button should display
WTL is a light weight template library for creating UI controls. Michael Dunn[^] has written some great articles about WTL. These couple of articles would be useful for you - WTL for MFC Programmers, Part IV - Dialogs and Controls[^] WTL for MFC Programmers, Part V - Advanced Dialog UI Classes[^]
«_Superman_» _I love work. It gives me something to do between weekends.
_Microsoft MVP (Visual C++) (October 2009 - September 2013)