Home > Back-end >  Implemented in c # winform panel mouse selection box, select dynamic mouse
Implemented in c # winform panel mouse selection box, select dynamic mouse

Time:11-20

I used a panel in the Winform draw some geometry, a polygon and rectangle and string, now I want to part choose delete from the graphics, my idea is on the geometric figure then get a transparent extendedPanel1, draw the mouse in the extendedPanel1 checkbox (similar to Windows file selection), and according to the coordinates and whether the area calculation of the bottom panel graphics in this area, so as to determine repainting panel,
Below is the graphics and code:
Red is I draw a geometric figure, green is the mouse choose rendering rectangle, the question now is, in the mouse choose many rectangle, extendedPanel1 is a new panel, give his inheritance in it defines transparency, now every time a new draw the mouse rectangular box box still exists in the previous step, even if I use Invalidate or doesn't work, if there is no transparency, you can achieve this choice, also is the same as other drawing software, but as a result of extendedPanel1 is transparent, every time just reload, but inside painting figure don't disappear, tried the cancel controls also doesn't fit, I don't know how to solve, or do you have a better solution? (but in winform)


//a new definition of transparent panel
Public class ExtendedPanel: Panel
{
Private const int WS_EX_TRANSPARENT=0 x20;
Public ExtendedPanel ()
{
SetStyle (ControlStyles. Opaque, true);
}

Private int opacity=0;
[DefaultValue (0))
Public int Opacity
{
The get
{
Return this. The opacity;
}
Set
{
If (value & lt; 0 | | value & gt; 100)
Throw new ArgumentException (" value must be between 0 and 100 ");
This. Opacity=value;
}
}
Protected override CreateParams CreateParams
{
The get
{
CreateParams cp=base. CreateParams;
Cp. ExStyle=cp. ExStyle | WS_EX_TRANSPARENT;
Return the cp;
}
}
Protected override void OnPaint (PaintEventArgs e)
{
Using (var brush=new SolidBrush (Color FromArgb (enclosing opacity * 255/100, enclosing BackColor)))
{
Um participant raphics. FillRectangle (brush, enclosing ClientRectangle);
}
Base. The OnPaint (e);
}
}

//mouse code
Private void SetSelectionRect ()
{
Int x, y;
Int width, height;

X=selectionStart. X & gt; SelectionEnd. X? SelectionEnd. X: selectionStart. X;
Y=selectionStart. Y & gt; SelectionEnd. Y? SelectionEnd. Y: selectionStart. Y;

Width=selectionStart. X & gt; SelectionEnd. X? SelectionStart. X-ray selectionEnd. X: selectionEnd) X - selectionStart. X;
Height=selectionStart. Y & gt; SelectionEnd. Y? SelectionStart. Y - selectionEnd. Y: selectionEnd) - selectionStart. Y Y;

Selection=new Rectangle (x, y, width, height);
}

Private void extendedPanel1_Paint (object sender, PaintEventArgs e)
{
Base. The OnPaint (e);
ExtendedPanel1. Opacity=0;
If (mouseDown)
{
Using (Pen Pen=new Pen (Color Green, 1 f))
{
Pen. DashStyle=DashStyle. Dash;
Um participant raphics. DrawRectangle (pen, selection);
}
}
}

Private void extendedPanel1_MouseDown (object sender, MouseEventArgs e)
{
SelectionStart=extendedPanel1. PointToClient (MousePosition);
MouseDown=true;
}

Private void extendedPanel1_MouseUp (object sender, MouseEventArgs e)
{
MouseDown=false;
SetSelectionRect ();
ExtendedPanel1. Invalidate ();
}

Private void extendedPanel1_MouseMove (object sender, MouseEventArgs e)
{
if (! MouseDown)
return;
SelectionEnd=extendedPanel1. PointToClient (MousePosition);
SetSelectionRect ();
ExtendedPanel1. Invalidate ();
}

CodePudding user response:

This is a C/C + + BBS, ask advice look for C #
  • Related