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));
}

积累的VC编程小技巧之视图的更多相关文章

  1. 积累的VC编程小技巧之打印相关

    1.修改打印预览的ToolBar 为AFX_IDD_PREVIEW_TOOLBAR这个ID创建一个DialogBar.则系统就会用新创建的DialogBar代替系统默认的那个 2.关于打印 1.要打印 ...

  2. 积累的VC编程小技巧之文件操作

    1.删除文件夹 // 删除文件夹及其所有内容void CBaseDoc::RemoveFolder(const CString &strPathName){    CString path = ...

  3. 积累的VC编程小技巧之按钮

    1.资源种创建的控件,对其属性的动态控制: 在对话框类的头文件里创建所要改变属性的控件的对象,如要改变一个Button(其ID为IDC_MyButton)的属性,则需创建Cbutton的对象m_but ...

  4. 积累的VC编程小技巧之框架窗口及其他

    1.修改主窗口风格 AppWizard生成的应用程序框架的主窗口具有缺省的窗口风格,比如在窗口标题条中自动添加文档名.窗口是叠加型的.可改变窗口大小等.要修改窗口的缺省风格,需要重载CWnd::Pre ...

  5. 积累的VC编程小技巧之滚动条

    1.设置滚动条的滚动大小 创建一个基于CScrollview的SDI Project(在第6步中选CScrollview) 若你已创建了,这步可以省略. 然后: 改为如 void CTestView: ...

  6. 积累的VC编程小技巧之编辑框

    1.如何让对话框中的编辑框接收对话框的消息 ////////////////////////////////////////////////// 如何让对话框中的CEdit控件类接收对话框的消息/// ...

  7. 积累的VC编程小技巧之标题栏和菜单

    1.窗口最大最小化按纽的控制 ①怎样在程序开始的时候让它最大化? ②vc++做出来的exe文件在窗体的右上方是没有最大化和最小化按钮的,怎样实现这一功能? ③如何在显示窗口时,使最大化按钮变灰?   ...

  8. 积累的VC编程小技巧之图标、光标及位图

    1.图标透明 (1).Windows中的图标其实是有两个图像组成的,其中一个用于与它要显示的位置的图像做“AND”操作,另一个作“XOR”操作. 透明:用“白色”AND,用“黑色”XOR 反色:用“白 ...

  9. 积累的VC编程小技巧之对话框

    1.用鼠标移动基于对话框的无标题栏程序的简单方法 void CVCTestDlg::OnLButtonDown(UINT nFlags, CPoint point) {    //一句话解决问题    ...

随机推荐

  1. 基于visual Studio2013解决算法导论之028散列表开放寻址

     题目 散列表 解决代码及点评 #include <iostream> #include <time.h> using namespace std; template & ...

  2. 基于visual Studio2013解决C语言竞赛题之0510求最大和

     题目

  3. hdu1853解题报告

    题意和解决回路匹配的思路如同hdu3488 (这里我第一次想到最短路,但是对于有回路这个不知道怎么处理,后来看了别人的解题报告才知道KM匹配,但是看到KM之后就自己想...想了很久....还是不知道回 ...

  4. Java进阶05 多线程

    链接地址:http://www.cnblogs.com/vamei/archive/2013/04/15/3000898.html 作者:Vamei 出处:http://www.cnblogs.com ...

  5. Python 第三篇(下):collections系列、集合(set)、单双队列、深浅copy、内置函数

     一.collections系列: collections其实是python的标准库,也就是python的一个内置模块,因此使用之前导入一下collections模块即可,collections在py ...

  6. 基于visual Studio2013解决C语言竞赛题之1004平均值

     题目 解决代码及点评 /************************************************************************/ /* 4. 编一个程序, ...

  7. LoadRunner脚本增强

    1.检查点 web_find() 和web_reg_find() 2.Block技术 如果对不同的事物进行不同次数的循环该怎么处理?默认情况下LoadRunner对所有的事物都是统一执行的,即虽然有多 ...

  8. POJ 1700 cross river (数学模拟)

                                                                                                       ...

  9. Collections在sort()简单分析法源

    Collections的sort方法代码: public static <T> void sort(List<T> list, Comparator<? super T& ...

  10. Hystrix 使用与分析

    转载请注明出处哈:http://hot66hot.iteye.com/admin/blogs/2155036 一:为什么需要Hystrix? 在大中型分布式系统中,通常系统很多依赖(HTTP,hess ...