c#怎么使用PostMessage函数模拟按键输入啊
[DllImport("user32.dll")]staticexternboolPostMessage(inthwnd,intmsg,uintwParam,uintlP...
[DllImport("user32.dll")]
static extern bool PostMessage(int hwnd, int msg, uint wParam, uint lParam);
private void button10_Click(object sender, EventArgs e)
{
PostMessage(this.hwnd, 72, 0, 0);
}
找到一个记事本程序的句柄,然后发一个按键消息 72是按H键, 可是不管用....这怎么回事啊... 展开
static extern bool PostMessage(int hwnd, int msg, uint wParam, uint lParam);
private void button10_Click(object sender, EventArgs e)
{
PostMessage(this.hwnd, 72, 0, 0);
}
找到一个记事本程序的句柄,然后发一个按键消息 72是按H键, 可是不管用....这怎么回事啊... 展开
展开全部
DllImport("user32.dll")]
static extern bool PostMessage(int hwnd, int msg, uint wParam, uint lParam);
参数说明:int hwnd, int msg, uint wParam, uint lParam
第一参数是记事本的窗口句柄,这点必须要确认
第二个参数是消息windows消息,用WM_CHAR试试,在C#中需要定义WM_CHAR或者直接填WM_CHAR的值0x0102
第三个参数填H的键码
第四个参数是特征码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace TankLibrary
{
public class Win32API
{
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("Gdi32.dll")]
public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
[DllImport("user32.dll")]
public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
const int MOUSEEVENTF_MOVE = 0x0001; //移动鼠标
public const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下
public const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下
const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键按下
const int MOUSEEVENTF_MIDDLEUP = 0x0040; //模拟鼠标中键抬起
const int MOUSEEVENTF_ABSOLUTE = 0x8000; //标示是否采用绝对坐标
public const int HWND_TOPMOST = -1;
public const int SWP_SHOWWINDOW = 40;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern int SetWindowPos(int hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rectangle rect);
[DllImport("user32.dll")]
public static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool SetForegroundWindow(IntPtr hwnd);
[DllImport("user32.dll", EntryPoint = "PostMessage")]
public static extern IntPtr PostMessage1(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int x, int y);
public static void ClickOn(IntPtr hwnd, int x, int y)
{
uint WM_LBUTTONDOWN = 0x0201;
uint WM_LBUTTONUP = 0x0202;
PostMessage1(hwnd, WM_LBUTTONDOWN, x, y);
PostMessage1(hwnd, WM_LBUTTONUP, x, y);
}
}
}
static extern bool PostMessage(int hwnd, int msg, uint wParam, uint lParam);
参数说明:int hwnd, int msg, uint wParam, uint lParam
第一参数是记事本的窗口句柄,这点必须要确认
第二个参数是消息windows消息,用WM_CHAR试试,在C#中需要定义WM_CHAR或者直接填WM_CHAR的值0x0102
第三个参数填H的键码
第四个参数是特征码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
namespace TankLibrary
{
public class Win32API
{
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("Gdi32.dll")]
public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
[DllImport("user32.dll")]
public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
const int MOUSEEVENTF_MOVE = 0x0001; //移动鼠标
public const int MOUSEEVENTF_LEFTDOWN = 0x0002; //模拟鼠标左键按下
public const int MOUSEEVENTF_LEFTUP = 0x0004; //模拟鼠标左键抬起
const int MOUSEEVENTF_RIGHTDOWN = 0x0008; //模拟鼠标右键按下
const int MOUSEEVENTF_RIGHTUP = 0x0010; //模拟鼠标右键抬起
const int MOUSEEVENTF_MIDDLEDOWN = 0x0020; //模拟鼠标中键按下
const int MOUSEEVENTF_MIDDLEUP = 0x0040; //模拟鼠标中键抬起
const int MOUSEEVENTF_ABSOLUTE = 0x8000; //标示是否采用绝对坐标
public const int HWND_TOPMOST = -1;
public const int SWP_SHOWWINDOW = 40;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern int SetWindowPos(int hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rectangle rect);
[DllImport("user32.dll")]
public static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern bool SetForegroundWindow(IntPtr hwnd);
[DllImport("user32.dll", EntryPoint = "PostMessage")]
public static extern IntPtr PostMessage1(IntPtr hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern bool SetCursorPos(int x, int y);
public static void ClickOn(IntPtr hwnd, int x, int y)
{
uint WM_LBUTTONDOWN = 0x0201;
uint WM_LBUTTONUP = 0x0202;
PostMessage1(hwnd, WM_LBUTTONDOWN, x, y);
PostMessage1(hwnd, WM_LBUTTONUP, x, y);
}
}
}
展开全部
函数使用出错,PostMessage第二个参数是被递送的消息类型,你应该传入一个键盘消息类型,而不是传入一个扫描码,扫描码应该在第三个参数传入。
PostMessage(this.hwnd, WM_KEYDOWN, 72, 0);
另外,PostMessage()只将消息放入窗体消息队列,但它并不等待窗体处理消息就返回,所以你要确定你的程序是否及时处理了你发送的消息,可以用Application.DoEvents();来让程序处理消息队列中的所有消息。
PostMessage(this.hwnd, WM_KEYDOWN, 72, 0);
另外,PostMessage()只将消息放入窗体消息队列,但它并不等待窗体处理消息就返回,所以你要确定你的程序是否及时处理了你发送的消息,可以用Application.DoEvents();来让程序处理消息队列中的所有消息。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐于2017-12-16
展开全部
int hwnd, int msg, uint wParam, uint lParam
第一参数是记事本的窗口句柄,这点必须要确认
第二个参数是消息windows消息,用WM_CHAR试试,在C#中需要定义WM_CHAR或者直接填WM_CHAR的值0x0102
第三个参数填H的键码
第四个参数是特征码
第一参数是记事本的窗口句柄,这点必须要确认
第二个参数是消息windows消息,用WM_CHAR试试,在C#中需要定义WM_CHAR或者直接填WM_CHAR的值0x0102
第三个参数填H的键码
第四个参数是特征码
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
比如:
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true,CharSet = CharSet.Auto)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true,CharSet = CharSet.Auto)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个还真没用过。再等等其他童鞋吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询