I have a filled image on my form in front of everything but most of it is transparant so i'm trying to click through it. I have this:
public class clickThrough
{
[DllImport("user32.dll", EntryPoint = "SetWindowLongW")]
private static extern IntPtr SetWindowLongPtr32(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW")]
private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
private delegate IntPtr WndProcDelegate(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam);
private static WndProcDelegate SetWindowProc(IntPtr hWnd, WndProcDelegate newWndProc)
{
IntPtr newWndProcPtr = Marshal.GetFunctionPointerForDelegate(newWndProc);
IntPtr oldWndProcPtr;
if (IntPtr.Size == 4)
oldWndProcPtr = SetWindowLongPtr32(hWnd, -4, newWndProcPtr);
else
oldWndProcPtr = SetWindowLongPtr64(hWnd, -4, newWndProcPtr);
return (WndProcDelegate)Marshal.GetDelegateForFunctionPointer(oldWndProcPtr, typeof(WndProcDelegate));
}
}
private void guna2PictureBox1_Click(object sender, EventArgs e)
{
int initialStyle = SetWindowLongPtrW(this.Handle, -20);
SetWindowLongPtrW(this.Handle, -20, initialStyle | 0x80000 | 0x20);
}
I get this error: The name 'SetWindowLongPtrW' does not exist in the current context
Related
I have a Handle for a ListBox from an external application.
Now I have a Listbox with x Items which I want to select with the WinApi.
I tried this with SETCURSEL but its unfortunatly doesnt work:
private void button2_Click(object sender, EventArgs e)
{
IntPtr chldWnd = NativeMethods.FindWindow("#32770", "Ansichten einfügen");
IntPtr ListBoxHandle = NativeMethods.FindWindowEx(chldWnd, IntPtr.Zero, "ListBox", null);
//MessageBox.Show(ButtonHandle.ToString());
NativeMethods.SendMessageInt(ListBoxHandle, NativeMethods.CB_SETCURSEL, 1, 2);
}
static class NativeMethods
{
public const int BM_CLICK = 0x00F5;
public const int WM_SETTEXT = 0x000C;
public const int VK_DOWN = 0x28;
public const int WM_KEYDOWN = 0x100;
public const int LB_SETSEL = 0x0185;
public const int CB_SETCURSEL = 0x014E;
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);
[DllImport("user32.dll", EntryPoint="PostMessage" ,CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessageInt(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr PostMessage(IntPtr hwnd, int wsg, IntPtr wParam, String lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
I believe that the way I use it will meet your need to select an item with a mouse click on the ListBox, Please, try it.
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
public void SetListItem(string windowTitle, int index, int item)
{
try
{
var windowHWnd = FindWindowByCaption(IntPtr.Zero, windowTitle);
var childWindows = GetChildWindows(windowHWnd);
const int LB_SETCURSEL = 0x0186;
const int downCode = 0x201;
const int upCode = 0x202;
IntPtr lParam = (IntPtr)9999; // The coordinates
IntPtr wParam = IntPtr.Zero;
SendMessage(childWindows.ToArray()[index], LB_SETCURSEL, item, "0");
SendMessage(childWindows.ToArray()[index], downCode, wParam, lParam); // Mouse button down
SendMessage(childWindows.ToArray()[index], upCode, wParam, lParam);
}
catch (Exception)
{
throw;
}
}
I've been experimenting and searching all day, and can't for my life figure out how to do this.
As the title reads, I want to be able to send mouse clicks that are ignored by something like GetAsyncKeyState().
Basically what I'm doing:
//While physically holding left mouse button...
while (GetAsyncKeyState(0x01) != 0)
{
//left mouse button virtually down (obviously already is down the first loop)
mouse_event(2, 0, 0, 0, 0);
Thread.Sleep(100);
//left mouse button virtually up
mouse_event(4, 0, 0, 0, 0);
}
Now, the while-loop stops because I virtually lift the button, so what I'm asking for is an alternative to mouse_event/GetAsyncKeyState (or some parameter I don't know about), so I can manipulate key states without that affecting the actual state.
For example, I've been able to do this in AutoHotkey using Send {LButton up} and GetKeyState("LButton", "P").
Any ideas?
MSLLHOOKSTRUCT contains LLMHF_INJECTED and LLMHF_LOWER_IL_INJECTED flags. Maybe you should take a look at it.
The code below is just an empty form with a timer, configured to be "On" and call "TimerOnTick" every seconds.
When you click, the output is "497, 361, 0" when the timer click the output is "497, 361, 1"
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace Syracuse
{
public partial class Form1 : Form
{
private const int WH_MOUSE_LL = 14;
private static LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_hookID = SetHook(_proc);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
UnhookWindowsHookEx(_hookID);
}
private void TimerOnTick(object sender, EventArgs e)
{
//left mouse button virtually down (obviously already is down the first loop)
mouse_event(2, 0, 0, 0, UIntPtr.Zero);
Thread.Sleep(100);
//left mouse button virtually up
mouse_event(4, 0, 0, 0, UIntPtr.Zero);
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages) wParam)
{
var hookStruct = (MSLLHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof (MSLLHOOKSTRUCT));
Console.WriteLine(hookStruct.pt.x + ", " + hookStruct.pt.y + ", " + hookStruct.flags);
}
// Todo create OnMouseUpExEvent, OnMouseDownExEvent witch provide MSLLHOOKSTRUCT informations.
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private static IntPtr SetHook(LowLevelMouseProc proc)
{
using (var curProcess = Process.GetCurrentProcess())
using (var curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
#region Enum & Struct
private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205
}
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
#endregion
#region Extern
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, UIntPtr dwExtraInfo);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
#endregion
}
}
I get this error when trying to implement a windows hook using SetWindowsHookEx and CallWndProc. I'm wondering if i implemented the hook correctly. Here is the code: This code will work as a keyboard hook if its replaced with LowLevelKeyboardProc
[DllImport("user32.dll")]
static extern IntPtr SetWindowsHookEx(int idHook, CallWndProc callback, IntPtr hInstance, uint threadId);
[DllImport("user32.dll")]
static extern bool UnhookWindowsHookEx(IntPtr hInstance);
[DllImport("user32.dll")]
static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, int wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);
private delegate IntPtr CallWndProc(int nCode, IntPtr wParam, IntPtr lParam);
const int WH_CALLWNDPROC = 4;
const int WM_PASTE = 0x302;
private CallWndProc _proc = hookProc;
private static IntPtr hhook = IntPtr.Zero;
public void SetHook()
{
IntPtr hInstance = LoadLibrary("User32");
hhook = SetWindowsHookEx(WH_CALLWNDPROC, _proc, hInstance, 0);
}
public static void UnHook()
{
UnhookWindowsHookEx(hhook);
}
public static IntPtr hookProc(int code, IntPtr wParam, IntPtr lParam)
{
if (code >= 0 && wParam == (IntPtr)WM_PASTE)
{
MessageBox.Show("Paste");
return (IntPtr)1;
}
else
return CallNextHookEx(hhook, code, (int)wParam, lParam);
}
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
UnHook();
}
private void Form1_Load(object sender, EventArgs e)
{
SetHook();
}
The module handle passed to SetWindowsHookEx should be the handle for your dll, not "user32".
I need to know when the enter key is pressed on a specific textbox in another application. I'm able to find the textbox and write/read text from it using the user32.dll. But how do I get one specific key?
The application is just a chat. The goal is to when I type on this application textbox something like '/time' and hit enter I want my application to read this command and output the current time, for exemple.
Here is the code I have to retrieve the handle of the application and its textfield child and writing/reading it.
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, IntPtr wParam, string lParam);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int Param, System.Text.StringBuilder text);
[DllImport("user32.dll")]
private static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);
[DllImport("user32.Dll")]
public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);
[DllImport("user32.dll")]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);
private IntPtr handle = IntPtr.Zero;
private string childClassName = "TComboEdit";
public bool findChildHandle(IntPtr hwnd, IntPtr lParam)
{
StringBuilder className = new StringBuilder();
GetClassName(hwnd, className, 120);
if (className.ToString() == childClassName)
{
handle = hwnd;
return false;
}
return true;
}
private void findControl(string className, string title)
{
IntPtr application = IntPtr.Zero;
application = FindWindow(className, title);
if (application == IntPtr.Zero)
MessageBox.Show("Aplicativo não encontrado");
else
EnumChildWindows(application, findChildHandle, IntPtr.Zero);
}
private void setTextToHandle(string msg)
{
SendMessage(handle, 0x000c, IntPtr.Zero, msg); //set text
PostMessage(handle, 0x0100, new IntPtr(0x0D), IntPtr.Zero); // key down (enter)
}
private void getTextFromHandle()
{
StringBuilder t = new StringBuilder();
SendMessage(handle, 0x0D, 100, t); //get text
MessageBox.Show(t.ToString());
}
I'm writing an application in c# and I need to know when the for the ground window has changed
I used SetWindowsHookEx but I don't get the call back when I switch between windows
my code:
private const int WH_CALLWNDPROC = 4;
private delegate IntPtr windowName(int nCode, IntPtr wParam, IntPtr lParam);
private static windowName _name = HookCallback;
private static IntPtr _hook = IntPtr.Zero;
public static void start()
{
_hook = SetHook(_name);
Application.Run();
UnhookWindowsHookEx(_hook);
}
private static IntPtr SetHook(windowName proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_CALLWNDPROC, proc, GetModuleHandle(curModule.ModuleName), 0);
}
}
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
browser = GetActiveWindow();
Console.WriteLine(browser);
return CallNextHookEx(_hook, nCode, wParam, lParam);
}
ok i have an answer
public static void start()
{
WinEventDelegate dele = new WinEventDelegate(WinEventProc);
IntPtr m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, dele, 0, 0, WINEVENT_OUTOFCONTEXT);
string window = GetActiveWindowTitle();
Console.WriteLine(window);
while (true)
{
if (window != GetActiveWindowTitle())
{
window = GetActiveWindowTitle();
Console.WriteLine(window);
}
}
}
delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
private static string GetActiveWindowTitle()
{
const int nChars = 256;
IntPtr handle = IntPtr.Zero;
StringBuilder Buff = new StringBuilder(nChars);
handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
public static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
Console.WriteLine(GetActiveWindowTitle());
}
#region imports
[DllImport("user32.dll")]
static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
private const uint WINEVENT_OUTOFCONTEXT = 0;
private const uint EVENT_SYSTEM_FOREGROUND = 3;
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
#endregion
its a bit messy but it works