This is a very basic user guide to give an overview of the architecture and how to interact with the library.
The main entry point to create a Window is the Window.Create method:
using NWindows;
using NWindows.Threading;
using System.Drawing;
var mainWindow = Window.Create(new()
{
Title = "Hello World",
StartPosition = WindowStartPosition.CenterScreen,
BackgroundColor = WindowSettings.Theme == WindowTheme.Light
? Color.FromArgb(245, 245, 245)
: Color.FromArgb(30, 30, 30)
});
Dispatcher.Current.Run();Will create the following window on Windows:
You will find more examples in the sample folder.
There is a couple of API that you will likely need to use:
- The
Dispatcherclass is the class that runs the message loop for the thread viaDispatcher.Current.Run(); - The
Keyboard,Mouse,Cursor,Clipboardclasses give access to input methods and interaction with the clipboard. - Both the dispatcher and the window publish their events via an
EventHubclass. See next section
NWindows is modeling events by using modern C# with record. It simplifies a lot the declaration of these events and it helps to have instant ToString() representation with their properties.
The Dispatcher class provides adding event listeners via the property Dispatcher.Current.Events to listen for:
- All the events below
- Application Idle events.
- Shutdown started/finished.
- Unhandled exceptions.
The Window class provides adding event listeners via the property Window.Events to listen for Window events:
- All events.
- Keyboard events.
- Text events (translated character typed).
- Mouse events.
- Frame events (closed, destroyed, size changed...etc.).
- Paint event.
- Close event.
- HitTest events (for border-less Windows).
- Clipboard events.
- System events (screen changes...).
For example, the following code is listening for frame events on a specific Window when the OS theme is changed:
mainWindow.Events.Frame += (window, evt) =>
{
// Update the background color if the theme changed
if (evt.ChangeKind == FrameChangeKind.ThemeChanged)
{
window.BackgroundColor = GetCurrentThemeColor();
}
};Or the following code is closing the Window when the <Escape> key is pressed:
mainWindow.Events.Keyboard += (_, evt) =>
{
if (evt.Key == Key.Escape)
{
mainWindow.Close();
evt.Handled = true;
}
};NOTE: In order to avoid allocations, events objects are cached across calls, so you should not cache them but copy their data during the call to the event handler!
A Window object provides the Handle property which returns the native handle for the associated OS dependent object.
Depending on the platforms, the Handle points to:
- On Windows: it is the
HWNDof the Window.