c# PointToClient与PointToScreen

这篇博客介绍了如何在Windows窗体应用程序中进行Panel控件的对齐操作,利用PointToClient和PointToScreen方法将屏幕坐标转换为工作区坐标,以实现将一个Panel对齐到另一个控件下方的功能。示例代码展示了如何在按钮点击事件中完成这一操作。
摘要由CSDN通过智能技术生成

参考:https://www.cnblogs.com/crwy/p/9285463.html

        //
        // 摘要:
        //     将指定屏幕点的位置计算成工作区坐标。
        //
        // 参数:
        //   p:
        //     要转换的屏幕坐标 System.Drawing.Point。
        //
        // 返回结果:
        //     一个 System.Drawing.Point,它表示转换后的 System.Drawing.Point、p(以工作区坐标表示)。
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public Point PointToClient(Point p);
        //
        // 摘要:
        //     将指定工作区点的位置计算成屏幕坐标。
        //
        // 参数:
        //   p:
        //     要转换的工作区坐标 System.Drawing.Point。
        //
        // 返回结果:
        //     一个 System.Drawing.Point,它表示转换后的 System.Drawing.Point、p(以屏幕坐标表示)。
        public Point PointToScreen(Point p);

 将一个Panel对齐到一个控件的下方,无论是单个窗体还是嵌套窗体:

        private void button2_Click(object sender, EventArgs e)
        {
            Control ctrl = (Control)sender;
            Point p1 = ctrl.PointToScreen(new Point(0, 0));
            Point p2 = this.PointToScreen(new Point(0, 0));
            this.Controls.Add(panel1);
            panel1.Location = new Point(p1.X - p2.X, p1.Y - p2.Y + ctrl.Height);
            panel1.BringToFront();
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值