Initializing a control's member variable from device context - c++

I'm writing a custom control derived from CWnd. I want to initialize a member variable (e.g: a memory dc) of my custom control class using its device context. Which is the correct way to do it? I guess device context won't be ready in the constructor. So what is the next option. Is it safe using the dc the OnCreate event? I'm probably in search of something like OnInitDialog, but its a custom control, not a dialog.
Update: I added the custom control to the parent dialog via resource editor. So there is no chance for getting WM_CREATE event as it has already been created before subclassing.

Device context can be created in OnCreate (WM_CREATE), after you call the base class' OnCreate method.
http://msdn.microsoft.com/en-us/library/dd318297(v=vs.85).aspx
From Programming Windows

Related

Possible to Superclass a Dialog Box in Win32?

I'm using raw Win32 and C++ for a project. As I understand it, I am able to superclass Windows controls by retrieving the class information, replacing the procedure, then registering this as a new class and using it when creating a new window. Subclassing is done by replacing the window's procedure after the window is created. The advantage of superclassing is that you are able to process messages before CreateWindow() returns.
I'm looking to see if it's possible to superclass a dialog box created with CreateDialog() because I'd like to use a resource file for the dialog layout. The problem is that I don't know how I would provide my superclass when I create a dialog box. Is it even possible? Any idea how MFC handles this?
If you use an extended dialog box template to create your dialog, you can specify a custom window class as part of the DLGTEMPLATEEX definition.
The dialog manager will create and layout your dialog as normal, and call your window procedure for any dialog messages. You can use the DefDlgProc function to obtain default processing for any dialog messages you don't want to handle yourself.

MFC - Change font in a CMFCToolTipCtrl used in CDialogEx control

I'm trying to display a ToolTip for a CStatic derived control in my dialog.
What I've already done:
Added a CMFCToolTipCtrl item to my CDialogEx member.
In the init dialog member I've specified CMFCToolTipInfo structure and passed it as argument in CMFCToolTipCtrl item constructor.
Call the EnableToolTips(); member for my CStaticExts and for my CDialogEx.
Overrided the PreTranslateMessage of my CDialogEx adding the "RelayEvent".
Set "Notify: TRUE" in the resource editor.
Doing so I managed to display the tooltip in a partially customized way (baloon and background color) but now I would like to enlarge the font, make it bold and, eventually, display an icon, similarly to the tool tips I can see on my toolbar.
I already tried calling "SetFont" and "SetIcon" methods for the CMFCToolTipCtrl item but it didn't work.
Is that possible?
The normal Font that is used in CMFCToolTipCtrl ist retrieved from a global data store inside the MFC (see GetGlobalData()->fontTooltip). This data structure AFX_GLOBAL_DATA is filled when the MFC is started. SetFont has no effect here.
If you want to change the behaviour you have to create your own CMFCToolTipCtrl class and overwrite OnDrawLabel. You have the source of the MFC so it is easy to provide your own implementation.

How to register a custom control on ATL/WTL dialog?

I've derived a custom control from ATL::CWindowImpl<CMyCustomControl> and declared DECLARE_WND_CLASS(_T("CMyCustomControl")).
I've also made a dialog resource with a custom control with the class name CMyCustomControl.
How do I go about registering the control properly so I can display it on the dialog?
There are thre typical way to create custom controls in WTL.
DECLARE_WND_CLASS/DECLARE_WND_SUPERCLASS + RegisterClassEx API to register class by name + custom control reference in dialog template to instantiate control through class name
Implement window class, esp. inheriting from CWindowImpl, and create control manually, esp. from OnCreate/OnInitDialog
Implement window class, instantiate standard control through dialog template, and subclass the control instance to alter its behavior (e.g. static with hyperlinks, custom list view, edit control with color highlighting etc)
With all three you need to do more than just a macro in class definition. You will find great examples here: http://www.viksoe.dk/code/all_wtl.htm under "Controls" section.
Certainly the fourth method is implementing an ActiveX control.

messages for cstatic

1.i have derived a class from CStatic called CLed_arr.
2.a cstatic control named IDC_leds was added to the dialog window in resource editor.
3.a member var called m_led_arr of the class CLed_arr was created for this IDC_leds.
4.the class CLed_arr contains an array Led[16] of objects of class CLed.
5.the class CLed itself is derived from CStatic.
6.i did'nt want to put these 16 led windows in resource editor,but wanted to create them at run time.for creation of these 16 windows the co-ordinates of the mother window i.e.IDC_leds are required.i tried the creation process in the CLed_arr constructor.but probably at this instant the window of the class is not available so i get exception.so i wanted to do it using some message handler using the creation the window message of this class i.e. CLed_arr.but the only message available for this class is BN_CLICKED.
7.pls help me in understanding as to why only BN_CLICKED message is available for this window,also what is the method if any available for adding additional messages for this window.
8.i'm able to create these 16 windows if i create them in OnitDialog(),but i want to develop this CLed_arr as a class whose basic procedures are within the class.
pls. hlp.
thnx
In MFC CWnd is the base class for most of the UI classes, for CStatic as well ofcourse.
So try handling WM_CREATE msg from CWnd. It will come once the window is created but just before it gets visible. For more information see here

Custom MFC control prevents dialog creation

I have a custom MFC control, subclassing CWnd. Other than providing OnPaint and PreSubclassWindow implementations, it overrides no default functionality and does nothing weird in construction except registering a window class in the constructor.
The control is added to the dialog using the dialog editor to add a custom control.
The dialog worked when it was a simple modal dialog deriving from CDialog, but we have code which calls CWnd::CreateDlgIndirect to instance dialogs and this fails with the custom control... but works if the custom control is removed from the resource template.
Found it!
I had the custom control register its window class in its own constructor. I had member in the dialog of this custom control type, so the ctor was being called when the dialog was created, as intended.
But, it turns out the base class I changed the dialog to derive from, instead of CDialog, was calling CreateDlgIndirect in its own ctor, before my new class' own initialisation was reached - so it was trying to create custom control before the window class was registered.
My (slightly messy solution) is to ensure the window class registration happens at application startup in the InitInstance method, before any dialog stuff happens.