| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 595 人关注过本帖
标题:半抄半写了个简单windows程序,出现个问题
只看楼主 加入收藏
Dam3000
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2007-6-5
收藏
 问题点数:0 回复次数:1 
半抄半写了个简单windows程序,出现个问题
我希望在按下ESC键后程序退出,但是窗口销毁了,进程中却仍然能看到程序占用内存。请大家告诉我哪部分资源没有释放?

代码如下:


#include <windows.h>
#include <windowsx.h>

LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){
    HWND  hwnd;
    WNDCLASSEX wc;
    MSG msg;

    memset(&wc,0,sizeof(wc));

    wc.cbSize=sizeof(wc);
    wc.style=CS_HREDRAW|CS_VREDRAW;

    wc.lpfnWndProc = WindowProc;//指向消息处理函数的指针

    wc.cbClsExtra=0; wc.cbWndExtra=0;
    wc.hInstance=hInstance;
    wc.hIcon = LoadIcon( hInstance,"IDI_APP_ICON" );
    wc.hIconSm= LoadIcon(hInstance,"IDI_APP_ICON");
    wc.hCursor= LoadCursor(NULL,IDC_ARROW);
    wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszClassName="SpinStudioMainWindow";
    wc.lpszMenuName=NULL;

    RegisterClassEx(&wc);

    hwnd= CreateWindowEx(0,"SpinStudioMainWindow","MyWindow",WS_POPUP,0,0,640,480,NULL,NULL,hInstance,NULL);
    if(!hwnd){
        ::MessageBox(NULL,"窗口创建失败","MyWindow",MB_ICONSTOP);
        return FALSE;
    }

    ShowWindow(hwnd,nShowCmd);
    UpdateWindow(hwnd);

    while(TRUE){
        if(GetMessage(&msg,NULL,0,0)){
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return TRUE;
}

LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam){
    switch(uMsg){
        case WM_PAINT:
            {
                PAINTSTRUCT ps;
                BeginPaint(hwnd,&ps);
                char buf[]="Hello World!!";
                TextOut(ps.hdc,0,0,buf,strlen(buf));
                EndPaint(hwnd,&ps);
            }
            return(1);
        case WM_KEYDOWN: //击键消息
            switch ( wParam ){
                    case VK_ESCAPE:
                        PostMessage ( hwnd, WM_CLOSE, 0, 0 ) ;
                        break ;
            }
            break ;
        case WM_CLOSE:{
            MessageBox(NULL,"Goodbey!!","MyWindow",MB_DEFBUTTON1);
            DestroyWindow(hwnd);
            break;
                      }
            
        case WM_DESTROY:{
            PostQuitMessage(0);
            break;
                        }
    }
    return DefWindowProc ( hwnd, uMsg, wParam, lParam ) ;
}
搜索更多相关主题的帖子: windows 
2008-12-13 23:25
megadeath
Rank: 4
等 级:业余侠客
帖 子:24
专家分:260
注 册:2008-8-29
收藏
得分:0 
资源释放没有问题,问题在消息循环函数上,while循环中没有退出。

修改如下:
    while(TRUE){
        if(GetMessage(&msg,NULL,0,0)){
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
            return TRUE;
    }
2008-12-14 20:34
快速回复:半抄半写了个简单windows程序,出现个问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.056943 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved