源代码
// MainFrm.cpp : implementation of the CMainFrame class // #include "stdafx.h" #include "MFCDraw.h" #include "MainFrm.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMainFrame IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) //{{AFX_MSG_MAP(CMainFrame) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code ! ON_WM_CREATE() //}}AFX_MSG_MAP END_MESSAGE_MAP() static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, }; ///////////////////////////////////////////////////////////////////////////// // CMainFrame construction/destruction CMainFrame::CMainFrame() { // TODO: add member initialization code here } CMainFrame::~CMainFrame() { } int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } // TODO: Delete these three lines if you don't want the toolbar to // be dockable m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar); return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMainFrame diagnostics #ifdef _DEBUG void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); } void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers
// MFCDraw.cpp : Defines the class behaviors for the application. // #include "stdafx.h" #include "MFCDraw.h" #include "MainFrm.h" #include "MFCDrawDoc.h" #include "MFCDrawView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMFCDrawApp BEGIN_MESSAGE_MAP(CMFCDrawApp, CWinApp) //{{AFX_MSG_MAP(CMFCDrawApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP // Standard file based document commands ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) // Standard print setup command ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMFCDrawApp construction CMFCDrawApp::CMFCDrawApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CMFCDrawApp object CMFCDrawApp theApp; ///////////////////////////////////////////////////////////////////////////// // CMFCDrawApp initialization BOOL CMFCDrawApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CMFCDrawDoc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CMFCDrawView)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; // The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) afx_msg void OnDcCpen(); afx_msg void OnDcGdibrush(); afx_msg void OnDcGdifont(); afx_msg void OnDcGdipen(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // ON_COMMAND(ID_DC_CPEN, OnDcCpen) // ON_COMMAND(ID_DC_GDIBRUSH, OnDcGdibrush) // ON_COMMAND(ID_DC_GDIFONT, OnDcGdifont) // ON_COMMAND(ID_DC_GDIPEN, OnDcGdipen) //}}AFX_MSG_MAP END_MESSAGE_MAP() // App command to run the dialog void CMFCDrawApp::OnAppAbout() { CAboutDlg aboutDlg; aboutDlg.DoModal(); } ///////////////////////////////////////////////////////////////////////////// // CMFCDrawApp message handlers void CAboutDlg::OnDcCpen() { // TODO: Add your command handler code here } void CAboutDlg::OnDcGdibrush() { // TODO: Add your command handler code here } void CAboutDlg::OnDcGdifont() { // TODO: Add your command handler code here } void CAboutDlg::OnDcGdipen() { // TODO: Add your command handler code here }
// MFCDrawDoc.cpp : implementation of the CMFCDrawDoc class // #include "stdafx.h" #include "MFCDraw.h" #include "MFCDrawDoc.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMFCDrawDoc IMPLEMENT_DYNCREATE(CMFCDrawDoc, CDocument) BEGIN_MESSAGE_MAP(CMFCDrawDoc, CDocument) //{{AFX_MSG_MAP(CMFCDrawDoc) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMFCDrawDoc construction/destruction CMFCDrawDoc::CMFCDrawDoc() { // TODO: add one-time construction code here } CMFCDrawDoc::~CMFCDrawDoc() { } BOOL CMFCDrawDoc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; // TODO: add reinitialization code here // (SDI documents will reuse this document) return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CMFCDrawDoc serialization void CMFCDrawDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here } } ///////////////////////////////////////////////////////////////////////////// // CMFCDrawDoc diagnostics #ifdef _DEBUG void CMFCDrawDoc::AssertValid() const { CDocument::AssertValid(); } void CMFCDrawDoc::Dump(CDumpContext& dc) const { CDocument::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMFCDrawDoc commands
// MFCDrawView.cpp : implementation of the CMFCDrawView class // #include "stdafx.h" #include "MFCDraw.h" #include "MFCDrawDoc.h" #include "MFCDrawView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CMFCDrawView IMPLEMENT_DYNCREATE(CMFCDrawView, CView) BEGIN_MESSAGE_MAP(CMFCDrawView, CView) //{{AFX_MSG_MAP(CMFCDrawView) ON_COMMAND(ID_DC_CPEN, OnDcCpen) ON_COMMAND(ID_DC_GDIBRUSH, OnDcGdibrush) ON_COMMAND(ID_DC_GDIFONT, OnDcGdifont) ON_COMMAND(ID_DC_GDIPEN, OnDcGdipen) ON_COMMAND(ID_GDI_BMP, OnGdiBmp) ON_COMMAND(ID_GDI_RGN, OnGdiRgn) ON_COMMAND(ID_GDI_LINE, OnGdiLine) ON_COMMAND(ID_GDI_ELLIPSE, OnGdiEllipse) ON_COMMAND(ID_GDI_RECT, OnGdiRect) ON_WM_LBUTTONUP() ON_WM_LBUTTONDOWN() //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMFCDrawView construction/destruction CMFCDrawView::CMFCDrawView() { // TODO: add construction code here // m_ptBegin; m_nDrawType=0; } CMFCDrawView::~CMFCDrawView() { } BOOL CMFCDrawView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CMFCDrawView drawing void CMFCDrawView::OnDraw(CDC* pDC) { CMFCDrawDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here } ///////////////////////////////////////////////////////////////////////////// // CMFCDrawView printing BOOL CMFCDrawView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMFCDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMFCDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMFCDrawView diagnostics #ifdef _DEBUG void CMFCDrawView::AssertValid() const { CView::AssertValid(); } void CMFCDrawView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMFCDrawDoc* CMFCDrawView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCDrawDoc))); return (CMFCDrawDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMFCDrawView message handlers void CMFCDrawView::OnDcCpen() { // TODO: Add your command handler code here CClientDC dc(this); CPen pen(PS_SOLID,5,RGB(255,0,0)); CPen *pOldPen=dc.SelectObject(&pen); dc.Rectangle(50,50,150,200); dc.SelectObject(pOldPen); pen.DeleteObject(); } void CMFCDrawView::OnDcGdibrush() { // TODO: Add your command handler code here CClientDC dc(this); CBitmap bitMap; bitMap.LoadBitmap(IDB_BITMAP1); // CBrush brush(HS_BDIAGONAL,RGB(0,255,0)); CBrush brush(&bitMap); CBrush *pOldBrush=dc.SelectObject(&brush); RECT rect={100,100,250,200}; dc.FillRect(&rect,&brush); dc.SelectObject(&pOldBrush); brush.DeleteObject(); } void CMFCDrawView::OnDcGdifont() { // TODO: Add your command handler code here CClientDC dc(this); CFont font; font.CreatePointFont(500,"黑体"); CFont *pOldFont=dc.SelectObject(&font); dc.TextOut(100,100,"hello CFont"); dc.SelectObject(pOldFont); font.DeleteObject(); } void CMFCDrawView::OnDcGdipen() { // TODO: Add your command handler code here } void CMFCDrawView::OnGdiBmp() { // TODO: Add your command handler code here CClientDC dc(this); //无法直接把位图选入dc,位图大小不固定。 CBitmap bmp; bmp.LoadBitmap(IDB_BITMAP2); //创建兼容dc CDC dcBmp; dcBmp.CreateCompatibleDC(&dc); //将位图选入兼容dc CBitmap*pOldBmp=dcBmp.SelectObject(&bmp); //将位图从兼容dc向当前dc拷贝 /* dc.BitBlt( 0,0, 253,200, &dcBmp, 0,0, SRCCOPY ); */ CRect rcClient; GetClientRect(&rcClient); dc.StretchBlt( 0,0, rcClient.Width(),rcClient.Height(), &dcBmp, 0,0, 253,200, SRCCOPY ); dcBmp.SelectObject(pOldBmp); dcBmp.DeleteDC(); bmp.DeleteObject(); } void CMFCDrawView::OnGdiRgn() { // TODO: Add your command handler code here CClientDC dc(this); CBrush brush(RGB(255,0,0)); CBrush* pOldBrush=dc.SelectObject(&brush); CRgn rgn1,rgn2,rgn3; rgn1.CreateRectRgn(100,100,300,200); rgn2.CreateEllipticRgn(150,150,300,250); rgn2.CombineRgn(&rgn1,&rgn2,RGN_DIFF); dc.FillRgn(&rgn2,&brush); dc.SelectObject(pOldBrush); brush.DeleteObject(); AfxGetMainWnd()->SetWindowRgn(rgn1,TRUE); //制作不规则窗口 按钮 //CWnd::SetWindowRgn } void CMFCDrawView::OnGdiLine() { // TODO: Add your command handler code here m_nDrawType=0; m_ptBegin=0; m_ptEnd=0; } void CMFCDrawView::OnGdiEllipse() { // TODO: Add your command handler code here m_nDrawType=2; } void CMFCDrawView::OnGdiRect() { // TODO: Add your command handler code here m_nDrawType=1; } void CMFCDrawView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default m_ptEnd=point; CClientDC dc(this); switch(m_nDrawType){ case 0:{ dc.MoveTo(m_ptBegin.x,m_ptBegin.y); dc.LineTo(m_ptEnd.x,m_ptEnd.y); break; } case 1:{ dc.Rectangle(m_ptBegin.x,m_ptBegin.y,m_ptEnd.x, m_ptEnd.y); break; } case 2:{ dc.Ellipse(m_ptBegin.x,m_ptBegin.y,m_ptEnd.x, m_ptEnd.y); break; } } CView::OnLButtonUp(nFlags, point); } void CMFCDrawView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default m_ptBegin=point; CView::OnLButtonDown(nFlags, point); }
原文地址:http://blog.csdn.net/u011185633/article/details/44907241