24小时热门版块排行榜    

查看: 131  |  回复: 0
当前主题已经存档。

zsglly

木虫 (著名写手)

[交流] 深入浅出 CPropertySheet

为了最大限度的发挥属性页的效用,首先让我们先从 CPropertySheet 继承一个新类,取名为 CMyPropSheet.

接着便可以进行下面的各种操作:

l        隐藏属性页默认按钮
隐藏掉Apply应用按钮:

propsheet.m_psh.dwFlags |= PSH_NOAPPLYNOW;
或隐藏掉Cancel取消按钮:

CWnd *pWnd = GetDlgItem( IDCANCEL );
pWnd->ShowWindow( FALSE );
  
l        移动属性页按钮
首先,要获取按钮的句柄,然后就可以象对待窗体一样处理它们了. 下面代码先隐藏掉Apply和Help铵钮,再把OK和Cancel按移动到右侧。  

BOOL CMyPropSheet::OnInitDialog ()  
{
    BOOL bResult = CPropertySheet::OnInitDialog();
  
    int ids [] = {IDOK, IDCANCEL};//, ID_APPLY_NOW, IDHELP };
     
    // Hide Apply and Help buttons
    CWnd *pWnd = GetDlgItem (ID_APPLY_NOW);
    pWnd->ShowWindow (FALSE);
    pWnd = GetDlgItem (IDHELP);
    pWnd->ShowWindow (FALSE);
     
    CRect rectBtn;
    int nSpacing = 6;        // space between two buttons...
  
    for( int i =0; i < sizeof(ids)/sizeof(int); i++)
    {
        GetDlgItem (ids )->GetWindowRect (rectBtn);
         
        ScreenToClient (&rectBtn);
        int btnWidth = rectBtn.Width();
        rectBtn.left = rectBtn.left + (btnWidth + nSpacing)* 2;
        rectBtn.right = rectBtn.right + (btnWidth + nSpacing)* 2;
  
        GetDlgItem (ids )->MoveWindow(rectBtn);
    }
  
     
    return bResult;
}
下面代码移动所有按钮到右侧,并且重新置属性页为合适的大小.

BOOL CMyPropSheet::OnInitDialog ()  
{
    BOOL bResult = CPropertySheet::OnInitDialog();
  
     
    int ids[] = { IDOK, IDCANCEL, ID_APPLY_NOW };
     
    CRect rectWnd;
    CRect rectBtn;
     
    GetWindowRect (rectWnd);
    GetDlgItem (IDOK)->GetWindowRect (rectBtn);
     
    int btnWidth = rectBtn.Width();
    int btnHeight = rectBtn.Height();
    int btnOffset = rectWnd.bottom - rectBtn.bottom;
    int btnLeft = rectWnd.right - rectWnd.left;
  
    rectWnd.bottom = rectBtn.top;
    rectWnd.right = rectWnd.right + btnWidth + btnOffset;
    MoveWindow(rectWnd);
     
    rectBtn.left = btnLeft;
    rectBtn.right = btnLeft + btnWidth;
  
    for (int i = 0; i < sizeof (ids) / sizeof (int); i++)
    {
        rectBtn.top = (i + 1) * btnOffset + btnHeight * i;
        rectBtn.bottom = rectBtn.top + btnHeight;
        GetDlgItem (ids )->MoveWindow (rectBtn);
    }
     
    return bResult;
}
l        改变属性页上的标签文字
首先修改TC_ITEM结构,然后用 SetItem 来修改标签文字,如下代码:

TC_ITEM item;
item.mask = TCIF_TEXT;
item.pszText = "New Label";
  
