In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to create a control with a mouse frame in C#". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "C# how to use the mouse frame to create controls" it!
First of all, Control _ Owner is defined, and the Control type is used because we not only need to add controls to Winform, but also add containers to other containers, such as Panel,GroupBox, Panel,GroupBox, etc.
Then restart the mouse event Control_MouseDown,Control_MouseMove,Control_MouseUp,Control_MouseEnter on the Control.
Directly on the source code
Using System
Using System.Drawing
Using System.Drawing.Drawing2D
Using System.Windows.Forms
Namespace SumBLL.DrawControl
{
# region mouse drag to create a control
/ / /
/ / Mouse drag to create event
/ / /
Public class MouseHook
{
Control _ Owner
/ / Child controls to be drawn
Private Control _ childCtrl
Private bool _ autosize
Private int _ CLickAtX
Private int _ ClickAtY
Private int _ MoveAtX
Private int _ MoveAtY
Private bool _ BeginDrag
Private bool _ BeginDrawControl
/ / /
/ / external public class
/ / /
Public Control DrawCtrl
{
Get
{
Return _ childCtrl
}
Set
{
_ childCtrl = value
}
}
/ / /
/ / Owner uses the Control type here because we not only need to add controls to the Winform
/ you also need to add containers to other containers, such as Panel,GroupBox.
/ / /
/ / /
Public MouseHook (Control Owner, bool autosize = true)
{
This._Owner = Owner
This._Owner.MouseDown + = new MouseEventHandler (this.Control_MouseDown)
This._Owner.MouseMove + = new MouseEventHandler (this.Control_MouseMove)
This._Owner.MouseUp + = new MouseEventHandler (this.Control_MouseUp)
This._Owner.MouseEnter + = new EventHandler (this.Control_MouseEnter)
This._BeginDrawControl = false
This._autosize = autosize
}
# Mouse events on region Control
Void Control_MouseDown (object sender, MouseEventArgs e)
{
/ / decide whether to select a control or a brush
If (_ childCtrl! = null)
{
This._CLickAtX = e.x
This._ClickAtY = e.Y
This._MoveAtX = e.x
This._MoveAtY = e.Y
This._BeginDrag = true
If (_ childCtrl! = null)
{
This._BeginDrawControl = true
}
Else
{
This._BeginDrawControl = false
}
}
}
Void Control_MouseMove (object sender, MouseEventArgs e)
{
If (_ childCtrl! = null)
{
If (this._BeginDrag)
{
/ / cancel the selection box drawn last time
Int iLeft, iTop, iWidth, iHeight
Pen pen
Rectangle rect
Pen = new Pen (this._Owner.BackColor)
If (this._BeginDrawControl = = true)
{
Pen.DashStyle = DashStyle.Solid
Pen.Width = 2
}
Else
{
Pen.DashStyle = DashStyle.Dot
}
ILeft = this._CLickAtX < this._MoveAtX? This._CLickAtX: this._MoveAtX
ITop = this._ClickAtY < this._MoveAtY? This._ClickAtY: this._MoveAtY
IWidth = Math.Abs (this._MoveAtX-this._CLickAtX)
IHeight = Math.Abs (this._MoveAtY-this._ClickAtY)
Rect = new Rectangle (iLeft, iTop, iWidth, iHeight)
This._Owner.CreateGraphics () .DrawRectangle (pen rect)
/ / redraw the selection box
This._MoveAtX = e.x
This._MoveAtY = e.Y
Pen = new Pen (Color.Black)
If (this._BeginDrawControl = = true)
{
Pen.DashStyle = DashStyle.Solid
Pen.Width = 2
}
Else
{
Pen.DashStyle = DashStyle.Dot
}
ILeft = this._CLickAtX < this._MoveAtX? This._CLickAtX: this._MoveAtX
ITop = this._ClickAtY < this._MoveAtY? This._ClickAtY: this._MoveAtY
IWidth = Math.Abs (this._MoveAtX-this._CLickAtX)
IHeight = Math.Abs (this._MoveAtY-this._ClickAtY)
Rect = new Rectangle (iLeft, iTop, iWidth, iHeight)
This._Owner.CreateGraphics () .DrawRectangle (pen rect)
}
}
}
Void Control_MouseUp (object sender, MouseEventArgs e)
{
This._BeginDrag = false
This._Owner.SuspendLayout ()
If (_ childCtrl! = null)
{
/ / cancel the selection box drawn last time
Int iLeft, iTop, iWidth, iHeight
Pen pen
Rectangle rect
Pen = new Pen (this._Owner.BackColor)
Pen.DashStyle = DashStyle.Dot
ILeft = this._CLickAtX < this._MoveAtX? This._CLickAtX: this._MoveAtX
ITop = this._ClickAtY < this._MoveAtY? This._ClickAtY: this._MoveAtY
IWidth = Math.Abs (this._MoveAtX-this._CLickAtX)
IHeight = Math.Abs (this._MoveAtY-this._ClickAtY)
Rect = new Rectangle (iLeft, iTop, iWidth, iHeight)
This._Owner.CreateGraphics () .DrawRectangle (pen rect)
If (_ childCtrl! = null)
{
AddControl (_ childCtrl, rect)
}
Else
{
/ / here is to drag the mouse and select the control, which will be given in the following introduction
}
}
This._Owner.Refresh ()
This._Owner.ResumeLayout ()
}
Void Control_MouseEnter (object sender, EventArgs e)
{
If (_ childCtrl! = null)
{
This._Owner.Cursor = Cursors.Cross
}
Else
{
This._Owner.Cursor = Cursors.Default
}
}
Private void AddControl (Control control, Rectangle rect)
{
Try
{
Control.Location = rect.Location
If (! _ autosize)
Control.Size = rect.Size
/ / control.Name = GetControlName (control)
/ / because .text cannot be set to a non-date type for the DataTimePiker control, the error is ignored
Try
{
Control.Text = GetControlType (control)
}
Catch {}
This._Owner.Controls.Add (control)
Control.Visible = true
This._Owner.Cursor = Cursors.Default
_ childCtrl = null
}
Catch (Exception e)
{
This._Owner.Cursor = Cursors.Default
_ childCtrl = null
}
}
Private string GetControlType (Control ctrl)
{
String strType = ctrl.GetType () .ToString ()
String strControlType
String [] strArr = strType.Split ("." .ToCharArray ())
StrControlType = strArr [strArr.Length-1] .trim ()
Return strControlType
}
Private string GetControlName (Control control)
{
/ / the control name is simply returned here. If necessary, you can do special processing by modifying this function.
Return control.GetType () Name
}
# endregion
}
# endregion
}
External calling method
/ / function class for dragging controls, which is directly instantiated when the form is initialized
Private MouseHook _ MouseHook
/ / initialize Panel display dynamic patrol points
This._MouseHook = new MouseHook (pnlRouteDetail)
/ / create a user control instance
UCPatRouteDetail ucrd = new UCPatRouteDetail (_ nowPatRouteDetail)
Ucrd.Name = _ nowPatRouteDetail.RouteID + _ nowPatRouteDetail.DetailID
Ucrd.MouseDown + = MouseDown
Ucrd.MouseMove + = MouseMove
Ucrd.MouseUp + = MouseUp
Ucrd.Delevent + = DelUserControl
/ / assign a new instance to the drawing place
_ MouseHook.DrawCtrl = ucrd
At this point, I believe you have a deeper understanding of "how to create a control with a mouse frame in C#". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.