I'm new to WindowsForms building a small sample application.
I'm trying to build an application that types text into a Notepad window and I'm getting the error FindWindow does not exist in current context when it's being imported from the dll.
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);
}
}
Your imported methods 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);
Related
I try to get a window active to send keyboard inputs using
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
IntPtr calcWindow = FindWindow(null, "Calculator");
if (SetForegroundWindow(calcWindow))
SendKeys.Send("10{+}10=");
}
I am new to c# and i saw i need to put the exact name that apears on the taskbar, but i try to sent keyboard events to DOSBox which the name of the window i want to select has a weird name that i tried to write multiple times but i didnt get the right thing, do you know how i can browse the windows already opened and select this one or how can i get the exact name
You can use System.Diagnostics.Process to find the process. You can find the process by its ProcessName and then get its MainWindowHandle.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
private void button1_Click(object sender, EventArgs e)
{
// find the process by its name, this is the process name and is not the window name
System.Diagnostics.Process process = System.Diagnostics.Process.GetProcesses()
.FirstOrDefault(p => p.ProcessName.Equals("DOSBox"));
if(process != null)
{
IntPtr calcWindow = process.MainWindowHandle;
SetForegroundWindow(calcWindow);
if (SetForegroundWindow(calcWindow))
SendKeys.SendWait("10{+}10=");
}
}
I'm making a c# overlay for a game.
It appears on the game but it won't hide if the game is minimized so I'm wondering if there's any way to check if a window is minimized/out of focus. I searched about it on google but I couldn't find anything useful.
To check the state of a window (normal, maximized or minimized) when you have the windows caption name or the windows handle, you can use this code:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsZoomed(IntPtr hWnd);
enum WinState
{
None,
Maximized,
Minimized,
Normal,
}
private static WinState GetWindowState(IntPtr hWnd)
{
WinState winState = WinState.None;
if (hWnd != IntPtr.Zero)
{
if (IsIconic(hWnd))
{
winState = WinState.Minimized;
}
else if (IsZoomed(hWnd))
{
winState = WinState.Maximized;
}
else
{
winState = WinState.Normal;
}
}
return winState;
}
private static WinState GetWindowState(string windowCaption)
{
return GetWindowState((IntPtr)FindWindow(null, windowCaption));
}
Usage:
string windowCaption = "New Tab - Google Chrome";
WinState winState = GetWindowState(windowCaption);
I have a simple WinForm app with a button, and on-click I am trying to populate search keyword on Google Chrome already running (on chrome window). When I execute this program and click the button, Chrome window get presented (restored from minimized) but the focus still remains on my WinForm app and SendKeys does not work.
Is there a better way to do this?
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
[DllImport("User32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
[DllImport("User32.DLL")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr chromeHandle = FindWindow("Chrome_WidgetWin_1", null);
if(chromeHandle == IntPtr.Zero)
{
MessageBox.Show("Chrome is not running");
return;
}
SetForegroundWindow(chromeHandle);
SendKeys.SendWait("search term");
}
}
}
My code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private const int WM_SYSCOMMAND = 274; private const int SC_MAXIMIZE = 61488;
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private void button1_Click(object sender, EventArgs e)
{
Process proc;
proc = Process.Start("Notepad++.exe");
proc.WaitForInputIdle();
SetParent(proc.MainWindowHandle,panel1.Handle);
//SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
}
}
When I execute notepad.exe instead of notepad++.exe, it works fine. Normal notepad comes inside the panel in the Windows Form. But when I use notepad++.exe, it is not seen inside the panel instead, is opened outside as a different window. I don't want this. My preference is notepad++ to be embedded inside panel my Windows Form, through which, I want to control the notepad++.
Add Sleep after setParent it will work.
SetParent(proc.MainWindowHandle,panel1.Handle);
Thread.Sleep(500)
When I have an application opened and I try to open again it, i want to restore the application launched with the actual size.
The code used is this:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
private const int SW_SHOW = 5;
public static void SetForegroundMyWindow(IntPtr windowHandle)
{
SetForegroundWindow(windowHandle);
ShowWindow(windowHandle, SW_SHOW);
}
With this code, when the window is maximized, the application is shown with the size without maximize.
Only with SetForegroundWindow, the application is not shown when it is minimized.
What is the way to get my window with the actual size?