MFC初步教程(二)
绘制简单的图形几乎是所有的VC教程中的必须内容,这里面涉及到几个概念,设备(device context),画刷(Pen)等。CDC是所有设备的父类,其子类包括CClientDC(仅在客户区绘制), CPaintDC, CWindowDC(可以在客户区和窗口非客户区绘制)等。所有继承于CWnd类的子类均可以构造设备类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #include <afxwin.h> class MFC_Tutorial_Window: public CFrameWnd { CPoint m_startPoint; CPoint m_endPoint; public : MFC_Tutorial_Window() { Create(NULL, "MFC Tutorial" ); } void OnLButtonDown( UINT nFlags, CPoint point); /* { CFrameWnd::OnLButtonDown(nFlags,point); m_startPoint = point; }*/ void OnLButtonUp( UINT nFlags, CPoint point); /* { CFrameWnd::OnLButtonUp(nFlags, point); m_endPoint = point; CClientDC dc(this); dc.MoveTo(m_startPoint); dc.LineTo(m_endPoint); }*/ DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP( MFC_Tutorial_Window, CFrameWnd) ON_WM_LBUTTONDOWN() //Macro to map the left button click to the handler ON_WM_LBUTTONUP() //Macro to map the left button click to the handler END_MESSAGE_MAP() void MFC_Tutorial_Window::OnLButtonDown( UINT nFlags, CPoint point) { m_startPoint = point; CFrameWnd::OnLButtonDown(nFlags,point); } void MFC_Tutorial_Window::OnLButtonUp( UINT nFlags, CPoint point) { m_endPoint = point; CPen pen(PS_SOLID, 1, RGB(255,0,0)); CWindowDC dc( this ); // 构造dc时必须传入一个CWnd类或其子类对象 CPen* pOldPen = dc.SelectObject(&pen); dc.MoveTo(m_startPoint); dc.LineTo(m_endPoint); dc.SelectObject(pOldPen); CFrameWnd::OnLButtonUp(nFlags, point); } class MyApp: public CWinApp { MFC_Tutorial_Window *wnd; public : BOOL InitInstance() { wnd = new MFC_Tutorial_Window(); m_pMainWnd = wnd; m_pMainWnd->ShowWindow(1); return 1; } }; MyApp theApp; |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!