Form graphics not set when form loads

后端 未结 2 769
感动是毒
感动是毒 2021-01-26 19:02

My form has a group box which contains two overlapping rectangles. The form\'s other controls are two sets of four numeric up down controls to set the rectangles\' colors. (nud

相关标签:
2条回答
  • 2021-01-26 19:20

    A group box, like any control, will paint itself when Windows sends it the WM_PAINT message. You can indeed use Control.CreateGraphics() to draw yourself, bypassing the normal paint logic. But that's not going to last and will randomly disappear when Windows decides that the control needs to repaint itself again. Which is obvious when you minimize the form and restore it. Less obvious on Vista and Win7 with Aero enabled, repaints are not necessary when your form is overlapped by another window. But it will be quite obvious on XP or with Aero disabled.

    You cannot make this work reliably, you have to use the Paint event. Not your form's, the group box controls'. Call its Invalidate() method to force it to repaint when the color changes.

    0 讨论(0)
  • 2021-01-26 19:32

    I suspect that painting in the Load event is a little premature. Try painting in the Paint event.

    Or:

    Could you just use a couple of Panel controls, and set their background colors?

    0 讨论(0)
提交回复
热议问题