본문 바로가기

C#

C# WinAPI - PostMessage char, string

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
private static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);

private enum WMessages : uint
{
	WM_MOUSEMOVE = 0x200,
	WM_LBUTTONDOWN = 0x201, //Left mousebutton down
	WM_LBUTTONUP = 0x202,  //Left mousebutton up
	WM_LBUTTONDBLCLK = 0x203, //Left mousebutton doubleclick
	WM_RBUTTONDOWN = 0x204, //Right mousebutton down
	WM_RBUTTONUP = 0x205,   //Right mousebutton up
	WM_RBUTTONDBLCLK = 0x206, //Right mousebutton doubleclick
	WM_KEYDOWN = 0x100,  //Key down
	WM_KEYUP = 0x101,   //Key up
	WM_SYSKEYDOWN = 0x104,
	WM_SYSKEYUP = 0x105,
	WM_CHAR = 0x102,     //Char
	WM_COMMAND = 0x111
}

private void SendChar(IntPtr handle, char key)
{
	PostMessage(handle, (uint)WMessages.WM_CHAR, key, IntPtr.Zero);
}

private void SendString(IntPtr handle, string str)
{
	foreach (char i in str)
	{
		SendChar(handle, i);
	}
}