【问题标题】:C# Spawn Windows Form on CursorC# 在光标上生成 Windows 窗体
【发布时间】:2013-01-11 19:48:44
【问题描述】:

这可能真的很容易,但我还是想不通。我正在尝试在光标位置(双击)生成我的 Windows 窗体。我能够得到我需要的所有其他东西(cursorXY,clickhandler),但我不知道如何在Show()它之前或之后编辑表单。

public Form2(int cursorX, int cursorY)
{
    Location = new Point(cursorX, cursorY);
...

这不起作用,我知道,因为它指的是它在其容器中的位置。我通过在调试时捕获form.Location 来收集它(它总是0,0)。所以编辑Location 到目前为止没有做任何事情。我可以移动容器或窗口吗?

【问题讨论】:

  • 使用PointToScreen()方法将鼠标位置转换为屏幕位置,适合表单的Location属性。

标签: c# forms location


【解决方案1】:

将表单的StartPosition设置为Manual

public Form2(int x, int y)
{
    InitializeComponent();
    this.StartPosition = FormStartPosition.Manual;
    this.Location = new Point(x, y);
}

【讨论】:

  • 这就是我所需要的!谢谢。不过,我还有另一个问题。您知道如何检索光标相对于活动应用程序的位置吗?我一直在检索的数据是桌面绝对的,如果用户当然不是处于全屏模式,这可能会导致问题。
  • @Joseph 使用PointToClient API。 this.PointToClient(Cursor.Position)
  • 壮观!再次感谢。
【解决方案2】:

将 StartPosition 设置为手动

public Form2(int x, int y)
{
   ...
   this.Location = new Point(x,y)
   this.StartPosition = FormStartPosition.Manual;
   ...
}

See for more

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2023-03-15
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多