C# How can I dock an application? - c#

I wrote an application in C# that brings another application to the foreground.
This function so far. However, there is a problem.
If you set the focus, the application is opened and brought to the foreground, but the Windows docking function does not work. The application is only ever opened in its last window size.
EDIT:
I forgot to mention that the docking should be seen on the Windows desktop (monitor edge).
Does anyone have any idea how to get the docking feature on?
[DllImport("../user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("../user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public void SetWindowToForeground(List<string> formattedPartList)
{
string windowTitle = formattedPartList[4];
int activeResize = 5; //
IntPtr mainWindowHandle = GetProcessIdByName(windowTitle);
SetForegroundWindow(mainWindowHandle);
ShowWindow(mainWindowHandle, activeResize);
}

Related

A way to stop Console from exiting by user or program [duplicate]

This question already has answers here:
Is there any way to prevent console application to close?
(2 answers)
Prevent the application from exiting when the Console is closed
(3 answers)
How to prevent app from closing before finishing a task?
(1 answer)
Closed 2 years ago.
I have searched far and wide and still found nothing.
What I'm trying to accomplish is preventing/stopping the console from exiting/terminating, for example clicking X on the console or having a different program closing it (I know for a fact that it is not possible to bypass Task Managers "Kill Task").
What I have been using is the following:
private delegate bool ConsoleCtrlHandlerDelegate(int sig);
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlHandlerDelegate handler, bool add);
static ConsoleCtrlHandlerDelegate _consoleCtrlHandler;
//...
_consoleCtrlHandler += s => {/* I have a while true loop right here with some extra code doing stuffs*/};
SetConsoleCtrlHandler(_consoleCtrlHandler, true);
//...
That does work...for about 5 seconds, then closes on it's own.
Please help.
Also, DO NOT SAY CTRL+F5, as it will not accomplish my goal. My goal is beyond debugging tools.
If you want an application to be working non-stop, you should run this as a Windows service rather than a console application.
With a little research, you can convert your application to a Windows Service and set appropriate user rights for starting and stopping the service.
You can't stop someone from killing a task, if they have the admin rights to kill your task. The best you can do is to create a user with admin privileges on the machine, then run the application under that user. That will prevent any task, other than a task with admin privileges from killing your app.
Now, as far as disabling the close button on your console app, you can use the Win32 DeleteMenu API to disable the X button. Here is an example:
public const int ScClose = 0xF060;
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
static void Main(string[] args)
{
// Get a pointer to the console window
IntPtr consoleWindow = GetConsoleWindow();
// Get a pointer to the system menu inside the console window
IntPtr systemMenu = GetSystemMenu(consoleWindow, false);
// Delete the close menu item
DeleteMenu(systemMenu, ScClose, 0);
}

How Can My Win32 App Steal Focus From My UWP App?

I've tried the following code:
[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr SetFocus(IntPtr hwnd);
void TakeFocus()
{
var process = Process.GetProcessesByName("myProcess").FirstOrDefault();
if (process != null)
{
// Tried each of the following:
ShowWindow(process.MainWindowHandle, 1);
ShowWindow(process.MainWindowHandle, 3);
ShowWindow(process.MainWindowHandle, 9);
ShowWindow(process.MainWindowHandle, 5);
SetFocus(process.MainWindowHandle);
SetForegroundWindow(process.MainWindowHandle);
}
}
I have a WPF companion app which runs in the background while a UWP app is running in the foreground. They communicate via WebSocket. I'm trying to create a method in the WPF app so it (or any other window) can steal focus from an activated UWP app, sending it into suspended state. Nothing I try seems to work, and there's no way to programatically make a UWP app suspend itself AFAIK without using the Launcher class (not an option for me, unless there's a way to call it without actually launching something-I haven't been able to do this). Normally I would assume it can't be done but I've seen programs that do it. Steam Big Picture Mode, for example, will steal focus from a UWP app when it is launched from a background process.
The supported way of suspending a UWP programmatically is available in the Spring 2018 update for Windows 10. It's already available in Insider builds/SDKs. This is the API to call:
https://learn.microsoft.com/en-us/uwp/api/windows.system.appresourcegroupinfo.startsuspendasync#Windows_System_AppResourceGroupInfo_StartSuspendAsync
IList<AppDiagnosticInfo> infos = await AppDiagnosticInfo.RequestInfoForAppAsync();
IList<AppResourceGroupInfo> resourceInfos = infos[0].GetResourceGroups();
await resourceInfos[0].StartSuspendAsync();
Here is a trivial sample app:
https://1drv.ms/u/s!AovTwKUMywTNoYQ3PrmBfZIGXmbULA

Show popup in background wpf

The task is this: I need to be able to show popup from my application but doing so should not interact with user typing something in skype, playing games, etc. The window of the application should me opened if it's minimized.
I have the instance of my Main Window. This instance contains code that will show the popup. Thing is I can't use something like this:
window.ShowActivated = false;
simply because this seams to work only if the window is first shown (which is not - the application is already running when I need to call the popup).
Now the other approach that works is using win api.
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
var currentForegroundWindow = GetActiveWindow();
window.Show();
SetForegroundWindow(currentForegroundWindow);
What are my concerns - if user is playing game or has low performance PC I can interrupt his actions and he can actually see the window I'm trying to show (which is annoying). So I tried this:
[DllImport("user32.dll", SetLastError=true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags);
SetWindowPos(window.Handle, Bottom, 0, 0, 0, 0, NOREDRAW | NOACTIVATE | NOMOVE);
Now this seams to be ideal. I should be showing the window and I should show it on the bottom. But - it simply don't do the trick. Am I doing something wrong?

Ensure single instance of a WPF application : Difficulty restoring the already running application to the foreground

Intention : Run a single instance of a WPF application. When a new instance, is started the already running instance should be set to the foreground.
While I have achieved most of it, I am facing a problem when the already running application is sitting in the notification tray. The code runs without an error, but fails to restore the window & set it to foreground. Code Snippet (c#):
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int cmdShow);
var currentProcess = Process.GetCurrentProcess();
string processName = currentProcess.ProcessName;
Process[] instances = Process.GetProcessesByName(processName);
if (instances.Length > 1)
{
foreach(var instance in instances)
{
if (!currentProcess.Id.Equals(instance.Id))
{
IntPtr hWnd = instance.MainWindowHandle;
if (IsIconic(hWnd))
ShowWindow(hWnd, SW_RESTORE);
SetForegroundWindow(hWnd);
}
}
currentProcess.Kill();
}
Can any one point out what am I doing wrong. To reiterate again, it works in the case when the already running window is in maximized state but in the hindsight. It fails when the already running window is minimized to the notification tray.
Thanks
Have you verified, that your code executes the ShowWindow when the already running process is in the systray? I am asking, because I don't think that IsIconic is the correct function to use: The documentation states that it "determines whether the specified window is minimized". If the process is in the systray it isn't minimized, it is hidden.
I think you should use IsWindowVisible instead.

Problems with CloseMainWindow() to close a Windows Explorer window

I´m facing a problem when trying to close a Windows Explorer (not Internet Explorer) window through another application, using the "Process.CloseMainWindow()" method; because it doesn´t close the Explorer window, it tries to close the full Windows (Operative System), by the way, Windows XP.
The code is as follows:
[DllImport("user32.dll")]
static extern int GetForegroundWindow();
[DllImport("user32.dll")]
private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);
public String[] exeCommand()
{
try
{
//Get App
Int32 hwnd = 0;
hwnd = GetForegroundWindow();
Process actualProcess = Process.GetProcessById(GetWindowProcessID(hwnd));
//Close App
if (!actualProcess.CloseMainWindow())
actualProcess.Kill();
}
catch { throw; }
return null;
}
Suppose that the "actualProcess" is "explorer.exe"
Any help will be appreciated!!
Salutes!
I believe this is because the main window for explore is considered the shell. You can however kill the process, but windows will start it right back up.

Categories