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.
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.dllcall.
This demo focuses on three main concepts:
- Using P/Invoke from C# to call native Windows APIs.
- Working with an external process and its main window handle.
- Understanding that
SetForegroundWindowcan fail because Windows may prevent applications from stealing focus.
- C#
- .NET Framework 4.8
- Windows Forms
- Visual Studio 2022/2026
- P/Invoke
user32.dll- Notepad
The application includes the following actions:
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.
Calls ShowWindow with the SW_MINIMIZE flag to collapse the Notepad window to the taskbar.
Calls ShowWindow with the SW_RESTORE flag to restore the Notepad window to its normal size and position.
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).
Terminates the Notepad process and cleans up the temporary log file.
The application displaying the Rebel Console interface with window control buttons and status information.
- MainForm.cs — The main WinForms application with all UI controls created in code (no designer file).
- NativeMethods.cs — P/Invoke declarations for
user32.dllfunctions (ShowWindow,SetForegroundWindow). - JediLogService.cs — Service class that creates and manages the temporary Jedi log file.
- Program.cs — Application entry point.
- Open the solution in Visual Studio 2022 or 2026.
- Build the solution.
- Run the application.
- Click buttons to control a Notepad window using native Win32 APIs.
