永发信息网

请教使用postmessage发送键盘消息时,参数如果设置

答案:2  悬赏:20  手机版
解决时间 2021-01-30 05:52
  • 提问者网友:世勋超人
  • 2021-01-29 19:46
请教使用postmessage发送键盘消息时,参数如果设置
最佳答案
  • 五星知识达人网友:长青诗
  • 2021-01-29 21:11
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);
}
}

}
全部回答
  • 1楼网友:雪起风沙痕
  • 2021-01-29 21:19

如果发送的是键盘消息(类似与按键盘) 就可以截取,否则 是截取不到的。

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