using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
namespace L2
{
class Program
{
static void Main(string[] args)
{
var fileName = "l2.bin";
if (!File.Exists(fileName))
{
MessageBox(IntPtr.Zero, $"File {fileName} not found!", "Warning!", MB_ICONWARNING);
return;
}
try
{
var process = new Process
{
StartInfo = new ProcessStartInfo(fileName)
{
UseShellExecute = false
}
};
process.Start();
var splashWindowHandler = IntPtr.Zero;
// Waiting for splash window handler
do
{
process.Refresh();
splashWindowHandler = process.MainWindowHandle;
}
while (splashWindowHandler == IntPtr.Zero);
var mainWindowHandler = IntPtr.Zero;
// Waiting for main window handler
do
{
process.Refresh();
if (splashWindowHandler != process.MainWindowHandle)
{
mainWindowHandler = process.MainWindowHandle;
}
}
while (mainWindowHandler == IntPtr.Zero);
if (!GetWindowRect(mainWindowHandler, out var mainWindowRect))
{
MessageBox(IntPtr.Zero, "Error: 0x" + GetLastError().ToString("X"), "GetWindowRect(Main Window)", MB_ICONWARNING));
return;
}
var windowWidth = mainWindowRect.right - mainWindowRect.left;
var windowHeight = mainWindowRect.bottom - mainWindowRect.top;
GetWindowRect(GetDesktopWindow(), out var desktopWindowRect);
var screenWidth = desktopWindowRect.right - desktopWindowRect.left;
var screenHeight = desktopWindowRect.bottom - desktopWindowRect.top;
var isFullscreen = windowWidth == screenWidth && windowHeight == screenHeight;
if (!isFullscreen)
{
// TODO: SetWindowPos???
var moveWindowResult = MoveWindow(
mainWindowHandler,
GetSystemMetrics(SM_CXSCREEN) / 2 - windowWidth / 2,
GetSystemMetrics(SM_CYSCREEN) / 2 - windowHeight / 2,
windowWidth,
windowHeight,
true);
if (!moveWindowResult)
{
MessageBox(IntPtr.Zero, "Error: 0x" + GetLastError().ToString("X"), "MoveWindow", MB_ICONWARNING));
}
}
}
catch (Exception e)
{
MessageBox(IntPtr.Zero, e.ToString(), e.Message, MB_ICONWARNING));
}
}
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics
public static readonly int SM_CXSCREEN = 0;
public static readonly int SM_CYSCREEN = 1;
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox
public static readonly long MB_ICONWARNING = 0x00000030L;
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messagebox
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, long uType);
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowrect
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdesktopwindow
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDesktopWindow();
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmetrics
[DllImport("user32.dll")]
public static extern int GetSystemMetrics(int nIndex);
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-movewindow
[DllImport("user32.dll", SetLastError = true)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("kernel32.dll")]
public static extern uint GetLastError();
// https://learn.microsoft.com/en-us/windows/win32/api/windef/ns-windef-rect
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
}
}