//Change the label of the first tab (0 is the index of the first tab)...
GetTabControl ()->SetItem (0, &item);
l        改变属性页标签文字的字体属性
如下代码:

    m_NewFont.CreateFont (14, 0, 0, 0, 800, TRUE, 0, 0, 1, 0, 0, 0, 0, _T("Arial" );
    GetTabControl()->SetFont (&m_NewFont);
l        在属性页标签上显示位图
可以用 CImageList 建立图像. 用 SetItem 来设置,如下代码所示:

BOOL CMyPropSheet::OnInitDialog ()
{
    BOOL bResult = CPropertySheet::OnInitDialog();
  
    m_imageList.Create (IDB_MYIMAGES, 13, 1, RGB(255,255,255));
    CTabCtrl *pTabCtrl = GetTabControl ();
    pTabCtrl->SetImageList (&m_imageList);
     
    TC_ITEM item;
    item.mask = TCIF_IMAGE;
    for (int i = 0; i < NUMBER_OF_TABS; i++)
    {
        item.iImage = i;
        pTabCtrl->SetItem (i, &item );
    }
  
    return bResult;
}
l        在属性页左下角显示位图
如下代码所示:  

void CMyPropSheet::OnPaint ()  
{
    CPaintDC dc(this); // device context for painting
     
    int nOffset = 6;
    // load IDB_BITMAP1 from our resources
    CBitmap bmp;
    if (bmp.LoadBitmap (IDB_BITMAP1))
    {
        // Get the size of the bitmap
        BITMAP bmpInfo;
        bmp.GetBitmap (&bmpInfo);
         
        // Create an in-memory DC compatible with the
        // display DC we're using to paint
        CDC dcMemory;
        dcMemory.CreateCompatibleDC (&dc);
         
        // Select the bitmap into the in-memory DC
        CBitmap* pOldBitmap = dcMemory.SelectObject (&bmp);
         
        // Find a bottom-left point for the bitmap in the client area
        CRect rect;
        GetClientRect (&rect);
        int nX = rect.left + nOffset;
        int nY = rect.top + (rect.Height () - bmpInfo.bmHeight) - nOffset;
         
        // Copy the bits from the in-memory DC into the on-
        // screen DC to actually do the painting. Use the centerpoint
        // we computed for the target offset.
        dc.BitBlt (nX, nY, bmpInfo.bmWidth, bmpInfo.bmHeight, &dcMemory,  
            0, 0, SRCCOPY);
         
        dcMemory.SelectObject (pOldBitmap);
    }
  
    // Do not call CPropertySheet::OnPaint() for painting messages
}
l        在属性页右下角显示3D文字Logo
如下代码:

void CMyPropSheet::OnPaint ()  
{
    /////////////////////////////////////////////////////////////////////////
         //在TAB按钮旁边显示3D文字提示,jingzhou xu
         Cstring m_LogoName = “属性页”;
//         if(m_LogoName == ""
//                return;
  
         GetWindowRect(rect);
         ScreenToClient(rect);
         
         LOGFONT logFont;
         ZeroMemory((void*)&logFont,sizeof(logFont));
         strcpy(logFont.lfFaceName,"宋体";
         logFont.lfHeight = -12;
         logFont.lfWeight = 400;
         logFont.lfCharSet = GB2312_CHARSET;
         logFont.lfOutPrecision = 3;
         logFont.lfClipPrecision = 2;  
         logFont.lfQuality = 1;
         logFont.lfPitchAndFamily = 2;
         m_font.CreateFontIndirect(&logFont);
         SetFont(&m_font);
         CFont         *pOldFont = pDC->SelectObject(&m_font);
  
                  rect.left += 6;
                  rect.right -= 6;
                  rect.bottom -= 1;
                  rect.top = rect.bottom - ITEMBUTTON_HEIGHT + 1;
         
  
         CFont m_LogoFont;
         CString sLogoString;
               
         m_LogoFont.CreateFont(rect.Height()*4/5, 0, 0, 0, FW_BOLD, 1, FALSE, FALSE,
                       DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
                       FIXED_PITCH | FF_ROMAN, "楷体_GB2312";
               
         sLogoString = m_LogoName;
               
         RECT m_rDataBox;
         CopyRect(&m_rDataBox,&rect);
               
         TEXTMETRIC tm;
         pDC->GetTextMetrics(&tm);
         CFont* oldFont = pDC->SelectObject(&m_LogoFont);
         CSize sz = pDC->GetTextExtent(sLogoString, sLogoString.GetLength());
         //用GetTextExtent来计算字体logo大小,依靠于设备环境,使用logo位于右下角
         m_rDataBox.left = m_rDataBox.right  - sz.cx - tm.tmAveCharWidth/2;
         m_rDataBox.top  = m_rDataBox.bottom - sz.cy - tm.tmHeight/5;
         pDC->SetBkMode(TRANSPARENT);
         //用3D字体显示,先黑后白,最后再用默认色
         COLORREF oldColor = pDC->SetTextColor(GetSysColor(COLOR_3DDKSHADOW));
         pDC->DrawText(sLogoString, sLogoString.GetLength(), &m_rDataBox, DT_VCENTER | DT_SINGLELINE | DT_CENTER);
         m_rDataBox.left -= tm.tmAveCharWidth;
         pDC->SetTextColor(GetSysColor(COLOR_3DHILIGHT));
         pDC->DrawText(sLogoString, sLogoString.GetLength(), &m_rDataBox, DT_VCENTER | DT_SINGLELINE | DT_CENTER);
         m_rDataBox.left += 3*tm.tmAveCharWidth/5;
         pDC->SetTextColor(RGB(0,0,255));
         pDC->DrawText(sLogoString, sLogoString.GetLength(), &m_rDataBox, DT_VCENTER | DT_SINGLELINE | DT_CENTER);
               
         //释放资源
         pDC->SelectObject(oldFont);
         pDC->SetTextColor(oldColor);   
         m_LogoFont.DeleteObject();
         /////////////////////////////////////////////////////////////////////////
}
  

l        在属性页中动态加入其它控件  
下面演示如何在左下角加入一Edit控件:

In MyPropSheet.h:

public:
    CEdit m_edit;
In MyPropSheet.cpp:

BOOL CMyPropSheet::OnInitDialog ()
{
    BOOL bResult = CPropertySheet::OnInitDialog ();
  
     
    CRect rect;
     
    int nHeight = 24;
    int nWidth = 120;
    int nOffset = 6;
     
    GetClientRect (&rect);
  
    // Find a bottom-left point for the edit control in the client area
    int nX = rect.left + nOffset;
    int nY = rect.top + (rect.Height() - nHeight) - nOffset;
     
    // finally create the edit control
    m_Edit.CreateEx (WS_EX_CLIENTEDGE, _T("EDIT", NULL,
                     WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,  
        nX, nY, nWidth, nHeight, m_hWnd, 0, 0 );
  
    return bResult;
}

[ Last edited by 幻影无痕 on 2006-11-27 at 08:14 ]
回复此楼

» 猜你喜欢

做人要厚道啊!厚道啊!
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 zsglly 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 中青基了要发朋友圈吗? +6 349506619 2026-08-28 6/300 2026-08-31 08:43 by kingkocxr
[基金申请] 基金不中,共勉 +12 eulota 2026-08-26 12/600 2026-08-31 08:42 by ZJTJZ
[基金申请] 能否申诉? +6 echo8914667 2026-08-30 7/350 2026-08-31 08:41 by kingkocxr
[基金申请] 有没有仍没收到信息的 +7 德尚中行 2026-08-27 8/400 2026-08-30 20:52 by purplejack
[考博] 找导师 +6 yuanjiabao 2026-08-29 7/350 2026-08-30 14:40 by 生科新手
[基金申请] 麻烦专家们看看评委们的意见(F口面上) +6 gdd2018 2026-08-28 11/550 2026-08-30 08:58 by 超级无敌华子
[基金申请] 国自然面上复盘~欢迎讨论 (金币+15) +15 晴天加油 2026-08-26 16/800 2026-08-29 18:28 by symmetry
[基金申请] 系统查不到 +11 董八千 2026-08-26 11/550 2026-08-28 18:06 by Leogzhya
[基金申请] 基金未中,这种答复是模板吗? +5 zhaosm1982 2026-08-27 6/300 2026-08-27 16:00 by lfy8008
[基金申请] 看板上这么多中的,有点像50人群里49个人都是骗子的那种感觉…… +5 a089 2026-08-26 6/300 2026-08-27 14:05 by jonewore
[基金申请] 为什么 国际(地区)合作与交流项目 没有放榜? 10+3 majunge000 2026-08-26 11/550 2026-08-27 08:42 by 北京莱茵编辑
[基金申请] 我不理解! +15 Edward_pc 2026-08-26 23/1150 2026-08-26 20:34 by zzuzxg
[基金申请] 出来了 +9 trojank 2026-08-26 9/450 2026-08-26 14:25 by 宝贝虫子
[基金申请] 为什么国自然不能直接公布 +4 bjdxyxy 2026-08-26 4/200 2026-08-26 13:12 by qingmu1201
[基金申请] 国际合作可查了,中了面上 (EPI+1)(金币+50) +18 Ldrop2023 2026-08-26 18/900 2026-08-26 11:15 by cmrandy
[基金申请] 国合可查了 +3 paperzjh 2026-08-26 3/150 2026-08-26 10:41 by LemmonTr
[基金申请] 今天务委会开完了,明天出结果吗 +19 angus9576 2026-08-25 23/1150 2026-08-26 10:03 by zp519
[基金申请] 牛来!米来!面来! +8 beefly 2026-08-26 8/400 2026-08-26 08:37 by xuzhipiao
[基金申请] 某些机构,以效率低为荣,以效率低作为存在感 +9 yuleib84 2026-08-25 10/500 2026-08-25 17:14 by alexon
[基金申请] 没有任何消息-是不是就凉了 +9 图啦图啦 2026-08-24 10/500 2026-08-25 11:59 by 南海小哥
信息提示
请填处理意见