gpt4 book ai didi

c# - FindWindow 在当前上下文中不存在

转载 作者:行者123 更新时间:2023-11-30 19:11:06 28 4
gpt4 key购买 nike

我是 WindowsForms 的新手,正在构建一个小型示例应用程序。

我正在尝试构建一个将文本键入记事本窗口的应用程序,但在从 dll 导入时出现错误 FindWindow does not exist in current context

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void MenuAbout_Click(object sender, System.EventArgs e)
{
Form1 frm = new Form1();
frm.ShowDialog();
}

private void Launch_Click(object sender, System.EventArgs e)
{

// find window handle of Notepad
IntPtr handle = FindWindow("Notepad", "Untitled - Notepad");
if (!handle.Equals(IntPtr.Zero))
{
// activate Notepad window
if (SetForegroundWindow(handle))
{
// send "Hello World!"
SendKeys.Send("Hello World!");
// send key "Tab"
SendKeys.Send("{TAB}");
// send key "Enter"
SendKeys.Send("{ENTER}");
}
}
//Process.Start(@"D:\\32-Bit Programs\\StarCraft II\\Support\\SC2Switcher.exe");
}


[DllImport("user32.dll", EntryPoint = "FindWindow")]
private extern IntPtr FindWindow(string lp1, string lp2);

[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private extern bool SetForegroundWindow(IntPtr hWnd);
}
}

最佳答案

您导入的方法 must also be static :

    [DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lp1, string lp2);

[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);

关于c# - FindWindow 在当前上下文中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13371836/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com