版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、動態(tài)控件的移動及縮放動態(tài)控件的移動及縮放,網(wǎng)上有很多,各有千秋。這一個(gè)實(shí)例源碼,是我自己學(xué)習(xí)C#在VS2010上編寫的。有一些不如意的地方,如縮放中有些發(fā)卡,請各位老師斧正。Victor.King2016.6.20 Email:victor_jx namespace ChangePositionSizecontrols partial class Form1 / <summary> / 必需的設(shè)計(jì)器變量。 / </summary> private System.ComponentModel.IContainer components = null; / <summ
2、ary> / 清理所有正在使用的資源。 / </summary> / <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) if (disposing && (components != null) components.Dispose(); base.Dispose(disposing); #region Windows 窗體設(shè)計(jì)器生成的代碼 / <
3、summary> / 設(shè)計(jì)器支持所需的方法 - 不要 / 使用代碼編輯器修改此方法的內(nèi)容。 / </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、;添加一個(gè)文本框" 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 = "刪除一個(gè)文本框" 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; /上個(gè)鼠標(biāo)坐標(biāo) private Point curPoi
19、nt; /當(dāng)前鼠標(biāo)坐標(biāo) const int Band = 6; /調(diào)整大小的響應(yīng)邊框 Size Square = new Size(Band, Band);/小矩形大小 Rectangle smallRects = new Rectangle8;/邊框中的八個(gè)小圓圈 Rectangle sideRects = new Rectangle4;/四條邊框,用來做響應(yīng)區(qū)域 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個(gè)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個(gè)側(cè)面小正方形的范圍 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個(gè)側(cè)面矩形的范圍 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); /定義當(dāng)前控件矩形的范圍 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;/設(shè)置為虛線,用虛線畫四邊,模擬微軟效果 g.DrawLines(pen, linePoints);/繪制四條邊線 /g.FillRectangles(Brushes.Blue, sideRects); g.FillRectangles(Brushes.White, smallRects); /填充8個(gè)小矩形的內(nèi)部 foreach (Rectangle small
34、Rect in smallRects) g.DrawRectangles(Pens.Black, smallRects); /繪制8個(gè)小矩形的黑色邊線 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;/獲得當(dāng)前鼠標(biāo)位置 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年滬教版選擇性必修1生物上冊階段測試試卷
- 創(chuàng)新的小學(xué)生字詞教學(xué)案例分享
- 從食材到菜品家庭菜譜設(shè)計(jì)的全解析
- 公共設(shè)施與社區(qū)服務(wù)類服飾品牌的聯(lián)名項(xiàng)目
- 2025中國航材集團(tuán)總部崗位招聘2人高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025中國電信股份限公司灣沚分公司招聘10人高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025中國城市規(guī)劃設(shè)計(jì)研究院公開招聘60名高校畢業(yè)生高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025下半年黑龍江省哈爾濱理工大學(xué)招聘專職輔導(dǎo)員心理健康教師26人歷年高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025下半年江蘇鹽城市水利局部分事業(yè)單位招聘15人歷年高頻重點(diǎn)提升(共500題)附帶答案詳解
- 2025下半年廣東江門市開平市招聘事業(yè)單位職員擬聘歷年高頻重點(diǎn)提升(共500題)附帶答案詳解
- DB34∕T 4324-2022 水泥土攪拌樁地基加固施工技術(shù)規(guī)程
- 2024年山東省青島高新區(qū)管委會選聘193人歷年高頻難、易錯(cuò)點(diǎn)500題模擬試題附帶答案詳解
- 2023年江蘇省普通高中信息技術(shù)學(xué)業(yè)水平考試題庫試題
- 醫(yī)學(xué)英語學(xué)習(xí)通超星期末考試答案章節(jié)答案2024年
- 數(shù)控機(jī)床發(fā)展歷史
- (浙教2024版)科學(xué)七年級上冊全冊知識點(diǎn)(新教材)
- 善讀無字之書(2023年廣東中考語文試卷議論文閱讀題及答案)
- 2024年北京平谷區(qū)初三九年級上學(xué)期期末數(shù)學(xué)試題
- 公司控股公司的協(xié)議書范本
- 2024版固定資產(chǎn)的轉(zhuǎn)讓協(xié)議書
- 幼兒園大班語言課件:不怕冷的大衣
評論
0/150
提交評論