Skip to content

razibit/AlwaysOnTop

Repository files navigation

Always On Top Manager for Windows 11

A lightweight Windows 11 desktop utility that allows users to set multiple application windows as "Always on Top" with customizable priority rankings.

Features

  • βœ“ Multi-Window Selection: Select multiple windows simultaneously to apply always-on-top behavior
  • βœ“ Priority Ranking System: Assign explicit priority numbers (1 = highest) to control window stacking order
  • βœ“ Dynamic Z-Order Management: Automatically maintains window order even when windows gain/lose focus
  • βœ“ Real-time Monitoring: Background service ensures windows stay in correct order (500ms polling)
  • βœ“ Clean UI: Modern WPF interface with visual feedback
  • βœ“ Low Resource Usage: Minimal CPU and memory footprint
  • βœ“ Portable Executable: Single self-contained .exe file

Architecture

Core Components

1. WindowsApi.cs - Win32 API Wrapper

  • Primary APIs Used:

    • EnumWindows - Enumerate all top-level windows
    • SetWindowPos - Modify window Z-order and topmost flag
    • GetWindowLong/SetWindowLong - Read/modify window styles (WS_EX_TOPMOST)
    • IsWindowVisible - Filter visible windows
    • DwmGetWindowAttribute - Detect cloaked windows (virtual desktops)
  • Key Constants:

    • HWND_TOPMOST (-1) - Places window above all non-topmost windows
    • HWND_NOTOPMOST (-2) - Removes topmost status
    • WS_EX_TOPMOST (0x0008) - Extended window style flag for topmost
    • SWP_NOACTIVATE (0x0010) - Prevents window activation when repositioning

2. WindowManager.cs - Priority Enforcement Engine

  • Maintains a list of managed windows with priority assignments
  • Background timer (500ms interval) monitors and corrects Z-order violations
  • Priority algorithm:
    1. Sort managed windows by priority (ascending)
    2. Apply WS_EX_TOPMOST flag to all managed windows
    3. Stack windows from lowest to highest priority using SetWindowPos
    4. Re-order if any window loses topmost status

3. WindowEnumerator.cs - Window Discovery

  • Filters out:
    • Invisible windows
    • Desktop/shell windows
    • Windows without titles
    • Cloaked windows (different virtual desktops)
    • The manager's own window
    • System background processes

4. WPF UI - User Interface

  • Modern Material Design-inspired interface
  • ListView with checkboxes for multi-select
  • Editable priority text boxes (numeric only)
  • Real-time window count and selection statistics
  • Color-coded buttons with hover effects

Priority Stacking Logic

Algorithm Overview

Priority 1 (Highest) β†’ Window A
Priority 2           β†’ Window B  
Priority 3 (Lowest)  β†’ Window C

Z-Order (top to bottom):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Window A   β”‚ ← Priority 1 (Always on top)
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Window B   β”‚ ← Priority 2 (Below A, above C)
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Window C   β”‚ ← Priority 3 (Below B)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Enforcement Strategy

  1. Initial Setup: When "Apply Always On Top" is clicked:

    • Set WS_EX_TOPMOST flag on all selected windows
    • Position windows using SetWindowPos with HWND_TOPMOST
  2. Continuous Monitoring: Timer checks every 500ms:

    • Verify all managed windows still have WS_EX_TOPMOST flag
    • If any window lost topmost status β†’ re-run full enforcement
    • Remove destroyed windows from managed list
  3. Z-Order Correction:

    // Step 1: Ensure topmost flag
    foreach (window in sortedWindows)
        SetWindowTopmost(window, true)
    
    // Step 2: Stack from bottom to top
    for (i = lowest_priority; i >= highest_priority; i--)
        SetWindowPos(window[i], HWND_TOPMOST, ...)
    
    // Step 3: Final ordering pass
    foreach (window in priority_order)
        SetWindowPos(window, HWND_TOP, ...)

Edge Cases Handled

1. Fullscreen Applications

  • Issue: Fullscreen apps may force themselves to the top
  • Solution: Background monitor detects and re-applies topmost status
  • Note: Some games with exclusive fullscreen may override temporarily

2. Minimized Windows

  • Issue: Minimized windows can't be "on top"
  • Detection: IsIconic() API check
  • Behavior: Topmost flag remains applied, takes effect when restored

3. Focus Changes

  • Issue: When user clicks a non-managed window, it may come to front
  • Solution: Managed windows remain topmost due to WS_EX_TOPMOST flag
  • Result: Normal windows always stay behind topmost windows

4. Virtual Desktops

  • Issue: Windows on other virtual desktops appear in EnumWindows
  • Solution: Use DwmGetWindowAttribute(DWMWA_CLOAKED) to detect and filter
  • Result: Only shows windows on current virtual desktop

5. Duplicate Priorities

  • Issue: Multiple windows with same priority number
  • Solution: UI warns user; relative order between duplicates is undefined
  • Recommendation: Assign unique priorities

6. Window Closure

  • Issue: Managed window is closed by user
  • Detection: IsWindow() returns false
  • Action: Automatically removed from managed list in next monitor cycle

7. Application Crashes

  • Issue: Managed window's process terminates unexpectedly
  • Detection: Window handle becomes invalid
  • Action: Gracefully removed from management

Building the Executable

Prerequisites

  • .NET 8.0 SDK or later (Download)
  • Windows 10/11 (64-bit)
  • Visual Studio 2022 (optional, for development)

Compilation Steps

Option 1: Command Line (Recommended)

