效果演示
当前程序从CStatic派生一个CStaticFilespec类,用于实现带省略号的长文件名。效果如图
上位机MFC省略号功能静态控件
点击按钮后,会将编辑框内容复制到文本框中,文本框空间不够显示内容的话,
会用省略号表示。
使用的文件源代码可以在后面复制使用,或直接下载工程项目:
实现过程:
1.新建立对话框工程,如上图添加控件并排版
控件ID:
IDC_EDIT1
IDC_EDIT2
IDC_STATIC1
IDC_STATIC2
2.将例程内两文件STATICFILESPEC.CPP,STATICFILESPEC.H复制与导入工程。
这样工程会多出一集成类CStaticFilespec。这样就可以在主对话框内中使用了。
3.在主对话框中包含类头文件及添加实例
#include "StaticFilespec.h"
CStaticFilespec m_wndStaticFileEnd;
CStaticFilespec m_wndStaticFilePath;
再初始化控件
SetDlgItemText(IDC_EDIT1,"GKBC8.COM,pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);");
SetDlgItemText(IDC_EDIT2,"D:\\Documents and Settings\\Administrator\\桌面\\CTest");
m_wndStaticFileEnd.SubclassDlgItem (IDC_STATIC1, this);
m_wndStaticFilePath.SubclassDlgItem (IDC_STATIC2, this);
m_wndStaticFilePath.SetPath (TRUE);
4.双击按钮控件,添加点击函数。实现控件内容设置,这样编译运行,就可以查看效果了。
- void CCTestDlg::OnButton1()
- {
- CString strText; // text to be displayed
- GetDlgItemText (IDC_EDIT1, strText);
- SetDlgItemText (IDC_STATIC1, strText);
- GetDlgItemText (IDC_EDIT2, strText);
- SetDlgItemText (IDC_STATIC2, strText);
- }
复制代码 下面是两文件源代码,可以复制使用
- #ifndef _StaticFilespec_h
- #define _StaticFilespec_h
- #if _MSC_VER >= 1000
- #pragma once
- #endif // _MSC_VER >= 1000
- /////////////////////////////////////////////////////////////////////////////
- // CStaticFilespec window
- class CStaticFilespec : public CWnd
- {
- // Construction
- public:
- CStaticFilespec
- (DWORD dwFormat = DT_LEFT | DT_NOPREFIX | DT_VCENTER,
- BOOL bPathEllipsis = FALSE);
- // Attributes
- public:
- // Operations
- public:
- BOOL IsPath() { return m_bPathEllipsis; }
- void SetPath (BOOL bPathEllipsis) { m_bPathEllipsis = bPathEllipsis; }
- DWORD GetFormat() { return m_dwFormat; }
- void SetFormat (DWORD dwFormat) { m_dwFormat = dwFormat; }
- // Overrides
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CStaticFilespec)
- //}}AFX_VIRTUAL
- // Implementation
- public:
- virtual ~CStaticFilespec();
- // Generated message map functions
- protected:
- //{{AFX_MSG(CStaticFilespec)
- afx_msg void OnPaint();
- afx_msg BOOL OnEraseBkgnd(CDC* pDC);
- afx_msg BOOL OnSetText (WPARAM wParam, LPARAM lParam);
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- BOOL m_bPathEllipsis; // flag: draw text as path
- DWORD m_dwFormat; // text format
- };
- /////////////////////////////////////////////////////////////////////////////
- //{{AFX_INSERT_LOCATION}}
- // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
- #endif
- // End StaticFilespec.h
复制代码
- #include "stdafx.h"
- #include "StaticFilespec.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CStaticFilespec
- CStaticFilespec::CStaticFilespec
- (DWORD dwFormat /* = DT_LEFT | DT_NOPREFIX | DT_VCENTER */,
- BOOL bPathEllipsis /* = FALSE */ )
- {
- m_bPathEllipsis = bPathEllipsis;
- m_dwFormat = dwFormat;
- }
- CStaticFilespec::~CStaticFilespec()
- {
- }
- BEGIN_MESSAGE_MAP(CStaticFilespec, CWnd)
- //{{AFX_MSG_MAP(CStaticFilespec)
- ON_MESSAGE( WM_SETTEXT, OnSetText )
- ON_WM_ERASEBKGND()
- ON_WM_PAINT()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CStaticFilespec message handlers
- BOOL CStaticFilespec::OnEraseBkgnd(CDC* pDC)
- {
- RECT rectWnd; // window rectangle
-
- // Erase background
- GetClientRect (&rectWnd);
- pDC->FillSolidRect (&rectWnd, ::GetSysColor (COLOR_3DFACE));
- return (TRUE);
- }
- void CStaticFilespec::OnPaint()
- {
- CPaintDC dc (this); // device context for painting
- DWORD dwFormat; // text format
- RECT rectWnd; // window rectangle
- CString strText; // window text
- CWnd* pWndParent = NULL; // parent window
- // Set default font
- pWndParent = GetParent();
- if (pWndParent)
- dc.SelectObject (pWndParent->GetFont());
- // Draw text
- GetWindowText (strText);
- GetClientRect (&rectWnd);
- dwFormat = m_dwFormat | (m_bPathEllipsis ? DT_PATH_ELLIPSIS : DT_END_ELLIPSIS);
- ::DrawTextEx (dc.m_hDC,
- strText.GetBuffer (0),
- strText.GetLength(),
- &rectWnd,
- dwFormat,
- NULL);
- strText.ReleaseBuffer();
-
- // Do not call CWnd::OnPaint() for painting messages
- }
- BOOL CStaticFilespec::OnSetText
- (WPARAM wParam,
- LPARAM lParam)
- {
- DefWindowProc (WM_SETTEXT, wParam, lParam);
- Invalidate();
- return (TRUE);
- }
-
- // End StaticFilespec.cpp
复制代码
|