Skip to content

Commit

Permalink
Deprecate constants for the Win32 enumerations (#832)
Browse files Browse the repository at this point in the history
  • Loading branch information
halildurmus committed Mar 21, 2024
1 parent cb8644e commit 810926b
Show file tree
Hide file tree
Showing 108 changed files with 50,435 additions and 593 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## 5.4.0-wip

- Fix constant values `ERROR_SERVICE_NOT_ACTIVE`, `KF_FLAG_ALIAS_ONLY`,
`QS_INPUT`, `STD_INPUT_HANDLE`, `STD_OUTPUT_HANDLE`, `STD_ERROR_HANDLE`,
`UIA_ItemTypePropertyId`, and `UIA_OrientationPropertyId` (#XXX)
- Introduce Win32 enumerations as extension types based on `int` (#XXX)
- Deprecate constants for the Win32 enumerations in favor of the newly
introduced extension types. For example, replace the usage of `ERROR_SUCCESS`
with `WIN32_ERROR.ERROR_SUCCESS` (#XXX)

You can automatically migrate your code to use the new constants by running
`dart fix --apply` in your terminal.

## 5.3.0

- Migrate away from `.elementAt` in favor of `operator +` (#825)
Expand Down
2 changes: 1 addition & 1 deletion example/bluetooth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void findRadioInfo(int hRadio) {

try {
final res = BluetoothGetRadioInfo(hRadio, radioInfo);
if (res == ERROR_SUCCESS) {
if (res == WIN32_ERROR.ERROR_SUCCESS) {
final bluetoothAddress = addressAsString(radioInfo.ref.address.rgBytes);
print('Radio name: ${radioInfo.ref.szName}');
print('Radio address: $bluetoothAddress');
Expand Down
28 changes: 19 additions & 9 deletions example/bluetoothle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ void main() {
..ref.setGUID(GUID_BLUETOOTHLE_DEVICE_INTERFACE);

final hDevInfo = SetupDiGetClassDevs(
interfaceGuid, nullptr, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
interfaceGuid,
nullptr,
NULL,
SETUP_DI_GET_CLASS_DEVS_FLAGS.DIGCF_PRESENT |
SETUP_DI_GET_CLASS_DEVS_FLAGS.DIGCF_DEVICEINTERFACE);
try {
return devicesByInterface(hDevInfo, interfaceGuid).toList();
} finally {
Expand All @@ -25,8 +29,14 @@ void main() {

for (final path in devicePaths) {
final pathPtr = path.toNativeUtf16();
final hDevice = CreateFile(pathPtr, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
final hDevice = CreateFile(
pathPtr,
0,
FILE_SHARE_MODE.FILE_SHARE_READ | FILE_SHARE_MODE.FILE_SHARE_WRITE,
nullptr,
FILE_CREATION_DISPOSITION.OPEN_EXISTING,
FILE_FLAGS_AND_ATTRIBUTES.FILE_ATTRIBUTE_NORMAL,
NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
final error = GetLastError();
print('CreateFile - Get Device Handle error: $error');
Expand Down Expand Up @@ -103,7 +113,7 @@ Iterable<String> devicesByInterface(
}

final error = GetLastError();
if (error != S_OK && error != ERROR_NO_MORE_ITEMS) {
if (error != S_OK && error != WIN32_ERROR.ERROR_NO_MORE_ITEMS) {
throw WindowsException(error);
}
} finally {
Expand All @@ -129,7 +139,7 @@ void printServicesByDevice(int hDevice) {
final bufferCountPtr = arena<USHORT>();
hr = BluetoothGATTGetServices(
hDevice, 0, nullptr, bufferCountPtr, BLUETOOTH_GATT_FLAG_NONE);
if (hr != HRESULT_FROM_WIN32(ERROR_MORE_DATA)) {
if (hr != HRESULT_FROM_WIN32(WIN32_ERROR.ERROR_MORE_DATA)) {
print('BluetoothGATTGetServices - Get Buffer Count error: $hr');
throw WindowsException(hr);
}
Expand Down Expand Up @@ -161,8 +171,8 @@ void printCharacteristicsByService(
final bufferCountPtr = arena<USHORT>();
hr = BluetoothGATTGetCharacteristics(hDevice, servicePtr, 0, nullptr,
bufferCountPtr, BLUETOOTH_GATT_FLAG_NONE);
if (hr != HRESULT_FROM_WIN32(ERROR_MORE_DATA)) {
if (hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
if (hr != HRESULT_FROM_WIN32(WIN32_ERROR.ERROR_MORE_DATA)) {
if (hr == HRESULT_FROM_WIN32(WIN32_ERROR.ERROR_NOT_FOUND)) {
return;
}
print('BluetoothGATTGetCharacteristics - Get Buffer Count error: $hr');
Expand Down Expand Up @@ -196,8 +206,8 @@ void printDescriptorsByCharacteristic(
final bufferCountPtr = arena<USHORT>();
hr = BluetoothGATTGetDescriptors(hDevice, characteristicPtr, 0, nullptr,
bufferCountPtr, BLUETOOTH_GATT_FLAG_NONE);
if (hr != HRESULT_FROM_WIN32(ERROR_MORE_DATA)) {
if (hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND)) {
if (hr != HRESULT_FROM_WIN32(WIN32_ERROR.ERROR_MORE_DATA)) {
if (hr == HRESULT_FROM_WIN32(WIN32_ERROR.ERROR_NOT_FOUND)) {
return;
}
print('BluetoothGATTGetDescriptors - Get Buffer Count error: $hr');
Expand Down
3 changes: 2 additions & 1 deletion example/com_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ Future<void> createIsolates() async {
/// ```
void main() async {
// The main thread is initialized for the COM apartment threading model.
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
CoInitializeEx(
nullptr, COINIT.COINIT_APARTMENTTHREADED | COINIT.COINIT_DISABLE_OLE1DDE);

// Should be mainSingleThreaded
print(getThreadContext().toString());
Expand Down
4 changes: 2 additions & 2 deletions example/com_demo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void main() {

// Initialize COM
var hr = CoInitializeEx(
nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
nullptr, COINIT.COINIT_APARTMENTTHREADED | COINIT.COINIT_DISABLE_OLE1DDE);
if (FAILED(hr)) throw WindowsException(hr);

// Create an instance of the FileOpenDialog class w/ IFileDialog interface
Expand Down Expand Up @@ -49,7 +49,7 @@ void main() {
// Use IFileOpenDialog.Show, which is inherited from IModalWindow
hr = fileOpenDialog.show(NULL);
if (FAILED(hr)) {
if (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) {
if (hr == HRESULT_FROM_WIN32(WIN32_ERROR.ERROR_CANCELLED)) {
print('Dialog cancelled.');
} else {
throw WindowsException(hr);
Expand Down
2 changes: 1 addition & 1 deletion example/commdlg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void main() {
// Set dialog flags:
// CC_RGBINIT: use rgbResult for the dialog default value
// CC_FULLOPEN: automatically open custom colors section of dialog
..ref.Flags = CC_RGBINIT | CC_FULLOPEN;
..ref.Flags = CHOOSECOLOR_FLAGS.CC_RGBINIT | CHOOSECOLOR_FLAGS.CC_FULLOPEN;

// Call the Win32 API to show dialog, passing pointer to the config struct
ChooseColor(cc);
Expand Down
14 changes: 7 additions & 7 deletions example/credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ void write(
final blob = examplePassword.allocatePointer();

final credential = calloc<CREDENTIAL>()
..ref.Type = CRED_TYPE_GENERIC
..ref.Type = CRED_TYPE.CRED_TYPE_GENERIC
..ref.TargetName = credentialName.toNativeUtf16()
..ref.Persist = CRED_PERSIST_LOCAL_MACHINE
..ref.Persist = CRED_PERSIST.CRED_PERSIST_LOCAL_MACHINE
..ref.UserName = userName.toNativeUtf16()
..ref.CredentialBlob = blob
..ref.CredentialBlobSize = examplePassword.length;
Expand All @@ -42,12 +42,12 @@ void write(
void read(String credentialName) {
print('Reading $credentialName ...');
final credPointer = calloc<Pointer<CREDENTIAL>>();
final result = CredRead(
credentialName.toNativeUtf16(), CRED_TYPE_GENERIC, 0, credPointer);
final result = CredRead(credentialName.toNativeUtf16(),
CRED_TYPE.CRED_TYPE_GENERIC, 0, credPointer);
if (result != TRUE) {
final errorCode = GetLastError();
var errorText = '$errorCode';
if (errorCode == ERROR_NOT_FOUND) {
if (errorCode == WIN32_ERROR.ERROR_NOT_FOUND) {
errorText += ' Not found.';
}
print('Error ($result): $errorText');
Expand All @@ -65,8 +65,8 @@ void read(String credentialName) {

void delete(String credentialName) {
print('Deleting $credentialName');
final result =
CredDelete(credentialName.toNativeUtf16(), CRED_TYPE_GENERIC, 0);
final result = CredDelete(
credentialName.toNativeUtf16(), CRED_TYPE.CRED_TYPE_GENERIC, 0);
if (result != TRUE) {
final errorCode = GetLastError();
print('Error ($result): $errorCode');
Expand Down
93 changes: 63 additions & 30 deletions example/customtitlebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ bool isWindowMaximized(int hwnd) {

try {
if (GetWindowPlacement(hwnd, windowPlacement) != FALSE) {
return windowPlacement.ref.showCmd == SW_SHOWMAXIMIZED;
return windowPlacement.ref.showCmd == SHOW_WINDOW_CMD.SW_SHOWMAXIMIZED;
}
return false;
} finally {
Expand Down Expand Up @@ -157,8 +157,9 @@ int paintButtons(int hwnd, int hdc, Pointer<PAINTSTRUCT> ps,
final titleBarHoverBrush = CreateSolidBrush(titleBarHoverColor);
final closeButtonColor = RGB(0xCC, 0x00, 0x00);
final buttonIconBrush = CreateSolidBrush(titleBarItemColor);
final buttonIconPen = CreatePen(PS_SOLID, 1, titleBarItemColor);
final hoveredButton = GetWindowLongPtr(hwnd, GWLP_USERDATA);
final buttonIconPen = CreatePen(PEN_STYLE.PS_SOLID, 1, titleBarItemColor);
final hoveredButton =
GetWindowLongPtr(hwnd, WINDOW_LONG_PTR_INDEX.GWLP_USERDATA);

final dpi = GetDpiForWindow(hwnd);
final iconDimension = dpiScale(10, dpi);
Expand Down Expand Up @@ -194,7 +195,7 @@ int paintButtons(int hwnd, int hdc, Pointer<PAINTSTRUCT> ps,

centerRectInParent(maximizeIconRect, maximizeButtonRect);
SelectObject(hdc, buttonIconPen);
SelectObject(hdc, GetStockObject(HOLLOW_BRUSH));
SelectObject(hdc, GetStockObject(GET_STOCK_OBJECT_FLAGS.HOLLOW_BRUSH));
Rectangle(hdc, maximizeIconRect.ref.left, maximizeIconRect.ref.top,
maximizeIconRect.ref.right, maximizeIconRect.ref.bottom);
} finally {
Expand All @@ -213,7 +214,7 @@ int paintButtons(int hwnd, int hdc, Pointer<PAINTSTRUCT> ps,
final fillBrush = CreateSolidBrush(closeButtonColor);
FillRect(hdc, closeButtonRect, fillBrush);
DeleteObject(fillBrush);
customPen = CreatePen(PS_SOLID, 1, RGB(0xFF, 0xFF, 0xFF));
customPen = CreatePen(PEN_STYLE.PS_SOLID, 1, RGB(0xFF, 0xFF, 0xFF));
SelectObject(hdc, customPen);
}

Expand Down Expand Up @@ -242,14 +243,15 @@ void drawWindowCaption(
final titleText = wsalloc(256);
final drawThemeOptions = calloc<DTTOPTS>()
..ref.dwSize = sizeOf<DTTOPTS>()
..ref.dwFlags = DTT_TEXTCOLOR
..ref.dwFlags = DTTOPTS_FLAGS.DTT_TEXTCOLOR
..ref.crText = titleBarItemColor;

try {
int? savedFont;

final hTheme = getWindowThemeHandle(hwnd);
if (SUCCEEDED(GetThemeSysFont(hTheme, TMT_CAPTIONFONT, logicalFont))) {
if (SUCCEEDED(GetThemeSysFont(
hTheme, THEME_PROPERTY_SYMBOL_ID.TMT_CAPTIONFONT, logicalFont))) {
final themeFont = CreateFontIndirect(logicalFont);
savedFont = SelectObject(hdc, themeFont);
}
Expand All @@ -263,7 +265,9 @@ void drawWindowCaption(
0,
titleText,
-1,
DT_VCENTER | DT_SINGLELINE | DT_WORD_ELLIPSIS,
DRAW_TEXT_FORMAT.DT_VCENTER |
DRAW_TEXT_FORMAT.DT_SINGLELINE |
DRAW_TEXT_FORMAT.DT_WORD_ELLIPSIS,
titleBarTextRect,
drawThemeOptions);

Expand Down Expand Up @@ -349,9 +353,12 @@ int mainWindowProc(int hwnd, int msg, int wParam, int lParam) {

final dpi = GetDpiForWindow(hwnd);

final frameX = GetSystemMetricsForDpi(SM_CXFRAME, dpi);
final frameY = GetSystemMetricsForDpi(SM_CYFRAME, dpi);
final padding = GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
final frameX =
GetSystemMetricsForDpi(SYSTEM_METRICS_INDEX.SM_CXFRAME, dpi);
final frameY =
GetSystemMetricsForDpi(SYSTEM_METRICS_INDEX.SM_CYFRAME, dpi);
final padding =
GetSystemMetricsForDpi(SYSTEM_METRICS_INDEX.SM_CXPADDEDBORDER, dpi);

final params = Pointer<NCCALCSIZE_PARAMS>.fromAddress(lParam);
final requestedClientRect = params.ref.rgrc[0]
Expand All @@ -378,7 +385,9 @@ int mainWindowProc(int hwnd, int msg, int wParam, int lParam) {
sizeRect.ref.top,
sizeRect.ref.right - sizeRect.ref.left,
sizeRect.ref.bottom - sizeRect.ref.top,
SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE);
SET_WINDOW_POS_FLAGS.SWP_FRAMECHANGED |
SET_WINDOW_POS_FLAGS.SWP_NOMOVE |
SET_WINDOW_POS_FLAGS.SWP_NOSIZE);

free(sizeRect);

Expand All @@ -405,8 +414,10 @@ int mainWindowProc(int hwnd, int msg, int wParam, int lParam) {
// Looks like adjustment happening in NCCALCSIZE is messing with the detection
// of the top hit area so manually fixing that.
final dpi = GetDpiForWindow(hwnd);
final frameY = GetSystemMetricsForDpi(SM_CYFRAME, dpi);
final padding = GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
final frameY =
GetSystemMetricsForDpi(SYSTEM_METRICS_INDEX.SM_CYFRAME, dpi);
final padding =
GetSystemMetricsForDpi(SYSTEM_METRICS_INDEX.SM_CXPADDEDBORDER, dpi);
final cursorPoint = calloc<POINT>()
..ref.x = LOWORD(lParam)
..ref.y = HIWORD(lParam);
Expand Down Expand Up @@ -434,7 +445,8 @@ int mainWindowProc(int hwnd, int msg, int wParam, int lParam) {
paintWindow(hwnd);

case WM_NCMOUSEMOVE:
final hoveredButton = GetWindowLongPtr(hwnd, GWLP_USERDATA);
final hoveredButton =
GetWindowLongPtr(hwnd, WINDOW_LONG_PTR_INDEX.GWLP_USERDATA);

final cursorPoint = calloc<POINT>();
GetCursorPos(cursorPoint);
Expand All @@ -458,7 +470,8 @@ int mainWindowProc(int hwnd, int msg, int wParam, int lParam) {
InvalidateRect(hwnd, closeRect, FALSE);
InvalidateRect(hwnd, maximizeRect, FALSE);
InvalidateRect(hwnd, minimizeRect, FALSE);
SetWindowLongPtr(hwnd, GWLP_USERDATA, newHoveredButton);
SetWindowLongPtr(
hwnd, WINDOW_LONG_PTR_INDEX.GWLP_USERDATA, newHoveredButton);
}
} finally {
free(minimizeRect);
Expand All @@ -469,13 +482,15 @@ int mainWindowProc(int hwnd, int msg, int wParam, int lParam) {
}

case WM_MOUSEMOVE:
final hoveredButton = GetWindowLongPtr(hwnd, GWLP_USERDATA);
final hoveredButton =
GetWindowLongPtr(hwnd, WINDOW_LONG_PTR_INDEX.GWLP_USERDATA);

if (hoveredButton != HoveredButton.none) {
final titleBarRect = getTitlebarRect(hwnd);
InvalidateRect(hwnd, titleBarRect, FALSE);
free(titleBarRect);
SetWindowLongPtr(hwnd, GWLP_USERDATA, HoveredButton.none);
SetWindowLongPtr(
hwnd, WINDOW_LONG_PTR_INDEX.GWLP_USERDATA, HoveredButton.none);
}

case WM_NCLBUTTONDOWN:
Expand All @@ -484,22 +499,26 @@ int mainWindowProc(int hwnd, int msg, int wParam, int lParam) {
//
// Ideally you also want to check that the mouse hasn't moved out or too much
// between DOWN and UP messages.
final hoveredButton = GetWindowLongPtr(hwnd, GWLP_USERDATA);
final hoveredButton =
GetWindowLongPtr(hwnd, WINDOW_LONG_PTR_INDEX.GWLP_USERDATA);
if (hoveredButton != HoveredButton.none) return 0;

case WM_NCLBUTTONUP:
final hoveredButton = GetWindowLongPtr(hwnd, GWLP_USERDATA);
final hoveredButton =
GetWindowLongPtr(hwnd, WINDOW_LONG_PTR_INDEX.GWLP_USERDATA);

// Map button clicks to the right messages for the window
switch (hoveredButton) {
case HoveredButton.close:
PostMessage(hwnd, WM_CLOSE, 0, 0);
return 0;
case HoveredButton.minimize:
ShowWindow(hwnd, SW_MINIMIZE);
ShowWindow(hwnd, SHOW_WINDOW_CMD.SW_MINIMIZE);
return 0;
case HoveredButton.maximize:
final mode = isWindowMaximized(hwnd) ? SW_NORMAL : SW_MAXIMIZE;
final mode = isWindowMaximized(hwnd)
? SHOW_WINDOW_CMD.SW_NORMAL
: SHOW_WINDOW_CMD.SW_MAXIMIZE;
ShowWindow(hwnd, mode);
return 0;
}
Expand All @@ -525,23 +544,37 @@ void main() {
final windowClass = calloc<WNDCLASSEX>()
..ref.cbSize = sizeOf<WNDCLASSEX>()
..ref.lpszClassName = windowClassName
..ref.style = CS_HREDRAW | CS_VREDRAW
..ref.style = WNDCLASS_STYLES.CS_HREDRAW | WNDCLASS_STYLES.CS_VREDRAW
..ref.hCursor = LoadCursor(NULL, IDC_ARROW)
..ref.lpfnWndProc = lpfnWndProc.nativeFunction;

RegisterClassEx(windowClass);

// Create the window.
const windowStyle = WS_THICKFRAME | // Standard resizeable window
WS_SYSMENU | // Explicitly ask for the titlebar to support snapping
WS_MAXIMIZEBOX | // Support maximizing via mouse dragging to screen top
WS_MINIMIZEBOX | // Support minimizing via clicking taskbar icon
WS_VISIBLE; // Make window visible after creation.
const windowStyle = WINDOW_STYLE.WS_THICKFRAME | // Standard resizeable window
WINDOW_STYLE
.WS_SYSMENU | // Explicitly ask for the titlebar to support snapping
WINDOW_STYLE
.WS_MAXIMIZEBOX | // Support maximizing via mouse dragging to screen top
WINDOW_STYLE
.WS_MINIMIZEBOX | // Support minimizing via clicking taskbar icon
WINDOW_STYLE.WS_VISIBLE; // Make window visible after creation.

final windowCaption = 'Win32 Custom Title Bar Example'.toNativeUtf16();

CreateWindowEx(WS_EX_APPWINDOW, windowClassName, windowCaption, windowStyle,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, NULL, nullptr);
CreateWindowEx(
WINDOW_EX_STYLE.WS_EX_APPWINDOW,
windowClassName,
windowCaption,
windowStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL,
NULL,
NULL,
nullptr);

final msg = calloc<MSG>();
while (GetMessage(msg, NULL, 0, 0) != 0) {
Expand Down

0 comments on commit 810926b

Please sign in to comment.