SoFunction
Updated on 2025-03-10

Implement the revolving light effect in the MFC dialog box

This article shares with you the effect of implementing the revolving * in the MFC dialog box and the display effect of text information loop playback for your reference. The specific content is as follows

Dialog Box

// Dialogclass CMFCDlg : public CDialogEx
{
// Constructpublic:
 CMFCDlg(CWnd* pParent = nullptr); // Standard constructor
// Dialog box data#ifdef AFX_DESIGN_TIME
 enum { IDD = IDD_DLG_M };
#endif

// accomplishprotected:
 HICON m_hIcon;
 int m_nLeft;
 CString m_szText;
 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support // The generated message mapping function virtual BOOL OnInitDialog();
 afx_msg void OnPaint();
 afx_msg HCURSOR OnQueryDragIcon();
 DECLARE_MESSAGE_MAP()
public:
 afx_msg void OnTimer(UINT_PTR nIDEvent);
};

: Implement files

// : Implementation file

#include ""
#include ""
#include ""
#include ""
#include ""

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMFCDlg dialog box
CMFCDlg::CMFCDlg(CWnd* pParent /*=nullptr*/)
 : CDialogEx(IDD_DLG_M, pParent)
{
 m_szText = _T("This is a looping message");
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFCDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMFCDlg, CDialogEx)
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_WM_TIMER()
END_MESSAGE_MAP()


// CMFCDlg message handler
BOOL CMFCDlg::OnInitDialog()
{
 CDialogEx::OnInitDialog();

 // Set the icon for this dialog box.  When the main application window is not a dialog box, the framework will automatically // Do this SetIcon(m_hIcon, TRUE);  // Set large icons SetIcon(m_hIcon, FALSE); // Set small icons
 CRect rect;
 GetClientRect(rect);
 m_nLeft = ;
 SetTimer(1, 60, NULL);
 
 return TRUE; // Return TRUE unless focus is set to the control}

// If you add a minimization button to the dialog box, you need the following code// to draw the icon.  For MFC applications that use document/view models,// This will be done automatically by the framework.
void CMFCDlg::OnPaint()
{
 if (IsIconic())
 {
 CPaintDC dc(this); // Device context for drawing
 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(()), 0);

 // center the icon in the workspace rectangle int cxIcon = GetSystemMetrics(SM_CXICON);
 int cyIcon = GetSystemMetrics(SM_CYICON);
 CRect rect;
 GetClientRect(&rect);
 int x = (() - cxIcon + 1) / 2;
 int y = (() - cyIcon + 1) / 2;

 // Draw icon (x, y, m_hIcon);
 }
 else
 {
 CDialogEx::OnPaint();
 }
}

// When the user drags the minimized window, the system calls this function to get the cursor//show.HCURSOR CMFCDlg::OnQueryDragIcon()
{
 return static_cast<HCURSOR>(m_hIcon);
}


void CMFCDlg::OnTimer(UINT_PTR nIDEvent)
{
 CRect rt;
 GetClientRect(rt);
 CClientDC dc(this);
 (GetSysColor(COLOR_3DFACE));
 (GetFont());
 CSize size = (m_szText);
 (m_nLeft, () - , m_szText);
 m_nLeft -= 5;
 if (m_nLeft +  <= 0)
 m_nLeft = ;
 CDialogEx::OnTimer(nIDEvent);
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.