360doc文章提取器 9433
文章列表
请规范网址
C#2008 获取点击PictureBox控件时在PictureBox中的坐标值
C#2008 获取点击PictureBox控件时在PictureBox中的坐标值
  • Thursday, August 19, 2010 2:33 PMpucx Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    如题,C#2008 获取点击PictureBox控件时在PictureBox中的坐标值,需要把这个坐标值传递给其他函数使用,谢谢!我需要的是在PictureBox控件里的坐标值

    我需要先给出个提示消息,确定之后再点击图像,但是这个过程怎么才可以使程序等待我点击PictureBox控件呢?我尝试用了MouseClick()事件,但是不行。

Answers

  • Monday, August 30, 2010 2:51 PM肖小勇MVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
        int originalWidth = this.pictureBox1.Image.Width;
        int originalHeight = this.pictureBox1.Image.Height;
        PropertyInfo rectangleProperty = this.pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance | BindingFlags.NonPublic);
        Rectangle rectangle = (Rectangle)rectangleProperty.GetValue(this.pictureBox1, null);
        int currentWidth = rectangle.Width;
        int currentHeight = rectangle.Height;
        double rate = (double)currentHeight / (double)originalHeight;
        int black_left_width = (currentWidth == this.pictureBox1.Width) ? 0 : (this.pictureBox1.Width - currentWidth) / 2;
        int black_top_height = (currentHeight == this.pictureBox1.Height) ? 0 : (this.pictureBox1.Height - currentHeight) / 2;
        int zoom_x = e.X - black_left_width;
        int zoom_y = e.Y - black_top_height;
        double original_x = (double)zoom_x * rate;
        double original_y = (double)zoom_y * rate;
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("原始尺寸{0}/{1}(宽/高)\r\n", originalWidth, originalHeight);
        sb.AppendFormat("缩放状态图片尺寸{0}/{1}(宽/高)\r\n", currentWidth, currentHeight);
        sb.AppendFormat("缩放比率{0}\r\n", rate);
        sb.AppendFormat("左留白宽度{0}\r\n", black_left_width);
        sb.AppendFormat("上留白高度{0}\r\n", black_top_height);
        sb.AppendFormat("当前鼠标坐标{0}/{1}(X/Y)\r\n", e.X, e.Y);
        sb.AppendFormat("缩放图中鼠标坐标{0}/{1}(X/Y)\r\n", zoom_x, zoom_y);
        sb.AppendFormat("原始图中鼠标坐标{0}/{1}(X/Y)\r\n", original_x, original_y);
        this.label1.Text = sb.ToString();
        }
        

    知识改变命运,奋斗成就人生!
    • Marked As Answer bypucx Tuesday, August 31, 2010 1:21 PM
    •  

All Replies

  • Thursday, August 19, 2010 4:23 PM肖小勇MVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    你好!

    MouseClick 事件中事件参数 MouseEventArgs 中可以获取 X,Y 坐标。

     

    private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
        // 点击时记录坐标
        int x = e.X;
        int y = e.Y;
        if (MessageBox.Show("你确定吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
        // 执行你的方法,无须模拟鼠标动作
        // YourMethod(x,y);
        }
        }