This commit is contained in:
2026-06-13 15:13:10 +03:00
parent 71707738e3
commit d98af991c7

View File

@@ -1,3 +1,4 @@
#include <shobjidl.h>
#include <stdio.h> #include <stdio.h>
#include <wchar.h> #include <wchar.h>
#include <windows.h> #include <windows.h>
@@ -9,25 +10,113 @@
// #ifdef __MINGW32__ // #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) { 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) { switch (uMsg) {
case WM_LBUTTONDOWN:
printf("State: msg: %s, num: %d\n", pState->msg, pState->num);
return 0;
case WM_DESTROY: case WM_DESTROY:
PostQuitMessage(0); PostQuitMessage(0);
return 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; PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps); HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 1)); 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); EndPaint(hwnd, &ps);
}
return 0; return 0;
} }
}
return DefWindowProc(hwnd, uMsg, wParam, lParam); 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) { int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
// Register the window class // Register the window class
const wchar_t* CLASS_NAME = L"Sample Window Class"; const wchar_t* CLASS_NAME = L"Sample Window Class";
@@ -39,6 +128,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
RegisterClass(&wc); RegisterClass(&wc);
State s = {
.msg = "This is state",
.num = 69420
};
// Create the window // Create the window
HWND hwnd = CreateWindowEx( HWND hwnd = CreateWindowEx(
0, // Optional window styles 0, // Optional window styles
@@ -52,7 +146,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
NULL, // Parent window NULL, // Parent window
NULL, // Menu NULL, // Menu
hInstance, // Instance handle hInstance, // Instance handle
NULL // Additional application data &s // Additional application data
); );
if (hwnd == NULL) { if (hwnd == NULL) {
@@ -62,9 +156,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine,
ShowWindow(hwnd, nCmdShow); ShowWindow(hwnd, nCmdShow);
show_open_dialog(hwnd);
// Message loop // Message loop
MSG msg = {}; MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0) > 0) { while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&msg); DispatchMessage(&msg);