Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I put my MFC Control initialization code

I'm writing an MFC CEdit derived control, and I need to add initialization code once the control's m_hwnd is filled.

Which function can I override or which message can I handle to achieve this?

I tried with OnCreate, but It seems to work only for dialogs

EDIT: The thing I'm initializing is the edit's cue banner

Thanks

like image 510
Goldorak84 Avatar asked Jul 13 '12 15:07

Goldorak84


2 Answers

Following Mark Ransom's hint, I finally found a better function to implement my intitialization. While overloading CWnd::SubclassWindow is a good idea, this function is not virtual and it would require a call from the subclass pointer. Calling SubclassWindow from a CWnd* would not work.

I found the function CWnd::PreSubclassWindow. It's virtual and is called just before SubclassWindow. Since m_hwnd is valid there, it is a good place to write the code I need. In addition, the function is virtual and is called automatically by the framework so I don't need to worry about having the good pointer type

like image 58
Goldorak84 Avatar answered Sep 20 '22 23:09

Goldorak84


OnCreate doesn't work if the control is on a dialog, because the control is created before it can be subclassed to your window class - that happens in the dialog's DoDataExchange.

You can override CWnd::SubclassWindow and call the base method before your own code.

like image 21
Mark Ransom Avatar answered Sep 24 '22 23:09

Mark Ransom