WTL subclassing - a proper way to subclass a gui window along with its children

WTL subclassing - a proper way to subclass a gui window along with its children

Post by creato » Sun, 07 Mar 2004 04:54:13


Hi,
kind of ot, this is place, is the closest to wtl, so sorry.
could anybody help me, point me to a ws, c++ source code, so on... with
'subclassing' of ComboBoxEx.
Let me put it this way; using WTL how do you properly subclass a ComboBoxEx
and some of the controls in it, so that the code could react to a key stroke
in the Edit control and additionally do some custom drawing?
Some background, if we take windows AddressBar(at least here on xp i'm
saying) and Spy on it, it's a Rebar which is, well you know what; the thing
is a ComboBoxEx32 within it all controls, including ComboBoxEx32 itself, are
subclasses except for the ToolBarWindow32(the go button). I googled for
several days no info on this subject, no why nor how.

the hierarchy:
ComboBoxEx32 <-subclassed
+---ToolBarWindow32
+---ComboBox <-subclassed
+Edit<-subclassed

if you were(what are the chances, are you?) AddressBar's designer/coder
please tell us:
why _exacly_ would you subclass those controls in the first place?
(e.g. answer:)) Edit - so that blah.. blah..would work like this not
that)
how did you implement the Enter in the Edit(i.e. the key we hit after the
url)?
how and why, why did you put ToolBarWindow32 into
ComboBoxEx32(SetParent/Compact code?)?

I would like to learn ATL/WTL and a project came up I took it for practice,
so here I am.

Thank you very much for reading this let alone replying to it.
 
 
 

WTL subclassing - a proper way to subclass a gui window along with its children

Post by Roge » Sun, 07 Mar 2004 23:23:32

"creator" <x@y> wrote in message
news: XXXX@XXXXX.COM ...
ComboBoxEx
stroke
thing
are
practice,

Here's an example of a class which can be used to either create a
superclassed ComboBoxEx window or subclass an existing ComboBoxEx window.
The class sends a NM_RETURN notification to its parent when the Enter key is
pressed.

typedef
CWinTraits<WS_CHILD|WS_BORDER|WS_CLIPCHILDREN|WS_CLIPSIBLINGS|CBS_DROPDOWN,
0> CComboBoxTraits;

class CMyComboBoxEx : public CWindowImpl<CMyComboBoxEx, CComboBoxEx,
CComboBoxTraits>
{
public:
DECLARE_WND_SUPERCLASS(NULL, CComboBoxEx::GetWndClassName())

CContainedWindowT<CEdit> m_wndEdit;

CMyComboBoxEx() :
m_wndEdit(this, 1)
{
}

BOOL SubclassWindow(HWND hWnd)
{
BOOL bRet = CWindowImplBaseT< CComboBoxEx,
CComboBoxTraits>::SubclassWindow(hWnd);
if(bRet)
{
HWND hWnd = GetEditCtrl();
ATLASSERT(::IsWindow(hWnd));
m_wndEdit.SubclassWindow(hWnd);
}
return bRet;
}

BEGIN_MSG_MAP(CMyComboBoxEx)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
ALT_MSG_MAP(1)
MESSAGE_HANDLER(WM_KEYDOWN, OnEditKeyDown)
END_MSG_MAP()

LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&
/*bHandled*/)
{
LRESULT lres = DefWindowProc(uMsg, wParam, lParam);

HWND hWnd = GetEditCtrl();
ATLASSERT(::IsWindow(hWnd));
m_wndEdit.SubclassWindow(hWnd);

return lres;
}

LRESULT OnEditKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL&
bHandled)
{
switch (wParam)
{
case VK_RETURN:
{
UINT idFrom = (UINT)GetWindowLong(GWL_ID);
NMHDR nmhdr = {m_hWnd, idFrom, NM_RETURN};
::SendMessage(GetParent(), WM_NOTIFY, (WPARAM)idFrom, (LPARAM)&nmhdr);
}
return 0;
}

bHandled = FALSE;
return 1;
}

};

If you create an app using the WTL wizard, you will need to add the
ICC_USEREX_CLASSES flag to the AtlInitCommonControls call that is made in
the app's CPP file.

-Roger



 
 
 

WTL subclassing - a proper way to subclass a gui window along with its children

Post by creato » Mon, 08 Mar 2004 03:56:58

k, thanks a lot, is just I googled a little more and found the code myself
a while ago.
I appreciate your help.

One more thing, can you please suggest/help me, how to _properly_(not just
looks the same, done right) add controls into ComboBoxEx, like the shell has
the 'go' button on the address bar?

You can download the source of the class, the executable(as always; nothing
malicious, but use it at your own risk), and some screen shots.

if you need to see the whole source tree pls let me know.

in case you will not execute the code, on the shots, I can't make the rest
of that 'embedded' toolbar, to the right from the 'go' button, stop
flickering while the bar is resized. I highlighted the area red.

http://elnour.biz/files/addrbar.zip

thanks

"Roger" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
the
is
CWinTraits<WS_CHILD|WS_BORDER|WS_CLIPCHILDREN|WS_CLIPSIBLINGS|CBS_DROPDOWN,


 
 
 

WTL subclassing - a proper way to subclass a gui window along with its children

Post by Roge » Mon, 08 Mar 2004 20:49:46

have not looked at your code, but I've done an "addressbar" in the past.
Some points I remember are:

1. Subclass the internal combobox using a CContainedWindowT member. Handle
the WM_WINDOWPOSCHANGING message sent to the internal combobox by changing
the cx member of the WINDOWPOS structure to cx - cxToolBar where cxToolBar
is the known width of the toolbar. This should cause the internal combobox
to be positioned correctly with a minimum of flickering.

2. Subclass the edit control as I previously posted so its WM_KEYDOWN
message can be handled.

3. Handle the WM_SIZE message sent to your CWindowImp/ComboBoxEx class by
first passing the message to DefWindowProc, and then positioning the toolbar
with SetWindowPos.

I dont remember exactly what I did to erase the toolbar's background and I
cant find my code. I believe I handled the NM_CUSTOMDRAW/CDDS_PREPAINT
notification sent to the ComboBoxEx by the toolbar by erasing the toolbar's
background and returning CDRF_DODEFAULT.

-Roger

"creator" <x@y> wrote in message
news:% XXXX@XXXXX.COM ...
myself
has
nothing
with
itself,
for
designer/coder
not
window.
key
CWinTraits<WS_CHILD|WS_BORDER|WS_CLIPCHILDREN|WS_CLIPSIBLINGS|CBS_DROPDOWN,
(LPARAM)&nmhdr);
in