Window.create constructor
Window.create({})
Creates a new Window with the specified parameters.
windowCaption
: The caption/title of the window.className
: The window class name.windowProc
: The window procedure callback function.hInstance
: The instance handle (defaults toGetModuleHandle(nullptr)
).dimensions
: The initial dimensions of the window (optional).iconPath
: The path to the icon file (.ico) (optional).
Throws an Exception if window creation fails.
Implementation
factory Window.create({
required String windowCaption,
required String className,
required Pointer<NativeFunction<WNDPROC>> windowProc,
int? hInstance,
math.Rectangle<int>? dimensions,
String? iconPath,
}) {
if (iconPath != null && !iconPath.endsWith('.ico')) {
throw ArgumentError.value(
iconPath,
'iconPath',
'Must be an icon file (.ico)',
);
}
return using((arena) {
final classNamePtr = className.toNativeUtf16(allocator: arena);
final windowCaptionPtr = windowCaption.toNativeUtf16(allocator: arena);
final iconPathPtr = iconPath?.toNativeUtf16(allocator: arena);
final wc = arena<WNDCLASS>();
wc.ref
..hbrBackground = GetStockObject(GET_STOCK_OBJECT_FLAGS.WHITE_BRUSH)
..hCursor = LoadCursor(NULL, IDC_ARROW)
..hInstance = hInstance ?? GetModuleHandle(nullptr)
..lpfnWndProc = windowProc
..lpszClassName = classNamePtr
..style = WNDCLASS_STYLES.CS_HREDRAW | WNDCLASS_STYLES.CS_VREDRAW;
if (iconPathPtr != null) {
wc.ref.hIcon = LoadImage(
NULL,
iconPathPtr,
GDI_IMAGE_TYPE.IMAGE_ICON,
NULL,
NULL,
IMAGE_FLAGS.LR_DEFAULTSIZE | IMAGE_FLAGS.LR_LOADFROMFILE,
);
}
RegisterClass(wc);
final scaleFactor =
dimensions != null ? scaleFactorForOrigin(dimensions) : 1.0;
final hwnd = CreateWindowEx(
0, // Optional window styles.
classNamePtr, // Window class
windowCaptionPtr, // Window caption
WINDOW_STYLE.WS_OVERLAPPEDWINDOW |
WINDOW_STYLE.WS_VISIBLE, // Window style
dimensions != null
? scale(dimensions.left, scaleFactor)
: CW_USEDEFAULT,
dimensions != null ? scale(dimensions.top, scaleFactor) : CW_USEDEFAULT,
dimensions != null
? scale(dimensions.width, scaleFactor)
: CW_USEDEFAULT,
dimensions != null
? scale(dimensions.height, scaleFactor)
: CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance ?? GetModuleHandle(nullptr), // Instance handle
nullptr, // Additional application data
);
if (hwnd == FALSE) throw Exception('Unable to create top-level window.');
return Window(hwnd);
});
}