chenluhua1980
5 天以前 4b7616f61a6a37cc917cdbcb131f91272ab2e6fb
SourceCode/Bond/Servo/MsgDlg.cpp
@@ -5,6 +5,7 @@
#include "Servo.h"
#include "MsgDlg.h"
#include "afxdialogex.h"
#include <memory>
// CMsgDlg 对话框
@@ -14,6 +15,7 @@
CMsgDlg::CMsgDlg(CWnd* pParent /*=nullptr*/)
   : CDialogEx(IDD_DIALOG_MSG, pParent)
{
   m_uiThreadId = ::GetCurrentThreadId();
   m_nCompleteCode = 0;
   m_dwData = 0;
   m_dwDataEx = 0;
@@ -22,11 +24,13 @@
   m_crTitle = CR_MSGBOX_TITLE;
   m_crMessage = CR_MSGBOX_MESSAGE;
   m_nIcon = MSG_BOX_TIP;
   m_bDelayClose = FALSE;
}
CMsgDlg::CMsgDlg(CString strTitle, CString strMessage)
   : CDialogEx(IDD_DIALOG_MSG, NULL)
{
   m_uiThreadId = ::GetCurrentThreadId();
   m_strTitle = strTitle;
   m_strMessage = strMessage;
   m_crBkgnd = CR_MSGBOX_BKGND;
@@ -39,6 +43,7 @@
CMsgDlg::CMsgDlg(int nIcon, CString strTitle, CString strMessage)
   : CDialogEx(IDD_DIALOG_MSG, NULL)
{
   m_uiThreadId = ::GetCurrentThreadId();
   m_strTitle = strTitle;
   m_strMessage = strMessage;
   m_crBkgnd = CR_MSGBOX_BKGND;
@@ -57,11 +62,60 @@
   CDialogEx::DoDataExchange(pDX);
}
bool CMsgDlg::IsUiThread() const
{
   return ::GetCurrentThreadId() == m_uiThreadId;
}
LRESULT CMsgDlg::OnMsgSetTitle(WPARAM wParam, LPARAM lParam)
{
   std::unique_ptr<CString> str(reinterpret_cast<CString*>(lParam));
   SetTitle(*str);
   return 0;
}
LRESULT CMsgDlg::OnMsgSetMessage(WPARAM wParam, LPARAM lParam)
{
   std::unique_ptr<CString> str(reinterpret_cast<CString*>(lParam));
   SetMessage(*str);
   return 0;
}
LRESULT CMsgDlg::OnMsgSetIcon(WPARAM wParam, LPARAM lParam)
{
   SetIcon(static_cast<int>(wParam));
   return 0;
}
LRESULT CMsgDlg::OnMsgSetMarquee(WPARAM wParam, LPARAM lParam)
{
   SetMarquee((BOOL)wParam, (int)lParam);
   return 0;
}
LRESULT CMsgDlg::OnMsgDelayClose(WPARAM wParam, LPARAM lParam)
{
   DelayClose((int)wParam);
   return 0;
}
LRESULT CMsgDlg::OnMsgSetComplete(WPARAM wParam, LPARAM lParam)
{
   SetCompleteCode((int)wParam);
   return 0;
}
BEGIN_MESSAGE_MAP(CMsgDlg, CDialogEx)
   ON_WM_TIMER()
   ON_WM_CTLCOLOR()
   ON_WM_SIZE()
   ON_MESSAGE(WM_MSGDLG_SET_TITLE, &CMsgDlg::OnMsgSetTitle)
   ON_MESSAGE(WM_MSGDLG_SET_MESSAGE, &CMsgDlg::OnMsgSetMessage)
   ON_MESSAGE(WM_MSGDLG_SET_ICON, &CMsgDlg::OnMsgSetIcon)
   ON_MESSAGE(WM_MSGDLG_SET_MARQUEE, &CMsgDlg::OnMsgSetMarquee)
   ON_MESSAGE(WM_MSGDLG_DELAY_CLOSE, &CMsgDlg::OnMsgDelayClose)
   ON_MESSAGE(WM_MSGDLG_SET_COMPLETE, &CMsgDlg::OnMsgSetComplete)
END_MESSAGE_MAP()
@@ -69,6 +123,10 @@
void CMsgDlg::SetCompleteCode(int code)
{
   if (!IsUiThread()) {
      PostMessage(WM_MSGDLG_SET_COMPLETE, (WPARAM)code, 0);
      return;
   }
   m_nCompleteCode = code;
   CWnd* pProgressCtrl = GetDlgItem(IDC_PROGRESS1);
   if(pProgressCtrl != nullptr) pProgressCtrl->ShowWindow(SW_HIDE);
@@ -101,6 +159,10 @@
void CMsgDlg::SetIcon(int nIcon)
{
   if (!IsUiThread()) {
      PostMessage(WM_MSGDLG_SET_ICON, (WPARAM)nIcon, 0);
      return;
   }
   m_nIcon = nIcon;
   if (::IsWindow(m_hWnd)) {
      UpdateIcon();
@@ -126,6 +188,13 @@
void CMsgDlg::SetTitle(CString strTitle)
{
   if (!IsUiThread()) {
      CString* pStr = new CString(strTitle);
      if (!PostMessage(WM_MSGDLG_SET_TITLE, 0, (LPARAM)pStr)) {
         delete pStr;
      }
      return;
   }
   m_strTitle = strTitle;
   if (::IsWindow(m_hWnd) && GetDlgItem(IDC_LABEL_TITLE) != NULL) {
      SetWindowText(m_strTitle);
@@ -135,6 +204,13 @@
void CMsgDlg::SetMessage(CString strMessage)
{
   if (!IsUiThread()) {
      CString* pStr = new CString(strMessage);
      if (!PostMessage(WM_MSGDLG_SET_MESSAGE, 0, (LPARAM)pStr)) {
         delete pStr;
      }
      return;
   }
   m_strMessage = strMessage;
   if (::IsWindow(m_hWnd) && GetDlgItem(IDC_LABEL_MSG) != NULL) {
      SetDlgItemText(IDC_LABEL_MSG, m_strMessage);
@@ -143,6 +219,10 @@
void CMsgDlg::SetMarquee(_In_ BOOL fMarqueeMode, _In_ int nInterval)
{
   if (!IsUiThread()) {
      PostMessage(WM_MSGDLG_SET_MARQUEE, (WPARAM)fMarqueeMode, (LPARAM)nInterval);
      return;
   }
   if (!::IsWindow(m_hWnd)) {
      return;
   }
@@ -169,7 +249,14 @@
void CMsgDlg::DelayClose(int nDelay)
{
   SetTimer(1, nDelay, NULL);
   if (!IsUiThread()) {
      PostMessage(WM_MSGDLG_DELAY_CLOSE, (WPARAM)nDelay, 0);
      return;
   }
   if (!m_bDelayClose) {
      m_bDelayClose = TRUE;
      SetTimer(1, nDelay, NULL);
   }
}
void CMsgDlg::ShowCloseButton(BOOL bVisible)
@@ -182,6 +269,7 @@
BOOL CMsgDlg::OnInitDialog()
{
   CDialogEx::OnInitDialog();
   m_uiThreadId = ::GetCurrentThreadId();
   SetWindowText(m_strTitle);
   SetDlgItemText(IDC_LABEL_TITLE, m_strTitle);