Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nosferatuapostle committed Jan 7, 2025
0 parents commit 7618261
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
bin/
obj/
Properties/

.git/
.vs/
.vscode/

BorderlessApp.csproj
BorderlessApp.csproj.user
151 changes: 151 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using System.Runtime.InteropServices;
using System.Text;

class Program
{
const int GWL_STYLE = -16;
const uint WS_CAPTION = 0x00C00000;
const uint WS_THICKFRAME = 0x00040000;
const uint SWP_FRAMECHANGED = 0x0020;

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lp_class_name, string lp_window_name);

[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowLong(IntPtr h_wnd, int n_index);

[DllImport("user32.dll", SetLastError = true)]
static extern uint SetWindowLong(IntPtr h_wnd, int n_index, uint dw_new_long);

[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr h_wnd, IntPtr h_wnd_insert_after, int x, int y, int cx, int cy, uint u_flags);

[DllImport("user32.dll", SetLastError = true)]
static extern bool EnumWindows(EnumWindowsProc lp_enum_func, IntPtr l_param);

[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowText(IntPtr h_wnd, StringBuilder lp_string, int n_max_count);

[DllImport("user32.dll", SetLastError = true)]
static extern bool IsWindowVisible(IntPtr h_wnd);

[DllImport("user32.dll", SetLastError = true)]
static extern int GetSystemMetrics(int n_index);

delegate bool EnumWindowsProc(IntPtr h_wnd, IntPtr l_param);

static void Main()
{
bool app_found = false;
while (!app_found)
{
Console.Clear();
Console.Write("App Name (partial): ");
string partial_name = Console.ReadLine();

var windows = FindWindowsByPartialName(partial_name);

if (windows.Count == 0)
{
Console.WriteLine("App not found. Please try again.");
continue;
}

int current_selection = 0;
bool is_running = true;

while (is_running)
{
Console.SetCursorPosition(0, 0);
Console.ResetColor();
Console.WriteLine("Select an app (use arrow keys to navigate and Enter to select):");

for (int i = 0; i < windows.Count; i++)
{
if (i == current_selection)
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
}
else
{
Console.ResetColor();
}

Console.WriteLine(windows[i].Title);
}

ConsoleKeyInfo key = Console.ReadKey(intercept: true);

if (key.Key == ConsoleKey.UpArrow)
{
current_selection = (current_selection - 1 + windows.Count) % windows.Count;
}
else if (key.Key == ConsoleKey.DownArrow)
{
current_selection = (current_selection + 1) % windows.Count;
}
else if (key.Key == ConsoleKey.Enter)
{
IntPtr h_wnd = windows[current_selection].Handle;

SetBorderless(h_wnd);

Console.ResetColor();
Console.Clear();
Console.WriteLine("App is running in borderless mode. Press any key to exit.");

Console.ReadKey();
RestoreWindow(h_wnd);
is_running = false;
}
}

app_found = true;
}
}

static void SetBorderless(IntPtr h_wnd)
{
uint styles = GetWindowLong(h_wnd, GWL_STYLE);
styles &= ~WS_CAPTION & ~WS_THICKFRAME;
SetWindowLong(h_wnd, GWL_STYLE, styles);

int screen_width = GetSystemMetrics(0);
int screen_height = GetSystemMetrics(1);

SetWindowPos(h_wnd, IntPtr.Zero, 0, 0, screen_width, screen_height, SWP_FRAMECHANGED);
Console.WriteLine("Set to fullscreen borderless");
}

static void RestoreWindow(IntPtr h_wnd)
{
uint styles = GetWindowLong(h_wnd, GWL_STYLE);
styles |= WS_CAPTION | WS_THICKFRAME;
SetWindowLong(h_wnd, GWL_STYLE, styles);

SetWindowPos(h_wnd, IntPtr.Zero, 0, 0, 1280, 720, SWP_FRAMECHANGED);
Console.WriteLine("Restored to windowed mode");
}

static List<(IntPtr Handle, string Title)> FindWindowsByPartialName(string partial_name)
{
var windows = new List<(IntPtr Handle, string Title)>();

EnumWindows((h_wnd, l_param) =>
{
if (!IsWindowVisible(h_wnd))
return true;

var title = new StringBuilder(256);
GetWindowText(h_wnd, title, title.Capacity);

if (title.ToString().Contains(partial_name, StringComparison.OrdinalIgnoreCase))
windows.Add((h_wnd, title.ToString()));

return true;
}, IntPtr.Zero);

return windows;
}
}
Binary file added README.md
Binary file not shown.

0 comments on commit 7618261

Please sign in to comment.