C# winfrom GDI+ 实现控件左右移动 如题 在线等 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 直接修改控件的location不就可以实现吗?不用GDI+吧? 如果你用gdi+绘制一幅图片的画并且显示在picktruebox中,实现picktruebox控件的左右拖动你可以在picktruebox的mousemove中方法记录移动的距离 private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { int x = e.X - pt.X; int y = e.Y - pt.Y; if ((e.Button & MouseButtons.Left) == MouseButtons.Left) { pictureBox1.Location = new Point(pictureBox1.Location.X + x, pictureBox1.Location.Y + y); } }在mousedown事件中记录picktruebox的位置 Point pt; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { pt = e.Location ; }这样就可以实现picktruebox在窗体中任意的移动。。 为什么要用GDI?给你一个用鼠标改变控件位置的代码吧,不知道是不是你想要的。using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace user_defined_from{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.CheckedListBox checkedListBox1; private System.Windows.Forms.ComboBox comboBox1; private System.ComponentModel.IContainer components; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.checkedListBox1 = new System.Windows.Forms.CheckedListBox(); this.comboBox1 = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(176, 64); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(88, 24); this.button1.TabIndex = 4; this.button1.Text = "button1"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(176, 112); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(88, 21); this.textBox1.TabIndex = 5; this.textBox1.Text = "textBox1"; // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2, this.menuItem3}); this.menuItem1.Text = "set"; // // menuItem2 // this.menuItem2.Index = 0; this.menuItem2.Text = "defined mode"; this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click); // // menuItem3 // this.menuItem3.Checked = true; this.menuItem3.Index = 1; this.menuItem3.Text = "user mode"; this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click); // // checkedListBox1 // this.checkedListBox1.Location = new System.Drawing.Point(136, 176); this.checkedListBox1.Name = "checkedListBox1"; this.checkedListBox1.Size = new System.Drawing.Size(112, 84); this.checkedListBox1.TabIndex = 6; // // comboBox1 // this.comboBox1.Location = new System.Drawing.Point(176, 144); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(121, 20); this.comboBox1.TabIndex = 7; this.comboBox1.Text = "comboBox1"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(336, 333); this.Controls.Add(this.comboBox1); this.Controls.Add(this.checkedListBox1); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Cursor = System.Windows.Forms.Cursors.Default; this.Menu = this.mainMenu1; this.Name = "Form1"; this.Text = "Form1"; this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown); this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void menuItem2_Click(object sender, System.EventArgs e) { menuItem3.Checked=false; menuItem2.Checked=true; for(int i=0;i<this.Controls.Count;i++) { this.Controls[i].Enabled=false; } } private void menuItem3_Click(object sender, System.EventArgs e) { menuItem3.Checked=true; menuItem2.Checked=false; for(int i=0;i<this.Controls.Count;i++) { this.Controls[i].Enabled=true; } } private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {// int x,y;// x=e.X;// y=e.Y;// if(button1.Enabled==false)// {// button1.Top = y;// button1.Left = x;// } } private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { int x,y; x=e.X; y=e.Y; for(int i=0;i<this.Controls.Count;i++) { if(x>Controls[i].Location.X && x<Controls[i].Location.X+Controls[i].Width) { if(y>Controls[i].Location.Y && y< Controls[i].Location.Y+Controls[i].Height) { if(e.Button==MouseButtons.Left) { if(Controls[i].Enabled==false) { this.Cursor=Cursors.SizeAll; Controls[i].Top = y-10; Controls[i].Left = x-10; } } } else { this.Cursor=Cursors.Default; } } else { this.Cursor=Cursors.Default; } } } private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { } }} GDI+.....这个好象和移动控件没关系Control.Location.X--;Control.Location.Y++; 你们看一下这段代码的效果我要的效果和这个差不多 void timerSpeed_Tick(object sender, EventArgs e) { if (!mouseDown) { count++; if (count > MaxCount) { MaxCount = new Random().Next(70) + 30; if (speedMode) timerSpeed.Interval = 50; count = 0; stepX = (float)new Random().NextDouble() * 3f + 1f; stepY = (float)new Random().NextDouble() * 0.5f; if (stepY < 0.3f) stepY = 0f; stepY = (new Random().Next(2) == 0 ? -1 : 1) * stepY; } left = (left + (toRight ? 1 : -1) * stepX); top = (top + stepY); FixLeftTop(); this.Left = (int)left; this.Top = (int)top; } frame++; if (frame >= frameCount) frame = 0; SetBits(FrameImage); } http://blog.csdn.net/dunao/archive/2009/02/05/3865053.aspx 参考一下吧! B程序及时获取A程序删除的数据 关于数据库BLOB字段的连续更新问题 字符串编码转换 querycommander for .net2.0? ucs2转成string listview 的checkbox的背景色! 这样的字符串如何实现替换 QQ刷Q币的好机会,有兴趣的朋友来参加吧, asp.net activex控件传值 一个简单问题,大侠请指教 急,printForm.用不了问题? 为什么picturebox上的label有背景色?
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
int x = e.X - pt.X;
int y = e.Y - pt.Y;
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
pictureBox1.Location = new Point(pictureBox1.Location.X + x, pictureBox1.Location.Y + y);
}
}在mousedown事件中记录picktruebox的位置
Point pt;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
pt = e.Location ;
}
这样就可以实现picktruebox在窗体中任意的移动。。
给你一个用鼠标改变控件位置的代码吧,不知道是不是你想要的。using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace user_defined_from
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.CheckedListBox checkedListBox1;
private System.Windows.Forms.ComboBox comboBox1;
private System.ComponentModel.IContainer components; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(176, 64);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 4;
this.button1.Text = "button1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(176, 112);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(88, 21);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "textBox1";
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3});
this.menuItem1.Text = "set";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "defined mode";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Checked = true;
this.menuItem3.Index = 1;
this.menuItem3.Text = "user mode";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// checkedListBox1
//
this.checkedListBox1.Location = new System.Drawing.Point(136, 176);
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.Size = new System.Drawing.Size(112, 84);
this.checkedListBox1.TabIndex = 6;
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(176, 144);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 7;
this.comboBox1.Text = "comboBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(336, 333);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.checkedListBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void menuItem2_Click(object sender, System.EventArgs e)
{
menuItem3.Checked=false;
menuItem2.Checked=true;
for(int i=0;i<this.Controls.Count;i++)
{
this.Controls[i].Enabled=false;
} } private void menuItem3_Click(object sender, System.EventArgs e)
{
menuItem3.Checked=true;
menuItem2.Checked=false;
for(int i=0;i<this.Controls.Count;i++)
{
this.Controls[i].Enabled=true;
} } private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
// int x,y;
// x=e.X;
// y=e.Y;
// if(button1.Enabled==false)
// {
// button1.Top = y;
// button1.Left = x;
// }
} private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
int x,y;
x=e.X;
y=e.Y;
for(int i=0;i<this.Controls.Count;i++)
{
if(x>Controls[i].Location.X && x<Controls[i].Location.X+Controls[i].Width)
{
if(y>Controls[i].Location.Y && y< Controls[i].Location.Y+Controls[i].Height)
{
if(e.Button==MouseButtons.Left)
{
if(Controls[i].Enabled==false)
{
this.Cursor=Cursors.SizeAll;
Controls[i].Top = y-10;
Controls[i].Left = x-10;
}
}
}
else
{
this.Cursor=Cursors.Default;
}
}
else
{
this.Cursor=Cursors.Default;
}
}
} private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
}
}
}
Control.Location.X--;Control.Location.Y++;
void timerSpeed_Tick(object sender, EventArgs e)
{
if (!mouseDown)
{
count++;
if (count > MaxCount)
{
MaxCount = new Random().Next(70) + 30;
if (speedMode) timerSpeed.Interval = 50; count = 0;
stepX = (float)new Random().NextDouble() * 3f + 1f;
stepY = (float)new Random().NextDouble() * 0.5f;
if (stepY < 0.3f) stepY = 0f;
stepY = (new Random().Next(2) == 0 ? -1 : 1) * stepY;
} left = (left + (toRight ? 1 : -1) * stepX);
top = (top + stepY);
FixLeftTop();
this.Left = (int)left;
this.Top = (int)top;
}
frame++;
if (frame >= frameCount) frame = 0; SetBits(FrameImage);
}