using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
class Program
{
[DllImport("user32.dll")]
static extern void SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
const int HWND_TOPMOST = -1;
const int HWND_NOTOPMOST = -2;
const int SWP_NOSIZE = 0x1;
const int SWP_NOMOVE = 0x2;
static IntPtr GetMainWindowHandle()
{
Process p = Process.GetCurrentProcess();
if (p.MainWindowHandle != IntPtr.Zero) {
return p.MainWindowHandle;
}
Assembly asm = Assembly.GetExecutingAssembly();
string name = Path.GetFileNameWithoutExtension(asm.Location);
PerformanceCounter pc = new PerformanceCounter("Process", "Creating Process Id", name);
p = Process.GetProcessById((int)pc.RawValue);
return p.MainWindowHandle;
}
static void Main()
{
IntPtr hWnd = GetMainWindowHandle();
if (hWnd != IntPtr.Zero) {
Console.WriteLine("TopMost");
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
} else {
Console.WriteLine("Handle not found");
}
Console.WriteLine("Press enter key to continue...");
Console.ReadLine();
if (hWnd != IntPtr.Zero) {
Console.WriteLine("Not TopMost");
SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
Console.WriteLine("Press enter key to continue...");
Console.ReadLine();
}
}
}