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

c++创建窗口 . 第一个窗口就卡住了 ,

2012-09-16 
c++创建窗口 .. 第一个窗口就卡住了 ,求助啊#include Windows.h#include stdio.hLRESULT CALLBACK Mai

c++创建窗口 .. 第一个窗口就卡住了 ,求助啊
#include <Windows.h>
#include <stdio.h>


LRESULT CALLBACK MainProc(
HWND hwnd , //handle to window
UINT uMsg , //message indentfier
WPARAM wParam , // message parameter
LPARAM lParam 
)  
{  
switch(uMsg)
{
case WM_CHAR:{
char st[20] ;
sprintf_s(st,"char is %d",wParam);
MessageBox(NULL ,st,"呵呵",0);
break;
//return 0;
}
case WM_LBUTTONDOWN:{
char wc[20] = "我勒个擦";
HDC hdc = GetDC(hwnd);
TextOut(hdc,100,300,wc,strlen(wc));
ReleaseDC(hwnd,hdc);
break;
//return 0;
}
case WM_PAINT:{
PAINTSTRUCT pt;
HDC hdcPaint = BeginPaint(hwnd,&pt); // pair;
TextOut(hdcPaint,60,60,"我勒个去啊",strlen("我勒个去啊"));
EndPaint(hwnd,&pt);//pair 
break;
//return 0;
}
case WM_CLOSE:{
if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
//return 0;
}
case WM_DESTROY:{
PostQuitMessage(0);
break;
}
default:{
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
}
return 0;
}

int WINAPI WinMain(
HINSTANCE mainInstance , //handle current instance
HINSTANCE hPreviousInstance , // handle previous instance
LPSTR lpCmdLine , // command line
int nCmdShow // show state
)
{
WNDCLASS w;
w.cbClsExtra = 0;
w.cbWndExtra = 0;
w.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
w.hInstance = mainInstance ;
w.hCursor = LoadCursor(NULL, IDC_ARROW);
w.hIcon = LoadIcon(NULL, IDI_APPLICATION );
w.lpfnWndProc = MainProc ;
w.lpszClassName = " First window " ;
w.lpszMenuName = "";
w.style = CS_VREDRAW | CS_HREDRAW ;

if (!RegisterClass(&w)){ //注册窗口类 .
MessageBox(NULL, TEXT("Error !"),"test",MB_ICONERROR) ;
return 0;
}



//创建窗口
HWND hMyWindow;
hMyWindow = CreateWindow(  
" Oh myWindow " , // lpClassName
" window name " , // lpWindowName
WS_OVERLAPPEDWINDOW , // window style
50 , // x
50 ,// y
600 ,// w
800 ,// h
NULL ,// parent window
NULL ,// menu 
mainInstance ,// handle current instance
NULL// lpParam may be NULL if no additional data is needed. 
);

//显示窗口
ShowWindow(hMyWindow,SW_SHOWNORMAL); 

//更新窗口

UpdateWindow(hMyWindow);

// 消息循环

MSG msg ;
while (GetMessage(&msg ,NULL , 0 ,0 )){

TranslateMessage(&msg);//转换信息
DispatchMessage(&msg);//发送信息

}
return 0;
}


就是这个代码了 , 可以产生进程 , 但是窗口就是不出来 ...

看了好久了都不知道哪不对 , 编译在 vs2010下 ... 看书上代码敲的 , 请各位帮忙看看 , 谢谢 !!

[解决办法]
CreateWindow的第一个参数要和注册时候的w.lpszClassName 一致
把CreateWindow的第一个参数改成"First window"
[解决办法]
使用的窗口类没有注册,可以参考楼上的修改
[解决办法]
1.设计窗口类
2.注册窗口类
3.创建窗口
4.显示及更新窗口
[解决办法]

C/C++ code
#include <Windows.h>#include <stdio.h>LRESULT CALLBACK MainProc(                          HWND hwnd , //handle to window                          UINT uMsg , //message indentfier                          WPARAM wParam , // message parameter                          LPARAM lParam                            )   {       switch(uMsg)    {    case WM_CHAR:{        char st[20] ;        sprintf(st,"char is %d",wParam);        MessageBox(NULL ,st,"呵呵",0);        break;        //return 0;                 }    case WM_LBUTTONDOWN:{        char wc[20] = "我勒个擦";        HDC hdc = GetDC(hwnd);        TextOut(hdc,100,300,wc,strlen(wc));        ReleaseDC(hwnd,hdc);        break;        //return 0;                        }    case WM_PAINT:{        PAINTSTRUCT pt;        HDC hdcPaint = BeginPaint(hwnd,&pt); // pair;        TextOut(hdcPaint,60,60,"我勒个去啊",strlen("我勒个去啊"));        EndPaint(hwnd,&pt); //pair          break;        //return 0;                  }    case WM_CLOSE:{        if(IDYES==MessageBox(hwnd,"是否真的结束?","weixin",MB_YESNO))        {            DestroyWindow(hwnd);        }        break;        //return 0;                  }    case WM_DESTROY:{        PostQuitMessage(0);        break;                    }    default:{        return DefWindowProc(hwnd,uMsg,wParam,lParam);            }    }    return 0;}int WINAPI WinMain(                   HINSTANCE mainInstance , //handle current instance                   HINSTANCE hPreviousInstance , // handle previous instance                   LPSTR lpCmdLine , // command line                   int nCmdShow // show state                   ){    WNDCLASS w;    w.cbClsExtra = 0;    w.cbWndExtra = 0;    w.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);    w.hInstance = mainInstance ;    w.hCursor = LoadCursor(NULL, IDC_ARROW);    w.hIcon = LoadIcon(NULL, IDI_APPLICATION );    w.lpfnWndProc = MainProc ;    w.lpszClassName = " First window " ;    w.lpszMenuName = "";    w.style = CS_VREDRAW | CS_HREDRAW ;        if (!RegisterClass(&w)){ //注册窗口类 .        MessageBox(NULL, TEXT("Error !"),"test",MB_ICONERROR) ;        return 0;    }                //创建窗口    HWND hMyWindow;    hMyWindow = CreateWindow(           " First window " , // lpClassName        " window name " , // lpWindowName        WS_OVERLAPPEDWINDOW , // window style        50 , // x        50 , // y        600 , // w        800 , // h        NULL , // parent window        NULL , // menu          mainInstance , // handle current instance        NULL // lpParam may be NULL if no additional data is needed.          );        //显示窗口    ShowWindow(hMyWindow,SW_SHOWNORMAL);          //更新窗口        UpdateWindow(hMyWindow);        // 消息循环        MSG msg ;    while (GetMessage(&msg ,NULL , 0 ,0 )){                TranslateMessage(&msg); //转换信息        DispatchMessage(&msg); //发送信息            }    return 0;} 

热点排行