[英]location of mouse to draw a image
這是我的項目界面的代碼,但是當我移動鼠標時我無法確定鼠標的位置!當我在屏幕上繪制圖像時,它無法繪制正確的位置!
public partial class Form1 : Form
{
int _countRouter = 0;
Point[] _posiRouter = new Point[100];
readonly Image _imgRouter = Image.FromFile(@"C:\Router2.png");
public Form1()
{
InitializeComponent();
}
private void btnRouter_MouseUp(object sender, MouseEventArgs e)
{
//panelMain.Cursor = new Cursor(Cursor.Current.Handle);
//Point x = Cursor.Position;
Point x = new Point(e.X, e.Y);
if ((x.X > 10 || x.X < 660) && (x.Y > 30 || x.Y < 350))
{
_posiRouter[_countRouter].X = x.X;// -_imgRouter.Width;
_posiRouter[_countRouter].Y = x.Y;// -_imgRouter.Height;
_countRouter++;
}
this.panelOption.Invalidate();
this.panelMain.Invalidate();
}
private void panelMain_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White,
new Rectangle ( 0, 0, this.ClientRectangle.Width ,
this.ClientRectangle.Height));
for (int x = 0; x < _countRouter; x++)
{
g.DrawImage(_imgRouter, _posiRouter[x]);
}
}
// ... ?
}
從MSDN :
鼠標坐標根據引發的事件而變化。 例如,當處理Control.MouseMove事件時,鼠標坐標值相對於引發該事件的控件的坐標。 與拖放操作有關的某些事件具有與表單原點或屏幕原點相關的關聯鼠標坐標值。
看一看,嘗試通過放置調試點來找出鼠標所在的位置Point x = new Point(eX, eY);
看看你得到什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.