From d98af991c75a22110655918f4fb510ea76f1211a Mon Sep 17 00:00:00 2001 From: Aaro Saila Date: Sat, 13 Jun 2026 15:13:10 +0300 Subject: [PATCH] update --- src/main.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 105 insertions(+), 9 deletions(-) diff --git a/src/main.c b/src/main.c index fa24d45..def03c4 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -9,25 +10,113 @@ // #ifdef __MINGW32__ +typedef struct { + const char* msg; + int num; +} State; + +void OnSize(HWND hwnd, UINT flag, int width, int height) { + printf("Window resize: flag: %d, width: %d, height: %d\n", flag, width, height); +} + LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { + State* pState = NULL; + if (uMsg == WM_CREATE) { + CREATESTRUCT* pCreate = (CREATESTRUCT*) lParam; + pState = (State*) pCreate->lpCreateParams; + SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) pState); + } else { + LONG_PTR ptr = GetWindowLongPtr(hwnd, GWLP_USERDATA); + pState = (State*) ptr; + } + switch (uMsg) { + case WM_LBUTTONDOWN: + printf("State: msg: %s, num: %d\n", pState->msg, pState->num); + return 0; + case WM_DESTROY: PostQuitMessage(0); return 0; - case WM_PAINT: { + case WM_CLOSE: + if (MessageBox(hwnd, L"Really quit?", L"My Application", MB_OKCANCEL) == IDOK) { + DestroyWindow(hwnd); + } + return 0; + + case WM_SIZE: + { + int width = LOWORD(lParam); + int height = HIWORD(lParam); + OnSize(hwnd, (UINT) wParam, width, height); + return 0; + } + + case WM_PAINT: + { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 1)); + RECT blue = { + .bottom = 100, + .left = 0, + .right = 100, + .top = 0 + }; + FillRect(hdc, &blue, (HBRUSH) (COLOR_BACKGROUND + 1)); EndPaint(hwnd, &ps); - } return 0; } + } return DefWindowProc(hwnd, uMsg, wParam, lParam); } +void show_open_dialog(HWND hwnd) { + HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); + if (FAILED(hr)) { + printf("COM init failed\n"); + exit(1); + } + +#define OPEN_DIALOG_CHECK_ERR(msg) \ + if (FAILED((hr))) { \ + printf("%s:%d: %s", __FILE__, __LINE__, (msg)); \ + exit(1); \ + } + + IFileOpenDialog* pFileOpen; + hr = CoCreateInstance( + &CLSID_FileOpenDialog, + NULL, + CLSCTX_ALL, + &IID_IFileOpenDialog, + (void**) &pFileOpen + ); + OPEN_DIALOG_CHECK_ERR("ERROR: Failed to create FileOpenDialog\n"); + + hr = pFileOpen->lpVtbl->Show(pFileOpen, hwnd); + OPEN_DIALOG_CHECK_ERR("ERROR: Failed to show FileOpenDialog\n"); + + IShellItem* pItem; + hr = pFileOpen->lpVtbl->GetResult(pFileOpen, &pItem); + OPEN_DIALOG_CHECK_ERR("ERROR: Failed to get open file dialog result.\n") + + PWSTR pszFilePath; + hr = pItem->lpVtbl->GetDisplayName(pItem, SIGDN_FILESYSPATH, &pszFilePath); + OPEN_DIALOG_CHECK_ERR("ERROR: Failed to get path\n"); + + MessageBox(NULL, pszFilePath, L"File Path", MB_OK); + + CoTaskMemFree(pszFilePath); + pItem->lpVtbl->Release(pItem); + pFileOpen->lpVtbl->Release(pFileOpen); + + CoUninitialize(); +} + int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) { // Register the window class const wchar_t* CLASS_NAME = L"Sample Window Class"; @@ -39,20 +128,25 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, RegisterClass(&wc); + State s = { + .msg = "This is state", + .num = 69420 + }; + // Create the window HWND hwnd = CreateWindowEx( - 0, // Optional window styles - CLASS_NAME, // Window class + 0, // Optional window styles + CLASS_NAME, // Window class L"Learn to Program Windows", // Window text - WS_OVERLAPPEDWINDOW, // Window style + WS_OVERLAPPEDWINDOW, // Window style // Size and position CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, - NULL, // Parent window - NULL, // Menu + NULL, // Parent window + NULL, // Menu hInstance, // Instance handle - NULL // Additional application data + &s // Additional application data ); if (hwnd == NULL) { @@ -62,9 +156,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, ShowWindow(hwnd, nCmdShow); + show_open_dialog(hwnd); + // Message loop - MSG msg = {}; + MSG msg = { }; while (GetMessage(&msg, NULL, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessage(&msg);