How to distinguish between a program’s GUI and Console window? - c#

I have written a code in c# that gets processes by name, activates the window of the desired process and sends some hotkeys to this process. The problem is that the desired process has two windows, a console window and a GUI window, as a result sometimes the GUI window is activated and everything works fine, but sometimes the console window is activated and nothing happens. So my question is, is there a way to distinguish between that process’ GUI and console window, so by program won’t get confused and only activate the GUI window ?
(By activating I mean bringing this process’ window to the foreground in order for the sending hotkeys to work. I have no access to the code of the desired process so I cannot change the way it starts up).

Related

SetForegroundWindow if child process has focus

I've developed an application that starts external processes (e.g. like the notepad etc.) and displays the main window of the process inside a panel in my application. (Like described in this thread)
The problem is that I get a lot of focus problems with that. E.g. when a window is over my application's window and I click on the window of the sub process, the main window does not come to the foreground.
I've tried to solve this problem by calling SetForegroundWindow from the main application when the child process receives focus, but as this and this thread describes this only works if the process is being debugged or if it is the foreground process. There is a workaround by calling AttachThreadInput, but that doesn't work 100% reliable.
SetForegroundWindow should also work if "The process was started by the foreground process", but in my case it is the other way arround. (The foreground process was started by the process)
Is there a way to get the right to set the foreground window if the focussed window is a child process of my process?
SetForegroundWindow sets the thread that created the specified window into the foreground.
BringWindowToTop brings the specified window to the top of the Z order.

C# Console Application Keypress capturing

Is it possible to check pressed keys in a Console Application in any other way than Console.ReadKey()?
I've changed the project settings to Windows Application so that the console window won't show. But if the console window isn't shown the application crashes since Console.ReadKey() requires a console window. Is there any workaround?
NOTE:
I don't want any window open, just track a key-press, through a thread.
You cannot receive key pressed event if you do not have a window. You hae to have a console or "desktop" window.
In case of console 'Console.ReadKey()' does the job, in case of WPF or Windows Forms 'KeyPressed' event does the job.

Starting application from background process

I'm working a small WPF based program for launching applications through system wide hotkeys implemented using hooking. I'm implementing it in C# and Visual Studio 2010.
When I detect the specific keypress I use Process.Start(...) to run the application. This works fine while Visual Studio is active, placing the new application in the foreground with input focus, as I would expect. If my launcher is in the background (behind another active program), it still detects the key and starts the application correctly, in front of everything else.
The problem is, that when I run the launcher without Visual Studio active, and my launcher application isn't front, neither will applications it starts. They appear in front of the launcher but behind the active application.
I can see that other software, like AutoHotkey, is able to do hotkey launching with this behavior, but I fail to see what I'm doing wrong.
Update: Just figured out a solution to this issue that works in my development environment. I first register a global hotkey through the Windows API RegisterHotKey using the launcher main window handle. The key could be any, but should be one that normally doesn't exist physically, F24 in my case. Then, whenever I detect a keypress through the hooks that should launch an app, I first call keybd_event to 'fake' a keypress for the hotkey.
For WPF use:
keybd_event((byte)KeyInterop.VirtualKeyFromKey(Key.F24),0,KEYEVENTF_KEYUP,0);
For WinForms use:
keybd_event((byte)Keys.F24,0,KEYEVENTF_KEYUP,0);
This will bring enough focus to the launcher, so that Process.Start(...) makes the executed program get in front. It does not bring the launcher window to front, nor does it make the launcher accept inputs.
If Activate() is called on the main window after the keybd_event(), this will bring the main window to front and allow for keyboard input, just as if the user had task switched.

Intercept Opening Window - C#

Is there some way to watch for / intercept a window opening in Windows and then to block said window from opening? This would be for an application that's already running and I wouldn't want to stop the process, just close the popup window it spawns.
Update:
A process is already running (e.g. it's in Task Manager) but has no visible windows.
At some random time, that process will popup a window
I manually close the window (click "OK").
The process continues to run at this point and will, again, popup a window a bit later (repeating these steps).
I want to automate step #3, where I have to manually close the popup, by intercepting that window opening and closing it or hiding it or never letting it open in the first place. I don't want to do this by polling the open windows. I want to receive some event that a window is about to open.
I do not control this other application, so I can't otherwise change it. And I don't want to kill the process, itself.
If you're trying to do what I asked (allow only one instance of a program to run at a time), here is an elegant solution using a Mutex. You can probably copy and paste most of that code to achieve what you want.

how to close console window after form is closed

i have a console application project. it has gui (winforms) and console window which is used as debug console (application's run log).
now, if i have got debug disabled i hide console window with ShowWindow native call.
the problem is: after i close application window by pressin the X button or by quitting it (Application.Exit()) i have a zombie console process.
if i close console on the very begining by Environment.Exit(0) the form is not been showed.
i have tried to modify forms Closed event by adding there Environment.Exit(0), but that did not work which means that there still remains a zombie console process.
how can i close console window on application exit?
If it is a console application, the console will not "close" until the process terminates. So you just simply need to close the form normally... assuming you "opened" it like you should.
To "open" the form, you need to run it. Run it by calling Application.Run(Form) (the same way you would initialize an actual form project). Since a console is allocated to your process, your form may write to Console.Out and you should see what you write.

Categories

Resources