I am creating a Windows Forms control derived from UserControl to be embedded in a WPF app. I have generally followed the procedures given in this link.
public ref class CTiledImgViewControl : public UserControl { ... virtual void OnPaint( PaintEventArgs^ e ) override; ... };
And in my CPP file:
void CTiledImgViewControl::OnPaint( PaintEventArgs^ e ) { UserControl::OnPaint(e); // do something interesting... }
Everything compiles and runs, however the OnPaint method is never getting called.
Any ideas of things to look for? I’ve done a lot with C++, but am pretty new to WinForms and WPF, so it could well be something obvious…
The
OnPaintwon’t normally get called in aUserControlunless you set the appropriate style when it is constructed using theSetStylemethod. You need to set theUserPaintstyle to true for theOnPaintto get called.Update
I recently encountered this issue myself and went digging for an answer. I wanted to perform some calculations during a paint (to leverage the unique handling of paint messages) but I wasn’t always getting a call to
OnPaint.After digging around with Reflector, I discovered that
OnPaintis only called if the clipping rectangle of the correspondingWM_PAINTmessage is not empty. MyUserControlinstance had a child control that filled its entire client region and therefore, clipped it all. This meant that the clipping rectangle was empty and so noOnPaintcall.I worked around this by overriding
WndProcand adding a handler forWM_PAINTdirectly as I couldn’t find another way to achieve what I wanted.