設置 | 登錄 | 註冊

作者共發了3篇帖子。

【程序】一個最簡單的MFC窗口程序

1樓 巨大八爪鱼 2016-3-1 13:49
【代碼】
#include <afxwin.h>

class CMyWnd : public CFrameWnd
{
public:
    CMyWnd()
    {
        Create(NULL, TEXT("My First MFC Program"));
    }
};

class CMyApp : public CWinApp
{
    BOOL InitInstance()
    {
        m_pMainWnd = new CMyWnd;
        m_pMainWnd->ShowWindow(m_nCmdShow);
        return CWinApp::InitApplication();
    }
};

CMyApp theApp;

【運行效果】

2樓 巨大八爪鱼 2016-3-1 14:12
如果各位很討厭MFC類名開頭的那個C的話,可以用typedef強制去掉:

3樓 巨大八爪鱼 2016-3-1 15:22
【使用Win32默認的細邊框的窗口程序】
#define _WIN32_WINNT 0x0502
#include <afxwin.h>

// 消除MFC類名開頭的C
typedef CFrameWnd FrameWnd;
typedef CString String;
typedef CWinApp WinApp;

// 定義窗口類
class MyWnd : public FrameWnd
{
public:
    MyWnd()
    {
        Create(NULL, TEXT("My First MFC Program"));
    }

    DECLARE_MESSAGE_MAP()
    afx_msg void OnPaint();
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
};

// 定義程序類
class MyApp : public WinApp
{
    BOOL InitInstance()
    {
        m_pMainWnd = new MyWnd;
        m_pMainWnd->ShowWindow(m_nCmdShow);
        return WinApp::InitApplication();
    }
};

MyApp theApp;

BEGIN_MESSAGE_MAP(MyWnd, FrameWnd)
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

// 可以直接用Visual Studio添加消息映射 (切換到類視圖)
void MyWnd::OnPaint()
{
    CPaintDC dc(this); // device context for painting
    // TODO: Add your message handler code here
    // Do not call FrameWnd::OnPaint() for painting messages
    String str = "這是一段字符串";
    dc.TextOut(2, 2, str);
}


int MyWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (FrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    // TODO:  Add your specialized creation code here
    // 去除默認的粗邊框
    ModifyStyleEx(WS_EX_CLIENTEDGE, NULL);

    return 0;
}


void MyWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default
    MessageBox(TEXT("You have clicked me."));

    FrameWnd::OnLButtonDown(nFlags, point);
}
【運行效果】

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
©2010-2025 Purasbar Ver3.0 [手機版] [桌面版]
除非另有聲明,本站採用知識共享署名-相同方式共享 3.0 Unported許可協議進行許可。