为什么我的简单 C++ Windows.h window 这么慢?
Why is my simple C++ Windows.h window so laggy?
我正在探索用 C++ 创建 windows 应用程序的选项,今天我尝试了“Windows.h”。在我尝试将文本渲染到 window 之前,它工作得很好(除了大量的样板代码)。编译时间猛增到 20 秒左右,window 非常滞后。有谁知道为什么这么基础的程序会这么慢?
Window.cpp:
#include "Window.h"
#include <vector>
#include <iostream>
LPCWSTR title = L"hello";
HWND textfield;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
}
textfield = CreateWindowW(L"STATIC", L"Text is here",
WS_VISIBLE | WS_CHILD | WS_BORDER, 20, 20, 300, 25, hWnd, NULL, NULL, NULL);
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
Window::Window()
: m_hInstance(GetModuleHandle(nullptr))
{
const wchar_t* CLASS_NAME = L"Kai Window Class";
WNDCLASS wndClass = {};
wndClass.lpszClassName = CLASS_NAME;
wndClass.hInstance = m_hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.lpfnWndProc = WindowProc;
RegisterClass(&wndClass);
DWORD style = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
std::vector<int> AspectRatio = {1920, 1080};
RECT rect;
rect.left = AspectRatio.at(0)/2;
rect.top = AspectRatio.at(1)/2;
rect.right = AspectRatio.at(0);
rect.bottom = AspectRatio.at(1);
AdjustWindowRect(&rect, style, false);
m_hWnd = CreateWindowEx(
0,
CLASS_NAME,
title,
style,
rect.left,
rect.top,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
m_hInstance,
NULL
);
ShowWindow(m_hWnd, SW_SHOW);
}
Window::~Window()
{
const wchar_t* CLASS_NAME = L"Kai Window Class";
UnregisterClass(CLASS_NAME, m_hInstance);
}
bool Window::ProcessMessages()
{
MSG msg = {};
while (PeekMessage(&msg, nullptr, 0u, 0u, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
return false;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return true;
}
Window.h:
#pragma once
#include <Windows.h>
#include <Vector>
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
class Window
{
public:
Window();
Window(const Window&) = delete;
Window& operator = (const Window&) = delete;
~Window();
bool ProcessMessages();
private:
HINSTANCE m_hInstance;
HWND m_hWnd;
};
Main.cpp:
#include <iostream>
#include "Window.h"
int main()
{
std::cout << "Creating Window...\n";
Window* pWindow = new Window();
bool running = true;
while (running)
{
if (!pWindow->ProcessMessages())
{
std::cout << "Closing Window...\n";
running = false;
}
// Render
Sleep(10);
}
delete pWindow;
return 0;
}
它很慢,因为每次 window 填写消息时您都在创建一个 window
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
}
///////////// This should not be here .....
textfield = CreateWindowW(L"STATIC", L"Text is here",
WS_VISIBLE | WS_CHILD | WS_BORDER, 20, 20, 300, 25, hWnd, NULL, NULL, NULL);
/////////////
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
您想创建标签一次,而不是每次调用 window 过程时。 WM_CREATE
处理程序中的一个好地方,即
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
textfield = CreateWindowW(L"STATIC", L"Text is here", WS_VISIBLE | WS_CHILD | WS_BORDER, 20, 20, 300, 25, hWnd, NULL, NULL, NULL);
return 0;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
我正在探索用 C++ 创建 windows 应用程序的选项,今天我尝试了“Windows.h”。在我尝试将文本渲染到 window 之前,它工作得很好(除了大量的样板代码)。编译时间猛增到 20 秒左右,window 非常滞后。有谁知道为什么这么基础的程序会这么慢?
Window.cpp:
#include "Window.h"
#include <vector>
#include <iostream>
LPCWSTR title = L"hello";
HWND textfield;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
}
textfield = CreateWindowW(L"STATIC", L"Text is here",
WS_VISIBLE | WS_CHILD | WS_BORDER, 20, 20, 300, 25, hWnd, NULL, NULL, NULL);
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
Window::Window()
: m_hInstance(GetModuleHandle(nullptr))
{
const wchar_t* CLASS_NAME = L"Kai Window Class";
WNDCLASS wndClass = {};
wndClass.lpszClassName = CLASS_NAME;
wndClass.hInstance = m_hInstance;
wndClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.lpfnWndProc = WindowProc;
RegisterClass(&wndClass);
DWORD style = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
std::vector<int> AspectRatio = {1920, 1080};
RECT rect;
rect.left = AspectRatio.at(0)/2;
rect.top = AspectRatio.at(1)/2;
rect.right = AspectRatio.at(0);
rect.bottom = AspectRatio.at(1);
AdjustWindowRect(&rect, style, false);
m_hWnd = CreateWindowEx(
0,
CLASS_NAME,
title,
style,
rect.left,
rect.top,
rect.right - rect.left,
rect.bottom - rect.top,
NULL,
NULL,
m_hInstance,
NULL
);
ShowWindow(m_hWnd, SW_SHOW);
}
Window::~Window()
{
const wchar_t* CLASS_NAME = L"Kai Window Class";
UnregisterClass(CLASS_NAME, m_hInstance);
}
bool Window::ProcessMessages()
{
MSG msg = {};
while (PeekMessage(&msg, nullptr, 0u, 0u, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
return false;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return true;
}
Window.h:
#pragma once
#include <Windows.h>
#include <Vector>
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
class Window
{
public:
Window();
Window(const Window&) = delete;
Window& operator = (const Window&) = delete;
~Window();
bool ProcessMessages();
private:
HINSTANCE m_hInstance;
HWND m_hWnd;
};
Main.cpp:
#include <iostream>
#include "Window.h"
int main()
{
std::cout << "Creating Window...\n";
Window* pWindow = new Window();
bool running = true;
while (running)
{
if (!pWindow->ProcessMessages())
{
std::cout << "Closing Window...\n";
running = false;
}
// Render
Sleep(10);
}
delete pWindow;
return 0;
}
它很慢,因为每次 window 填写消息时您都在创建一个 window
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
}
///////////// This should not be here .....
textfield = CreateWindowW(L"STATIC", L"Text is here",
WS_VISIBLE | WS_CHILD | WS_BORDER, 20, 20, 300, 25, hWnd, NULL, NULL, NULL);
/////////////
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
您想创建标签一次,而不是每次调用 window 过程时。 WM_CREATE
处理程序中的一个好地方,即
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
textfield = CreateWindowW(L"STATIC", L"Text is here", WS_VISIBLE | WS_CHILD | WS_BORDER, 20, 20, 300, 25, hWnd, NULL, NULL, NULL);
return 0;
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}