简单的说就是实现能对图片通过鼠标的拖拽进行观看,因为有的图片太大,在panel中不能全不显示出来,所以要求能拖拽,希望高手指点.
this.pictureBox1.Location.X = lx + (ex - sx);
this.pictureBox1.Location.Y = ly + (ey - sy);
我用了上面的方法试探重新定位PictureBox的Location,但是location的属性不能更改,所以就不行了.小弟很是困惑,希望高手的指点.如果有源码更是感谢了.复杂的要求就是:当一个按钮被按下的时候,就可以对图片通过鼠标的拖拽进行观看.当再次点击这个按钮就不能拖拽了.如果能把鼠标指针变成一个小手之类的更好了.只要能实现简单的功能,复杂的不感奢求了.
this.pictureBox1.Location.X = lx + (ex - sx);
this.pictureBox1.Location.Y = ly + (ey - sy);
我用了上面的方法试探重新定位PictureBox的Location,但是location的属性不能更改,所以就不行了.小弟很是困惑,希望高手的指点.如果有源码更是感谢了.复杂的要求就是:当一个按钮被按下的时候,就可以对图片通过鼠标的拖拽进行观看.当再次点击这个按钮就不能拖拽了.如果能把鼠标指针变成一个小手之类的更好了.只要能实现简单的功能,复杂的不感奢求了.
解决方案 »
- ExecuteReader的关闭
- 有没有判断一个值是否为数值型的函数
- c#中没有整除运算符,如何实现整除功能呀?比如 5整除以2等于2
- 关闭窗体并刷新另一窗体数据
- 问个:定时发短信的技术问题
- 请教前辈如何在一个窗体调用另一个窗体创建的控件?
- Split,急啊
- 如何在datagrid中插入一行后,Updata到数据库中(WinForm)
- 为什么CheckedListBo.SelectionMode不能选择MultiSimple/MultiExtended?
- 为什么我使用VS·NET粘贴为HTML代码时,源代码总是自动的被添加了一些其它们的代码
- 同志们
- 请问.net类库下的XmlNode和XmlElement有什么区别?
System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
String[] str_Drop=(String[])e.Data.GetData(DataFormats.FileDrop,true);;
string tempstr;
Bitmap bkImage;
tempstr = str_Drop[0];
try
{
bkImage = new Bitmap(tempstr);
this.Size = new System.Drawing.Size(bkImage.Width,
bkImage.Height);
this.BackgroundImage = bkImage;}
catch {}
}
加載事件代碼的語句
this.DragEnter += new System.Windows.Forms.DragEventHandler(
this.Form1_DragEnter);
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;namespace 拖动图片
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.PictureBox[] pictureBox=new PictureBox[2];
//private System.Windows.Forms.PictureBox pictureBox2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
for(int i=0;i<2;i++)
{
this.pictureBox[i].MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
this.pictureBox[i].MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox[i].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
}
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.SuspendLayout();
this.pictureBox[0] = new System.Windows.Forms.PictureBox();
this.pictureBox[1] = new System.Windows.Forms.PictureBox();
//
// pictureBox1
//
this.pictureBox[0].BackColor = System.Drawing.Color.Transparent;
this.pictureBox[0].Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox[0].Location = new System.Drawing.Point(200, 192);
this.pictureBox[0].Name = "pictureBox1";
this.pictureBox[0].Size = new System.Drawing.Size(88, 72);
this.pictureBox[0].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox[0].TabIndex = 0;
this.pictureBox[0].TabStop = false;
/*this.pictureBox[1].MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
this.pictureBox[1].MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox[1].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);*/
//
// pictureBox2
//
//this.Opacity=0.8;
this.pictureBox[1].BackColor = System.Drawing.Color.DodgerBlue;
this.pictureBox[1].Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
this.pictureBox[1].Location = new System.Drawing.Point(200, 72);
this.pictureBox[1].Name = "pictureBox2";
this.pictureBox[1].Size = new System.Drawing.Size(88, 64);
this.pictureBox[1].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.pictureBox[1].TabIndex = 1;
this.pictureBox[1].TabStop = false;
/*this.pictureBox[2].MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
this.pictureBox[2].MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox[2].MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
*/
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(432, 348);
this.Controls.Add(this.pictureBox[0]);
this.Controls.Add(this.pictureBox[1]);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} bool whetherSelected=false;
Point p=new Point(); #region 事件代码
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
whetherSelected=true;
p.X=Cursor.Position.X;
p.Y=Cursor.Position.Y;
} private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
whetherSelected=false;
} private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(whetherSelected==true)
{
//加入一个函数,得到是哪个pictureBox
int index=-1;
for(int i=0;i<2;i++)
if(this.pictureBox[i].Capture)
index=i;
this.pictureBox[index].Left=this.pictureBox[index].Left+(Cursor.Position.X-p.X);
this.pictureBox[index].Top=this.pictureBox[index].Top+(Cursor.Position.Y-p.Y);
this.pictureBox[1-index].SendToBack(); p.X=Cursor.Position.X;
p.Y=Cursor.Position.Y;
}
}
#endregion
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace shi12
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; 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()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
String[] str_Drop=(String[])e.Data.GetData(DataFormats.FileDrop,true);;
string tempstr;
Bitmap bkImage;
tempstr = str_Drop[0];
try
{
bkImage = new Bitmap(tempstr);
this.Size = new System.Drawing.Size(bkImage.Width,
bkImage.Height);
this.BackgroundImage = bkImage;}
catch {}
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace Example
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
private void InitializeComponent()
{
//
// Form1
//
this.AllowDrop = true;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.DragEnter += new System.Windows.Forms.DragEventHandler(
this.Form1_DragEnter);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
String[] str_Drop=(String[])e.Data.GetData(DataFormats.FileDrop,true);;
string tempstr;
Bitmap bkImage;
tempstr = str_Drop[0];
try
{
bkImage = new Bitmap(tempstr);
this.Size = new System.Drawing.Size(bkImage.Width,
bkImage.Height);
this.BackgroundImage = bkImage;}
catch {}
} }
}
這是完整代碼,你拖圖片到上面就OK
你也可以自己修改為你所需要的
this.pictureBox1.Top = ly + (ey - sy);
/// 显示图片
/// </summary>
private void drawImg(Bitmap bmpIn)
{
if(bmpIn==null)return;
Bitmap bmp=bmpIn;
Graphics g=this.CreateGraphics();
if(this.startPoint.X<0)
{
this.startPoint.X=0;
}
if(this.startPoint.X>=bmp.Width-this.ww)
{
this.startPoint.X=bmp.Width-this.ww;
}
if(this.startPoint.Y<0)
{
this.startPoint.Y=0;
}
if(this.startPoint.Y>bmp.Height-this.hh)
{
this.startPoint.Y=bmp.Height-this.hh;
}
if(this.ww>bmp.Width)
{
this.ww=bmp.Width;
}
else
{
this.ww=225;
}
if(this.hh>bmp.Height)
{
this.hh=bmp.Height;
}
else
{
this.hh=215;
}
Rectangle srcRec=new Rectangle(this.startPoint.X,this.startPoint.Y,this.ww,this.hh);
GraphicsUnit units=GraphicsUnit.Pixel;
g.DrawImage(bmp,this.x0,this.y0,srcRec,units);
this.downPoint.X=this.upPoint.X;
this.downPoint.Y=this.upPoint.Y;
} //当触笔接触屏幕时进行的处理
private void frmImage_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.X>=this.x0 && e.X<=233 && e.Y>=this.y0 && e.Y<=261)
{
this.timer1.Enabled=true;
this.downPoint.X=e.X;
this.downPoint.Y=e.Y;
this.upPoint.X=e.X;
this.upPoint.Y=e.Y;
this.redrawSign=true;
}
else
{
this.redrawSign=false;
}
} //当触笔离开屏幕时进行的处理
private void frmImage_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(this.redrawSign==true)
{
this.upPoint.X=e.X;
this.upPoint.Y=e.Y;
this.xStep=this.downPoint.X-this.upPoint.X;
this.yStep=this.downPoint.Y-this.upPoint.Y;
this.startPoint.X=this.startPoint.X+this.xStep;
this.startPoint.Y=this.startPoint.Y+this.yStep;
this.timer1.Enabled=false;
this.drawImg(bmp);
}
else
{
return;
}
} //定时刷新图片,产生图片不断移动的效果
private void timer1_Tick(object sender, System.EventArgs e)
{
this.xStep=this.downPoint.X-this.upPoint.X;
this.yStep=this.downPoint.Y-this.upPoint.Y;
this.startPoint.X=this.startPoint.X+this.xStep;
this.startPoint.Y=this.startPoint.Y+this.yStep;
this.drawImg(bmp);
} //当鼠标在屏幕上移动时
private void frmImage_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(this.redrawSign==false) return;
this.upPoint.X=e.X;
this.upPoint.Y=e.Y;
} //保证窗体重绘时图片能够正常显示
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint(e);
this.drawImg(this.bmp);
}
因為你沒有把創體的可拖設為true,默認為false
this.AllowDrop = true;
感谢lovebaby999,你的代码是另一种实现方式.
两个人的方法还是不错的.大家有没有什么方法让在鼠标点击的部分在显示区的中间显示呢???
{
//放大
if((flag == true) && (type == 1))
{
int fx = (this.Left + this.Width+5)/2;
int fy = (this.Top + this.Height+20)/2;
int u =(int)((p.X-this.Left)*1.1)+this.Left;
int v =(int)((p.Y-this.Top)*1.1)+this.Top; this.pictureBox1.Scale(1.1f,1.1f);
if(this.pictureBox1.Capture)
{
this.pictureBox1.Left = this.pictureBox1.Left+(fx - u);
this.pictureBox1.Top = this.pictureBox1.Top+(fy - v);
} /*int fx = (this.Left + this.Width)/2;
int fy = (this.Top + this.Height)/2; int u =(int)((p.X-this.pictureBox1.Left)*1.1)+this.pictureBox1.Left;
int v =(int)((p.Y-this.pictureBox1.Top)*1.1)+this.pictureBox1.Top; this.pictureBox1.Scale(1.1f,1.1f);
if(this.pictureBox1.Capture)
{
this.pictureBox1.Left = this.pictureBox1.Left+(fx - u);
this.pictureBox1.Top = this.pictureBox1.Top+(fy - v);
}*/ /*int fx = (this.Left + this.Width)/2;
int fy = (this.Top + this.Height)/2;
double u = (p.X-this.pictureBox1.Left)*1.0/this.pictureBox1.Width;
double v = (p.Y-this.pictureBox1.Top)*1.0/this.pictureBox1.Height;
this.pictureBox1.Scale(1.1f,1.1f);
int hx = (int)(this.pictureBox1.Width * u + this.pictureBox1.Location.X);
int hy = (int)(this.pictureBox1.Height * v + this.pictureBox1.Location.Y);
this.pictureBox1.Left = this.pictureBox1.Left + (fx - hx);
this.pictureBox1.Top = this.pictureBox1.Top + (fy - hy);*/
}
//缩小
else if((flag == true) && (type == 2))
{
int fx = (this.Left + this.Width+5)/2;
int fy = (this.Top + this.Height+20)/2; int u =(int)((p.X-this.Left)*0.9)+this.Left;
int v =(int)((p.Y-this.Top)*0.9)+this.Top; this.pictureBox1.Scale(0.9f,0.9f);
if(this.pictureBox1.Capture)
{
this.pictureBox1.Left = this.pictureBox1.Left+(fx - u);
this.pictureBox1.Top = this.pictureBox1.Top+(fy - v);
} /*int fx = (this.Left + this.Width)/2;
int fy = (this.Top + this.Height)/2;
double u = (p.X-this.pictureBox1.Left)*1.0/this.pictureBox1.Width;
double v = (p.Y-this.pictureBox1.Top)*1.0/this.pictureBox1.Height;
this.pictureBox1.Scale(0.9f,0.9f);
int hx = (int)(this.pictureBox1.Width * u + this.pictureBox1.Location.X);
int hy = (int)(this.pictureBox1.Height * v + this.pictureBox1.Location.Y);
this.pictureBox1.Left = this.pictureBox1.Left + (fx - hx);
this.pictureBox1.Top = this.pictureBox1.Top + (fy - hy);*/
}
flag = false;
}
这里面是实现放大和缩小的,并把所点击的部分挪动到显示区form窗体的中间,注释里面的也可以实现,就是方法不一样,其实也差不多.
现在有出现一个新的问题,无论是用什么样子的方法,在窗体弹出来的开始位置都是没有问题的,一旦把form窗体拖动到别的位置,再执行放大和缩小的操作,就只能是在原来的form窗体位置中间显示了.这是为什么呢??????????
int fy = panel1.Height/2;int u =(int)((p.X-this.Left)*1.1);
int v =(int)((p.Y-this.Top)*1.1);
这样就可以了,呵呵。结帖了.