I know how to simulate mouse and keyboard events, but they act as if the user did them, so they will affect the window that is active. What I need is to simulate one of those inputs, but in a Window that is not active.
I'm not saying that it is minimized, imagine for example, you have msPaint, and notepad. Notepad is in front of paint. And you want to simulate mouse clicks in certain coordinates of the paint window, but without setting it active, making it possible for the user to keep using notepad which is in fron of paint.
Is this possible at all? Thanks!
I've tried this:
const int WM_KEYDOWN = 0x100;
Thread.Sleep(5000);
Process x = Process.Start(#"C:\WINDOWS\NOTEPAD.EXE");
PInvokes.PlatformInvokeUSER32.SendMessage(x.MainWindowHandle, WM_KEYDOWN, ((int)Keys.W), 0);
but it doesn't work =( Doesn't do anything :(
You can try the UI automation API. It supports also legacy applications.
Maybe try the PostMessage function instead:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern int PostMessage(
int hWnd, int msg, int wParam, IntPtr lParam);
Depending on the target app, there could be lots of issues. Some apps trigger on WM_KEYDOWN or WM_KEYUP instead of WM_CHAR. Also, the key might already be down and is being ignored. For targets like webbrowsers, you need to get the right window handle. Winspector can get that for you.
Your code wont work because SendMessage requires that the window you're sending to be active.
Try PostMessage.
Related
I'm developing a C Windows forms application and I need to block every input from the keyboard and the mouse while the user is using the stylus on a Wacom tablet.
I've tried using global mouse and keyboard hooks as described here to manage all the mouse and keyboard events generated by Windows and blocking the ones not generated by the stylus.
Everything is OK for the keyboard, but I'm not able to tell the source of mouse events. I've tried this kind of approach:
// [DllImport( "user32.dll" )]
// private static extern uint GetMessageExtraInfo( );
uint extra = GetMessageExtraInfo();
bool isPen = ( ( extra & 0xFFFFFF00 ) == 0xFF515700 );
as described here, but it doesn't work, since the value of extra is always 4283912448, reghardless of the source of the event.
Is there something I'm doing wrong?
I need to get an input point (caret position, window/control) that is focused. My application/service needs to detect when the user starts typing and then replace the characters that were typed with predetermined values. The trick is that I don't know where the user wants to type(I don't want to limit this to several applications).
I think that I know how to get/replace the text using:
[DllImport("USER32.DLL", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wparam, StringBuilder text);
How do I get the control that is focused? How do I know from where to get the text and where to send the replacement?
I am doing this in C#, WPF.
Thank you!
If you just want to emulate keyboard activity, you can use keybd_event or SendInput. These automatically deliver the keypresses to the window with focus, so you don't have to detect it yourself.
I am presuming that since you have tagged your question as WinAPI your are trying to intercept keystrokes from another application as in Keyboard Hooking. Take a look at these Links:
Intercepting and Blocking Keystrokes
How do I set a low level mouse hook and keyboard hook in C#?
How to set a Windows hook in Visual C# .NET
A Simple C# Global Low Level Keyboard Hook
How can I change another program's -- let's say Skype's -- window's size, from my C# program?
You can use MoveWindow (Where hWnd is the window you want to move):
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
MoveWindow(ApplicationHandle, 600, 600, 600, 600, true);
If you don't know the window pointer, you can use the FindWindow functionality.
Also worth a read is MSDN SetWindowPos (Very similar to MoveWindow).
You need to get the window handle of the other program, use Process.MainWindowHandle or FindWindow.
Having this, you can PInvoke SetWindowPos() to move, resize, change the Z-order or the min/max/restore state of the window.
I would use the Windows Api SetWindowPos
check this one out: Using SetWindowPos in C# to move windows around
of course first you should know the handle of the window you want to resize, this can be done in many ways like getting the process by name then the MainWindow of that process or with EnumWindow or FindWindow APIs
So, I thought this would be simple and, well, I was wrong. Here is a simplified description of the problem:
I am writing a small application for our manufacturing folks that will grab a screenshot of the entire desktop as well as the foreground window when they click the application's icon in the system tray. Currently, I am using the Win32 method "GetforegroundWindow" in the MouseMove event of the NotifyIcon to save the foreground window handle and take the screenshot in the Click event.
This does work sometimes, but if I click the icon very quickly I actually capture the task bar instead of the foreground window. I am not sure why this is happening (I do understand that the task bar is a window, I don't understand why sometimes it seems to have focus in MouseMove before I have clicked), and I have had little luck using the EnumWindows method as well, likely because I do not completely understand how it works.
It would seem that, if I were able to get the z position of each window using only the window handle, this would be an easy problem to solve using EnumWindows. I have not found a method to do that however.
So, I ask you guys; how would you write a method to locate the foreground window reliably, given that it may not have focus at the time? Either my google-fu is failing me or the information on this is sparse. Thanks in advance.
The taskbar is as valid a foreground window as any. When you click it, it will temporarily be the foreground window. And if you click Start and press Escape for example, it will be the foreground window until you click off of it.
You can probably use GetWindow with HWND_NEXT passing in the window handle of the taskbar.
Nevermind, since the taskbar is a topmost window, GetWindow (or GetNextWindow, etc) will operate differently. I would suggest revisiting the EnumWindows solution which is probably your best bet.
If the form you want the snapshot of is the same as the form linked to the task bar, you really do not need to use GetforegroundWindow. Just use the Form.hWnd and pass that into the function getting the snapshot. You may need to make it the top window by calling
[DllImport( "user32.dll" )]
public static extern IntPtr SetForegroundWindow( IntPtr hWnd );
or
[DllImport( "user32.dll" )]
public static extern bool BringWindowToTop( HandleRef hWnd );
If you want the whole desktop, then you probably just need to put in a Thread.Sleep to make sure the foreground window has had enough to come to the top before getting the desktop snapshot.
putting the src from my comment here for better printing
[DllImport( "user32.dll" )]
public static extern IntPtr GetForegroundWindow();
[DllImport( "user32.dll" )]
public static extern IntPtr GetActiveWindow();
// this or the next line not both
IntPtr curWindow = GetActiveWindow();
IntPtr curWindow = GetForegroundWindow();
BringWindowToTop( window );
System.Threading.Thread.Sleep( 500 );
Where the thread spleep gives the window enough time to come to the top of the Z order.
I am interested in writing an application that overlays a small heads up display (HUD) over another application, in VB.NET. What is an example of this?
I will need to enumerate all open windows to find the window that I want, and then overlay some text in a specific position on the window. If the user moves that window, my text will need to follow. (I will probably be painting the text in a loop over and over).
Edit: nobody answered my original query - I added C# to the keywords to see if any of gurus in that language might have an answer.
You can use WinApi to enumerate windows.
You can start googling with
[DllImport("user32.dll")]
public static extern int EnumWindows(EnumWindowsProc ewp, int lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, ref WapiRect lpRect);
When you have found your window and has its handle, there is no problem to plot on it with something like
Graphics g = Graphics.FromHwnd(win.Handle);
g.FillRectangle(new SolidBrush(Color.White), 0, 0, 1000, 1000);
But to overlay... One possible solution is to create own border less form(it can be made even transparent) and place your text on it. Then just place this special form on top of another application.