If you import all necessary functions from gdi32.dll you can try something like this:
private void DrawRectangle(bool draw)
{
Graphics gr = Graphics.FromHwnd(this.Handle);
if( this.IsRectVisible == true )//erase the rect if it's there
{
IntPtr hdc = gr.GetHdc();
SetROP2(hdc, R2_MERGEPENNOT);
MoveToEx(hdc, theRectDownX, theRectDownY, IntPtr.Zero );
LineTo(hdc, theRectDownX, theRectUpY);
LineTo(hdc, theRectUpX, theRectUpY);
LineTo(hdc, theRectUpX, theRectDownY);
LineTo(hdc, theRectDownX, theRectDownY);
gr.ReleaseHdc();
this.IsRectVisible = false;
}
if (draw) //draw selection rectangle
{
this.theRectDownX = Math.Min(this.myXmin, this.myXmax);
this.theRectDownX = Math.Min(this.theButtonDownX, this.theRectDownX);
this.theRectDownY = Math.Min(this.myYmin, this.myYmax);
this.theRectDownY = Math.Min(this.theButtonDownY, this.theRectDownY);
this.theRectUpX = Math.Max(this.myXmin, this.myXmax);
this.theRectUpX = Math.Max(this.theButtonDownX, this.theRectUpX);
this.theRectUpY = Math.Max(this.myYmin, this.myYmax);
this.theRectUpY = Math.Max(this.theButtonDownY, this.theRectUpY);
IntPtr hdc = gr.GetHdc();
SetROP2(hdc, R2_MERGEPENNOT);
MoveToEx(hdc, theRectDownX, theRectDownY, IntPtr.Zero);
LineTo(hdc, theRectDownX, theRectUpY);
LineTo(hdc, theRectUpX, theRectUpY);
LineTo(hdc, theRectUpX, theRectDownY);
LineTo(hdc, theRectDownX, theRectDownY);
gr.ReleaseHdc();
this.IsRectVisible=true;
}
}
There should be no need for the extra loop.
Commenter-1