C# PictureBox控件 显示位置问题
问题很简单,我想把一张比控件大一点的图像放在控件上面,然后可以响应鼠标的拖动调整图像的显示位置.大家可以想像一下红警里面把鼠标移到屏幕边上的时候的样子.大概就是做成这个效...
问题很简单,我想把一张比控件大一点的图像放在控件上面,然后可以响应鼠标的拖动调整图像的显示位置.
大家可以想像一下红警里面把鼠标移到屏幕边上的时候的样子.大概就是做成这个效果.
所以,如何设置一幅图片在PictureBox里面的显示位置?如果能用坐标控制就好了. 展开
大家可以想像一下红警里面把鼠标移到屏幕边上的时候的样子.大概就是做成这个效果.
所以,如何设置一幅图片在PictureBox里面的显示位置?如果能用坐标控制就好了. 展开
2个回答
展开全部
在 Paint事件里自己画
这个你参考一下
using System;
using System.Drawing;
using System.Windows.Forms;
public class Program
{
private static void Main()
{
Application.Run(new MainForm());
}
}
public class MainForm : Form
{
public MainForm()
{
PictureBox pic = new PictureBox();
Image img = Image.FromFile("image.png"); // 加载图像
Point pti = Point.Empty, pts = Point.Empty;
bool dow = false;
pic.BorderStyle = BorderStyle.FixedSingle;
pic.Dock = DockStyle.Fill;
pic.Parent = this;
pic.MouseDown += delegate(object o, MouseEventArgs e)
{
Rectangle rec = new Rectangle(pti, img.Size);
if (!rec.Contains(e.Location)) return;
pts.X = e.X - pti.X;
pts.Y = e.Y - pti.Y;
dow = true;
};
pic.MouseMove += delegate(object o, MouseEventArgs e)
{
Rectangle rec = new Rectangle(pti, img.Size);
if (!dow) return;
pti.X = (e.X - pts.X);
pti.Y = (e.Y - pts.Y);
pic.Refresh();
};
pic.MouseUp += delegate(object o, MouseEventArgs e)
{
dow = false;
};
pic.Paint += delegate(object o, PaintEventArgs e)
{
e.Graphics.DrawImage(img, new Rectangle(pti, img.Size));
};
ClientSize = new Size(256, 256);
}
}
这个你参考一下
using System;
using System.Drawing;
using System.Windows.Forms;
public class Program
{
private static void Main()
{
Application.Run(new MainForm());
}
}
public class MainForm : Form
{
public MainForm()
{
PictureBox pic = new PictureBox();
Image img = Image.FromFile("image.png"); // 加载图像
Point pti = Point.Empty, pts = Point.Empty;
bool dow = false;
pic.BorderStyle = BorderStyle.FixedSingle;
pic.Dock = DockStyle.Fill;
pic.Parent = this;
pic.MouseDown += delegate(object o, MouseEventArgs e)
{
Rectangle rec = new Rectangle(pti, img.Size);
if (!rec.Contains(e.Location)) return;
pts.X = e.X - pti.X;
pts.Y = e.Y - pti.Y;
dow = true;
};
pic.MouseMove += delegate(object o, MouseEventArgs e)
{
Rectangle rec = new Rectangle(pti, img.Size);
if (!dow) return;
pti.X = (e.X - pts.X);
pti.Y = (e.Y - pts.Y);
pic.Refresh();
};
pic.MouseUp += delegate(object o, MouseEventArgs e)
{
dow = false;
};
pic.Paint += delegate(object o, PaintEventArgs e)
{
e.Graphics.DrawImage(img, new Rectangle(pti, img.Size));
};
ClientSize = new Size(256, 256);
}
}
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询