我正在探索用 C 創建 Windows 應用程式的選項,今天我嘗試了“Windows.h”。在我嘗試將文本渲染到視窗之前,它運行良好(除了大量的樣板代碼)。編譯時間躍升至 20 秒左右,而且視窗非常滯后。有誰知道為什么這樣一個基本程式會這么慢?
視窗.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;
}
視窗.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;
};
主.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;
}
uj5u.com熱心網友回復:
它很慢,因為每次視窗欄位訊息時您都在創建一個視窗
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);
}
您希望創建一次標簽,而不是每次呼叫視窗程序時。一個好地方是在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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/379271.html
上一篇:無法將Env變數傳遞給azure管道yaml中的powershell腳本(非行內)
下一篇:log4j-vulnerability-log4j1.2.17是否易受攻擊(無法在源代碼中找到任何jndi代碼)?
