DX9桌面窗口管理器高CPU

时间:2018-05-13 08:08:03

标签: c++ visual-studio directx directx-9

桌面窗口管理器在绘制DX9窗口时使用一吨CPU。 我确定它为什么使用这么多CPU https://imgur.com/a/Bz7AVro

LRESULT CALLBACK WinProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam){
Sleep(12);
switch (Message){   
case WM_PAINT:
    Render();
    break;

case WM_CREATE:
    DwmExtendFrameIntoClientArea(hWnd, &Margin);
    break;

case WM_DESTROY:
    PostQuitMessage(1);
    return 0;

default:
    return DefWindowProc(hWnd, Message, wParam, lParam);
    break;
}
return 0;

}

ref class CMAIN {     上市:         void StartIt(){Main(); } };

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hSecInstance,LPSTR nCmdLine,INT nCmdShow){

Thread^ main;
CMAIN^ cMain = gcnew CMAIN();
main = gcnew Thread(gcnew ThreadStart(cMain, &CMAIN::StartIt));
main->Name = "main";
main->Start();

CreateThread(0, 0, (LPTHREAD_START_ROUTINE)SetWindowToTarget, 0, 0, 0);

WNDCLASSEX wClass;
wClass.cbClsExtra = NULL;
wClass.cbSize = sizeof(WNDCLASSEX);
wClass.cbWndExtra = NULL;
wClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0, 0, 0));
wClass.hCursor = LoadCursor(0, IDC_ARROW);
wClass.hIcon = LoadIcon(0, IDI_APPLICATION);
wClass.hIconSm = LoadIcon(0, IDI_APPLICATION);
wClass.hInstance = hInstance;
wClass.lpfnWndProc = WinProc;
wClass.lpszClassName = lWindowName;
wClass.lpszMenuName = lWindowName;
wClass.style = CS_VREDRAW | CS_HREDRAW;

if(!RegisterClassEx(&wClass))
    exit(1);

tWnd = FindWindow(0, tWindowName);
if (tWnd){
    GetWindowRect(tWnd, &tSize);
    Width = tSize.right - tSize.left;
    Height = tSize.bottom - tSize.top;
    hWnd = CreateWindowEx(WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED, lWindowName, lWindowName,  WS_POPUP, 1, 1, Width, Height, 0, 0, 0, 0);
    SetLayeredWindowAttributes(hWnd, 0, 1.0f, LWA_ALPHA);
    SetLayeredWindowAttributes(hWnd, 0, RGB(0, 0, 0), LWA_COLORKEY);
    ShowWindow( hWnd, SW_SHOW);
}

DirectXInit(hWnd);

while (!directXExit){
    Sleep(12);
    if(PeekMessage(&Message, hWnd, 0, 0, PM_REMOVE)){
        DispatchMessage(&Message);
        TranslateMessage(&Message);
    }
}
return 0;

}

void SetWindowToTarget(){

while(true){

    tWnd = FindWindow(0, tWindowName);
    if (tWnd){

        GetWindowRect(tWnd, &tSize);
        Width = tSize.right - tSize.left;
        Height = tSize.bottom - tSize.top;
        DWORD dwStyle = GetWindowLong(tWnd, GWL_STYLE);
        if(dwStyle & WS_BORDER){
            tSize.top += 23;
            Height -= 23;
        }
        MoveWindow(hWnd, tSize.left, tSize.top, Width, Height, true);
    }
    Sleep(1500);
}

}

1 个答案:

答案 0 :(得分:0)

获取有关您尝试执行的操作的更多信息会很有用,并且很难确切地看到代码中的WM_PAINT处理程序可能会发生什么(因为您调用了一个名为Render但不提供该代码的函数。但乍一看,我认为至少有一件事会迫使高CPU使用率,这就是你的消息循环。由于调用PeekMessage而调用Sleep(12),你基本上会在该循环中旋转,这基本上会在该窗口上给你一个~80fps的更新;因为您正在使用DWM组合代码(DwmExtendFrameIntoClient),我的猜测是您因此而在DWM中触发80Hz更新。如果您在该循环中实时渲染某些内容,那么这一切都是预期的并且无法避免(除非您将渲染循环放在单独的线程上)。但是,如果您只是回复WM_PAINT条消息,那么使用GetMessage而不是PeekMessage的消息泵可以更好地为您提供服务,因为{ {1}}将在Windows消息队列为空时挂起该线程,并且只有在需要更新某些内容时才会触发CPU工作。