Doing this is really easy, but when you add a submenu, you can see that the submenu arrow appears automatically. You could think 'cool, less work for me !'. But windows draws the arrow in white when selected. And that is not what we want !!
And now, how do we tell to windows not to draw that submenu arrow ? I've gone through all the MSDN and no one say anything about it. My first try was to add a clip region to my Graphics object, I thought it would prevent windows from drawing it, but it had no effect.
But this way I discovered something interesting : Gdi+ clipping and Gdi clipping are totally independent. If you define a clipping region with Gdi+, you can still draw in that region using Gdi, and the reverse thing is also true. To prevent windows from drawing the submenu arrow, you must clip the region using the Gdi API.
Once you known it, the answer is really simple :
private static void ProtectFromDrawing(Graphics g,
Rectangle rect)
{
IntPtr hdc = g.GetHdc();
try
{
ExcludeClipRect (hdc,
rect.Left,
rect.Top,
rect.Right,
rect.Bottom);
}
finally
{
g.ReleaseHdc(hdc);
}
}
[DllImport("Gdi32.dll")]
public static extern int ExcludeClipRect(
IntPtr hdc, // handle to DC
int nLeftRect, // x-coord of upper-left corner
int nTopRect, // y-coord of upper-left corner
int nRightRect, // x-coord of lower-right corner
int nBottomRect // y-coord of lower-right corner
);
You just have to call ProtectFromDrawing at the end of your drawing function.
Partager