MFC dialog box to achieve the effect of the horse

  • 2020-09-28 09:03:34
  • OfStack

The example of this paper shares the display effect of MFC dialog box and text information playing in a loop for your reference. The specific content is as follows

CMFCDlg. h dialog


// CMFCDlg.h  dialog 
class CMFCDlg : public CDialogEx
{
//  structure 
public:
 CMFCDlg(CWnd* pParent = nullptr); //  Standard constructor 

//  Dialog data 
#ifdef AFX_DESIGN_TIME
 enum { IDD = IDD_DLG_M };
#endif

//  implementation 
protected:
 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);
};

MFCDlg. cpp: Implementation file


// MFCDlg.cpp:  Implementation file 


#include "pch.h"
#include "framework.h"
#include "MFC.h"
#include "MFCDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMFCDlg  dialog 

CMFCDlg::CMFCDlg(CWnd* pParent /*=nullptr*/)
 : CDialogEx(IDD_DLG_M, pParent)
{
 m_szText = _T(" This is a 1 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();

 //  Sets the icon for this dialog box.   When the main window of the application is not a dialog box, the framework will automatically 
 //  Perform this operation 
 SetIcon(m_hIcon, TRUE);  //  Set the big icon 
 SetIcon(m_hIcon, FALSE); //  Set the little icon 

 CRect rect;
 GetClientRect(rect);
 m_nLeft = rect.right;
 SetTimer(1, 60, NULL);
 
 return TRUE; //  Returns unless focus is set to the control  TRUE
}

//  If you add a minimize button to the dialog box, you need the following code 
//  To draw the icon.   For working with documents / Viewmodel  MFC  The application, 
//  This will be done automatically by the framework. 

void CMFCDlg::OnPaint()
{
 if (IsIconic())
 {
 CPaintDC dc(this); //  The device context used to draw 

 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

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

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

// The system calls this function to get the cursor when the user drags the minimize window 
// Display. 
HCURSOR CMFCDlg::OnQueryDragIcon()
{
 return static_cast<HCURSOR>(m_hIcon);
}


void CMFCDlg::OnTimer(UINT_PTR nIDEvent)
{
 CRect rt;
 GetClientRect(rt);
 CClientDC dc(this);
 dc.SetBkColor(GetSysColor(COLOR_3DFACE));
 dc.SelectObject(GetFont());
 CSize size = dc.GetOutputTextExtent(m_szText);
 dc.TextOut(m_nLeft, rt.Height() - size.cy, m_szText);
 m_nLeft -= 5;
 if (m_nLeft + size.cx <= 0)
 m_nLeft = rt.right;
 CDialogEx::OnTimer(nIDEvent);
}

Related articles: