首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

做个简单的windows程序,可是无法显示创建的窗口解决方法

2012-04-22 
做个简单的windows程序,可是无法显示创建的窗口我是在VS2008下运行的,下面是代码:C/C++ code#include win

做个简单的windows程序,可是无法显示创建的窗口
我是在VS2008下运行的,下面是代码:

C/C++ code
#include <windows.h>#include <stdio.h>const size_t namesize = 100;const TCHAR classname[namesize] = L"wenxing";LRESULT CALLBACK    WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){    switch(message)    {    case WM_CHAR:        wchar_t szChar[20];        //sprintf(szChar,"char is code %d",wparam);        MessageBox(hwnd,szChar,L"char",0);        break;    }    return 0;}int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,LPSTR lpCmdLine,int nShowCmd){    WNDCLASS wnd;    wnd.style = CS_HREDRAW | CS_VREDRAW;    wnd.lpfnWndProc = WndProc;    wnd.cbClsExtra = 0;    wnd.cbWndExtra = 0;    wnd.hInstance = hInstance;    wnd.hIcon = LoadIcon(NULL,IDI_ERROR);    wnd.hCursor = LoadCursor(NULL,IDC_ARROW);    wnd.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);    wnd.lpszMenuName = NULL;    wnd.lpszClassName = classname;        ATOM atom = RegisterClass(&wnd);        if(!atom)        MessageBox(NULL,L"1233",L"lqm",MB_OKCANCEL);    HWND hand = NULL;    hand = CreateWindow(classname,L"Hello",WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,hInstance,NULL);    ShowWindow(hand,SW_SHOWNORMAL);    if(!hand)    {        if(IDYES == MessageBox(NULL,L"创建失败!",L"lqm",MB_YESNO))            exit(0);    }        UpdateWindow(hand);}

我的问题是程序编译没有错误,看进程中也能查到这个程序在运行,可是就无法显示窗口,请高手指点一下。

[解决办法]
把窗口处理函数的返回值改为::DefWindowProc(hwnd,message,wparam,lparam);
CreateWindow会发送一个WM_CREATE消息给WndProc,如果你自己不处理,也不调用默认的函数来处理,窗口就创建失败了

一般消息都是调用::DefWindowProc(hwnd,message,wparam,lparam);来处理,而不是直接返回0


C/C++ code
LRESULT CALLBACK    WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){    switch(message)    {    case WM_CHAR:        wchar_t szChar[20];        //sprintf(szChar,"char is code %d",wparam);        MessageBox(hwnd,szChar,L"char",0);        break;    }    return ::DefWindowProc(hwnd,message,wparam,lparam);} 

热点排行