在C#窗体中如何实现图片的动态移动,即鼠标可拖动图片到任意位置,能否给出具体C#代码?多谢了
解决方案 »
- ThreadPool 与 beginInvoke
- datagridview 绑定问题
- .xsd .edmx 哪个更适合制作数据字典?
- 做一个WF窗体的启动项,就是让这个程序启动的时候首先会有一个图片出来,然后开始程序,后台代码怎么弄啊,前面的视图上需要PictureBox之外还需要什么吗
- winform C# 如何动态更新一个列表
- 请教下,软件工程的书哪些比较好?有无和.NET有关的?
- 串口通讯
- 关于分辨率的问题,请大家帮助?
- 怎样获取WORD表内容的行号列号
- 不知大家想过没有 ,这样行得通吗,详情请看。。
- c# winfrom 遍历问题
- c# 怎样实现在加载大量数据的时候显示“正在加载,请稍等......" 加载完后再关闭
楼主去看看吧 这有你要的例子
{
pictureBox1.Left = e.X;
pictureBox1.Top = e.Y;
this.Refresh();
}
{
public Form1()
{
InitializeComponent();
pictureBox1.Paint+=new PaintEventHandler(pictureBox1_Paint);
this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
}
float zoom;
Point oldPoint,newPoint;
bool IsDown;
bool IsLoadBmp;
Bitmap bmp;
private void Form1_Load(object sender, EventArgs e)
{
IsLoadBmp = IsDown = false;
zoom = 1.0f;
oldPoint = new Point(0,0);
newPoint = new Point(0,0);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
if (IsLoadBmp)
{
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
rect.X = newPoint.X;
rect.Y = newPoint.Y;
rect.Width = (int)(rect.Width * zoom);
rect.Height = (int)(rect.Height * zoom);
g.FillRectangle(Brushes.White, pictureBox1.ClientRectangle);
g.DrawImage(bmp, rect, new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
}
}
private void OpenImg_Click(object sender, EventArgs e)
{
OpenFileDialog ofg = new OpenFileDialog();
ofg.Filter = "图片文件(*.bmp)|*.bmp;*.jpg;*.jpeg;*.png;*.gif";
ofg.Multiselect = false;
if (ofg.ShowDialog() == DialogResult.OK)
{
Form1_Load(null, null);
Image img = Image.FromFile(ofg.FileName);
bmp = new Bitmap(img);
if (pictureBox1.Width < bmp.Width || pictureBox1.Height < bmp.Height)
zoom = Math.Min((float)pictureBox1.Width / (float)bmp.Width, (float)pictureBox1.Height / (float)bmp.Height);
else
zoom = 1.0f;
img.Dispose();
IsLoadBmp = true;
pictureBox1.Refresh();
}
}
private void Form1_MouseWheel(object sender, MouseEventArgs e)
{
zoom += (float)e.Delta/1000;
if (zoom < 0)
zoom = 0.0f;
if (zoom > 100)
zoom = 10.0f;
pictureBox1.Refresh();
} private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
IsDown = true;
oldPoint.X =e.Location.X-newPoint.X;
oldPoint.Y = e.Location.Y - newPoint.Y;
this.Cursor = Cursors.Hand;
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (IsDown)
{
Point temp = e.Location;
newPoint.X=temp.X - oldPoint.X;
newPoint.Y=temp.Y - oldPoint.Y;
pictureBox1.Refresh();
}
} private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
IsDown = false;
this.Cursor = Cursors.Default;
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://www.google.com");
}
}
int x0, y0;
bool lbtclk = false;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
x0 = e.X;
y0 = e.Y;
lbtclk = (e.Button == System.Windows.Forms.MouseButtons.Left);
} private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (lbtclk)
{
pictureBox1.Left += (e.X - x0);
pictureBox1.Top += (e.Y - y0);
}
} private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
lbtclk = false;
}