为什么没有人从单源文件学MFC
mfc吧
全部回复
仅看楼主
level 2
就像学VB一样快速入门
2026年01月30日 04点01分 1
吧务
level 12
给个例子[阴险]
2026年02月03日 03点02分 2
level 2
// MFC_ClickAndDraw_ToolbarFixed.cpp
#include <afxwin.h>
#include <afxext.h>
#include <afxres.h>
#include <tchar.h>
class CMainWindow : public CFrameWnd {
public:
CMainWindow() {
Create(NULL, _T("点击绘图示例 - 工具栏图标持久化"));
// === 菜单 ===
m_menuBar.CreateMenu();
m_fileMenu.CreatePopupMenu();
m_fileMenu.AppendMenu(MF_STRING, ID_APP_EXIT, _T("退出(&X)"));
m_menuBar.AppendMenu(MF_POPUP, (UINT_PTR)m_fileMenu.m_hMenu, _T("&文件"));
SetMenu(&m_menuBar);
// === 工具栏 ===
if (!m_toolBar.Create(this)) return;
UINT buttons[] = { ID_APP_EXIT };
m_toolBar.SetButtons(buttons, 1);
// ✅ 关键修复:位图作为成员变量持久保存(不使用局部变量)
CreateChineseBitmap(m_toolbarBitmap, _T("退"));
m_toolBar.SetBitmap((HBITMAP)m_toolbarBitmap); // 不 Detach!
RecalcLayout();
EnableDocking(CBRS_ALIGN_ANY);
m_toolBar.EnableDocking(CBRS_ALIGN_TOP);
DockControlBar(&m_toolBar);
ModifyStyle(0, WS_CLIPCHILDREN);
}
private:
// === 成员变量 ===
CMenu m_menuBar;
CMenu m_fileMenu;
CToolBar m_toolBar;
CArray<CPoint, CPoint&> m_clickPoints;
CBitmap m_toolbarBitmap; // ← 持久保存工具栏位图
// === 创建汉字位图(支持中文)===
void CreateChineseBitmap(CBitmap& bmp, LPCTSTR pszChar) {
CDC* pDC = GetDC();
if (!pDC) return;
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
bmp.CreateCompatibleBitmap(pDC, 16, 16);
CBitmap* pOldBmp = dcMem.SelectObject(&bmp);
// 填充背景(工具栏默认色)
dcMem.FillSolidRect(0, 0, 16, 16, ::GetSysColor(COLOR_BTNFACE));
dcMem.SetTextColor(RGB(0, 0, 0));
dcMem.SetBkMode(TRANSPARENT);
// 使用系统字体(兼容 VS2012 + 中文)
NONCLIENTMETRICS ncm = { sizeof(NONCLIENTMETRICS) };
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, 0, &ncm, 0);
CFont font;
font.CreateFontIndirect(&ncm.lfMenuFont);
CFont* pOldFont = dcMem.SelectObject(&font);
CRect rect(0, 0, 16, 16);
dcMem.DrawText(pszChar, -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
dcMem.SelectObject(pOldFont);
dcMem.SelectObject(pOldBmp);
ReleaseDC(pDC);
}
// === 绘制所有点击点 ===
void DrawPoints(CDC* pDC) {
if (m_clickPoints.GetSize() == 0) return;
CPen pen(PS_SOLID, 1, RGB(0, 0, 255)); // 蓝色边框
CBrush brush(RGB(255, 255, 255)); // 白色填充
CPen* pOldPen = pDC->SelectObject(&pen);
CBrush* pOldBrush = pDC->SelectObject(&brush);
for (int i = 0; i < m_clickPoints.GetSize(); ++i) {
CPoint pt = m_clickPoints[i];
pDC->Ellipse(pt.x - 4, pt.y - 4, pt.x + 4, pt.y + 4);
}
pDC->SelectObject(pOldPen);
pDC->SelectObject(pOldBrush);
}
// === 消息处理函数 ===
afx_msg void OnPaint() {
CPaintDC dc(this);
DrawPoints(&dc);
}
afx_msg void OnSize(UINT nType, int cx, int cy) {
CFrameWnd::OnSize(nType, cx, cy); // ← 确保工具栏重新布局
}
afx_msg void OnMouseMove(UINT nFlags, CPoint point) {
CString str;
str.Format(_T("移动: (%d, %d) | 共 %d 点"), point.x, point.y, m_clickPoints.GetSize());
SetWindowText(str);
}
afx_msg void OnLButtonDown(UINT nFlags, CPoint point) {
SetFocus();
}
afx_msg void OnLButtonUp(UINT nFlags, CPoint point) {
m_clickPoints.Add(point);
Invalidate(); // 触发 OnPaint,重绘客户区(不影响工具栏)
}
afx_msg void OnFileExit() {
PostMessage(WM_CLOSE);
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMainWindow, CFrameWnd)
ON_COMMAND(ID_APP_EXIT, &CMainWindow::OnFileExit)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_PAINT()
ON_WM_SIZE() // ← 处理窗口大小变化
END_MESSAGE_MAP()
// === 应用程序类 ===
class CMyApp : public CWinApp {
public:
virtual BOOL InitInstance() override {
InitCommonControls(); // 初始化通用控件(含工具栏)
m_pMainWnd = new CMainWindow();
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
};
CMyApp theApp;
2026年02月03日 12点02分 3
level 2
新建win32空文件添加上帖源代码,启用mfc库完了
2026年02月03日 13点02分 4
1