본문 바로가기

컴퓨터 과학 & 영상처리 관련/C# / .NET / JAVA

C# picture box 내 mouse 좌표 계산 시

       private void pictureBox1_Click(object sender, EventArgs e)

        {

            if (sender.GetType() == pictureBox1.GetType())

            {

                PictureBox pic = (PictureBox)sender;

                int x = Control.MousePosition.X;

                int y = Control.MousePosition.Y;


                Point mousePos = new Point(x, y); //프로그램 내 좌표

                Point mousePosPtoClient = pic.PointToClient(mousePos);  //picbox 내 좌표

                Point mousePosPtoScreen = pic.PointToScreen(mousePos);  //스크린 내 좌표 (좌우 스크린 합친듯?)


                this.Text = x.ToString() + ", " + y.ToString() +

                    ", " + mousePosPtoClient.X.ToString() + ", " + mousePosPtoClient.Y.ToString() +

                    ", " + mousePosPtoScreen.X.ToString() + ", " + mousePosPtoScreen.Y.ToString();


                if (((MouseEventArgs)e).Button == MouseButtons.Left)

                {

                    //do something                    

                }

                if (((MouseEventArgs)e).Button == MouseButtons.Right)

                {

                    //do something

                    

                }

            }

        }




        

        int GetInverseYval(int height, int inputY)

        {

            for (int i = 0, y = height - 1; i < height; i++, y--)

                if (i == inputY)

                    return y;

            return -1;

        }