我刚开始在网上学习C ++后开始了解WINAPI。我知道我应该阅读更多有关WINAPI的内容,但我想同时进行学习和编码。
这是我的代码。它的目的是检查WINAPI中的函数,但我一直收到错误。我的代码出了什么问题?
#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <WinUser.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
return 0;
}
ShowWindow(hwnd, nCmdShow);
HPOWERNOTIFY WINAPI RegisterPowerSettingNotification(_In_ HANDLE hwnd, _In_ LPCGUID GUID_LIDCLOSE_ACTION, _In_ DWORD DEVICE_NOTIFY_WINDOW_HANDLE);//the problem is HERE i think why is not accepting DEVICE_NOTIFY_WINDOW_HANDLE if i leave it blank it works and compile but i don't get a msg
// Run the message loop.
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_POWERBROADCAST:
PostQuitMessage(0);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
EndPaint(hwnd, &ps);
}
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
错误:
Error 1 error C2143: syntax error : missing ')' before 'constant' c:\users\0110\documents\visual studio 2013\projects\lidclosingevent\lidclosingevent\source.cpp 48 1 LidClosingEvent
Error 2 error C2143: syntax error : missing ';' before 'constant' c:\users\0110\documents\visual studio 2013\projects\lidclosingevent\lidclosingevent\source.cpp 48 1 LidClosingEvent
Error 3 error C2059: syntax error : ')' c:\users\0110\documents\visual studio 2013\projects\lidclosingevent\lidclosingevent\source.cpp 48 1 LidClosingEvent
4 IntelliSense: expected a ')' c:\Users\0110\documents\visual studio 2013\Projects\LidClosingEvent\LidClosingEvent\Source.cpp 48 121 LidClosingEvent
我使用VS2013并以空项目开始。
答案 0 :(得分:3)
看起来您将RegisterPowerSettingNotification函数签名复制到main方法中并更改了它的参数。
你有:
HPOWERNOTIFY WINAPI RegisterPowerSettingNotification(_In_ HANDLE hwnd, _In_ LPCGUID GUID_LIDCLOSE_ACTION, _In_ DWORD DEVICE_NOTIFY_WINDOW_HANDLE);
当你应该:
RegisterPowerSettingNotification(hWnd, &GUID_LIDCLOSE_ACTION, DEVICE_NOTIFY_WINDOW_HANDLE);