Re: ? Create Not Called in Dialog for Subclassed Control

Tech-Archive recommends: Fix windows errors by optimizing your registry



See below...
On Wed, 17 Dec 2008 13:01:21 -0500, "Alec S." <nospam@xxxxxxxxx> wrote:

Okay, I'm baffled.

I've created a control derived from CListCtrl. I want to have it set itself to
be a virtual list control (ie not have to use the resource editor to do that).

The problem is that LVS_OWNERDATA can only be applied when the control is
created, not changed later. The bigger problem is that because it's a subclassed
control in a dialog box, it does not get Create, OnCreate, or PreCreateWindow
called before it is subclassed, on PreSubclassWindow is called, and it is too
late at that point because it is already created.
****
So you create it at design time with the OwnerData flag.

PreSubclassWindow:
ASSERT(GetStyle() & LVS_OWNERDATA); // requires Owner Data style

You can also consider adding, instead,
OnInitDialog:

if( (list.GetStyle() & LVS_OWNERDATA) == 0)
{ /* not ownerdata */
DWORD style = list.GetStyle();
style |= LVS_OWNERDATA;
DWORD id = list.GetDlgCtrlId();

CRect r;
list.GetWindowRect(&r);
ScreenToClient(&r);

CMyListCtrl newctl;
newctl.Create(style, r, this, id);
newctl.SetWindowPos(&list, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
list.DestroyWindow();
list.Attach(newctl.Detach();
} /* not ownerdata */

Note that there is nothing that disallows having, for a short period of time, two controls
with the same ID, because, during the time when these two controls exist, nobody is trying
to access them by control ID.

Note that this trick CANNOT be used in the PreSubclassWindow handler because the
SubclassWindow function (that calls PreSubclassWindow) retains the original HWND of the
window, which this code would invalidate. You can also move this code into your subclass
and call in EnsureUserData, and therefore call
list.EnsureUserData();
you would just change a few parameters of the code to make it work in the context (such as
using GetParent() instead of this for the parent...)
*****

I have found a few posts where people explained the above points, but no
solutions were provided. I have also found a few pages in which people applied
the LVS_OWNERDATA style in PreCreateWindow or Create, but I can only assume they
were using a form-view instead of a dialog (or else the code wasn't tested and
didn't actually work).

Again, I'm stumped. How can I set a style of a control in a dialog at creation
if it is subclassed? (It is subclassed via the dialog?s DoDataExchange.)
****
You won't ever get a notification such as PreCreateWindow or OnCreate because the window
has already been created long before the subclassing
joe
****

(I recently had VS put in the code for each of those functions then used
breakpoints to see which ones actually get called, and I could swear that at
least one of the create ones did, so I don't know why they are not getting hit
now.)


Thanks.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: CDialog -> SubclassWindow von eigenem Control (abgeleitet von CWnd)
    ... ist es möglich in einer abgeleiteten CDialog-Klasse ein eigenes Control zu "SubclassWindow", ohne dieses im Resourcen-Editor in das Dialog-Template einzutragen? ... Meine eigenes Control soll in einer eigenen CDialog-Klasse gesubclasst und somit immer vorhanden sein, ...
    (microsoft.public.de.vc)
  • CDialog -> SubclassWindow von eigenem Control (abgeleitet von CWnd)
    ... ist es möglich in einer abgeleiteten CDialog-Klasse ein eigenes Control zu "SubclassWindow", ohne dieses im Resourcen-Editor in das Dialog-Template einzutragen? ... Meine eigenes Control soll in einer eigenen CDialog-Klasse gesubclasst und somit immer vorhanden sein, ...
    (microsoft.public.de.vc)
  • Re: Ownerdraw CListBox - dont get it
    ... If you had a control variable for your listbox, e.g., ... I had a standard listbox working in a dialog. ... I derived a class from CListBox, ... in fact, DDX_Control actually calls SubclassWindow). ...
    (microsoft.public.vc.mfc)
  • Re: Child Window Creation
    ... Is this in the context of subclassing a dialog control, ... but it was getting handled before the window ... DDX_Control, and you post a message from within PreSubclassWindow, ...
    (microsoft.public.vc.mfc)
  • Re: How can I subclass a control on a Formview
    ... OnPreSubclassWindow but somehow reading PreSubclassWindow, ... PreSubclassWIndow is called only if you have a control variable bound to the control; ... I want to save the User selected colours in the registry. ... FormView OnInitialUpdate. ...
    (microsoft.public.vc.mfc)