Hello Rainer,
I can reproduce this problem. When specify a TransparencyKey for the form,
the form become a layered window, it seems that
ControlPaint.DrawReversibleFrame() method draws figures beneath the
layered
window. Anyway, you can call the Rectangle() API to draw the rubber band
on
the form, and call ControlPaint.DrawReversibleFrame() method to draw
rubber
band outside the form.
For example:
private void MyDrawReversibleRectangle(Point p1, Point p2)
{
Rectangle rc = new Rectangle();
GDI32 gdi = new GDI32();
gdi.DrawRectangle(this.CreateGraphics(), p1, p2);
// Convert the points to screen coordinates.
p1 = PointToScreen(p1);
p2 = PointToScreen(p2);
// Normalize the rectangle.
if (p1.X < p2.X)
{
rc.X = p1.X;
rc.Width = p2.X - p1.X;
}
else
{
rc.X = p2.X;
rc.Width = p1.X - p2.X;
}
if (p1.Y < p2.Y)
{
rc.Y = p1.Y;
rc.Height = p2.Y - p1.Y;
}
else
{
rc.Y = p2.Y;
rc.Height = p1.Y - p2.Y;
}
// Draw the reversible frame.
ControlPaint.DrawReversibleFrame(rc,
Color.Black, FrameStyle.Thick);
}
The GDI32 class:
public enum RasterOps
{
R2_BLACK = 1,
R2_NOTMERGEPEN,
R2_MASKNOTPEN,
R2_NOTCOPYPEN,
R2_MASKPENNOT,
R2_NOT,
R2_XORPEN,
R2_NOTMASKPEN,
R2_MASKPEN,
R2_NOTXORPEN,
R2_NOP,
R2_MERGENOTPEN,
R2_COPYPEN,
R2_MERGEPENNOT,
R2_MERGEPEN,
R2_WHITE,
R2_LAST
}
public enum BrushStyles
{
BS_SOLID = 0,
BS_NULL = 1,
BS_HATCHED = 2,
BS_PATTERN = 3,
BS_INDEXED = 4,
BS_DIBPATTERN = 5,
BS_DIBPATTERNPT = 6,
BS_PATTERN8X8 = 7,
BS_MONOPATTERN = 9
}
public enum PenStyles
{
PS_SOLID = 0,
PS_DASH = 1,
PS_DOT = 2,
PS_DASHDOT = 3,
PS_DASHDOTDOT = 4
}
class GDI32
{
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool Rectangle(IntPtr hdc, int X1, int Y1,
int X2, int Y2);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreatePen(PenStyles enPenStyle, int
nWidth, int crColor);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreateSolidBrush(BrushStyles
enBrushStyle, int crColor);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool DeleteObject(IntPtr hObject);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr SelectObject(IntPtr hdc, IntPtr
hObject);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern int SetROP2(IntPtr hdc, int enDrawMode);
protected Color borderColor;
protected Color fillColor;
protected int lineWidth;
protected IntPtr hdc, oldBrush, oldPen, gdiPen, gdiBrush;
protected BrushStyles brushStyle;
protected PenStyles penStyle;
public GDI32()
{
borderColor = Color.Transparent;
fillColor = Color.White;
lineWidth = 1;
brushStyle = BrushStyles.BS_NULL;
penStyle = PenStyles.PS_SOLID;
}
public Color BrushColor
{
get { return fillColor; }
set { fillColor = value; }
}
public BrushStyles BrushStyle
{
get { return brushStyle; }
set { brushStyle = value; }
}
public Color PenColor
{
get { return borderColor; }
set { borderColor = value; }
}
public PenStyles PenStyle
{
get { return penStyle; }
set { penStyle = value; }
}
public int PenWidth
{
get { return lineWidth; }
set { lineWidth = value; }
}
protected int GetRGBFromColor(Color fromColor)
{
return fromColor.ToArgb() & 0xFFFFFF;
}
public void DrawRectangle(Graphics g, Point p1, Point p2)
{
InitPenAndBrush(g);
Rectangle(hdc, p1.X, p1.Y, p2.X, p2.Y);
Dispose(g);
}
protected void InitPenAndBrush(Graphics g)
{
hdc = g.GetHdc();
gdiPen = CreatePen(penStyle, lineWidth,
GetRGBFromColor(PenColor));
gdiBrush = CreateSolidBrush(brushStyle,
GetRGBFromColor(fillColor));
if (PenColor == Color.Transparent) SetROP2(hdc,
(int)RasterOps.R2_XORPEN);
oldPen = SelectObject(hdc, gdiPen);
oldBrush = SelectObject(hdc, gdiBrush);
}
protected void Dispose(Graphics g)
{
SelectObject(hdc, oldBrush);
SelectObject(hdc, oldPen);
DeleteObject(gdiPen);
DeleteObject(gdiBrush);
g.ReleaseHdc(hdc);
g.Dispose();
}
}
If you have any questions or concerns, please don't hesitate to let me
know.
Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support
Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.