Skip to content

fernandezja/winforms-user32-window-control

Repository files navigation

winforms-user32-window-control

CI

A small experimental C# .NET Framework 4.8 WinForms project exploring user32.dll P/Invoke calls to minimize, restore, and bring a Notepad window to the foreground.

Purpose

The goal is to understand how a WinForms application can call native Win32 APIs from user32.dll using P/Invoke (DllImport). This is a hands-on experiment showing the practical difference between referencing a managed .NET assembly and invoking a native Windows DLL.

The example opens a temporary text file named bitacora-jedi.txt in Notepad and then uses native Windows functions to:

  • Minimize the Notepad window.
  • Restore the Notepad window.
  • Bring the Notepad window to the foreground.
  • Display the window handle and title.
  • Show the result of the last native user32.dll call.

What this project demonstrates

This demo focuses on three main concepts:

  1. Using P/Invoke from C# to call native Windows APIs.
  2. Working with an external process and its main window handle.
  3. Understanding that SetForegroundWindow can fail because Windows may prevent applications from stealing focus.

Technologies

  • C#
  • .NET Framework 4.8
  • Windows Forms
  • Visual Studio 2022/2026
  • P/Invoke
  • user32.dll
  • Notepad

Features

The application includes the following actions:

Open Jedi Log

Creates a temporary file named bitacora-jedi.txt and opens it with Notepad.

The file contains sample text such as:

Jedi log initialized.
Coordinates received from Tatooine.
Preparing jump to hyperspace.
May the Force be with the process.

Minimize Jedi Log

Calls ShowWindow with the SW_MINIMIZE flag to collapse the Notepad window to the taskbar.

Restore Jedi Log

Calls ShowWindow with the SW_RESTORE flag to restore the Notepad window to its normal size and position.

Bring Jedi Log to Front

Calls SetForegroundWindow to attempt to activate the Notepad window and bring it to the front. The function will return false if Windows blocks the focus-stealing attempt (a deliberate OS restriction).

Close Jedi Log

Terminates the Notepad process and cleans up the temporary log file.

Screenshot

Rebel Console — Starwars.RebeldeWindowControlApp

The application displaying the Rebel Console interface with window control buttons and status information.

Project Structure

  • MainForm.cs — The main WinForms application with all UI controls created in code (no designer file).
  • NativeMethods.cs — P/Invoke declarations for user32.dll functions (ShowWindow, SetForegroundWindow).
  • JediLogService.cs — Service class that creates and manages the temporary Jedi log file.
  • Program.cs — Application entry point.

Running the Application

  1. Open the solution in Visual Studio 2022 or 2026.
  2. Build the solution.
  3. Run the application.
  4. Click buttons to control a Notepad window using native Win32 APIs.

About

Experimental C# .NET Framework 4.8 WinForms project exploring user32.dll through P/Invoke to control a Notepad window: minimize, restore, and bring it to the foreground

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages