You can, but you should know that the graphics rendered in this event won't be permanently kept on this control; very first invalidation will overwrite the pixels you just rendered with the graphics rendered by overridden
OnPaint
, and, if
base.OnPaint
was called, the event handlers of the event
Paint
would also come into the game. This is how it works.
If you can take it into account in your logic, you could render something using the instance of
System.Drawing.Graphics
obtained from the window using the method
System.Drawing.Graphics.FromHwnd
:
https://msdn.microsoft.com/en-us/library/system.drawing.graphics.fromhwnd(v=vs.110).aspx,
https://msdn.microsoft.com/en-us/library/system.drawing.graphics%28v=vs.110%29.aspx.
However, I would not advise you to do that, unless you understand very well what you are doing.
Alternatively, you could better do all the rendering in your
OnPaint
. This is the idea: you render all graphics from some data model, vector graphics data kept in memory. On click, you modify the data model according to click data, and then immediately invalidate all the scene or part of it using one of the these methods:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.invalidate%28v=vs.110%29.aspx.
Internally, this is all based on processing of
WM_PAINT
window message, which is very cunning and highly optimized. For some background, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...)),
capture the drawing on a panel,
Drawing Lines between mdi child forms,
How to speed up my vb.net application?,
Zoom image in C# .net mouse wheel.
—SA