VC++画图总结 // sunxinLab1View.h : CsunxinLab1View 类的接口 // #pragma once class CsunxinLab1View : public CView { protected: // 仅从序列化创建 CsunxinLab1View(); DECLARE_DYNCREATE(CsunxinLab1View) // 属性 public: CsunxinLab1Doc* GetDocument() const; // 操作 public: CPoint m_point; bool IsDraw ; // 重写 public: virtual void OnDraw(CDC* pDC); // 重写以绘制该视图 virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: virtual BOOL OnPreparePrinting(CPrintInfo* pInfo); virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo); virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo); // 实现 public: virtual ~CsunxinLab1View(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // 生成的消息映射函数 protected: DECLARE_MESSAGE_MAP() public: afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); afx_msg void OnMouseMove(UINT nFlags, CPoint point); }; #ifndef _DEBUG // sunxinLab1View.cpp 中的调试版本 inline CsunxinLab1Doc* CsunxinLab1View::GetDocument() const { return reinterpret_cast<CsunxinLab1Doc*>(m_pDocument); } #endif // sunxinLab1View.cpp : CsunxinLab1View 类的实现 // #include "stdafx.h" #include "sunxinLab1.h" #include "sunxinLab1Doc.h" #include "sunxinLab1View.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CsunxinLab1View IMPLEMENT_DYNCREATE(CsunxinLab1View, CView) BEGIN_MESSAGE_MAP(CsunxinLab1View, CView) // 标准打印命令 ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() END_MESSAGE_MAP() // CsunxinLab1View 构造/析构 CsunxinLab1View::CsunxinLab1View() { // TODO: 在此处添加构造代码 IsDraw = false; } CsunxinLab1View::~CsunxinLab1View() { } BOOL CsunxinLab1View::PreCreateWindow(CREATESTRUCT& cs) { // TODO: 在此处通过修改 // CREATESTRUCT cs 来修改窗口类或样式 return CView::PreCreateWindow(cs); } // CsunxinLab1View 绘制 void CsunxinLab1View::OnDraw(CDC* /*pDC*/) { CsunxinLab1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: 在此处为本机数据添加绘制代码 } // CsunxinLab1View 打印 BOOL CsunxinLab1View::OnPreparePrinting(CPrintInfo* pInfo) { // 默认准备 return DoPreparePrinting(pInfo); } void CsunxinLab1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: 添加额外的打印前进行的初始化过程 } void CsunxinLab1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: 添加打印后进行的清理过程 } // CsunxinLab1View 诊断 #ifdef _DEBUG void CsunxinLab1View::AssertValid() const { CView::AssertValid(); } void CsunxinLab1View::Dump(CDumpContext& dc) const { CView::Dump(dc); } CsunxinLab1Doc* CsunxinLab1View::GetDocument() const // 非调试版本是内联的 { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CsunxinLab1Doc))); return (CsunxinLab1Doc*)m_pDocument; } #endif //_DEBUG // CsunxinLab1View 消息处理程序 void CsunxinLab1View::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 m_point = point; IsDraw = true; CView::OnLButtonDown(nFlags, point); } void CsunxinLab1View::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 // CClientDC dc(GetParent());//or this //dc.MoveTo(m_point); //dc.LineTo(point); ////第二种方式 //CDC* cdc = GetDC(); //cdc->MoveTo(m_point); //cdc->LineTo(point); //::ReleaseDC(AfxGetMainWnd()->m_hWnd,cdc); ////画到桌面上 // CWindowDC dc(GetDesktopWindow());//or this //dc.MoveTo(m_point); //dc.LineTo(point); ////红颜色的画笔 //CDC* cdc = GetDC(); //CPen pen(/*PS_SOLID,PS_DOT*/PS_DASH,1,RGB(255,0,0)); //cdc->SelectObject(&pen); // cdc->MoveTo(m_point); // cdc->LineTo(point); ////画刷的使用,画矩形 //CClientDC dc(this); //CBrush brush(/*PS_SOLID,PS_DOT*/RGB(0,0,0)); //dc.SelectObject(&brush); //dc.FillRect(new CRect(m_point,point),&brush); //cdc->MoveTo(m_point); //cdc->LineTo(point); IsDraw = false; CView::OnLButtonUp(nFlags, point); } void CsunxinLab1View::OnMouseMove(UINT nFlags, CPoint point) { // TODO: 在此添加消息处理程序代码和/或调用默认值 if(IsDraw){ CDC* cdc = GetDC(); cdc->MoveTo(m_point); cdc->LineTo(point); m_point = point; CView::OnMouseMove(nFlags, point); } } ============================================= 文本处理总结 // sunxinLab2View.h : CsunxinLab2View 类的接口 // #pragma once class CsunxinLab2View : public CView { protected: // 仅从序列化创建 CsunxinLab2View(); DECLARE_DYNCREATE(CsunxinLab2View) // 属性 public: CsunxinLab2Doc* GetDocument() const; // 操作 public: CBitmap m_bitmap; CPoint m_inputPosition; CStringList m_strLst; int m_nWidth; // 重写 public: virtual void OnDraw(CDC* pDC); // 重写以绘制该视图 virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: virtual BOOL OnPreparePrinting(CPrintInfo* pInfo); virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo); virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo); // 实现 public: virtual ~CsunxinLab2View(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // 生成的消息映射函数 protected: DECLARE_MESSAGE_MAP() public: afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags); afx_msg void OnTimer(UINT_PTR nIDEvent); }; #ifndef _DEBUG // sunxinLab2View.cpp 中的调试版本 inline CsunxinLab2Doc* CsunxinLab2View::GetDocument() const { return reinterpret_cast<CsunxinLab2Doc*>(m_pDocument); } #endif // sunxinLab2View.cpp : CsunxinLab2View 类的实现 // #include "stdafx.h" #include "sunxinLab2.h" #include "sunxinLab2Doc.h" #include "sunxinLab2View.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CsunxinLab2View IMPLEMENT_DYNCREATE(CsunxinLab2View, CView) BEGIN_MESSAGE_MAP(CsunxinLab2View, CView) // 标准打印命令 ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview) ON_WM_CREATE() ON_WM_CHAR() ON_WM_TIMER() END_MESSAGE_MAP() // CsunxinLab2View 构造/析构 CsunxinLab2View::CsunxinLab2View() { // TODO: 在此处添加构造代码 m_nWidth = 0; } CsunxinLab2View::~CsunxinLab2View() { } BOOL CsunxinLab2View::PreCreateWindow(CREATESTRUCT& cs) { // TODO: 在此处通过修改 // CREATESTRUCT cs 来修改窗口类或样式 return CView::PreCreateWindow(cs); } // CsunxinLab2View 绘制 void CsunxinLab2View::OnDraw(CDC* pDC) { CsunxinLab2Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; pDC->TextOut(0,200,_T("红亮工作室, http://hl-workroom.com")); // TODO: 在此处为本机数据添加绘制代码 } // CsunxinLab2View 打印 BOOL CsunxinLab2View::OnPreparePrinting(CPrintInfo* pInfo) { // 默认准备 return DoPreparePrinting(pInfo); } void CsunxinLab2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: 添加额外的打印前进行的初始化过程 } void CsunxinLab2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: 添加打印后进行的清理过程 } // CsunxinLab2View 诊断 #ifdef _DEBUG void CsunxinLab2View::AssertValid() const { CView::AssertValid(); } void CsunxinLab2View::Dump(CDumpContext& dc) const { CView::Dump(dc); } CsunxinLab2Doc* CsunxinLab2View::GetDocument() const // 非调试版本是内联的 { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CsunxinLab2Doc))); return (CsunxinLab2Doc*)m_pDocument; } #endif //_DEBUG // CsunxinLab2View 消息处理程序 int CsunxinLab2View::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; ////创建一般的插入符 //CClientDC dc(this); //TEXTMETRIC tm; //dc.GetTextMetrics(&tm); // //CreateSolidCaret(tm.tmAveCharWidth,tm.tmHeight); //ShowCaret(); ////创建图形插入符 //m_bitmap.LoadBitmapW(IDB_BITMAP1); //CreateCaret(&m_bitmap); //ShowCaret(); //cdc->SelectClipPath(RGN_AND); //for(int i = 0 ;i<300;i+=10){ // cdc->MoveTo(i,0); // cdc->LineTo(i,300); // cdc->MoveTo(0,i); // cdc->LineTo(300,i); //} // TODO: 在此添加您专用的创建代码 m_inputPosition.x = 0; m_inputPosition.y = 0; m_strLst.AddTail(_T("")); SetTimer(1,50,NULL); CClientDC dc(this); //CString s= _T("aaa");////获得字符串宽度 // CSize len = dc.GetTextExtent(s); return 0; } ////键盘事件处理 void CsunxinLab2View::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { // //m_inputPosition.x = 0; //m_inputPosition.y = 0; // // TODO: 在此添加消息处理程序代码和/或调用默认值 //CClientDC dc(this); //TEXTMETRIC tm; //dc.GetTextMetrics(&tm); // //CBrush brush(RGB(255,255,255)); // //CRect rect(0,0,1000,800); //dc.FillRect(&rect,&brush); // //if(nChar == 0xd){//回车 // m_strLst.AddTail(_T("")); //} //else if(nChar == 0x8){//退格 // if(m_strLst.GetTail().Compare(_T("")) == 0){ // m_strLst.RemoveTail(); // } // else{ // m_strLst.GetTail().Delete(m_strLst.GetTail().GetLength() - 1); // } //} //else{ // m_strLst.GetTail().AppendChar(nChar); //} // //POSITION rPos; //rPos = m_strLst.GetHeadPosition(); //while (rPos != NULL) //{ // m_inputPosition.x = 0; // CString str = m_strLst.GetNext(rPos); // for(int i = 0 ;i < str.GetLength();i ++){ // CString s ; // s.AppendChar(str.GetAt(i)); // m_inputPosition.x += tm.tmAveCharWidth; // dc.TextOut(m_inputPosition.x,m_inputPosition.y,s); // } // m_inputPosition.x += tm.tmAveCharWidth; // m_inputPosition.y += tm.tmHeight; // //} //m_inputPosition.y -= tm.tmHeight; // SetCaretPos(m_inputPosition); CView::OnChar(nChar, nRepCnt, nFlags); } ////卡拉OK效果 void CsunxinLab2View::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调用默认值 m_nWidth++; CClientDC dc(this); TEXTMETRIC tm; dc.GetTextMetrics(&tm); CRect rect(0,200,m_nWidth,tm.tmHeight + 200); dc.SetTextColor(RGB(255,0,0)); CString s ; s.LoadString(IDS_STRING101); dc.DrawText(_T("红亮工作室, http://hl-workroom.com"),rect,DT_LEFT); CView::OnTimer(nIDEvent); }