Winform C #: Simple Runtime Control Resizing and Dragging & Moving Class

xiaoxiao2021-03-06  104

/ * C #: Simple Runtime Control Sizing and Dragging Class

First, declare the following code to change from the CodeProject of the same name (Xie Yuan Jim Korovess: ~! @ # $% ^ & *): Http://www.codeproject.com/cs/misctrl/cspickboxxsample1.asp

I did the following modification:

1. Add an ArrayList storage wiredControls2. Increase the unwireLallWiredControls () method to switch to the "Non-Design" Status 3. Increase the WireControls () method, used to "design" status to the control 4. Add support keyboard movement or change

I will show my code to the code! * /

// You can save below code to one file named Noname1.cs // Execute "CSC Noname1.cs" Directly!

// pickboxusing system; using system.drawing; using system.componentmodel; using system.windows.form; using system.data;

// namespace PickBoxTest // {///

/// This class implements sizing and moving functions for /// runtime editing of graphic controls /// public class PickBox {// // PRIVATE CONSTANTS AND Variables //

private const int BOX_SIZE = 8; private Color BOX_COLOR = Color.White; // private ContainerControl m_container; private Control m_control; private Label [] lbl = new Label [8]; private int startl; private int startt; private int startw; private int starth; private int startx; private int starty; private bool dragging; private Cursor [] arrArrow = new Cursor [] {Cursors.SizeNWSE, Cursors.SizeNS, Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS, Cursors.SizENESW, CURSORS.SIZEWE}; privat

Private const INT min_size = 20;

PRIVATE FORM PARENTFORM;

// // constructor create 8 SIZING HANDLES & WIRES MOUSE Events // To Each That Implement Sizing Functions // Public Pickbox () {for (INT I = 0; I <8; i ) {LBL [I] = New Label ); Lbl [i] .Tabindex = i; lbl [i] .flatstyle = 0; lbl [i] .borderstyle = borderstyle.fixedsingle; lbl [i] .backcolor = Box_color; lbl [i] .cursor = arrarrow [i ]; Lbl [i] .text = ""; lbl [i] .bringtofront (); lbl [i] .MOUsedown = new mouseeventhandler (this.lbl_mousedown); lbl [i] .Mousemove = new mouseeventhandler (this. LBL_MOUSEMOVE; lbl [i] .Mouseup = new mouseeventhandler (this.lbl_mouseup);}} // // public methods //

//// Wires a clicet handler to the passed control // That Attaches a Pick Box To the Control When IT IT ITKED //

// add by playyuer@microshaoft.com // Store Controls for Design (Resize or Move) private arraylist wiredcontrols = new arraylist ();

Public Void WireControl (Control CTL) {// add by Playyuer@microshaoft.com // Store Wired Controls WiredControls.Add (CTL);

IF (ParentForm! = CTL.Findform ()) {ParentForm = CTL.Findform (); //parentform.keypreview = true;

CTL.Click = New EventHandler (this.selectControl);

// add by playyuer@microshaoft.com // support resizing or moving control by keyboard ctl.keydown = new keyeventhandler (CTL_KeyDown);

// add by playyuer@Microshaoft.com // for Wire Controls that in WiredControls ArrayList public void WireControls () {if (WiredControls.Count> 0) {System.Collections.IEnumerator ie = WiredControls.GetEnumerator (); while (ie. MoveNext ()) {this.WireControl ((Control) ie.Current);}}} // add by playyuer@Microshaoft.com // for UnWire Controls that in WiredControls ArrayList public void UnWireAllWiredControls () {System.Collections.IEnumerator ie = WiredControls.GetEnumerator (); Control c; while (ie.MoveNext ()) {c = (Control) ie.Current; c.Click - = new EventHandler (this.SelectControl); c.KeyDown - = new KeyEventHandler (ctl_KeyDown );

if (m_control is Control) {m_control.Cursor = oldCursor; // Remove event any pre-existing event handlers appended by this class m_control.MouseDown - = new MouseEventHandler (this.ctl_MouseDown); m_control.MouseMove - = new MouseEventHandler (this. CTL_MOUSEMOVE); m_control.mouseup - = new mouseeventhandler (this.ctl_mouseup);

m_control.keydown - = new keyeventhandler (CTL_KeyDown);

M_Control = null;}} this.hideHandles ();

/ / / Private Methods /

// // attachs a pick box to the sender control // private void selectcontrol (Object sender, eventargs e) {

IF (M_Control Is Control) {m_control.cursor = OldCursor;

// Remove event any pre-existing event handlers appended by this class m_control.MouseDown - = new MouseEventHandler (this.ctl_MouseDown); m_control.MouseMove - = new MouseEventHandler (this.ctl_MouseMove); m_control.MouseUp - = new MouseEventHandler (this. CTL_MOUSEUP);

M_Control = null;}

M_Control = (Control) Sender;

// Add event handlers for moving the selected control around m_control.MouseDown = new MouseEventHandler (this.ctl_MouseDown); m_control.MouseMove = new MouseEventHandler (this.ctl_MouseMove); m_control.MouseUp = new MouseEventHandler (this.ctl_MouseUp); // Add Sizing Handles To Control's Container (INT i = 0; I <8; i ) {m_control.parent.controls.add (lbl [i]); lbl [i]. Bringtofront (); }

// position Sizing Handles Around Control MoveHandles ();

// Display sqing handles showhandles ();

Oldcursor = m_control.cursor; m_control.cursor = cursors.sizeall;}

Public void remove () {hidehandles (); m_control.cursor = Oldcursor;

Private void showhandles () {if (m_control! = null) {for (INT i = 0; i <8; i ) {lbl [i] .visible = true;}}}

Private void hidehandles () {for (int i = 0; i <8; i ) {lbl [i] .visible = false;}}

private void MoveHandles () {int sX = m_control.Left - BOX_SIZE; int sY = m_control.Top - BOX_SIZE; int sW = m_control.Width BOX_SIZE; int sH = m_control.Height BOX_SIZE; int hB = BOX_SIZE / 2;

int [] arrposx = new int [] {SX HB, SX SW / 2, SX SW-HB, SX SW-HB, SX SW-HB, SX SW / 2, SX HB, SX HB}; int [] arrposy = new int [] {SY HB, SY HB, SY HB, SY SH / 2, SY SH-HB, SY SH-HB, SY SH-HB, SY SH / 2}; for (int i = 0; i <8; i ) LBL [I] .SetBounds (Arrposx [i], arrposy [i], box_size, box_size);} / // mouse events That Implement SIZING Of the picked control /

// // Store control position and size when mouse button pushed over // any sizing handle // private void lbl_MouseDown (object sender, MouseEventArgs e) {dragging = true; startl = m_control.Left; startt = m_control.Top; startw = M_Control.Width; StartH = m_control.height; hidehandles ();

/// size the picked control in accounting Dragged // 0 1 2 // 7 3 // 6 5 4 // Private Void LBL_MOUSEMOVE (Object Sender, MouseEventArgs E) {INT L = M_Control.Left; int; int W = m_control.width; int T = m_control.top; int h = m_control.height; if (Dragging) {switch ((label) sender) .Tabindex) {cas 0: // Dragging Top-Left Sizing Box L = startl eX Min_Size? StartW EX: Min_Size; T = StartT EY MIN_SIZE startw eX: MIN_SIZE? Break; case 4: // Dragging Right-Bottom Sizing Box W = StartW EX> Min_size? StartW EX: min_size; h = starth EY> min_size? Starth EY: min_size; break; case 5: // Dragging Center-bottom Sizing Box H = STARTH EY> Min_size? StartH EY: min_size; Break;

Case 6: // Dragging Left-Bottom Sizing Box L = StartL EX min_size ? StartH EY: Min_Size; Break; Case 7: // Dragging Left-middle Sizing Box L = Startl EX

/ // mouse Events That Move THE PICKED Control Around The Form /

// // Get mouse pointer starting position on mouse down and hide sizing handles // private void ctl_MouseDown (object sender, MouseEventArgs e) {dragging = true; startx = e.X; starty = e.Y; HideHandles ();}

// // Reposition the dragged control // private void ctl_MouseMove (object sender, MouseEventArgs e) {if (dragging) {int l = m_control.Left eX - startx; int t = m_control.Top eY - starty; int w = M_Control.width; int h = m_control.height;

L = (l <0)? 0: ((L W> m_control.parent.clientRectangle.width)? m_control.parent.clientRectangle.Width - w: L); t = (t <0)? 0: (( T h> m_control.parent.clientRectangle.height)? m_control.parent.clientRectangle.Height - H: T); m_control.LEFT = L; m_control.top = t;}}

