注册 登录
编程论坛 C++教室

刚学VC++编写了一个窗口,但是关闭按钮没用,求各位大神帮忙,问题关键在WM_CLOSE消息不起作用

fua_fua 发布于 2015-01-05 19:43, 516 次点击
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinSunProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
    );

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{

    WNDCLASS wndcls;
    MSG msg;
    HWND hwnd;
        wndcls.cbClsExtra=0;
        wndcls.cbWndExtra=0;
        wndcls.hbrBackground=(HBRUSH)GetStockObject(DKGRAY_BRUSH);
        wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
        wndcls.hIcon=LoadIcon(NULL,IDI_ERROR);
        wndcls.hInstance=hInstance;
        wndcls.lpfnWndProc=WinSunProc;
        wndcls.lpszClassName="SUNXIN2006";
        wndcls.lpszMenuName=NULL;
        wndcls.style=CS_HREDRAW | CS_VREDRAW;

        RegisterClass(&wndcls);

        hwnd=CreateWindow("SUNXIN2006","hello",
            WS_OVERLAPPEDWINDOW,0,0,600,400,NULL,NULL,hInstance,NULL);

        ShowWindow(hwnd,SW_SHOWNORMAL);
        UpdateWindow(hwnd);

        while(GetMessage(&msg,NULL,0,0));
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }        
        return msg.wParam;

}
LRESULT CALLBACK WinSunProc(
        HWND hwnd,
        UINT uMsg,
        WPARAM wParam,
        LPARAM lParam
        )
{
    switch(uMsg)
    {
    case WM_CHAR:
        char szchar[20];
        sprintf(szchar,"char code is %d",wParam);
        MessageBox(hwnd,szchar,"char",0);
        break;

    case WM_LBUTTONDOWN:
        MessageBox(hwnd,"mouse click!","message",0);
        HDC hdc;
        hdc=GetDC(hwnd);
        TextOut(hdc,0,50,"你是我的眼",strlen("你是我的眼"));
        ReleaseDC(hwnd,hdc);
        break;

    case WM_PAINT:
        HDC hDC;
        PAINTSTRUCT ps;
        hDC=BeginPaint(hwnd,&ps);
        TextOut(hDC,0,80,"我是周方武",strlen("我是周方武"));
        EndPaint(hwnd,&ps);
        break;

    case WM_CLOSE:
        DestroyWindow(hwnd);
        
        break;

    //    if (IDYES==MessageBox(hwnd,"是否真的要关闭程序?","message",MB_YESNO))
    //    {
        
    //    }
   

    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
}
3 回复
#2
fua_fua2015-01-05 19:53
可以复制代码到vc++当中,运行程序的时候可以观察现象,关闭不了窗口!
#3
rjsp2015-01-06 09:09
第一,我建议你找本正儿八经的书看看(不是那些傻屄人瞎编的傻屄书)
比如 CreateWindow("SUNXIN2006"
正儿八经的书要么写 CreateWindowA("SUNXIN2006"
要么写 CreateWindowW(L"SUNXIN2006"
要么写 CreateWindow( TEXT("SUNXIN2006")
其它更随意的错误不列举了

第二,也就是你想知道的错误所在
while(GetMessage(&msg,NULL,0,0)); // 这后面的分号,自己找,别人很难在你一大坨代码中找出如此细微的,在别人手中不会出现的错误。
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}   
#4
fua_fua2015-01-07 10:10
回复 3楼 rjsp
真心的谢谢你!
1