There're a lot of ways to simulate a mouse click in an inactive window. I want my program to make clicks even when it's minimized and of course without capturing the main mouse.
Specifically, my C# program will contain a Web Control displaying some flash content and this is where I want the clicks to occur.
here's the techniques I tried:
1.[DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
2.[DllImport("user32.dll", SetLastError = true)]
public static extern bool SendMessage(int hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
Both of them did not do any good in my case. Surprisingly, I was able to use the SendMessage() function to send mouse clicks in a minimized chrome displaying some flash content. but, it doesn't seem to do that in my C# program. Is there a possibility that my program not receiving the message for some reason?
Any Hints on how can I do this?
Related
Google chrome is able to execute the SendMessage Function messages
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SendMessage(int hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
I wanna have an insight on how or why does it execute them?
and can I create an C# app on my own to execute SendMessage Commands?
The task is this: I need to be able to show popup from my application but doing so should not interact with user typing something in skype, playing games, etc. The window of the application should me opened if it's minimized.
I have the instance of my Main Window. This instance contains code that will show the popup. Thing is I can't use something like this:
window.ShowActivated = false;
simply because this seams to work only if the window is first shown (which is not - the application is already running when I need to call the popup).
Now the other approach that works is using win api.
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
var currentForegroundWindow = GetActiveWindow();
window.Show();
SetForegroundWindow(currentForegroundWindow);
What are my concerns - if user is playing game or has low performance PC I can interrupt his actions and he can actually see the window I'm trying to show (which is annoying). So I tried this:
[DllImport("user32.dll", SetLastError=true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
SetWindowPos(window.Handle, Bottom, 0, 0, 0, 0, NOREDRAW | NOACTIVATE | NOMOVE);
Now this seams to be ideal. I should be showing the window and I should show it on the bottom. But - it simply don't do the trick. Am I doing something wrong?
So I know this is a topic that has been touched on quite a lot on this website and this is yet another question devoted to it.
I'm trying to send keystrokes to a certain game and I believe the game to be ignoring my keystrokes. I know I've found the window because before I send the keystroke, I set the game to the foreground and that is working. I've tried SendKeys.Send, SendWait, and am currently trying the Windows API via SendMessage, and that's not working either. I'm using a method that I found via another user on SO. Here's what I'm working with:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
namespace KeySendTest2
{
class Program
{
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
public static void SendKeyStroke(IntPtr window, ushort key)
{
const uint WM_KEYDOWN = 0x100;
const uint WM_KEYUP = 0x0101;
SendMessage(window, WM_KEYDOWN, ((IntPtr)key), (IntPtr)0);
SendMessage(window, WM_KEYUP, ((IntPtr)key), (IntPtr)0);
}
static void Main(string[] args)
{
IntPtr hWnd = FindWindow("the class here", null);
SetForegroundWindow(hWnd);
System.Threading.Thread.Sleep(3000);
while (true)
{
SendKeyStroke(hWnd, (ushort)Keys.F3); // Doesn't work
//SendKeys.SendWait(Keys.F3); -- Also doesn't work
}
}
}
}
The game is a Windows application that is not a web browser. Does anyone have any idea that could point me in the right direction of why it might be ignoring the keystrokes? I know it can be done as there are several utilities built for the game that send keystrokes to the game, but they aren't open source so I have no way of seeing what method they are using.
In spite of official recommendations, many games don't use the message loop for input. Rather, they use the depreciated DirectInput API or other means.
I have had success using the AutoIT library from C# code for this purpose.
I've been using SendMessage to send mouse clicks to a couple of windows. One being a game(everything works perfectly), but the other window, being a 3rd party tool for the game is having trouble with SendMessage. If the window is 'not minimized' everything works fine, don't matter if window is completely covered up by another. But if that same window is minimized then nothing happens, I checked with spy++ and the messages are indeed getting received but not being processed (correct term?). I've tried to solve this last couple days, doing both searches on here and Google alike, many of topics but nothing helped?
//MyImports
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
And this is how I have it wrapped
public static void LClick(string windowclass, int x, int y)
{
IntPtr WHandle = FindWindow(windowclass, null);
SendMessage(WHandle, (int)WMessages.WM_LBUTTONDOWN, (IntPtr)1, (IntPtr)MakeLParam(x, y));
SendMessage(WHandle, (int)WMessages.WM_LBUTTONUP, (IntPtr)0, (IntPtr)MakeLParam(x, y));
}
I have tried focus, activate. One thing that might be useful info is that the third party program is being loaded as module("Qt5QWindowIcon") of the game.
I tried PostMessage as well, and it does the same thing as SendMessage() same problem when minimized.
This game does allow for macroing and unattended macroing, Hints the third part tool designed to execute the macros (published by the creators) I'm just trying to simulate a mouse click on the program to start the macro.
I would just use SendInput, but the entire purpose of my program is to run in background.
I 've faced same problem .Please change assembly name and project name (Changed name not started with name on that you are throwing/posting windows message)then rebuild it and check it. Now you will able to debug.
I want to build a software that when a user press Ctrl+Alt+P the monitor turn off, he can type his password and when the monitor is off, after he finish typing he press Ctrl+Alt+O and got the screen back.
I manage to do it all but the only problem I have is when the user start typing his password the computer detect user activity and turn the screen on automatically.
I use this method to turn screen off
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
public static void Main(string[] args)
{
Form f = new Form();
bool turnOff; //set true if you want to turn off, true if on
SendMessage(f.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, turnOff ? 2 : -1);
}
Anyone has an idea how can I prevent the monitor from turning on during user activity?
I don't mind which script language to use, I prefer C#.
Thanks ahead.