我多次編碼。但它甚至在一個簡單的控制臺 hello word 應用程式中似乎都不起作用。應該歸咎于 hWND、lambda 還是 lambda 的鑄造?
void sleeper()
{
Sleep(10000);
}
int main()
{
SetTimer
(GetConsoleWindow(), 1, 1000, [](HWND, UINT, UINT_PTR, DWORD)
{
printf("Hello World!");
}
);
sleeper();
return 0;
}
它沒有給我警告,畢竟它與下一個示例不太相似。
這是另一個應用程式,一個可能有相同問題的丑陋視窗應用程式,沒有 timerproc 執行:
else if (message == WM_PAINT)
{
PAINTSTRUCT ps;
static HDC hdc = BeginPaint(hWnd, &ps);// TODO: Add any drawing code that uses hdc here...
static std::vector<std::vector<int>> color = std::vector<std::vector<int>>(0x7F7F7FFF);
SetTimer
(hWnd, 1, 5000, [](HWND hWnd, UINT nMsdg, UINT_PTR unnamedParam, DWORD dwTime)->void
{
int x = 0;
int y = 0;
SetPixelV(hdc, x, y, (COLORREF)color[x][y]);
}
);
EndPaint(hWnd, &ps);
}
現在在第二個示例中,它具有我認為的訊息處理功能,但它沒有繪制像素。我必須說我太習慣于撰寫腳本了。
uj5u.com熱心網友回復:
您不能將 Lamba 強制轉換為 TIMEPROC* 或使用不同于默認呼叫約定的任何其他型別的函式指標(不能指定 lambda 的呼叫約定)。Lambda 是可呼叫的物件。這種型別類似于一個類,帶有一個成員函式。
除此之外,您必須為 TIMERPROC 鉤子使用正確的宣告。這是:
// definition from the MS website. It is incomplete (typical from MS)
// ref: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nc-winuser-timerproc
void Timerproc(
HWND unnamedParam1,
UINT unnamedParam2,
UINT_PTR unnamedParam3,
DWORD unnamedParam4
)
// the actual definition from winuser.h.
typedef VOID (CALLBACK* TIMERPROC)(HWND, UINT, UINT_PTR, DWORD):
// note the use of CALLBACK, which expands to __stdcall. That's the very important
// detail missing from the actual documentation.
你可以將你的 timeproc 宣告為一個獨立的函式,或者一個類的靜態成員函式,不幸的是,你可以傳遞給回呼的 onluy 引數是一個 HWND,這意味著如果你想將任何額外的引數傳遞給你的回呼,您必須使用靜態(又名全域)變數。
示例 1。
void CALLBACK myTimerProc(HWND, UINT, UINT_PTR, DWORD)
{
printf("Hello World!");
}
int main()
{
// NOTE nIDEvent, the timer ID has to be unique for the window and NON-ZERO,
// See MS documentation here: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-settimer
SetTimer(GetConsoleWindow(), 1, 1000, myTimerProc);
sleeper();
return 0;
}
Example2,如果要在本地定義:
int main()
{
struct local // or any other
{
static void CALLBACK timerProc(HWND, UINT, UINT_PTR, DWORD)
{
printf("Hello World!");
}
}
SetTimer(GetConsoleWindow(), 1, 1000, local::timerProc);
sleeper();
return 0;
}
編輯:作為參考,TIMERPROC 回呼的實際引數。
來源:http : //www.icodeguru.com/VC&MFC/MFCReference/html/_mfc_cwnd.3a3a.settimer.htm
void CALLBACK EXPORT TimerProc(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent // timer identification
DWORD dwTime // system time
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/362586.html
上一篇:如何檢查對信號的回應
下一篇:如何填充圖的下部直到指定范圍
