动态控件的移动和缩放_第1页
动态控件的移动和缩放_第2页
动态控件的移动和缩放_第3页
动态控件的移动和缩放_第4页
动态控件的移动和缩放_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、动态控件的移动及缩放动态控件的移动及缩放,网上有很多,各有千秋。这一个实例源码,是我自己学习C#在VS2010上编写的。有一些不如意的地方,如缩放中有些发卡,请各位老师斧正。Victor.King2016.6.20 Email:victor_jx namespace ChangePositionSizecontrols partial class Form1 / <summary> / 必需的设计器变量。 / </summary> private System.ComponentModel.IContainer components = null; / <summ

2、ary> / 清理所有正在使用的资源。 / </summary> / <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) if (disposing && (components != null) components.Dispose(); base.Dispose(disposing); #region Windows 窗体设计器生成的代码 / <

3、summary> / 设计器支持所需的方法 - 不要 / 使用代码编辑器修改此方法的内容。 / </summary> private void InitializeComponent() System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1); this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.button1 =

4、 new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); (System.ComponentModel.ISupportInitialize)(this.pictureBox1).BeginInit(); this.SuspendLayout(); / / pictureBox1 / this.pictureBox1.BackgroundImage = (System.Drawing

5、.Image)(resources.GetObject("pictureBox1.BackgroundImage"); this.pictureBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom; this.pictureBox1.Location = new System.Drawing.Point(4, 4); this.pictureBox1.Name = "pictureBox1" this.pictureBox1.Size = new System.Drawing

6、.Size(500, 347); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; / / button1 / this.button1.Location = new System.Drawing.Point(21, 358); this.button1.Name = "button1" this.button1.Size = new System.Drawing.Size(99, 23); this.button1.TabIndex = 1; this.button1.Text = "

7、;添加一个文本框" this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); / / button2 / this.button2.Location = new System.Drawing.Point(126, 358); this.button2.Name = "button2" this.button2.Size = new System.Drawing.Size(99, 23); this.b

8、utton2.TabIndex = 2; this.button2.Text = "删除一个文本框" this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); / / button3 / this.button3.Location = new System.Drawing.Point(231, 358); this.button3.Name = "button3" this.button3.

9、Size = new System.Drawing.Size(99, 23); this.button3.TabIndex = 3; this.button3.Text = "退出" this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); / / Form1 / this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoSca

10、leMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(508, 393); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.pictureBox1); this.Name = "Form1" this.Text = "Form1" t

11、his.Load += new System.EventHandler(this.Form1_Load); (System.ComponentModel.ISupportInitialize)(this.pictureBox1).EndInit(); this.ResumeLayout(false); #endregion private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button but

12、ton2; private System.Windows.Forms.Button button3; using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using ChangePositionSizecontrols.ControlsMoveSize;namespace ChangePositio

13、nSizecontrols public partial class Form1 : Form public Form1() InitializeComponent(); int i = 1; int x = 20; int y = 20; Control curCon = new Control(); string focuseCon; MouseOperation MOperation = new MouseOperation(); private void Form1_Load(object sender, EventArgs e) private void button1_Click(

14、object sender, EventArgs e) pictureBox1.Refresh(); Label textbox = new Label(); textbox.Name = "TextBox" + i; textbox.Text = "TextBox" + i; textbox.Location = new System.Drawing.Point(x, y); textbox.AutoSize = false; textbox.Width = 100; textbox.Height = 25; textbox.BackColor = C

15、olor.Transparent; textbox.BorderStyle = BorderStyle.FixedSingle; curCon = textbox; pictureBox1.Controls.Add(curCon); curCon.BringToFront(); i = i + 1; AddEvents(); private void button2_Click(object sender, EventArgs e) if (curCon.Name = focuseCon) curCon.Dispose(); pictureBox1.Refresh(); curCon.Name

16、 = "" else MessageBox.Show("请选择要删除的文本框!"); private void button3_Click(object sender, EventArgs e) this.Close(); this.Dispose(); void AddEvents() curCon.MouseClick +=new MouseEventHandler(curCon_MouseClick); void curCon_MouseClick(object sender, MouseEventArgs e) curCon =(Control)

17、sender; curCon.Focus(); focuseCon = curCon.Name; pictureBox1.Refresh(); if (e.Button = MouseButtons.Left) MOperation.Draw(pictureBox1, curCon); MOperation.AddEvents(curCon, pictureBox1); using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;using Syst

