form裏有個panel,panle裏有個button,獲取這個button在form裏的座標。

form1.PointToClient( panel1.PointToScreen(button1.Location) );

 

這個步驟分爲2步

第一步,將buttion1在panel1中的座標轉換爲屏幕座標

第二部,將屏幕座標轉換爲在form1中的座標。

 

    下面的代碼實例使用用戶能夠將圖像或圖像文件拖到窗體上,並使它再放置點顯示。每次繪製窗體時,都重寫OnPaint方法以重新繪製圖像;

否則圖像將保持到下一次重新繪製。DragEnter事件處理方法決定拖到窗體中的數據的類型,並提供適當的反饋,如果Image可以從數據中創建,

則DragDrop事件處理方法就會在該窗體上先生此圖像。因此DragEventArgs.X和DragEventArgs.Y值爲屏幕座標,所以使用PrintToClient方法將

它們轉換成工作區座標。

 


        public Form1()
        {
           // InitializeComponent();
            this.AllowDrop = true;
            this.DragDrop += new DragEventHandler(this.Form1_DragDrop);
            this.DragEnter += new DragEventHandler(this.Form1_DragEnter);

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            // If there is an image and it has a location,
            // paint it when the Form is repainted.
            base.OnPaint(e);
            if (this.picture != null && this.pictureLocation != Point.Empty)
            {
                e.Graphics.DrawImage(this.picture, this.pictureLocation);
            }
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            // Handle FileDrop data.
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                // Assign the file names to a string array, in
                // case the user has selected multiple files.
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                try
                {
                    // Assign the first image to the picture variable.
                    this.picture = Image.FromFile(files[0]);
                    // Set the picture location equal to the drop point.
                    this.pictureLocation = this.PointToClient(new Point(e.X, e.Y));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }

            // Handle Bitmap data.
            if (e.Data.GetDataPresent(DataFormats.Bitmap))
            {
                try
                {
                    // Create an Image and assign it to the picture variable.
                    this.picture = (Image)e.Data.GetData(DataFormats.Bitmap);
                    // Set the picture location equal to the drop point.
                    this.pictureLocation = this.PointToClient(new Point(e.X, e.Y));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
            // Force the form to be redrawn with the image.
            this.Invalidate();

        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            // If the data is a file or a bitmap, display the copy cursor.
            if (e.Data.GetDataPresent(DataFormats.Bitmap) ||
               e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }

        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章