【使用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);
}
【運行效果】
