读书人

第一回发帖。写了一个窗口的helloworl

发布时间: 2013-11-02 19:41:10 作者: rapoo

第一次发帖。写了一个窗口的helloworld运行成功但是不显示
代码如下,请大神帮忙看看,新手不太会debug


#include "windows.h"
#include "tChar.h"
//定义全局变量和函数
HINSTANCE hInst;
HINSTANCE hInstance;
MSG msg;
wchar_t lpszClassName[] = _T("hehe");
wchar_t * ShowText;

//声明响应函数(回调)
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void OnLButtonDown(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void OnDestory(HWND hWnd,UINT message,WPARAM wPARAM,LPARAM lParam);

//声明窗口类
class CFrameWnd
{
public:
HWND hWnd;
public:
int RegisterWindow();
void Create(LPCTSTR lpszClassName,LPCTSTR lpWindowName);
void ShowWindow(int nCmdShow);
void UpdateWindow();
};

//定义类中的函数
//RegisterWindow()设计窗口
int CFrameWnd::RegisterWindow()
{
WNDCLASS wndclass;
wndclass.style = 0;
//wndclass.style = CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = lpszClassName;
//wndclass.hInstance = hInstance;

//注册窗口
return RegisterClass(&wndclass);
}

//创建窗口
void CFrameWnd::Create(LPCTSTR lpszClassName,LPCTSTR lpWindowName)
{
RegisterWindow();
hInst = hInstance;
hWnd = CreateWindow(lpszClassName,lpWindowName,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,
NULL,NULL,hInstance,NULL);
}

//显示窗口
void CFrameWnd::ShowWindow(int nCmdShow)
{
::ShowWindow(hWnd,nCmdShow);
}

//注册窗口
void CFrameWnd::UpdateWindow()
{
::UpdateWindow(hWnd);
}


//声明应用程序类
class CWinApp
{
public:
CFrameWnd* m_pMainWnd;
public:
BOOL InitInstance(int nCmdShow);
int Run();
~CWinApp();
};

//定义类中的函数
//InitInstance()函数
BOOL CWinApp::InitInstance(int nCmdShow)
{
m_pMainWnd = new CFrameWnd;
m_pMainWnd->Create(NULL,_T("封装的应用程序窗口"));
m_pMainWnd->ShowWindow(nCmdShow);
m_pMainWnd->UpdateWindow();

return TRUE;
}

//Return 函数
int CWinApp::Run()
{
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

//析构函数
CWinApp::~CWinApp()
{
delete m_pMainWnd;
}

//构造应用程序类对象
CWinApp theApp;//包括窗口的 设计,注册,创建,显示,更新 以及 消息的循环

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
int ResultCode;
theApp.InitInstance(nCmdShow);//窗口一系列的步骤

ResultCode = theApp.Run();//消息一系列的步骤
return ResultCode;
}

//窗口函数
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_LBUTTONDOWN:
OnLButtonDown(hWnd,message,wParam,lParam);
break;
case WM_PAINT:
OnPaint(hWnd,message,wParam,lParam);
break;
case WM_DESTROY:
OnDestory(hWnd,message,wParam,lParam);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}

//消息响应函数

//响应鼠标单机消息
void OnLButtonDown(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
ShowText=_T("面向对象的分装性");
InvalidateRect(hWnd,NULL,1);
}

//刷新消息
void OnPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hWnd,&ps);
TextOut(hdc,50,50,ShowText,16);
EndPaint(hWnd,&ps);
}

//关闭消息
void OnDestory(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PostQuitMessage(0);
}


[解决办法]
看那代码,那教程太烂了,估计那作者也没有验证过!

窗口类,相当于一个窗口模版,你要创建什么样的窗口,就要通过窗口类名称来指明;窗口运行的重要函数WndProc就是在窗口类中指定!

读书人网 >VC/MFC

热点推荐