MFC创建非模态对话框并修改CStatic文字

//由IDD_STATUS_DIALOG创建一个对话框
CDialog* pCheckNetStatusDlg = new CDialog(); pCheckNetStatusDlg->Create(IDD_STATUS_DIALOG, this); RECT statusWndClientRect, mainWndClientRect, mainWndRect; pCheckNetStatusDlg->GetClientRect(&statusWndClientRect); this->GetClientRect(&mainWndClientRect); this->GetWindowRect(&mainWndRect); pCheckNetStatusDlg->ShowWindow(SW_SHOW); pCheckNetStatusDlg->MoveWindow((mainWndClientRect.right - statusWndClientRect.right) / 2 + mainWndRect.left, (mainWndClientRect.bottom - statusWndClientRect.bottom) / 2 + mainWndRect.top, statusWndClientRect.right, statusWndClientRect.bottom); CString strStatus = "当前状态";
//修改IDC_STATIC_STATUS的文字 CWnd* pWndStatus = pCheckNetStatusDlg->GetDlgItem(IDC_STATIC_STATUS); //设置新的文字 pWndStatus->SetWindowText(strStatus); CDC* pDC = GetDC(); //计算文字长度 CSize sizeStatus = pDC->GetTextExtent(strStatus); RECT rectStatus; //得到文字在屏幕上的位置 pWndStatus->GetWindowRect(&rectStatus); //设置文字位置 rectStatus.left = (rectStatus.left + rectStatus.right) / 2 - sizeStatus.cx / 2; rectStatus.right = rectStatus.left + sizeStatus.cx; pCheckNetStatusDlg->ScreenToClient(&rectStatus); //调整大小 pWndStatus->MoveWindow(&rectStatus); //刷新窗口 Invalidate()是发送一个命令WM_PAINT来重画窗口, WM_PAINT是优先级比较低的, UpdateWindow是立即重画, 优先级高 pCheckNetStatusDlg->UpdateWindow();

  

原文地址:https://www.cnblogs.com/barrysgy/p/3666696.html