I'm in the process of exploring options for creating windows applications in C , and today I tried "Windows.h". It was working pretty well (apart from the copious boilerplate code) until I tried rendering text to the window. Compiling times jumped up to like 20 seconds, and the window was extremely laggy. Does anyone know why it is that such a basic program would be so slow?
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;
}
CodePudding user response:
It's slow because you are creating a window every time your window fields a message
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);
}
You want to create the label once, not every time the window procedure is called. A good place is in the WM_CREATE
handler i.e.
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);
}