my Problem is, that i want to draw a rectangle over an existing Textbox.
I have a solution now, but the Textbox is always redrawing, which is a behavior i do not want.
here is the code
private bool isDragging = false;
void Form2_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
endPos = e.Location;
Rectangle rect;
if (endPos.Y > startPos.Y)
{
rect = new Rectangle(startPos.X, startPos.Y,
endPos.X - startPos.X, endPos.Y - startPos.Y);
}
else
{
rect = new Rectangle(endPos.X, endPos.Y,
startPos.X - endPos.X, startPos.Y - endPos.Y);
}
Region dragRegion = new Region(rect);
this.Invalidate();
}
}
void Form2_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
this.Invalidate();
}
protected override CreateParams CreateParams
{
get
{
CreateParams cp;
cp = base.CreateParams;
cp.Style &= 0x7DFFFFFF; //WS_CLIPCHILDREN
return cp;
}
}
void Form2_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
startPos = e.Location;
}
// this is where we intercept the Paint event for the TextBox at the OS level
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 15: // this is the WM_PAINT message
// invalidate the TextBox so that it gets refreshed properly
Input.Invalidate();
// call the default win32 Paint method for the TextBox first
base.WndProc(ref m);
// now use our code to draw extra stuff over the TextBox
break;
default:
base.WndProc(ref m);
break;
}
}
protected override void OnPaint(PaintEventArgs e)
{
if (isDragging)
{
using (Pen p = new Pen(Color.Gray))
{
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
e.Graphics.DrawRectangle(p,
startPos.X, startPos.Y,
endPos.X - startPos.X, endPos.Y - startPos.Y);
}
}
base.OnPaint(e);
}
the problem here is, the textbox is flickering, and the Rectangle is not set in front of the textbox after finish dragging.
How do i solve this ?