• 积累的VC编程小技巧之视图


    1.如何得到视图指针

    [问题提出]
        现在你有一个多线程的Demo,你想在多线程里处理视图指针里的函数,我们给这个函数起个名字:Put();该如何实现呢?
       //有两种方法可以实现你的要求:
       //1)第一种方法:
       //要是多线程不是在App.cpp里出现,那么要在多线程的.cpp中加上extern CYourApp theApp;
       //获得文档模板:
       POSITION curTemplatePos = theApp.GetFirstDocTemplatePosition();
       CDocTemplate *m_doc=theApp.GetNextDocTemplate(curTemplatePos);

       //获得文档:
       curTemplatePos=m_doc->GetFirstDocPosition();
       CYourDoc *m_pdoc=(CA8Doc*)m_doc->GetNextDoc(curTemplatePos);
       
       //获得视图:
       curTemplatePos=m_pdoc->GetFirstViewPosition();
       CYourView *m_pview=(CYourView*)m_pdoc->GetNextView(curTemplatePos);

       //调用视图函数:
       m_pview->Put();

       //2)第二种方法:
       //获得窗体指针:
       CMainFrame *pFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;

       //获得与该窗体符合的视图:
       CYourView *m_pView = (CYourView *) pFrame->GetActiveView();

       //调用视图函数:
       m_pView->Put();

    2.如何设置有背景颜色的文本

    (1)[解决方法]
        用到了CDC::SetBkMode();
      
      [程序实现] 
        void CMyView::OnDraw(CDC* pDC)
        {
           CMyDoc* pDoc = GetDocument();
           ASSERT_VALID(pDoc);
           CRect rcView;//加這兩句
           GetClientRect(rcView);
           // TODO: add draw code for native data here
           CString str (_T("Perfect Text...")); 
           pDC->SetBkMode(TRANSPARENT); 
           rcView.OffsetRect (1,1); 
           pDC->SetTextColor(RGB (0,0,0)); 
           pDC->DrawText(str,str.GetLength(),rcView,DT_SINGLELINE | DT_CENTER | DT_VCENTER); 
           rcView.OffsetRect(-1,-1); 
           pDC->SetTextColor(RGB (255,0,0)); 
           pDC->DrawText(str,str.GetLength(),rcView,DT_SINGLELINE | DT_CENTER | DT_VCENTER); 
        }

    (2) 建立名为My的SDI或MDI,并响应WM_ERASEBKGND.
        BOOL CMyView::OnEraseBkgnd(CDC* pDC) 
        {
         // TODO: Add your message handler code here and/or call default
         CBrush Brush (RGB(114,147,171)); 
         // Select the brush into the device context . 
         CBrush* pOldBrush = pDC->SelectObject(&Brush); 
         // Get the area that needs to be erased . 
         CRect ViewClip; 
         pDC->GetClipBox(&ViewClip); 
         //Paint the area. 
         pDC->PatBlt(ViewClip.left,ViewClip.top,ViewClip.Width(),ViewClip.Height(),PATCOPY); 
         //Unselect brush out of device context . 
         pDC->SelectObject (pOldBrush ); 
         // Return nonzero to half fruther processing . 

         return TRUE;
         return CView::OnEraseBkgnd(pDC);
        }
        此方法也适合基类是EditView的SDI或MDI的情况,但是字体的颜色和底色不行.建议用WM_CTLCOLOR.

     

    3.CDC中的竖排文本

    在OnDraw成员函数中我想让文本竖直对齐,但CDC类似乎不支持该处理

    方法一:如果你的竖直对齐是指旋转文本的话,下面的代码会对你有帮助:该代码检查一个Check box控制,查看文本是否需要旋转.

    // m_pcfYTitle is a CFont* to the selected font.
    // m_bTotateYTitle is a bool (==TRUE if rotated)

    void CPage1::OnRotateytitle()
    {
    LOGFONT lgf;
    m_pcfYTitle->GetLogFont(&lgf);
    m_bRotateYTitle=
            ((CButton*)GetDlgItem(IDC_ROTATEYTITLE))->GetCheck()>0;

    // escapement is reckoned clockwise in 1/10ths of a degree:
    lgf.lfEscapement=-(m_bRotateYTitle*900);
    m_pcfYTitle->DeleteObject();

    m_pcfYTitle->CreateFontIndirect(&lgf);
    DrawSampleChart();
    }
    注意如果你从CFontDialog中选择了不同的字体,你应该自己设定LOGFONT的lfEscapement成员.将初始化后的lfEscapement值传到CFontDialog中.

    方法二:还有一段代码可参考:

    LOGFONT LocalLogFont;

    strcpy(LocalLogFont.lfFaceName, TypeFace);

    LocalLogFont.lfWeight = fWeight;
    LocalLogFont.lfEscapement = Orient;
    LocalLogFont.lfOrientation = Orient;

    if (MyFont.CreateFontIndirect(&LocalLogFont))

    {
       cMyOldFont = cdc->SelectObject(&MyFont);
    }

     

    4.串太长时往让其末尾显示一个省略号(在SDI或MDI的View中)

      [问题提出]
        如何在串太长时往让其末尾显示一个省略号(在SDI或MDI的View中)?
      [程序实现]
        建立名为My的SDI或MDI工程.
        void CMyView::OnDraw(CDC* pDC)
        {
           CMyDoc* pDoc = GetDocument();
           ASSERT_VALID(pDoc);
           // TODO: add draw code for native data here
           pDC->DrawText(CString("It's a long string,so we will add a '...' at the end."),CRect (110, 110, 180, 130),DT_LEFT | DT_END_ELLIPSIS); 
           //Add ellpsis to middle of string if it does not fit 
           pDC->DrawText(CString("It's a long string,so we will add a '...' at the end."),CRect (110, 140, 300, 160),DT_LEFT | DT_PATH_ELLIPSIS); 
        }

    5.修改视图背景

    How do I change the background color of a view?

    To change the background color for a CView, CFrameWnd, or CWnd object, process the WM_ERASEBKGND message. The following code shows how: 

    BOOL CSampleView::OnEraseBkgnd(CDC* pDC)
    {
        // Set brush to desired background color.
        CBrush backBrush(RGB(255, 128, 128));
        // Save old brush.
        CBrush* pOldBrush = pDC->SelectObject(&backBrush);
        CRect rect;
        pDC->GetClipBox(&rect);     // Erase the area needed.
        pDC->PatBlt(rect.left, rect.top, rect.Width(), 
        rect.Height(), PATCOPY);
        pDC->SelectObject(pOldBrush);
        return TRUE;
    }

    I solved the problem like this:

    HBRUSH dlgtest::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
        switch (nCtlColor)
        {
            case CTLCOLOR_BTN:
            case CTLCOLOR_STATIC:
            {
                pDC->SetBkMode(TRANSPARENT);
            }
            case CTLCOLOR_DLG:
            {
                CBrush*     back_brush;
                COLORREF    color;
                color = (COLORREF) GetSysColor(COLOR_BTNFACE);
                back_brush = new CBrush(color);
                return (HBRUSH) (back_brush->m_hObject);
            }
        }
        return(CFormView::OnCtlColor(pDC, pWnd, nCtlColor));
    }

  • 相关阅读:
    资源汇总 | 墨天轮2021年数据库技术直播回顾
    2022年1月国产数据库大事记墨天轮
    墨天轮2021年度中国数据库魔力象限
    国产数据库考试资料汇总(持续更新)
    mongodb关机重启
    python pandas csv 大文件 DataFrame转换为List
    2021MongoDB中文社区年度表彰 | 星汉灿烂,感谢有你
    【网络研讨会】“专家面对面”MongoDB模式设计
    Centos7更换阿里云镜像
    jdbc中mysql多条SQL批量操作(CRUD)
  • 原文地址:https://www.cnblogs.com/lidabo/p/3703521.html
Copyright © 2020-2023  润新知