最近忙於一個小項目,用MFC做一個對話框:1.顯示自定義文字 2.多行文本居中顯示 3.文字顏色支持自定義 4.窗口透明度支持自定義 5.窗口自動隱藏
一、新建一個基於對對話框的MFC程序
二、添加子窗口來動態顯示文本,對應的類是CDlgShowMsg,子窗口的屬性 Title bar 屬性設為FALSE,不顯示標題欄。
三、CDlgShowMsg類的實現
.h實現
class CDlgShowMsg : public CDialog { DECLARE_DYNAMIC(CDlgShowMsg) public: CDlgShowMsg(CWnd* pParent = NULL); // 標准構造函數 virtual ~CDlgShowMsg(); // 對話框數據 #ifdef AFX_DESIGN_TIME enum { IDD = IDD_DLGDRAW }; #endif protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 virtual BOOL OnInitDialog(); DECLARE_MESSAGE_MAP() public: afx_msg void OnPaint(); public: void ShowMsgInfo(CString strMsg); void SetWindowWidthAndHeight(int iWidth,int iHeight);//窗口的寬和高 void SetWindowTransparency(double dAlpha);//(0~1.0) 窗口透明度 void SetPosWindow(int iXPos,int iYPos,int iWidth,int iHeigth);//窗口的位置和大小 void SetTextColor(int iRed,int iGreen, int iBlue);//可以修改文本顏色 private: CString m_strMsg;//顯示文本 int m_iWidth;//窗口寬 int m_iHeight;//窗口高 int m_iRed;//RGB的R int m_iGreen;//RGB的G int m_iBlue;//RGB的B UINT m_uTimeID;//定時器ID public: afx_msg void OnTimer(UINT_PTR nIDEvent); };
.cpp實現
CDlgShowMsg::CDlgShowMsg(CWnd* pParent /*=NULL*/) : CDialog(IDD_DLGDRAW, pParent) , m_iWidth(200) , m_iHeight(100) , m_iRed(255) , m_iGreen(255) , m_iBlue(255) { } CDlgShowMsg::~CDlgShowMsg() { } void CDlgShowMsg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BOOL CDlgShowMsg::OnInitDialog() { CDialog::OnInitDialog(); return TRUE; } BEGIN_MESSAGE_MAP(CDlgShowMsg, CDialog) ON_WM_PAINT() ON_WM_TIMER() END_MESSAGE_MAP() // CDlgShowMsg 消息處理程序 void CDlgShowMsg::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: 在此處添加消息處理程序代碼 CBrush brush; CRect rc; GetWindowRect(&rc); //獲得窗口矩形 brush.CreateSolidBrush(RGB(255, 255, 255)); dc.SelectObject(brush); dc.PatBlt(0, 0, rc.Width(), rc.Height(), PATCOPY); CRect rect; GetClientRect(&rect); CDC* pDc = GetDC(); CFont new_font; //創建字體宋體格式 100為字高 VERIFY(new_font.CreatePointFont(250, _T("宋體"), pDc)); //選擇該字體進入PDC CFont* default_font = pDc->SelectObject(&new_font); //設置字體背景為透明 pDc->SetBkMode(TRANSPARENT); //設置字體顏色 pDc->SetTextColor(RGB(m_iRed,m_iGreen,m_iBlue)); CRect boundary(0,0,m_iWidth,m_iHeight);//設置文本要顯示矩形區域 CRect rect1(0,0,boundary.Width(),boundary.Height());//設置文字顯示臨時區域 int height = pDc->DrawText(m_strMsg, rect, DT_CALCRECT | DT_CENTER | DT_EDITCONTROL | DT_WORDBREAK);//計算完成后將原來的區域賦回rect rect1 = boundary; if (boundary.Height()>height) { rect1.top += (boundary.Height() - height)/2;//計算空白高度的一半 } //顯示文本,居中顯示 //pDc->DrawText(m_strMsg, rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);//單行 居中顯示 pDc->DrawText(m_strMsg, rect1, DT_VCENTER | DT_CENTER |DT_EDITCONTROL | DT_WORDBREAK);//多行 居中顯示 垂直巨宗 左右居中 自動換行 //恢復PDC的缺省字體 pDc->SelectObject(default_font); //釋放font對象 new_font.DeleteObject(); // 不為繪圖消息調用 CDialog::OnPaint() } void CDlgShowMsg::ShowMsgInfo(CString strMsg) { m_strMsg = strMsg; ShowWindow(SW_SHOW); m_uTimeID = SetTimer(1, 5000, NULL); CRect rc; GetClientRect(&rc); InvalidateRect(rc, FALSE); } void CDlgShowMsg::SetWindowWidthAndHeight(int iWidth, int iHeight) { m_iWidth = iWidth; m_iHeight = iHeight; } void CDlgShowMsg::SetWindowTransparency(double dAlpha) { // 設置分層屬性 SetWindowLong(GetSafeHwnd(), GWL_EXSTYLE, GetWindowLong(GetSafeHwnd(), GWL_EXSTYLE) | WS_EX_LAYERED); SetLayeredWindowAttributes( 0, 255* dAlpha , LWA_ALPHA); } void CDlgShowMsg::SetPosWindow(int iXPos, int iYPos, int iWidth, int iHeigth) { SetWindowPos(NULL, iXPos, iYPos, iWidth, iHeigth, SWP_SHOWWINDOW); } void CDlgShowMsg::SetTextColor(int iRed, int iGreen, int iBlue) { m_iRed = iRed; m_iGreen = iGreen; m_iBlue = iBlue; } void CDlgShowMsg::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息處理程序代碼和/或調用默認值 KillTimer(m_uTimeID); ShowWindow(SW_HIDE); CDialog::OnTimer(nIDEvent); }
四、使用
在CtestDrawTextDlg中定義一個私有的 CDlgShowMsg* m_pShowMsg變量,在OnInitDialog函數中創建子窗口
if (m_pShowMsg == NULL) { m_pShowMsg = new CDlgShowMsg; m_pShowMsg->Create(IDD_DLGDRAW, this); m_pShowMsg->ShowWindow(SW_HIDE);//先隱藏 }
void CtestDrawTextDlg::OnBnTestClicked() { // TODO: 在此添加控件通知處理程序代碼 if (m_pShowMsg) { m_pShowMsg->SetTextColor(255, 255, 0);//黃色的文本顏色 m_pShowMsg->ShowMsgInfo(_T("白雪公和七個小矮人哈哈哈哈哈1111")); m_pShowMsg->SetWindowTransparency(0.9);//窗口透明度 m_pShowMsg->SetPosWindow(100, 150, 400, 100);//窗口位置和大小 m_pShowMsg->SetWindowWidthAndHeight(400, 100);//窗寬和高 } }