Last active
December 24, 2025 22:56
-
-
Save sunmeat/3238988daaa65dd331cb2c0aa12db319 to your computer and use it in GitHub Desktop.
PostMessage example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Diagnostics; | |
| using System.Runtime.InteropServices; | |
| class Program | |
| { | |
| [DllImport("user32.dll", CharSet = CharSet.Auto)] | |
| public static extern IntPtr FindWindow(string className, string? windowName); | |
| [DllImport("user32.dll", CharSet = CharSet.Auto)] | |
| public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string? lpszWindow); | |
| [DllImport("user32.dll", CharSet = CharSet.Auto)] | |
| public static extern bool SetForegroundWindow(IntPtr hWnd); | |
| [DllImport("user32.dll", CharSet = CharSet.Auto)] | |
| public static extern uint PostMessage(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam); | |
| const uint WM_CHAR = 0x0102; | |
| static void Main() | |
| { | |
| string notepadPath = @"C:\\Program Files\\Notepad++\\notepad++.exe"; | |
| if (!File.Exists(notepadPath)) | |
| { | |
| Console.WriteLine("Notepad++ не знайдений!"); | |
| return; | |
| } | |
| Process.Start(notepadPath); | |
| Thread.Sleep(2000); | |
| IntPtr hwndNotepad = IntPtr.Zero; | |
| while (hwndNotepad == IntPtr.Zero) | |
| { | |
| hwndNotepad = FindWindow("Notepad++", null); | |
| Thread.Sleep(500); | |
| } | |
| if (hwndNotepad == IntPtr.Zero) | |
| { | |
| Console.WriteLine("Не знайдено вікно Notepad++."); | |
| return; | |
| } | |
| IntPtr hwndScintilla = IntPtr.Zero; | |
| while (hwndScintilla == IntPtr.Zero) | |
| { | |
| hwndScintilla = FindWindowEx(hwndNotepad, IntPtr.Zero, "Scintilla", null); | |
| Thread.Sleep(500); | |
| } | |
| if (hwndScintilla == IntPtr.Zero) | |
| { | |
| Console.WriteLine("Не знайдено текстове поле в Notepad++."); | |
| return; | |
| } | |
| SetForegroundWindow(hwndNotepad); | |
| Thread.Sleep(500); | |
| string textToSend = "Доброго вечора! Як справи?"; | |
| foreach (char c in textToSend) | |
| { | |
| PostMessage(hwndScintilla, WM_CHAR, c, IntPtr.Zero); | |
| Thread.Sleep(50); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment