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);
SendMessage(handle, 0x0100, 0x11, 0);
SendMessage(handle, 0x0100, 0x53, 0);
SendMessage(handle, 0x0101, 0x53, 0);
SendMessage(handle, 0x0101, 0x11, 0);
}
( ), 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);
IntPtr CTRL_KEY = new IntPtr(0x11);
uint KEY_DOWN = 0x0100;
uint KEY_UP = 0x0101;
IntPtr S_KEY = new IntPtr(0x53);
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) {
ShowWindow(mainWindowHandle, 1);
SetForegroundWindow(mainWindowHandle);
uint intReturn;
INPUT structInput;
structInput = new INPUT();
structInput.type = 1;
structInput.ki.wScan = 0x1D;
structInput.ki.time = 0;
structInput.ki.dwFlags = 0;
structInput.ki.wVk = 0x11;
intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT()));
structInput.ki.wScan = 0x1F;
structInput.ki.wVk = 0x53;
intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT()));
structInput.ki.dwFlags = 0x0002;
intReturn = SendInput(1, ref structInput, Marshal.SizeOf(typeof(INPUT)));
structInput.ki.wVk = 0x11;
intReturn = SendInput(1, ref structInput, Marshal.SizeOf(new INPUT()));
}
CTRL + S , .
- , "FLASH" ( )?
5: WM_SYSCOMMAND
IntPtr handle = GetWindow(mainWindowHandle, 5);
PostMessage(handle, 0x0112, 0xF100, 'f');
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 , .
-