Sending CTRL-S message to window

I want to save a TextPad window using C # code; I can recognize the window handle, but I don’t know how to send CTRL-S to this window. I want to use the P / Invoke API for this. In addition, this TextPad window will be inactive because my application will be active at this time.

[DllImport("user32.dll")]
private static extern int SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);

Send Ctrl + To Window

I reviewed this discussion, which is very similar to my problem. I logically understand that I need to do 4 sending messages, as shown below

  • KeyDown CTRL key
  • Key KeyDown S
  • Key KeyUp S
  • KeyUp CTRL key

I am not sure how to send the correct parameters to SendMessage. To close the window, I use

SendMessage(hWnd, 0x0010, 0, 0);

I got this from the MSDN library.

- , , ?

- 1
spy ++, CTRL-S ""

1. WM_KEYDOWN nVirtKey:VK_Control, 
2. WM_KEYDOWN nVirtKey:'S' .. some other messates ..
3. WM_KEYUP nVirtKey:VK_Control. 
4. WM_KEYUP nVirtKey:'S'. 

- 2


       private IntPtr startnotepad() {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = @"notepad.exe";
            String fileName = baseDirectory + textbox1.Text;
            psi.Arguments = @"""" + fileName + @"""";
            psi.WindowStyle = ProcessWindowStyle.Minimized;
            Process p = Process.Start(psi); 
            return p.MainWindowHandle;
        }

        private void SaveNotepad(IntPtr handle)
        {
            IntPtr handle = GetWindow(handle, 5); // get the keyboard focus handle
            // verified the handle with Spy ++.
            SendMessage(handle, 0x0100, 0x11, 0); // keydown ctrl
            SendMessage(handle, 0x0100, 0x53, 0); // keydown S
            //SendMessage(handle, 0x0101, 0x11, 0); --- I tried keeping "Keyup Ctrl" here as well.
            SendMessage(handle, 0x0101, 0x53, 0); // keyup s
            SendMessage(handle, 0x0101, 0x11, 0); // keyup ctrl            
        }

( ), WM_KEYDOWN - CTRL.. WM_KEYDOWN - 'S'.. KEYUP 'S' KEYUP CTRL spy ++. - , ? , - .

3

PostMessage SendMessage @Hans, .


 [DllImport("user32.dll")]
 public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);  

        private void Save(IntPtr mainWindowHandle)
        {               
            IntPtr handle = GetWindow(mainWindowHandle, 5); // getChild window
            IntPtr CTRL_KEY = new IntPtr(0x11);
            uint KEY_DOWN = 0x0100;
            uint KEY_UP = 0x0101;
            IntPtr S_KEY = new IntPtr(0x53);

            //SetForegroundWindow(p.MainWindowHandle);
            PostMessage(handle, KEY_DOWN, CTRL_KEY, IntPtr.Zero);
            PostMessage(handle, KEY_DOWN, S_KEY, IntPtr.Zero);
            PostMessage(handle, KEY_UP, S_KEY, IntPtr.Zero);
            PostMessage(handle, KEY_UP, CTRL_KEY, IntPtr.Zero);                      
        } 

CTRL + S CTRL S "" . , "S", "" . , CTRL + S, , Postmessage . - ( KEY UP CTRL "C01F0001" )

, postmessage?


// does not work
PostMessage(handle, KEY_UP, CTRL_KEY, new IntPtr(0xC01F0001);

4: , " " . .

 
SendInput: , . , .


 private void Save_Notepad(IntPtr mainWindowHandle) {
            //SetActiveWindow(mainWindowHandle);
            //SetFocus(GetWindow(mainWindowHandle, 5));
            ShowWindow(mainWindowHandle, 1); // show window 
            SetForegroundWindow(mainWindowHandle);
            //SetActiveWindow(mainWindowHandle);
            //SetFocus(GetWindow(mainWindowHandle, 5));
            //IntPtr returnvalue = SetFocus(mainWindowHandle);
            uint intReturn;
            INPUT structInput;
            structInput = new INPUT();
            structInput.type = 1;// keyboard input
            // key down
            structInput.ki.wScan = 0x1D;
            structInput.ki.time = 0;
            structInput.ki.dwFlags = 0;
            // key down Ctrl
            structInput.ki.wVk = 0x11;  //0x1D; //
            intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT()));
            // key down S
            structInput.ki.wScan = 0x1F;
            structInput.ki.wVk = 0x53; //0x41;//0x53;  //0x1F;//
            intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT()));
            // key up 
            structInput.ki.dwFlags = 0x0002; // key up
            // key up S
            intReturn = SendInput(1, ref structInput, Marshal.SizeOf(typeof(INPUT)));
            // key up CTRL
            structInput.ki.wVk = 0x11;  //0x1D; //
            intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT()));
            //ShowWindow(mainWindowHandle, 2); // minimize it again 
        }

CTRL + S , . - , "FLASH" ( )?

5: WM_SYSCOMMAND


IntPtr handle = GetWindow(mainWindowHandle, 5);
// send Alt F message to Notepad.
// http://msdn.microsoft.com/en-us/library/ms646360(v=VS.85).aspx
// I can see this is working.
PostMessage(handle, 0x0112, 0xF100, 'f');  // send WM_SYSCOMMAND
// how to send s on this window ??
// I have tried things which I have learned so far. 

S- , WM_SYSCOMMAND. - , ?


PostMessage(handle, KEY_DOWN, S_KEY, 0x1F0001); // POST does not work
// tried with mainwindowhandle as well
PostMessage(handle, 0x111, 'S', 0); // WM_COMMAND does not work
PostMessage(handle, 0x0112, 0xF100, 's');  // WM_SYSCOMMAND does not work (and does not make sence either)

^ S . sendkeys (^ s) 2-3 , .

-

+3
2

SendMessage ( PostMessage, ) , CTRL. SendInput().

, Ctrl + S, WM_COMMAND. Spy ++, , , Ctrl + S . WM_COMMAND, , SendMessage() . , , .

+4

, , Ctrl + S - , , , .

, Alt-F, 'S' :

PostMessage(handle, 0x0112, 0xF100, 0x0046);
PostMessage(handle, 0x0102, 0x0053, 0);

Alt-F , "S" - .

- WM_COMMAND WM_MENUSELECT, MF_MOUSESELECT:

IntPtr menu = GetMenu(handle);
IntPtr subMenu = GetSubMenu(menu, 0);//0 = first menu item
uint menuItemID = GetMenuItemID(subMenu, 2);//2 = second item in submenu
SendMessage(handle, 0x0111, 0x20000000 + menuItemID, menu);

- WM_COMMAND menuItemID:

PostMessage(handle, 0x0111, 0x0003, 0x0);

0x0003 ( ) , , WM_COMMAND spy ++ , .

, WM_SYSKEYUP/DOWN WM_MENUCOMMAND, , Ctrl-C, Ctrl-V , , ..

.

+3

Source: https://habr.com/ru/post/1795267/


All Articles