18、em.Drawing.Drawing2D;using System.Windows.Forms;namespace ChangePositionSizecontrols.ControlsMoveSize class MouseOperation int mark; Control curCon; Point ctlLocation = new Point(); Size ctlSize = new Size(); PictureBox picBox = new PictureBox(); private Point lastPoint; /上个鼠标坐标 private Point curPoi

19、nt; /当前鼠标坐标 const int Band = 6; /调整大小的响应边框 Size Square = new Size(Band, Band);/小矩形大小 Rectangle smallRects = new Rectangle8;/边框中的八个小圆圈 Rectangle sideRects = new Rectangle4;/四条边框,用来做响应区域 Rectangle ControlRect; Point linePoints = new Point5; Graphics g; MoveControl MControl = new MoveControl(); SizeCha

20、nge SChange = new SizeChange(); public void AddEvents(Control ctl,PictureBox pcb) curCon = ctl; picBox = pcb; curCon.MouseDown += new MouseEventHandler(curCon_MouseDown); picBox.MouseDown+=new MouseEventHandler(picBox_MouseDown); curCon.MouseMove += new MouseEventHandler(curCon_MouseMove); picBox.Mo

21、useMove += new MouseEventHandler(picBox_MouseMove); curCon.MouseUp += new MouseEventHandler(curCon_MouseUp); picBox.MouseUp+=new MouseEventHandler(picBox_MouseUp); void curCon_MouseDown(object sender, MouseEventArgs e) lastPoint = Cursor.Position; MControl.getPoint(lastPoint.X, lastPoint.Y); void pi

22、cBox_MouseDown(object sender, MouseEventArgs e) lastPoint = Cursor.Position; SChange.getPoint(lastPoint.X, lastPoint.Y); void picBox_MouseMove(object sender, MouseEventArgs e) if (curCon.Focused) int mark = SetCursorShape(picBox, curCon); if (e.Button = MouseButtons.Left) SChange.ChangeSize(mark, cu

23、rCon); void curCon_MouseMove(object sender, MouseEventArgs e) if (curCon.Focused) int mark = SetCursorShape(picBox, curCon); if (e.Button = MouseButtons.Left) MControl.ControlMove(curCon, picBox); void curCon_MouseUp(object sender, MouseEventArgs e) if (e.Button = MouseButtons.Left) picBox.Refresh()

24、; Draw(picBox, curCon); void picBox_MouseUp(object sender, MouseEventArgs e) if (e.Button = MouseButtons.Left) picBox.Refresh(); Draw(picBox, curCon); void ReplyRect() curCon.BringToFront(); ctlLocation = new Point(curCon.Location.X, curCon.Location.Y); ctlSize = new Size(curCon.Size.Width, curCon.S

25、ize.Height); /定义4个4角小正方形的范围 smallRects0 = new Rectangle(new Point(ctlLocation.X - Square.Width, ctlLocation.Y - Square.Height), Square); smallRects1 = new Rectangle(new Point(ctlLocation.X + ctlSize.Width-1, ctlLocation.Y - Square.Height), Square); smallRects2 = new Rectangle(new Point(ctlLocation.X

26、 + ctlSize.Width-1, ctlLocation.Y + ctlSize.Height-1), Square); smallRects3 = new Rectangle(new Point(ctlLocation.X - Square.Width, ctlLocation.Y + ctlSize.Height-1), Square); /定义4个侧面小正方形的范围 smallRects4 = new Rectangle(new Point(ctlLocation.X - Square.Width, ctlLocation.Y + (ctlSize.Height / 2) - (S

27、quare.Height / 2), Square); smallRects5 = new Rectangle(new Point(ctlLocation.X + (ctlSize.Width / 2) - (Square.Width / 2), ctlLocation.Y - Square.Height), Square); smallRects6 = new Rectangle(new Point(ctlLocation.X + ctlSize.Width-1, ctlLocation.Y + (ctlSize.Height / 2) - (Square.Height / 2), Squa

28、re); smallRects7 = new Rectangle(new Point(ctlLocation.X + (ctlSize.Width / 2) - (Square.Width / 2), ctlLocation.Y + ctlSize.Height-1), Square); /定义4个侧面矩形的范围 sideRects0 = new Rectangle(ctlLocation.X, ctlLocation.Y - Square.Height, ctlSize.Width, Square.Height); sideRects1 = new Rectangle(ctlLocation

29、.X + ctlSize.Width, ctlLocation.Y, Square.Width, ctlSize.Height); sideRects2 = new Rectangle(ctlLocation.X, ctlLocation.Y + ctlSize.Height, ctlSize.Width, Square.Height); sideRects3 = new Rectangle(ctlLocation.X - Square.Width, ctlLocation.Y, Square.Width, ctlSize.Height); /定义当前控件矩形的范围 ControlRect =

30、 new Rectangle(new Point(ctlLocation.X, ctlLocation.Y), ctlSize); public void Draw(PictureBox pcb,Control ctl) picBox = pcb; curCon = ctl; if (curCon.Focused) ctlLocation = new Point(curCon.Location.X, curCon.Location.Y); ctlSize = new Size(curCon.Size.Width, curCon.Size.Height); picBox.BringToFront

31、(); ReplyRect(); linePoints0 = new Point(ctlLocation.X - Square.Width / 2, ctlLocation.Y - Square.Height / 2); /右上 linePoints1 = new Point(ctlLocation.X + ctlSize.Width + Square.Width / 2 - 1, ctlLocation.Y - Square.Height / 2); /右下 linePoints2 = new Point(ctlLocation.X + ctlSize.Width + Square.Widt

32、h / 2 - 1, ctlLocation.Y + ctlSize.Height + Square.Height / 2 - 1); /左下 linePoints3 = new Point(ctlLocation.X - Square.Width / 2, ctlLocation.Y + ctlSize.Height + Square.Height / 2 - 1); /左上 linePoints4 = new Point(ctlLocation.X - Square.Width / 2, ctlLocation.Y - Square.Height / 2); curCon.BringToF

33、ront(); picBox.Refresh(); g = picBox.CreateGraphics(); Pen pen = new Pen(Color.Black); pen.DashStyle = DashStyle.Dot;/设置为虚线,用虚线画四边,模拟微软效果 g.DrawLines(pen, linePoints);/绘制四条边线 /g.FillRectangles(Brushes.Blue, sideRects); g.FillRectangles(Brushes.White, smallRects); /填充8个小矩形的内部 foreach (Rectangle small

34、Rect in smallRects) g.DrawRectangles(Pens.Black, smallRects); /绘制8个小矩形的黑色边线 public int SetCursorShape(PictureBox pcb, Control ctl) picBox = pcb; curCon = ctl; curPoint = picBox.PointToClient(Control.MousePosition); ReplyRect(); if(curCon.Bounds.Contains(curPoint) Cursor.Current = Cursors.SizeAll; ma

35、rk = 0; else if (smallRects0.Contains(curPoint) Cursor.Current = Cursors.SizeNWSE; mark = 1; else if (smallRects1.Contains(curPoint) Cursor.Current = Cursors.SizeNESW; mark = 2; else if (smallRects2.Contains(curPoint) Cursor.Current = Cursors.SizeNWSE; mark = 3; else if (smallRects3.Contains(curPoin

36、t) Cursor.Current = Cursors.SizeNESW; mark = 4; else if (sideRects0.Contains(curPoint) Cursor.Current = Cursors.SizeNS; mark = 5; else if (sideRects1.Contains(curPoint) Cursor.Current = Cursors.SizeWE; mark = 6; else if (sideRects2.Contains(curPoint) Cursor.Current = Cursors.SizeNS; mark = 7; else i

37、f (sideRects3.Contains(curPoint) Cursor.Current = Cursors.SizeWE; mark = 8; else Cursor.Current = Cursors.Arrow; mark = 9; return mark; using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Drawing;using System.Windows.Forms;namespace ChangePositionSizecontro

38、ls.ControlsMoveSize class MoveControl Point lastPoint = new Point(); Point curPoint = new Point(); Control curCon = new Control(); public void ControlMove(Control ctl,PictureBox pcb) curCon = ctl; pcb.Refresh(); DrawDragBound(curCon); curPoint = Cursor.Position;/获得当前鼠标位置 int x = curPoint.X - lastPoi

39、nt.X; int y = curPoint.Y - lastPoint.Y; curCon.Location = new Point(curCon.Location.X + x, curCon.Location.Y + y); lastPoint = curPoint; public static void DrawDragBound(Control ctrl) ctrl.Refresh(); Graphics g = ctrl.CreateGraphics(); int width = ctrl.Width; int height = ctrl.Height; Point ps = new Point5new Point(0,0),new Point(width -1,0), new Point(width -1,height -1),new Point(0,height-1),new Point(0,0); g.DrawLines(new Pen(Color.Black), ps); public void getPoint(int x, int y) lastPoint = new Point(x, y); using System;using System.Col

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论