您现在的位置: 天下网吧 >> 网吧天地 >> 天下码农 >> 微信小程序 >> 正文

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

2011-1-5vczx佚名

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) 建立名为MySDIMDI,并响应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);
    }
    此方法也适合基类是EditViewSDIMDI的情况,但是字体的颜色和底色不行.建议用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中选择了不同的字体,你应该自己设定LOGFONTlfEscapement成员.将初始化后的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.串太长时往让其末尾显示一个省略号(SDIMDIView)

  [问题提出]
    如何在串太长时往让其末尾显示一个省略号(SDIMDIView)?
  [程序实现]
    建立名为MySDIMDI工程.
    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));
}

欢迎访问最专业的网吧论坛,无盘论坛,网吧经营,网咖管理,网吧专业论坛 https://bbs.txwb.com

关注天下网吧微信/下载天下网吧APP/天下网吧小程序,一起来超精彩

本文来源:vczx 作者:佚名

声明
声明:本站所发表的文章、评论及图片仅代表作者本人观点,与本站立场无关。若文章侵犯了您的相关权益,请及时与我们联系,我们会及时处理,感谢您对本站的支持!联系邮箱:support@txwb.com,系统开号,技术支持,服务联系QQ:1175525021本站所有有注明来源为天下网吧或天下网吧论坛的原创作品,各位转载时请注明来源链接!
天下网吧 网吧天下