我的C++窗口程序运行,但我看不到
My C++ window program run but i cannot see
下面是代码。运行时,不显示窗口,也没有错误消息。我做错了什么?
//WinApp.h
#pragma once
#include<Windows.h>
class WinApp
{
private: HWND hWnd;
MSG msg;
static WinApp *instance;
public:
WinApp(void);
~WinApp(void);
void CreateWnd(HINSTANCE hInstance, int iCmdShow);
int Run(HINSTANCE hInstance, int iCmdShow);
void Release();
static HRESULT CALLBACK WndProc(HWND hwnd, UINT imsg, WPARAM wParam, LPARAM lParam);// window proc
static WinApp* GetInstance();
};
//WinApp.cpp
#include "WinApp.h"
#include<Windows.h>
WinApp::WinApp(void)
{
hWnd=NULL;
ZeroMemory(&msg,sizeof(MSG));
}
WinApp::~WinApp(void)
{
delete hWnd;
}
void WinApp::CreateWnd(HINSTANCE hInstance, int iCmdShow){
WNDCLASSEX WndClassex;
ZeroMemory(&WndClassex, sizeof(WNDCLASSEX));
WndClassex.cbSize=sizeof(WNDCLASSEX);
WndClassex.hCursor=LoadCursor(NULL, IDC_ARROW);
WndClassex.hIcon=LoadIcon(NULL, IDI_APPLICATION);
WndClassex.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
WndClassex.hInstance=hInstance;
WndClassex.style=CS_HREDRAW|CS_VREDRAW;
WndClassex.lpszClassName=L" ";
WndClassex.lpszMenuName=NULL;
WndClassex.lpfnWndProc=&WinApp::WndProc;
RegisterClassEx(&WndClassex);
hWnd=CreateWindowEx(0,
L" ",
L"UNUSUAL",
WS_CAPTION|WS_MINIMIZEBOX|WS_SYSMENU|WS_THICKFRAME,
0,
0,
512,
512,
NULL,
NULL,
hInstance,
NULL
);
ShowWindow(hWnd, SW_RESTORE);
UpdateWindow(hWnd);
}
void WinApp::Release(){
delete this;
}
int WinApp::Run(HINSTANCE hInstance, int iCmdShow){
this->CreateWnd(hInstance, iCmdShow);
while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
WinApp* WinApp::instance=0;
WinApp* WinApp::GetInstance(){
if(instance==NULL){
instance=new WinApp;
}
return instance;
}
HRESULT WinApp::WndProc(HWND hwnd, UINT imsg,WPARAM wParam, LPARAM lParam ){
switch (imsg)
{
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
hdc = BeginPaint (hwnd, &ps);
GetClientRect (hwnd, &rect);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));
EndPaint (hwnd, &ps);
break;
case WM_QUIT:
PostQuitMessage(0);
WinApp::GetInstance()->Release();
break;
default: DefWindowProc(hwnd, imsg, wParam, lParam);
break;
}
return 0;
}
//main.cpp
#include<Windows.h>
#include"WinApp.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpszCmdLine, int iCmdShow){
WinApp::GetInstance()->Run(hInstance, iCmdShow);
return 0;
}
你的代码有很多问题。
但除此之外,你的窗口没有显示的原因是WinApp::WndProc
没有返回DefWindowProc
的结果。
所以只需改变
default:
DefWindowProc(hwnd, imsg, wParam, lParam);
break;
自
default:
return DefWindowProc(hwnd, imsg, wParam, lParam);
相关文章:
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 为什么我的 FreeRTOS 任务看不到类成员?
- 为什么我看不到字符串?
- 我想使用 "cout" 命令慢慢打印文本,但我真的找不到任何解决方案
- handleMessage看不到我的类和函数
- 为什么即使链接器找到目标文件,我的程序也看不到函数定义?
- C++概念看到我的类型的函数,但看不到 std::vector 的函数
- QML可以看到我的Q_GADGET,但看不到Q_OBJECT
- 看不到我在 c++ 中传递给另一个函数的主函数中的值
- 我需要创建一个函数在用户下打印"=",但由于变量是使用 main() 声明的,因此函数看不到参数
- 为什么我看不到被捕获的图像
- 为什么在从 c++ 中的函数返回对象的情况下,我看不到正在调用的复制构造函数?
- 为什么Qt看不到我的插槽?
- 为什么我看不到任何输出迭代这个向量?
- 我的C++窗口程序运行,但我看不到
- 看不到我的 c++ 程序 (debian) 的输出
- Project1.exe已停止工作.我似乎找不到我的SDL代码到底出了什么问题
- 当我尝试将类放入标头中时,当我使用类中的字符串进行编译时,我收到找不到成员的错误
- 我收到"找不到标识符"错误
- 即使图书馆存在,我也找不到