【发布时间】:2018-11-18 12:28:32
【问题描述】:
我试着做一个普通的窗口,但是当这个窗口被创建时我发现他正在吃太多的CPU。所以this is the image of task manager when window is running'。我应该如何优化我的代码以减少处理器负载,让应用能够在没有强大处理器负载的情况下在后台模式下工作?
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
void LError();
bool CreateMainWindow(HINSTANCE hinst, int width, int height);
WNDCLASSEX MC;
HWND Hmain;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow) {
MSG msg;
if (!CreateMainWindow(hInstance, 800, 600)) {
LError();
}
ShowWindow(Hmain, nCmdShow);
UpdateWindow(Hmain);
HACCEL hAccel = LoadAccelerators(hInstance, NULL);
BOOL bRet = 0;
while (bRet = GetMessage(&msg, nullptr, 0, 0))
{
if (-1 == bRet) break;
if (!TranslateAccelerator(Hmain, hAccel, &msg))
{
if (!IsDialogMessage(Hmain, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
return msg.wParam;
}
void LError() {
DWORD err = GetLastError();
// Translate ErrorCode to String.
LPTSTR Error = 0;
if (::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
err,
0,
(LPTSTR)&Error,
0,
NULL) == 0)
{
MessageBox(NULL, TEXT("Error Translating"), TEXT("Error"), NULL);
}
if (Error = LPTSTR("The operation completed successefully")) {
return;
}
MessageBox(NULL, Error, TEXT("GetCurrentDirectory Error"), MB_OK | MB_ICONWARNING);
// Free the buffer.
if (Error)
{
::LocalFree(Error);
Error = 0;
}
}
bool CreateMainWindow(HINSTANCE hinst, int width, int height) {
LPCSTR Cname = "MainWindow";
MC.cbSize = sizeof(MC);
MC.style = CS_HREDRAW | CS_VREDRAW;
MC.style = 0;
MC.lpfnWndProc = WndProc;
MC.lpszMenuName = NULL;
MC.lpszClassName = Cname;
MC.cbWndExtra = NULL;
MC.cbClsExtra = NULL;
MC.hIcon = LoadIcon(NULL, IDI_WINLOGO);
MC.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
MC.hCursor = LoadCursor(NULL, IDC_ARROW);
MC.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
MC.hInstance = HINSTANCE(hinst);
if (!RegisterClassEx(&MC)) {
return 0;
}
RECT dspl;
GetWindowRect(GetDesktopWindow(), &dspl);
Hmain = CreateWindow(TEXT("MainWindow"),
LPCSTR("Calendar"),
WS_OVERLAPPED,
dspl.right - width, 0 ,
width, height,
(HWND)NULL,
NULL,
HINSTANCE(hinst),
NULL);
if (!Hmain) {
return 0;
}
SetWindowLong(Hmain, GWL_EXSTYLE, WS_EX_LAYERED);
SetLayeredWindowAttributes(Hmain, RGB(254, 254, 254), 150, LWA_ALPHA | LWA_COLORKEY);
return 1;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
HDC hDC;
PAINTSTRUCT PS;
RECT rect;
switch (Msg) {
case WM_PAINT:
break;
case WM_KEYUP:
switch(wParam) {
case VK_ESCAPE:
PostQuitMessage(NULL); break;
}break;
case WM_DESTROY:
PostQuitMessage(NULL);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
【问题讨论】:
-
暂时没有做任何 WinApi 的东西,但是看看你的代码 - 我不禁认为被捕获但未处理的 WM_PAINT 消息是你问题的根源 - 从记忆中,你的方案将导致 WM_PAINT 消息被触发,直到您或 DefWndProc 处理它们。由于两者都没有这样做,痛苦!
-
显示一个窗口应该使用几乎正好 0.00 % 的 CPU。您必须在这里有错误,或者您的系统上有其他问题。这与“优化 Windows API”无关。
-
使用
GetWindowRect(GetDesktopWindow(), &dspl);和dspl来计算窗口尺寸会在多显示系统上占用大量时间。 -
@Petar 不要仅仅因为你会选择其他词而编辑问题。
-
对这个问题投了 5 次反对票并关闭它然后删除它是不公平的。感谢版主撤消删除。