Finding the class name of the On-Screen Keyboard? - c#

I am attempting to use this code sample to control the Windows XP On-Screen Keyboard (OSK.exe) from a C# (.NET 3.5) Winforms application:
[DllImport("User32.dll")]public static extern Int32 SetForegroundWindow(int hWnd);
[DllImport("user32.dll")]public static extern int FindWindow(string lpClassName, string lpWindowName);
private void BringToFront(string className,string CaptionName)
{
SetForegroundWindow(FindWindow(className,CaptionName));
}
private void Form1_Load(object sender, EventArgs e)
{
BringToFront("Notepad", "Untitled - Notepad");
}
How do I determine the accurate className? I assume that the CaptionName is 'On-Screen Keyboard'.

Looks like the classname is: "OSKMainClass"
Here is the code I used to find this. It's just a simple C# Forms App
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int hWnd = FindWindow(null, "On-Screen Keyboard");
StringBuilder buffer = new StringBuilder(128);
GetClassName(hWnd, buffer, buffer.Capacity);
MessageBox.Show(buffer.ToString());
}
Got this from the following sources Activate Any Window With API
and MSDN GetClassName function

Related

Cant select the window i want to sent keyboard inputs

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=");
}
}

How to enumerate through multiple windows of one process c#

I have this code that I copied from here:
using System.Runtime.InteropServices;
...
protected delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
protected static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
protected static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
protected static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);
[DllImport("user32.dll")]
protected static extern bool IsWindowVisible(IntPtr hWnd);
protected static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam)
{
int size = GetWindowTextLength(hWnd);
if (size++ > 0 && IsWindowVisible(hWnd))
{
StringBuilder sb = new StringBuilder(size);
GetWindowText(hWnd, sb, size);
Console.WriteLine(sb.ToString());
}
return true;
}
static void Main(string[] args)
{
EnumWindows(new EnumWindowsProc(EnumTheWindows), IntPtr.Zero);
}
But I'm a newbie to C# and I don't exactly get the code. How do I filter e.g. that only the windows of Word are printed? (The processName of word is WINWORD)

how can minimize and maximize window folders in C#

I'm trying to minimize window folders and applications with voice ( speech recognition ) and this is my code :
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
private struct POINTAPI
{
public int x;
public int y;
}
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
private struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public POINTAPI ptMinPosition;
public POINTAPI ptMaxPosition;
public RECT rcNormalPosition;
}
private void button1_Click(object sender, EventArgs e)
{
WindowAction_MinimizeNotepad();
}
void WindowAction_MinimizeNotepad()
{
System.IntPtr app_hwnd;
WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
app_hwnd = FindWindow("chrome", null);
GetWindowPlacement(app_hwnd, ref wp);
wp.showCmd = 2;
SetWindowPlacement(app_hwnd, ref wp);
}
i know how to use that code with button but i don't know how to use it with speech recognition so, how can i minimize windows with voice ? thanks.
First You need to Register for speech recognition event like:
On Page load
// Register handler for the SpeechRecognized event.
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(srEvent_SpeechRecognized);
Then create a speech recognition event handler, Like :
void srEvent_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
You may want to have a look at Microsoft.Speech.Recognition.SpeechRecognitionEngine.
Using this you can subscribe to events and proceed with whatever action you want to perform on those events.
Get more information at https://msdn.microsoft.com/en-us/library/hh378426(v=office.14).aspx

Hide TaskBar in WinForms Application [duplicate]

This question already has answers here:
How do I make a WinForms app go Full Screen
(10 answers)
Closed 9 years ago.
How can I hide the Windows taskbar when I run my C# WinForms application?
I tried some code, but it opens in maximized view with the taskbar.
Do you have any sample code or suggestions?
Just add this class into your project .it works as you expected.
using System;
using System.Runtime.InteropServices;
public class Taskbar
{
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
[DllImport("user32.dll")]
public static extern int FindWindowEx(int parentHandle, int childAfter, string className, int windowTitle);
[DllImport("user32.dll")]
private static extern int GetDesktopWindow();
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;
protected static int Handle
{
get
{
return FindWindow("Shell_TrayWnd", "");
}
}
protected static int HandleOfStartButton
{
get
{
int handleOfDesktop = GetDesktopWindow();
int handleOfStartButton = FindWindowEx(handleOfDesktop, 0, "button", 0);
return handleOfStartButton;
}
}
private Taskbar()
{
// hide ctor
}
public static void Show()
{
ShowWindow(Handle, SW_SHOW);
ShowWindow(HandleOfStartButton, SW_SHOW);
}
public static void Hide()
{
ShowWindow(Handle, SW_HIDE);
ShowWindow(HandleOfStartButton, SW_HIDE);
}
}
USAGE:
Taskbar.Hide();
You need to set WinForms application from property like below
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
You need to use P/INVOKE
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;
int hwnd = FindWindow("Shell_TrayWnd","");
ShowWindow(hwnd,SW_HIDE);
I hope that helps

Launch System Screensaver from C# Windows Form

Hopefully this is a simple one, but can anyone provide some simple c# code that will launch the currently configured screensaver?
Here is a good site showing how to work with all aspects of the screensaver. See the comments at the end for the code to start the screensaver.
http://www.codeproject.com/KB/cs/ScreenSaverControl.aspx
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
//...
private const int SC_SCREENSAVE = 0xF140;
private const int WM_SYSCOMMAND = 0x0112;
//...
public static void SetScreenSaverRunning()
{
SendMessage
(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
}

Categories

Resources