I guess this is generic issue and not limited to ComboBox, however I have specifically problem with ComboBox. I extended ComboBox object with MyCB MyCB : ComboBox
)
What happens is every time I hover over the control, leave the control, expand selection box or select a value, the control flickers. For a short while I can see default (non-replaced) control which is being instantly replaced with mine.
I believe what's happening is that Windows first draws the "original" control (by calling base.WndProc()
) and then repaints it with mine.
The question is, can I somehow stop windows from painting it's own control and instantly paint mine?
Below is code overriding WndProc
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
Graphics gg = this.CreateGraphics();
gg.FillRectangle(BorderBrush, this.ClientRectangle);
// ... //
//Draw the arrow
gg.FillPath(ArrowBrush, pth);
// ... //
if(this.Text == "")
gg.DrawString("-- SELECT --", this.Font, new SolidBrush(Color.Black), rf, sf);
else
gg.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), rf, sf);
gg.Dispose();
}
}
What have I tried so far:
I know that I can't do this:
if (m.Msg == WM_PAINT) { ... } else { base.WndProc(ref m); }
as that will cause control to repaint itself infinitely (not sure why)
I was able to remove flickering which happened when mouse leaves/enters the control by adding this code
if (m.Msg == WM_MOUSEFIRST || m.Msg == WM_MOUSELEAVE) // 0x0200 0x02A3 { m.Result = (IntPtr)1; } else { base.WndProc(ref m); if (m.Msg == WM_PAINT) { ... } }
however this doesn't solve the problem completely
- I looked into ILSpy to check on ComboBox's WndProc but there were so many windows messages that I didn't know which of those I could possibly immitate to achive my goal