目前共有3篇帖子。 內容轉換:不轉換▼
 
點擊 回復
262 2
【程序】一个最简单的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);
}
【运行效果】

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:262 回複數:2
評論數: ?
作者: 巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2016-3-1 15:22
 
©2010-2024 Arslanbar Ver2.0
除非另有聲明,本站採用創用CC姓名標示-相同方式分享 3.0 Unported許可協議進行許可。