| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1023 人关注过本帖
标题:简单的窗口创建遇到问题
取消只看楼主 加入收藏
windyfzz
Rank: 2
等 级:禁止访问
帖 子:90
专家分:12
注 册:2010-7-25
结帖率:68%
收藏
已结贴  问题点数:20 回复次数:2 
简单的窗口创建遇到问题
#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
windyfzz
Rank: 2
等 级:禁止访问
帖 子:90
专家分:12
注 册:2010-7-25
收藏
得分:0 
回复 2楼 东海一鱼
这样该可以了,谢谢你。

能解释下为什么 m_hWnd前要加wnd.限定呢? m_hWnd不是全局的吗?
2010-08-06 09:41
windyfzz
Rank: 2
等 级:禁止访问
帖 子:90
专家分:12
注 册:2010-7-25
收藏
得分:0 
回复 4楼 东海一鱼
谢谢,我懂了。是我有点晕了,呵呵。
2010-08-06 21:37
快速回复:简单的窗口创建遇到问题
数据加载中...
 
   



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

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