// // Display sizing handles around picked control once dragging has completed // private void ctl_MouseUp (object sender, MouseEventArgs e) {dragging = false; MoveHandles (); ShowHandles ();} private void ctl_KeyDown (object sender, KeyEventArgs e) {IF (E.MODIFIERS == Keys.Control) {switch (e.keycode) {case keys.right: m_control.Left = 10; Break; Case Keys.left: m_control.Left - = 10; Break; Case Keys .UP: m_control.top - = 10; Break; Case Keys.down: m_control.top = 10; Break;} movehandles ();} else if (E.MODIFIERS == Keys.shift) {Switch (E.KeyCode ) {Case keys.right: m_control.width = 10; Break; Case Keys.left: m_control.width - = 10; Break; Case Keys.up: m_control.height - = 10; Break; Case Keys.down: M_Control . Height = 10; Break;} movehandles ();} // ((Control) Sender) .select ();}} //}

/ / =========================================================================================================================================================================================== ==

// WinForm Test // Form1 // Using System.drawing; // Using System.Collections; // Using System.comPonentModel; // Using System.Windows.Forms; // Using System.data;

Summary Description of // Namespace WindowsApplication1 // {///

/// Form1. /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.DataGrid dataGrid1; / // /// The required designer variable. /// private System.ComponentModel.Container components = null; private System.Windows.Forms.DataGrid dataGrid2; private System.Windows.Forms.Button button2; // add by playyuer@Microshaoft.com private PickBox pb = New PickBox (); public form1 () {// // Windows Form Designer Support for // InitializationComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.button1 = new System.Windows.Forms.Button (); this.textBox1 = new System.Windows.Forms.TextBox (); this.dataGrid1 = new System. Windows.Forms.DataGrid (); this.dataGrid2 = new System.Windows.Forms.DataGrid (); this.button2 = new System.Windows.Forms.Button (); ((System.ComponentModel.ISupportInitialize) (this.dataGrid1 ))). Begininit (); (this.dataGrid2)). Begininit (); this.suspendlayout (); // // button1 // this.button1.location = new system.drawing. Point (24, 16); this.button1.name = "button1"; this.button1.size = new system.drawing.size (104, 40); this.button1.tabindex = 0; this.button1.text = " Switch "; this.Button1.click = new system.eventhandler (this.button1_click); /// textBox1 // this.textBox1.location = new system.drawing.point (144, 32); this.textbox1.name = "TextBox1"; this.textBox1.size = new system.drawing.size (128, 21); this.textBox1.tabindex = 1; this.textbox1.t ext = "textBox1"; // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point (32 , 80); this.DataGrid1.name = "DataGrid1"; this.dataGrid1.size = new system.drawing.size (160, 72); this.dataGrid1.tabindex = 2; /// DataGrid2 // this.dataGrid2 .DataMember = ""; this.dataGrid2.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid2.Location = new System.Drawing.Point (64, 208); this.dataGrid2.Name = "dataGrid2";

This.DataGrid2.size = new system.drawing.size (168, 64); this.dataGrid2.tabindex = 3; // // button2 // this.button2.location = new system.drawing.point (280, 144) This.button2.name = "button2"; this.button2.size = new system.drawing.size (152, 56); this.button2.tabindex = 4; this.button2.text = "button2"; // / / Form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (608, 381); this.Controls.add (this.button2); this.Controls .Add (this.DataGrid2); this.controls.add (this.DataGrid1); this.Controls.add (this.TextBox1); this.controls.add (this.button1); this.name = "form1"; this.Name = "form1"; this. .Text = "Form1"; this.KeyDown = new System.Windows.Forms.KeyEventHandler (this.Form1_KeyDown); this.Load = new System.EventHandler (this.Form1_Load); ((System.ComponentModel.ISupportInitialize) ( THIS.DATAGRID1)))))))). Endinit (); (this.DataGrid2)). endinit (); this.ResumeLayout (false);} #ENDREGION

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.run (new form1 ());}

Private void form1_keydown (Object sender, system.windows.keyeventargs e) {

}

Bool B = false; private void button1_click (object sender, system.eventargs e) {if (! b) {this.text = "design time";

Pb.wireControl (DataGrid1); Pb.wireControl (TextBox1); Pb.wireControl (Button2); Pb.wireControl (DataGrid2);

} Else {this.text = "run time"; pb.unwireAllwiredControls ();} b =! B;}

Private Void Form1_Load (Object Sender, System.Eventargs E) {

} //}

转载请注明原文地址:https://www.9cbs.com/read-100591.html

New Post(0)