请教使用postmessage发送键盘消息时,参数如果设置
展开全部
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);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询