我制作了一个简单的win32应用程序。我把我的GUI代码放在一个类中,但是我遇到了一些错误。
Main.cpp的
#include "Form.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
Form form(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
form.show();
form.set_title("Testing this stuff");
}
Form.h
#pragma once
#include <Windows.h>
class Form
{
private:
HWND hwnd;
char* title = "Form";
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
public:
Form(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
void set_title(const char* title);
void show();
~Form();
};
Form.cpp
#include "Form.h"
LRESULT CALLBACK Form::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_KEYDOWN:
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
}
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
Form::Form(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = &WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);// LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "test_class";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), IMAGE_ICON, 16, 16, 0);
if (RegisterClassEx(&wc))
{
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "test_class", title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, NULL, NULL, NULL, NULL);
if (!hwnd == NULL)
{
ShowWindow(this->hwnd, SW_NORMAL);
UpdateWindow(this->hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
}
void Form::show() {
ShowWindow(this->hwnd, SW_NORMAL);
UpdateWindow(this->hwnd);
}
void Form::set_title(const char* title) {
SetWindowText(this->hwnd, title);
}
Form::~Form()
{
}
现在,我想要它做的是显示窗口并更改标题;但是,GUI不会显示,而set_title也不起作用。我也尝试过set_title:
SendMessage(this->hwnd, WM_SETTEXT, 0, (LPARAM)title);
这也不起作用。另外,this->hwnd
是我在班上制作的变量。我不确定错误是什么,以及如何解决它。我想在.NET中制作类似Windows Form Application的Form系统,但不使用任何框架(仅限win32)。