问题
Can anyone please help me understand how to override OnPaint()
for a dialog class derived from CDialog
.
Here is the code:
ColorImageDlg *pDlg = NULL;
pDlg = new ColorImageDlg;
pDlg->DoModal();
delete pDlg;
I'm overriding OnInitDialog()
and it's getting called.
But while overriding OnPaint()
it is not getting called.
Can any one please help me fixing it?
回答1:
First of all what is the point of creating the instance of the dialog on heap? You can simply do:
ColorImageDlg dlg;
dlg.DoModal();
You need to modify your message map like this:
BEGIN_MESSAGE_MAP(ColorImageDlg, CDialog)
ON_WM_PAINT()
END_MESSAGE_MAP()
Use VS Class Wizard to avoid problems like that.
回答2:
If you can't use the ClassWizard then there is another way. Here is a resource about it:
(VS2015 version) https://msdn.microsoft.com/en-us/library/dey7ke4c.aspx
(VS2008 version) https://msdn.microsoft.com/en-us/library/dey7ke4c(v=vs.90).aspx)
But basically, once you have defined the dialog resource and attached it to a new class, make sure the Class View tab is selected:
Next, make sure your dialog class is selected in the class view:
Then, click on the Messages icon of the Properties panel:
Scroll down the list of messages and locate WM_PAINT
. Then click the dropdown arrow and select the option to add it:
As you can see, it has inserted all the needed code:
Hope this helps.
来源:https://stackoverflow.com/questions/37722069/i-have-added-an-onpaint-function-to-my-dialog-class-but-its-not-getting-called