# Navigate to project directory
cd G:\always-on-top

# Restore dependencies
dotnet restore

# Build Release configuration (single-file exe)
dotnet publish -c Release -r win-x64 --self-contained true -p:PublishSingleFile=true

# Output location:
# bin\Release\net8.0-windows\win-x64\publish\AlwaysOnTop.exe

Option 2: Visual Studio

  1. Open AlwaysOnTop.csproj in Visual Studio 2022
  2. Set build configuration to Release
  3. Right-click project β†’ Publish
  4. Choose Folder profile
  5. Configure:
    • Target Runtime: win-x64
    • Deployment Mode: Self-contained
    • Produce Single File: True
  6. Click Publish

Build Configuration Details

The .csproj file is configured for optimal deployment:

<PublishSingleFile>true</PublishSingleFile>       <!-- Single .exe -->
<SelfContained>true</SelfContained>               <!-- Includes .NET runtime -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier>    <!-- Windows 64-bit -->
<PublishReadyToRun>true</PublishReadyToRun>       <!-- AOT compilation -->
<PublishTrimmed>false</PublishTrimmed>            <!-- Keep full framework (WPF needs it) -->

Optimizations

  • Self-Contained: No .NET installation required on target machine
  • Single-File: All dependencies bundled into one .exe (~70-90 MB)
  • ReadyToRun: Ahead-of-time compilation for faster startup
  • No Trimming: Required for WPF compatibility

Testing the Build

# Navigate to output directory
cd bin\Release\net8.0-windows\win-x64\publish

# Run the executable
.\AlwaysOnTop.exe

Usage Instructions

  1. Launch Application: Run AlwaysOnTop.exe

  2. Refresh Window List: Click "πŸ”„ Refresh Windows" to detect all open windows

  3. Select Windows: Check the boxes next to windows you want to keep on top

  4. Assign Priorities:

    • Enter priority numbers in the "Priority" column
    • Lower numbers = higher priority (1 is highest)
    • Example: Priority 1 stays above Priority 2
  5. Apply: Click "βœ“ Apply Always On Top"

  6. Manage:

    • Remove Selected: Uncheck windows and click to remove topmost behavior
    • Clear All: Remove all managed windows at once
  7. Background Operation: The app continues monitoring in the background. Minimize it or leave it running.

Technical Specifications

Resource Usage

  • Memory: ~30-50 MB (typical)
  • CPU: <1% (idle), ~2-3% (during Z-order enforcement)
  • Disk: ~75 MB (single-file executable)

Compatibility

  • OS: Windows 10 version 1809+ / Windows 11
  • Architecture: x64 only
  • .NET: Self-contained (no installation required)

API Dependencies

  • user32.dll: Core window management
  • dwmapi.dll: Desktop Window Manager (virtual desktop support)

Known Limitations

  1. Exclusive Fullscreen Games: May temporarily override topmost behavior

    • Workaround: Use borderless windowed mode
  2. UAC/Admin Windows: Cannot manage windows running with higher privileges

    • Solution: Run Always On Top Manager as administrator
  3. Protected System Windows: Some system dialogs (e.g., UAC prompts) always override

  4. Performance: With 50+ managed windows, monitoring may slow down

    • Recommendation: Manage only essential windows

Security Considerations

  • No Network Access: Application is completely offline
  • No Data Collection: No telemetry or analytics
  • No Registry Modifications: All settings stored in memory
  • Open Source: Code is fully auditable

Troubleshooting

Issue: "Window not staying on top"

  • Cause: Window's application may be resetting its own Z-order
  • Solution: Increase monitoring frequency or restart the application

Issue: "Cannot see my window in the list"

  • Cause: Window is minimized, on another virtual desktop, or has no title
  • Solution: Restore window, switch virtual desktop, or ensure it has a title bar

Issue: "High CPU usage"

  • Cause: Too many managed windows or very fast-changing window states
  • Solution: Reduce number of managed windows or increase timer interval

Development

Project Structure

AlwaysOnTop/
β”œβ”€β”€ AlwaysOnTop.csproj          # Project configuration
β”œβ”€β”€ App.xaml / App.xaml.cs      # Application entry point
β”œβ”€β”€ MainWindow.xaml             # UI layout (WPF)
β”œβ”€β”€ MainWindow.xaml.cs          # UI logic and event handlers
β”œβ”€β”€ Models/
β”‚   └── WindowInfo.cs           # Window data model with INotifyPropertyChanged
β”œβ”€β”€ Services/
β”‚   β”œβ”€β”€ WindowEnumerator.cs     # Window discovery and filtering
β”‚   └── WindowManager.cs        # Priority enforcement and monitoring
└── Native/
    └── WindowsApi.cs           # P/Invoke declarations for Win32 APIs

Adding New Features

Example: Custom Hotkeys

  1. Add hotkey registration in MainWindow.xaml.cs
  2. Use RegisterHotKey from user32.dll
  3. Handle WM_HOTKEY messages in WndProc override

Example: Persistent Settings

  1. Add JSON serialization for window priorities
  2. Save to AppData\Local\AlwaysOnTop\settings.json
  3. Load on application startup

License

This is a demonstration project. Modify and distribute as needed.

Credits

Built with:

  • C# / .NET 8.0
  • Windows Presentation Foundation (WPF)
  • Win32 API (user32.dll, dwmapi.dll)

Version: 1.0.0
Last Updated: January 2026
Platform: Windows 11 (compatible with Windows 10 1809+)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors