| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1023 人关注过本帖
标题:简单的窗口创建遇到问题
只看楼主 加入收藏
windyfzz
Rank: 2
等 级:禁止访问
帖 子:90
专家分:12
注 册:2010-7-25
结帖率:68%
收藏
已结贴  问题点数:20 回复次数:6 
简单的窗口创建遇到问题
#include<windows.h>
#include <stdio.h>
LRESULT CALLBACK LiProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
    );

class CWnd
{
public:
    CWnd()
    {
       hwnd=NULL;
    }

    BOOL CreateEx(DWORD dwExStyle,      // extended window style
                LPCTSTR lpClassName,  // registered class name
                LPCTSTR lpWindowName, // window name
                DWORD dwStyle,        // window style
                int x,                // horizontal position of window
                int y,                // vertical position of window
                int nWidth,           // window width
                int nHeight,          // window height
                HWND hWndParent,      // handle to parent or owner window
                HMENU hMenu,          // menu handle or child identifier
                HINSTANCE hInstance,  // handle to application instance
                LPVOID lpParam);        // window-creation data
    BOOL ShowWindow(int nCmdShow);
    BOOL UpdateWindow();
   
public:
    HWND  hwnd;
};

BOOL CWnd::CreateEx(DWORD dwExStyle,      // extended window style
                LPCTSTR lpClassName,  // registered class name
                LPCTSTR lpWindowName, // window name
                DWORD dwStyle,        // window style
                int x,                // horizontal position of window
                int y,                // vertical position of window
                int nWidth,           // window width
                int nHeight,          // window height
                HWND hWndParent,      // handle to parent or owner window
                HMENU hMenu,          // menu handle or child identifier
                HINSTANCE hInstance,  // handle to application instance
                LPVOID lpParam)        // window-creation data
{
     hwnd=::CreateWindowEx(dwExStyle,lpClassName,lpWindowName,dwStyle,x,y,
                    nWidth,nHeight,hWndParent,hMenu,hInstance,lpParam);
    if( hwnd!=NULL)
        return TRUE;
    else
        return FALSE;
}

BOOL CWnd::ShowWindow(int nCmdShow)
{
    return ::ShowWindow( hwnd,nCmdShow);
}

BOOL CWnd::UpdateWindow()
{
    return ::UpdateWindow(hwnd);
}




int WINAPI WinMain(
  HINSTANCE hInstance,      // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,          // command line
  int nCmdShow              // show state
)
{
    WNDCLASS wndclass;
    wndclass.cbClsExtra=0;
    wndclass.cbWndExtra=0;
    wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    wndclass.hCursor=LoadCursor(NULL,IDC_CROSS);
    wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
    wndclass.hInstance=hInstance;
    wndclass.lpfnWndProc=LiProc;
    wndclass.lpszClassName="exe";
    wndclass.lpszMenuName=NULL;
    wndclass.style=CS_HREDRAW|CS_VREDRAW;
    RegisterClass(&wndclass);

    CWnd wnd;
    wnd.CreateEx(0,"exe","简单的窗口创建",WS_OVERLAPPEDWINDOW,100,100,600,500,NULL,NULL,hInstance,
                        NULL);
    wnd.ShowWindow(SW_SHOWNORMAL);
    wnd.UpdateWindow();

    MSG msg;
    while(GetMessage(&msg, hwnd,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

LRESULT CALLBACK LiProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
    )
    {   
        switch(uMsg)
        {
        case  WM_CHAR:
            char str[20];
            sprintf(str, " char is%d",wParam);
            
            MessageBox(hwnd,str,"消息盒子",0);
            break;
        case  WM_LBUTTONDOWN:
            MessageBox(hwnd,"mouse clicked","消息盒子",0);
            HDC hDc;
            hDc=GetDC( hwnd);
            TextOut(hDc,0,100,"Hello word, hello every people!",strlen("Hello word, hello every people!"));
            ReleaseDC( hwnd,hDc);  
            break;
   
        case  WM_CLOSE:
            if(IDYES==MessageBox( hwnd,"确定退出?","消息盒子",MB_YESNO))
            {
                DestroyWindow( hwnd);
            }
            break;
        case  WM_PAINT:
            PAINTSTRUCT pt;
            HDC hdc;
            hdc=BeginPaint( hwnd,&pt);
            TextOut(hdc,0,50,"Hello word!",strlen("Hello word!"));
            EndPaint(hwnd,&pt);
            break;
        case  WM_DESTROY:
            PostQuitMessage(0);  
            break;
        default:
            return DefWindowProc( hwnd,uMsg,wParam,lParam);
            break;
        }
       return 0;   
    }

出现错误 error C2065: 'hwnd' : undeclared identifier
         fatal error C1903: unable to recover from previous error(s); stopping compilation

为什么会这样啊(刚学MFC)
搜索更多相关主题的帖子: 窗口 
2010-08-05 15:20
东海一鱼
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:48
帖 子:757
专家分:4760
注 册:2009-8-10
收藏
得分:10 
  CWnd wnd;
    wnd.CreateEx(0,"exe","简单的窗口创建",WS_OVERLAPPEDWINDOW,100,100,600,500,NULL,NULL,hInstance,
                        NULL);
    wnd.ShowWindow(SW_SHOWNORMAL);
    wnd.UpdateWindow();

    MSG msg;
    while(GetMessage(&msg, wnd.hwnd,0,0))   //填0更好
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;

举世而誉之而不加劝,举世而非之而不加沮,定乎内外之分,辩乎荣辱之境,斯已矣。彼其于世未数数然也。
2010-08-05 22:48
windyfzz
Rank: 2
等 级:禁止访问
帖 子:90
专家分:12
注 册:2010-7-25
收藏
得分:0 
回复 2楼 东海一鱼
这样该可以了,谢谢你。

能解释下为什么 m_hWnd前要加wnd.限定呢? m_hWnd不是全局的吗?
2010-08-06 09:41
东海一鱼
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:48
帖 子:757
专家分:4760
注 册:2009-8-10
收藏
得分:10 
不是啊,hWnd是你定义的CWnd的类成员变量。
class CWnd
{
public:
    CWnd()
    {
       hwnd=NULL;
    }

    BOOL CreateEx(DWORD dwExStyle,      // extended window style
                LPCTSTR lpClassName,  // registered class name
                LPCTSTR lpWindowName, // window name
                DWORD dwStyle,        // window style
                int x,                // horizontal position of window
                int y,                // vertical position of window
                int nWidth,           // window width
                int nHeight,          // window height
                HWND hWndParent,      // handle to parent or owner window
                HMENU hMenu,          // menu handle or child identifier
                HINSTANCE hInstance,  // handle to application instance
                LPVOID lpParam);        // window-creation data
    BOOL ShowWindow(int nCmdShow);
    BOOL UpdateWindow();
   
public:
    HWND  hwnd;
};

举世而誉之而不加劝,举世而非之而不加沮,定乎内外之分,辩乎荣辱之境,斯已矣。彼其于世未数数然也。
2010-08-06 19:59
windyfzz
Rank: 2
等 级:禁止访问
帖 子:90
专家分:12
注 册:2010-7-25
收藏
得分:0 
回复 4楼 东海一鱼
谢谢,我懂了。是我有点晕了,呵呵。
2010-08-06 21:37
x88484532
Rank: 2
等 级:论坛游民
帖 子:34
专家分:30
注 册:2010-8-5
收藏
得分:0 
不错,学习了
2010-08-09 15:34
ylr42315
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-8-16
收藏
得分:0 
菜中菜回复都不知道说些什么?
2010-08-17 11:06
快速回复:简单的窗口创建遇到问题
数据加载中...
 
   



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

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