I've tried several solutions found, like the one ->
http://www.pcreview.co.uk/forums/console-writeline-hangs-if-user-click-into-console-window-t1412701.html
But, I observed that mode in GetConsoleMode(IntPtr hConsoleHandle, out int mode) will be different for different console app. It is not constant.
Can I disable mouse clicks (right/left buttons) on a console application to achieve the same scenario. I found that it can be done with IMessageFilter but only for Window Form Application and not for console application.
For those like me that like no-brainer code to copy/paste, here is the code inspired from the accepted answer:
using System;
using System.Runtime.InteropServices;
static class DisableConsoleQuickEdit {
const uint ENABLE_QUICK_EDIT = 0x0040;
// STD_INPUT_HANDLE (DWORD): -10 is the standard input device.
const int STD_INPUT_HANDLE = -10;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
internal static bool Go() {
IntPtr consoleHandle = GetStdHandle(STD_INPUT_HANDLE);
// get current console mode
uint consoleMode;
if (!GetConsoleMode(consoleHandle, out consoleMode)) {
// ERROR: Unable to get console mode.
return false;
}
// Clear the quick edit bit in the mode flags
consoleMode &= ~ENABLE_QUICK_EDIT;
// set the new mode
if (!SetConsoleMode(consoleHandle, consoleMode)) {
// ERROR: Unable to set console mode
return false;
}
return true;
}
}
If you want to disable quick edit mode, you need to call GetConsoleMode to get the current mode. Then clear the bit that enables quick edit, and call SetConsoleMode. Assuming you have the managed prototypes for the unmanaged functions, you would write:
const int ENABLE_QUICK_EDIT = 0x0040;
IntPtr consoleHandle = GetConsoleWindow();
UInt32 consoleMode;
// get current console mode
if (!GetConsoleMode(consoleHandle, out consoleMode))
{
// Error: Unable to get console mode.
return;
}
// Clear the quick edit bit in the mode flags
mode &= ~ENABLE_QUICK_EDIT;
// set the new mode
if (!SetConsoleMode(consoleHandle, consoleMode))
{
// ERROR: Unable to set console mode
}
If you want to disable mouse input, you want to clear the mouse input bit.
const int ENABLE_MOUSE_INPUT = 0x0010;
mode &= ~ENABLE_MOUSE_INPUT;
Having read above answers, GetConsoleWindow() can not be used. Instead GetStdHandle() must be used.
So here's a copy and paste class for enabling/disabling QuickEditMode. Call ConsoleWindow.QuickEditMode(false); to disable quick edit mode for the console window.
using System;
using System.Runtime.InteropServices;
public static class ConsoleWindow
{
private static class NativeFunctions
{
public enum StdHandle : int
{
STD_INPUT_HANDLE = -10,
STD_OUTPUT_HANDLE = -11,
STD_ERROR_HANDLE = -12,
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr GetStdHandle(int nStdHandle); //returns Handle
public enum ConsoleMode : uint
{
ENABLE_ECHO_INPUT = 0x0004,
ENABLE_EXTENDED_FLAGS = 0x0080,
ENABLE_INSERT_MODE = 0x0020,
ENABLE_LINE_INPUT = 0x0002,
ENABLE_MOUSE_INPUT = 0x0010,
ENABLE_PROCESSED_INPUT = 0x0001,
ENABLE_QUICK_EDIT_MODE = 0x0040,
ENABLE_WINDOW_INPUT = 0x0008,
ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200,
//screen buffer handle
ENABLE_PROCESSED_OUTPUT = 0x0001,
ENABLE_WRAP_AT_EOL_OUTPUT = 0x0002,
ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004,
DISABLE_NEWLINE_AUTO_RETURN = 0x0008,
ENABLE_LVB_GRID_WORLDWIDE = 0x0010
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
}
public static void QuickEditMode(bool Enable)
{
//QuickEdit lets the user select text in the console window with the mouse, to copy to the windows clipboard.
//But selecting text stops the console process (e.g. unzipping). This may not be always wanted.
IntPtr consoleHandle = NativeFunctions.GetStdHandle((int)NativeFunctions.StdHandle.STD_INPUT_HANDLE);
UInt32 consoleMode;
NativeFunctions.GetConsoleMode(consoleHandle, out consoleMode);
if (Enable)
consoleMode |= ((uint)NativeFunctions.ConsoleMode.ENABLE_QUICK_EDIT_MODE);
else
consoleMode &= ~((uint)NativeFunctions.ConsoleMode.ENABLE_QUICK_EDIT_MODE);
consoleMode |= ((uint)NativeFunctions.ConsoleMode.ENABLE_EXTENDED_FLAGS);
NativeFunctions.SetConsoleMode(consoleHandle, consoleMode);
}
}
For those who use vb.net
Const ENABLE_QUICK_EDIT As UInteger = &H40
Const STD_INPUT_HANDLE As Integer = -10
<DllImport("kernel32.dll", SetLastError:=True)>
Public Function GetStdHandle(ByVal nStdHandle As Integer) As IntPtr
End Function
<DllImport("kernel32.dll")>
Private Function GetConsoleMode(ByVal hConsoleHandle As IntPtr, <Out> ByRef lpMode As UInteger) As Boolean
End Function
<DllImport("kernel32.dll")>
Private Function SetConsoleMode(ByVal hConsoleHandle As IntPtr, ByVal dwMode As UInteger) As Boolean
End Function
Friend Function Go() As Boolean
Dim consoleHandle As IntPtr = GetStdHandle(STD_INPUT_HANDLE)
Dim consoleMode As UInteger
If Not GetConsoleMode(consoleHandle, consoleMode) Then
Return False
End If
consoleMode = consoleMode And Not ENABLE_QUICK_EDIT
If Not SetConsoleMode(consoleHandle, consoleMode) Then
Return False
End If
Return True
End Function
sub main()
go()
end sub
By used combination of codes on below, I'm be able to enable or disable the Quick Edit mode.
const int ENABLE_QUICK_EDIT = 0x0040;
// STD_INPUT_HANDLE (DWORD): -10 is the standard input device.
const int STD_INPUT_HANDLE = -10;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out int lpMode);
[DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, int dwMode);
To enable, simple do currentConsoleMode &= ENABLE_QUICK_EDIT;
And to disable, do
currentConsoleMode &= ~ENABLE_QUICK_EDIT
And then call SetConsoleMode.
I just happened to stumble over the same problem with quick edit mode enabled in my console application, which is written in C, and has been working under windows 7 32 bit for ages. After porting (well not really porting, but adapting some code lines) it to windows 10 64 Bit (still being a 32 bit application), I observed the same behaviour.
So I searched for a solution.
But for a reason unknown to me, the code works the other way round, i.e setting the bit ENABLE_QUICK_EDIT_MODE in the mode parameter actually disables quick edit mode. And resetting the bit enables the quick edit mode...???
Here is my code:
/// <summary>
/// This flag enables the user to use the mouse to select and edit text. To enable
/// this option, you must also set the ExtendedFlags flag.
/// </summary>
const int QuickEditMode = 64;
// ExtendedFlags must be combined with
// InsertMode and QuickEditMode when setting
/// <summary>
/// ExtendedFlags must be enabled in order to enable InsertMode or QuickEditMode.
/// </summary>
const int ExtendedFlags = 128;
BOOLEAN EnableQuickEdit()
{
HWND conHandle = GetStdHandle(STD_INPUT_HANDLE);
int mode;
DWORD dwLastError = GetLastError();
if (!GetConsoleMode(conHandle, &mode))
{
// error getting the console mode. Exit.
dwLastError = GetLastError();
return (dwLastError == 0);
}
else
dwLastError = 0;
mode = mode & ~QuickEditMode;
if (!SetConsoleMode(conHandle, mode | ExtendedFlags))
{
// error setting console mode.
dwLastError = GetLastError();
}
else
dwLastError = 0;
return (dwLastError == 0);
}
BOOLEAN DisableQuickEdit()
{
HWND conHandle = GetStdHandle(STD_INPUT_HANDLE);
int mode;
DWORD dwLastError = GetLastError();
if (!GetConsoleMode(conHandle, &mode))
{
// error getting the console mode. Exit.
dwLastError = GetLastError();
return (dwLastError == 0);
}
else
dwLastError = 0;
mode = mode | QuickEditMode;
if (!SetConsoleMode(conHandle, mode))
{
// error getting the console mode. Exit.
dwLastError = GetLastError();
}
else
dwLastError = 0;
return (dwLastError == 0);
}
Greetings Wolfgang
Related
I've already checked out:
SetWindowPos not working on Form.Show()
Launch an application and send it to second monitor?
However, none of these solutions seem to work for me. I want to open an external program on a different monitor.
This is my current code:
public const int SWP_NOSIZE = 0x0001;
public const int SWP_NOZORDER = 0x0004;
public const int SWP_SHOWWINDOW = 0x0040;
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
[DllImport("user32.dll")]
public static extern bool UpdateWindow(IntPtr hWnd);
Process application = new Process();
application.StartInfo.UseShellExecute = false;
application.StartInfo.FileName = ".......";
if (application.Start())
{
Rectangle monitor = Screen.AllScreens[1].Bounds; // for monitor no 2
SetWindowPos(
application.MainWindowHandle,
IntPtr.Zero,
monitor.Left,
monitor.Top,
monitor.Width,
monitor.Height,
SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
UpdateWindow(application.MainWindowHandle); // tried even with application.Handle
}
First of all, you don't need UpdateWindow, calling SetWindowPos is probably enough. You just need to make sure that the window handle is created (because the process is being started). Simply add the following line before calling SetWindowPos:
application.WaitForInputIdle();
If WaitForInputIdle() doesn't work for you, you might try something like:
while (application.MainWindowHandle == IntPtr.Zero)
{
await Task.Delay(100);
}
The following code works fine for me:
Process application = new Process();
application.StartInfo.UseShellExecute = false;
application.StartInfo.FileName = "notepad.exe";
if (application.Start())
{
application.WaitForInputIdle();
/* Optional
while (application.MainWindowHandle == IntPtr.Zero)
{
await Task.Delay(100);
} */
Rectangle monitor = Screen.AllScreens[1].Bounds; // for monitor no 2
SetWindowPos(
application.MainWindowHandle,
IntPtr.Zero,
monitor.Left,
monitor.Top,
monitor.Width,
monitor.Height,
SWP_NOZORDER | SWP_NOSIZE | SWP_SHOWWINDOW);
}
Note that this will only set the position of the window, not its size though. If you want the size to be changed as well, you'll need to remove the SWP_NOSIZE flag.
I'm trying to embed the osk in a wpf window or a user control and I've found the code below and it's working for notepad but for tabtip.exe, it's saying that it doesn't have a graphical interface??
WaitForInputIdle failed. This could be because the process does not have a graphical interface.
I tried letting it sleep for awhile instead of calling waitForInputIdle method but it throws another exception:
Process has exited, so the requested information is not available.
But in my task manager, I can still see TabTip.exe running.
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private System.Windows.Forms.Panel _panel;
private Process _process;
public MainWindow()
{
InitializeComponent();
_panel = new System.Windows.Forms.Panel();
windowsFormsHost1.Child = _panel;
}
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32")]
private static extern IntPtr SetParent(IntPtr hWnd, IntPtr hWndParent);
[DllImport("user32")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
private const int SWP_NOZORDER = 0x0004;
private const int SWP_NOACTIVATE = 0x0010;
private const int GWL_STYLE = -16;
private const int WS_CAPTION = 0x00C00000;
private const int WS_THICKFRAME = 0x00040000;
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
if (_process != null)
{
_process.Refresh();
_process.Close();
}
}
private void ResizeEmbeddedApp()
{
if (_process == null)
return;
SetWindowPos(_process.MainWindowHandle, IntPtr.Zero, 0, 0, (int)_panel.ClientSize.Width, (int)_panel.ClientSize.Height, SWP_NOZORDER | SWP_NOACTIVATE);
}
protected override Size MeasureOverride(Size availableSize)
{
Size size = base.MeasureOverride(availableSize);
ResizeEmbeddedApp();
return size;
}
private void button1_Click_1(object sender, RoutedEventArgs e)
{
button1.Visibility = Visibility.Hidden;
ProcessStartInfo psi = new ProcessStartInfo("C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe");
_process = Process.Start(psi);
Thread.Sleep(500);
//_process.WaitForInputIdle();
SetParent(_process.MainWindowHandle, _panel.Handle);
// remove control box
int style = GetWindowLong(_process.MainWindowHandle, GWL_STYLE);
style = style & ~WS_CAPTION & ~WS_THICKFRAME;
SetWindowLong(_process.MainWindowHandle, GWL_STYLE, style);
// resize embedded application & refresh
ResizeEmbeddedApp();
}
}
}
Edit: Inspired by rene's comment, I've tried to obtain the window ptr as below and used spy++ to verify that the address that FindWindow gives is pointing to the correct window, but it's still not moving:
IntPtr KeyboardWnd = FindWindow("IPTip_Main_Window", null);
int style = GetWindowLong(KeyboardWnd, GWL_STYLE);
style = style & ~WS_CAPTION & ~WS_THICKFRAME;
SetWindowLong(KeyboardWnd, GWL_STYLE, style);
SetWindowPos(KeyboardWnd, IntPtr.Zero, 0, 0, (int)_panel.ClientSize.Width, (int)_panel.ClientSize.Height, SWP_NOZORDER | SWP_NOACTIVATE);
Edit 2: My first thought was that tab tip couldn't be resized, but then I noticed a behavior when I try to move the window across two different screen, it'll resize to fit the screen size, so I'm sure there must be a way to resize, so I started spy++(x64) to check :
Edit 3: after tinkering abit with user32 api and no progress, I've tried to use a memory scanner to scan for the x and y position of tabtip and change it, however, it's not refreshing until a repaint is triggered, I'm wondering the feasibility going down that path.
Can you try to run your handle code in STA thread? I had a similar issue with native window, which I had resolved using STA thread.
var thread = new Thread(() => {
// Your code here
});
thread.TrySetApartmentState(ApartmentState.STA);
thread.Start();
I had a similar problem, and the reason I had it was that I started a program that needed to be run by an administrator with a non-administrative program, and it would pop up with WaitForInputIdle failed. This could be because the process does not have a graphical interface, so I assume you try starting your program with an administrator
I've created some global Hot keys for my application and I want them to work only if my application is active. (It should not work if my application is not the active form).
So how can I check if my C# winform application is the active form among all the other windows applications?
I tried
if(this.Focused)
//Do somthing
But it's not working
Try this:
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
public static bool Activates()
{
var x = GetForegroundWindow();
if (x == IntPtr.Zero) {
return false;
}
var y = Process.GetCurrentProcess().Id;
int i;
GetWindowThreadProcessId(x, out i);
return i == y;
}
You can also refer: C#: Detecting which application has focus
You can use Windows API function GetForegroundWindow and GetWindowText.
GetForegroundWindow :
The GetForegroundWindow function returns a handle to the window with which the user is currently working.
GetWindowText:
The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer.
Add below code to declare API functions :
[ DllImport("user32.dll") ]
static extern int GetForegroundWindow();
[ DllImport("user32.dll") ]
static extern int GetWindowText(int hWnd, StringBuilder text, int count);
Start a timer :
private void timer1_Tick(object sender, System.EventArgs e)
{
GetActiveWindow();
}
Active window function :
private void GetActiveWindow()
{
const int nChars = 256;
int handle = 0;
StringBuilder Buff = new StringBuilder(nChars);
handle = GetForegroundWindow();
if ( GetWindowText(handle, Buff, nChars) > 0 )
{
this.captionWindowLabel.Text = Buff.ToString();
this.IDWindowLabel.Text = handle.ToString();
}
}
I basically have code that starts the keyboard, but it opens in alphanumeric portion and the box for editing is a NumericUpDown with numbers. So, I want to open tabtip.exe aka the onscreen keyboard in windows 8.1 with the numerpad focused. Here is my current code for opening tabtip, but again it does not open with numpad by default:
using System.Runtime.InteropServices; //added for keyboard closure
using System.Windows.Interop; //Keyboard closure - must add reference for WindowsBase
//Added for keyboard closure
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(int hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(String sClassName, String sAppName);
//open keyboard
void openKeyboard()
{
ProcessStartInfo startInfo = new ProcessStartInfo(#"C:\Program Files\Common Files\Microsoft Shared\ink\TabTip.exe");
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(startInfo);
}
//close keyboard
void closeKeyboard()
{
uint WM_SYSCOMMAND = 274;
uint SC_CLOSE = 61536;
IntPtr KeyboardWnd = FindWindow("IPTip_Main_Window", null);
PostMessage(KeyboardWnd.ToInt32(), WM_SYSCOMMAND, (int)SC_CLOSE, 0);
}
There's also seems to be some registry edit you can do, but I can't seem to get it to make the numpad portion of the taptip keyboard in windows 8.1 to show:
Windows 8 Desktop App: Open tabtip.exe to secondary keyboard (for numeric textbox)
Currently with Windows 8.1, not much functionality seems to be programmatically exposed.
The code below will cause the tabtip.exe to read the registry because the original process is killed.
It is not completely reliable, but is a way to for it to respond to some registry values.
The part about docking is optional, it forces it to dock each time via the registry change.
The process.Kill(); should be in a try/catch since it occasionally doesn't have permission and can throw an exception.
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string sClassName, string sAppName);
[DllImport("user32.dll")]
public static extern IntPtr PostMessage(int hWnd, uint msg, int wParam, int lParam);
private static void KillTabTip()
{
// Kill the previous process so the registry change will take effect.
var processlist = Process.GetProcesses();
foreach (var process in processlist.Where(process => process.ProcessName == "TabTip"))
{
process.Kill();
break;
}
}
public void ShowTouchKeyboard(bool isVisible, bool numericKeyboard)
{
if (isVisible)
{
const string keyName = "HKEY_CURRENT_USER\\Software\\Microsoft\\TabletTip\\1.7";
var regValue = (int) Registry.GetValue(keyName, "KeyboardLayoutPreference", 0);
var regShowNumericKeyboard = regValue == 1;
// Note: Remove this if do not want to control docked state.
var dockedRegValue = (int) Registry.GetValue(keyName, "EdgeTargetDockedState", 1);
var restoreDockedState = dockedRegValue == 0;
if (numericKeyboard && regShowNumericKeyboard == false)
{
// Set the registry so it will show the number pad via the thumb keyboard.
Registry.SetValue(keyName, "KeyboardLayoutPreference", 1, RegistryValueKind.DWord);
// Kill the previous process so the registry change will take effect.
KillTabTip();
}
else if (numericKeyboard == false && regShowNumericKeyboard)
{
// Set the registry so it will NOT show the number pad via the thumb keyboard.
Registry.SetValue(keyName, "KeyboardLayoutPreference", 0, RegistryValueKind.DWord);
// Kill the previous process so the registry change will take effect.
KillTabTip();
}
// Note: Remove this if do not want to control docked state.
if (restoreDockedState)
{
// Set the registry so it will show as docked at the bottom rather than floating.
Registry.SetValue(keyName, "EdgeTargetDockedState", 1, RegistryValueKind.DWord);
// Kill the previous process so the registry change will take effect.
KillTabTip();
}
Process.Start("c:\\Program Files\\Common Files\\Microsoft Shared\\ink\\TabTip.exe");
}
else
{
var win8Version = new Version(6, 2, 9200, 0);
if (Environment.OSVersion.Version >= win8Version)
{
const uint wmSyscommand = 274;
const uint scClose = 61536;
var keyboardWnd = FindWindow("IPTip_Main_Window", null);
PostMessage(keyboardWnd.ToInt32(), wmSyscommand, (int)scClose, 0);
}
}
}
You can call the above method from a custom version of a TextBox where OnTouchDown is overridden and an additional DependencyProperty is created to indicate if the field uses the NumericKeyboard:
#region NumericKeyboard
public static readonly DependencyProperty NumericKeyboardProperty = DependencyProperty.Register("NumericKeyboard", typeof(bool), typeof(CustomTextBox), new FrameworkPropertyMetadata(false));
/// <summary> Returns/set the "NumericKeyboard" state of the CustomTextBox. </summary>
public bool NumericKeyboard
{
get { return (bool)GetValue(NumericKeyboardProperty); }
set { SetValue(NumericKeyboardProperty, value); }
}
#endregion
protected override void OnTouchDown(TouchEventArgs e)
{
base.OnTouchDown(e);
Focus();
if (IsReadOnly == false)
ShowTouchKeyboard(true, NumericKeyboard);
}
Currently, I have not had any success using similar techniques to position the TabTip window around the screen when in a floating (non-docked) state.
I read many answers on how to send command to cmd prompt.
But I do not want to use StreamWriter or similar stuff to input and get output.
I want to use SendMessage to send my string or say command to the cmd prompt window.
Can anyone please help on this?
Just to give detail about my application.
1. My application is a WinForm App.
2. It has 4-5 buttons.
3. Button1 opens the command prompt window while Button5 closes or exits the command prompt window.
4. Button 2,3,4 are command buttons. When user clicks the Button2 command 1 is send to command prompt window. Similar when button 3 and 4 are clicked command 2 and command 3 are sent to the same command prompt window.
Let me know if anybody has a written code which sends string to command prompt.
Thanks and regards,
Rahul
I have got the solution.
I used PostMessage().
Cmd has handle and you can send string to it.
I was just struggling to find the right way to get the handle.
Now I got one.
/*
* Created by SharpDevelop.
* User: Rahul
* Date: 5/12/2011
* Time: 1:49 AM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace GetChildWindows
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
IntPtr hWnd = FindWindow(null, "Untitled - Notepad");
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(IntPtr hWnd, [MarshalAs(UnmanagedType.U4)] uint Msg, int wParam, int lParam);
const int WM_KEYDOWN = 0x0100;
const int WM_KEYUP = 0x0101;
const int WM_CHAR = 0x0102;
public static IntPtr cmdHwnd = IntPtr.Zero;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
foreach (IntPtr child in GetChildWindows(FindWindow(null, "WinPlusConsole")))
{
StringBuilder sb = new StringBuilder(100);
GetClassName(child, sb, sb.Capacity);
if (sb.ToString() == "ConsoleWindowClass")
{
// uint wparam = 0 << 29 | 0;
// string msg = "Hello";
// int i = 0;
// for (i = 0 ; i < msg.Length ; i++)
// {
// //PostMessage(child, WM_KEYDOWN, (IntPtr)Keys.Enter, (IntPtr)wparam);
// PostMessage(child, WM_CHAR, (int)msg[i], 0);
// }
// PostMessage(child, WM_KEYDOWN, (IntPtr)Keys.Enter, (IntPtr)wparam);
cmdHwnd = child;
}
}
}
/// <summary>
/// Returns a list of child windows
/// </summary>
/// <param name="parent">Parent of the windows to return</param>
/// <returns>List of child windows</returns>
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
/// <summary>
/// Callback method to be used when enumerating windows.
/// </summary>
/// <param name="handle">Handle of the next window</param>
/// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param>
/// <returns>True to continue the enumeration, false to bail</returns>
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
{
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
}
list.Add(handle);
// You can modify this to check to see if you want to cancel the operation, then return a null here
return true;
}
/// <summary>
/// Delegate for the EnumChildWindows method
/// </summary>
/// <param name="hWnd">Window handle</param>
/// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param>
/// <returns>True to continue enumerating, false to bail.</returns>
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
void BtnHelloClick(object sender, EventArgs e)
{
uint wparam = 0 << 29 | 0;
string msg = "Hello";
int i = 0;
for (i = 0 ; i < msg.Length ; i++)
{
//PostMessage(child, WM_KEYDOWN, (IntPtr)Keys.Enter, (IntPtr)wparam);
PostMessage(cmdHwnd, WM_CHAR, (int)msg[i], 0);
}
PostMessage(cmdHwnd, WM_KEYDOWN, (IntPtr)Keys.Enter, (IntPtr)wparam);
}
void BtnCommandClick(object sender, EventArgs e)
{
uint wparam = 0 << 29 | 0;
string msg = textBox1.Text;
int i = 0;
for (i = 0 ; i < msg.Length ; i++)
{
//PostMessage(child, WM_KEYDOWN, (IntPtr)Keys.Enter, (IntPtr)wparam);
PostMessage(cmdHwnd, WM_CHAR, (int)msg[i], 0);
}
PostMessage(cmdHwnd, WM_KEYDOWN, (IntPtr)Keys.Enter, (IntPtr)wparam);
}
void TextBox1TextChanged(object sender, EventArgs e)
{
}
}
}
Thanks to all!
It seems unlikely that you would be able to achieve this with SendMessage(). SendMessage() requires a window handle and I don't believe that cmd.exe has a suitable window handle to receive the message.
My advice is to look for a method that solves your problem rather than deciding what solution you want and trying to make it fit the problem.
its not clear from your explenation (at least not to me) why you cant use streamWriter in the scenario you describe. can you elaborate on that?
i'd check out the Process class for talking to other programs via stdin/out