标题:初学MFC 下面这个程序窗口显示不出来,检查了好像没什么问题,求解。
只看楼主
qiqid
Rank: 2
等 级:论坛游民
帖 子:36
专家分:12
注 册:2013-3-31
结帖率:100%
 问题点数:0 回复次数:0 
初学MFC 下面这个程序窗口显示不出来,检查了好像没什么问题,求解。
//对消息处理函数进行了封装的windows程序

//包含头文件——————————————
#include<windows.h>

//定义全局变量————————————————
HINSTANCE hInstance;
MSG msg;
char *ShowText;
char lpClassName[]="class";
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

//CCmdTarget——————————
class CCmdTarget
{
public:
    HWND hWnd;
    //窗口函数
    LRESULT CALLBACK AfxWndProc(HWND hWnd,
        UINT message,
        WPARAM wParam,
        LPARAM lParam);
    virtual void OnLButtonDown();            //鼠标消息处理函数
    void OnDraw(HDC hdc);                    //重绘消息处理函数
    virtual void OnDestroy();                //WM_DESTROY消息处理函数
};

//CCmdTarget的成员函数————————————
LRESULT CALLBACK CCmdTarget::AfxWndProc(HWND hWnd,
                                        UINT message,
                                        WPARAM wParam,
                                        LPARAM lParam)
{
    switch(message)
    {
    case WM_LBUTTONDOWN:
        OnLButtonDown();
        break;
    case WM_PAINT:
        HDC hdc;
        PAINTSTRUCT ps;
        hdc=BeginPaint(hWnd,&ps);
        OnDraw(hdc);
        EndPaint(hWnd,&ps);
        break;
    case WM_DESTROY:
        OnDestroy();
        break;
    default:
        return DefWindowProc(hWnd,message,wParam,lParam);
    }

    return 0;
}

void CCmdTarget::OnLButtonDown()
{
}

void CCmdTarget::OnDraw(HDC hdc)
{
    TextOut(hdc,50,50,ShowText,16);
}

void CCmdTarget::OnDestroy()
{
    ::PostQuitMessage(0);
}

//窗口类————————————
class CFrameWnd:public CCmdTarget
{
public:
    HWND hWnd;
public:
    int RegisterWindow();
    void Create(LPCTSTR lpClassName,LPCTSTR lpWindowName);
    void ShowWindow(int nCmdShow);
    void UpdateWindow();
};

//窗口类的成员函数——————————————
int CFrameWnd::RegisterWindow()
{
    WNDCLASS wc;

    wc.cbClsExtra=0;
    wc.cbWndExtra=0;
    wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.hCursor=LoadCursor(NULL,IDC_ARROW);
    wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wc.hInstance=hInstance;
    wc.lpfnWndProc=WndProc;
    wc.lpszClassName=lpClassName;
    wc.lpszMenuName=ShowText;
    wc.style=0;

    return RegisterClass(&wc);
}

void CFrameWnd::Create(LPCTSTR lpClassName,LPCTSTR lpWindowName)
{
    RegisterWindow();

    hWnd=CreateWindow(lpClassName,
        lpWindowName,
        WS_OVERLAPPEDWINDOW,
        150,150,600,400,
        NULL,
        NULL,
        hInstance,
        NULL);
}

void CFrameWnd::ShowWindow(int nCmdShow)
{
    ::ShowWindow(hWnd,nCmdShow);
}

void CFrameWnd::UpdateWindow()
{
    ::UpdateWindow(hWnd);
}

//应用程序类——————————————
class CWinApp
{
public:
    CWinApp *m_pCurrentWinMain;
public:
    CWinApp();
public:
    CFrameWnd *m_pMainWnd;
public:
    virtual BOOL InitInstance(int nCmdShow);
    int Run();
};

CWinApp::CWinApp()
{
    m_pCurrentWinMain=this;
}

BOOL CWinApp::InitInstance(int nCmdShow)
{
    m_pMainWnd=new CFrameWnd;

    m_pMainWnd->Create(NULL,"一个程序");
    m_pMainWnd->ShowWindow(nCmdShow);
    m_pMainWnd->UpdateWindow();

    return TRUE;
}

int CWinApp::Run()
{
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

//派生的窗体类————————————————
class CMyWnd:public CFrameWnd
{
    void OnLButtonDown();
};

void CMyWnd::OnLButtonDown()
{
    ShowText="重新定义的消息处理";
    InvalidateRect(hWnd,NULL,1);
}

class CMyApp:public CWinApp                    //CWinApp类的派生类CMyApp
{
    BOOL InitInstance(int nCmdShow);
};

CMyApp::InitInstance(int nCmdShow)
{
    CMyWnd *pMainWnd=new CMyWnd;

    pMainWnd->Create(NULL,"另一个程序");
    pMainWnd->ShowWindow(nCmdShow);
    pMainWnd->UpdateWindow();

    return TRUE;
}

CMyApp MyApp;                //CWinApp的对象MyApp

CWinApp *AfxGetApp()
{
    return MyApp.m_pCurrentWinMain;
}

//主函数——————————————
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpCmdLine,
                     int nCmdShow)
{
    int ResultCode=-1;
   
    CWinApp *pApp;
    pApp=AfxGetApp();
    pApp->InitInstance(nCmdShow);

    return ResultCode=pApp->Run();
}

//窗口函数——————————————————
LRESULT CALLBACK WndProc(HWND hWnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam)
{
    CWinApp *pApp=AfxGetApp();
    return pApp->m_pMainWnd->AfxWndProc(hWnd,message,wParam,lParam);
}


搜索更多相关主题的帖子: windows message include public 
2013-08-07 16:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-418754-1-1.html




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

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