ошибка при попытке создания окна(winapi)
Начал изучать Windows API, первым делом посоветовали научиться создавать окна. Код полностью переписан с "обучалки" дабы запустить его и разобраться как он работает:
#include <Windows.h>
int CALLBACK wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR szCmdLine, int nCmdShow) {
MSG msg{};//structure which stored info about message and universal comunication way between OS and windows
HWND hwnd{};//descriptor for window handle-pointer on the core in which stored info about window which is registered in the system
WNDCLASSEX wc{sizeof(WNDCLASSEX)};//structure which contains info about window, like: size, depth e.t.c
wc.cbClsExtra = 0;//cbClsExtra makes additional mem allocation in window's class
wc.cbWndExtra = 0;//like cbClsExtra
wc.hbrBackground = reinterpret_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));//takes descriptor to fill the window by brush
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);//cursor handler
wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);//icon handler
wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);//small icon handler
wc.hInstance = hInstance;
wc.lpfnWndProc = [](HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)->LRESULT {// function pointer and messages processing
switch (uMsg) {
case WM_DESTROY:
{
PostQuitMessage(EXIT_SUCCESS);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
};
wc.lpszClassName = L"MyAppClass";
wc.lpszMenuName = nullptr;//menu name pointer
wc.style = CS_VREDRAW | CS_HREDRAW;//window style by default
if (!RegisterClassEx(&wc))
return EXIT_FAILURE;
if (hwnd = CreateWindow(wc.lpszClassName, L"Title", WS_OVERLAPPEDWINDOW, 0, 0, 600, 600, nullptr, nullptr, wc.hInstance, nullptr); hwnd == INVALID_HANDLE_VALUE)
return EXIT_FAILURE;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
}
У автора код рабочий. К сожалению, у меня нет. Ругается на ";" Во втором if блоке: требуется круглая скобка ")" После попытки запуска выплывает новая ошибка: error C2429: для функция языка "Инструкции INIT в if/switch" нужен флаг компилятора "/std:c++17" Как можно решить данную ошибку? P.S, наверное важная информация: автор использует VS 2017, у меня же 2019 версия.
Ответы (1 шт):
окошко можно перемещать за любое его место, закрытие окна (выход из приложения) осуществляется по нажатию и отпусканию правой кнопки мыши именно на окне приложения. вот код простейшего окна на чистом WinAPI. писал на Visual C++.
#include <Windows.h>
char szAppName [] = "My first application";
LRESULT CALLBACK WndMainProc (HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT pPaintStruct;
switch (uiMsg)
{
case WM_CREATE:
MoveWindow (hWnd, (GetSystemMetrics (SM_CXSCREEN) - 640) >> 1, (GetSystemMetrics (SM_CYSCREEN) - 400) >> 1, 640, 400, TRUE);
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
case WM_PAINT:
hDC = BeginPaint (hWnd, &pPaintStruct);
EndPaint (hWnd, &pPaintStruct);
break;
case WM_LBUTTONDOWN:
SendMessage (hWnd, WM_NCLBUTTONDOWN, (WPARAM) HTCAPTION, 0);
break;
case WM_RBUTTONUP:
SendMessage (hWnd, WM_DESTROY, 0, 0);
break;
default:
return DefWindowProc (hWnd, uiMsg, wParam, lParam);
}
return 0;
}
int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
HWND hWnd;
MSG pMSG;
WNDCLASSEX pWndClassEx;
pWndClassEx.cbSize = sizeof (WNDCLASSEX);
pWndClassEx.style = CS_HREDRAW | CS_VREDRAW | CS_DROPSHADOW;
pWndClassEx.lpfnWndProc = WndMainProc;
pWndClassEx.cbWndExtra = pWndClassEx.cbClsExtra = 0;
pWndClassEx.hInstance = hInstance;
pWndClassEx.hIconSm = pWndClassEx.hIcon = LoadIcon (NULL, IDI_APPLICATION);
pWndClassEx.hCursor = LoadCursor (NULL, IDC_ARROW);
pWndClassEx.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
pWndClassEx.lpszClassName = pWndClassEx.lpszMenuName = szAppName;
RegisterClassEx (&pWndClassEx);
hWnd = CreateWindowEx (0, szAppName, szAppName, WS_POPUP | WS_DLGFRAME, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
return 0;
ShowWindow (hWnd, iCmdShow);
UpdateWindow (hWnd);
while (GetMessage (&pMSG, NULL, 0, 0))
{
TranslateMessage (&pMSG);
DispatchMessage (&pMSG);
}
return (int) pMSG.wParam;
}