我可以在 MFC 应用程序中定义全局变量的位置

Where I can define a global variable in an MFC application?

本文关键字:全局变量 位置 定义 MFC 应用程序 我可以      更新时间:2023-10-16

我想在 MFC 应用程序内声明一个全局变量,以便应用程序中的每个操作都可以看到此变量并对其进行操作,但我不知道我可以在哪里声明该变量。 在此代码中,我有许多按钮操作,我需要声明一个在此操作之间共享的字符串变量。

// CalculatorDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Calculator.h"
#include "CalculatorDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
 public:
CAboutDlg();
 // Dialog Data
enum { IDD = IDD_ABOUTBOX };
public:
static CString myValue;
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  // Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) {}
void CAboutDlg::DoDataExchange(CDataExchange* pDX){
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

// CCalculatorDlg dialog

CCalculatorDlg::CCalculatorDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCalculatorDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCalculatorDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CCalculatorDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CCalculatorDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON3, &CCalculatorDlg::OnBnClickedButton3)
ON_BN_CLICKED(IDC_BUTTON2, &CCalculatorDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON6, &CCalculatorDlg::OnBnClickedButton6)
ON_BN_CLICKED(IDC_BUTTON5, &CCalculatorDlg::OnBnClickedButton5)
ON_BN_CLICKED(IDC_BUTTON4, &CCalculatorDlg::OnBnClickedButton4)
ON_BN_CLICKED(IDC_BUTTON9, &CCalculatorDlg::OnBnClickedButton9)
ON_BN_CLICKED(IDC_BUTTON8, &CCalculatorDlg::OnBnClickedButton8)
ON_BN_CLICKED(IDC_BUTTON7, &CCalculatorDlg::OnBnClickedButton7)
 END_MESSAGE_MAP()
 // CCalculatorDlg message handlers
     BOOL CCalculatorDlg::OnInitDialog()
    {
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
        pSysMenu->AppendMenu(MF_SEPARATOR);
        pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
}
// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);         // Set big icon
SetIcon(m_hIcon, FALSE);        // Set small icon
// TODO: Add extra initialization here
return TRUE;  // return TRUE  unless you set the focus to a control
 }
 void CCalculatorDlg::OnSysCommand(UINT nID, LPARAM lParam)
 {
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
}
else
{
    CDialog::OnSysCommand(nID, lParam);
}
 }
 // If you add a minimize button to your dialog, you will need the code below
 //  to draw the icon.  For MFC applications using the document/view model,
 //  this is automatically done for you by the framework.
void CCalculatorDlg::OnPaint()
{
if (IsIconic())
{
    CPaintDC dc(this); // device context for painting
    SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
    // Center icon in client 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
{
    CDialog::OnPaint();
}
  }
 // The system calls this function to obtain the cursor to display while the user drags
 //  the minimized window.
 HCURSOR CCalculatorDlg::OnQueryDragIcon()
 {
return static_cast<HCURSOR>(m_hIcon);
  }

 void CCalculatorDlg::OnBnClickedButton1()
{
/*
CString s="ABC";
    LPCTSTR str_name = _T("Hello ")*/;
    SetDlgItemText(IDC_EDIT1,_T"9");
/*CAboutDlg::myValue="9";*/
}
void CCalculatorDlg::OnBnClickedButton3(){}
void CCalculatorDlg::OnBnClickedButton2(){}
void CCalculatorDlg::OnBnClickedButton6(){}
void CCalculatorDlg::OnBnClickedButton5(){}
void CCalculatorDlg::OnBnClickedButton4(){}
void CCalculatorDlg::OnBnClickedButton9(){}
void CCalculatorDlg::OnBnClickedButton8(){}
void CCalculatorDlg::OnBnClickedButton7(){}

如果您的项目使用预编译标头(99.9999% 的概率将其用作默认 MFC 项目设置),则可以在预编译头文件中声明此变量,通常其名称为 stdafx.h,并在任何相应翻译单元(.cpp 文件)的全局范围内定义它。通常,这可以是包含派生自CWinApp的应用程序类YourProjectName.cpp文件。

// stdafx.h
extern int globalVar; // Global variable declaration, note extern keyword
// YourProjectName.cpp - global scope
int globalVar = 42; // Global variable definition   

预编译头stdafx.h首先包含在项目的每个.cpp文件中,因此globalVar将在项目代码中的任何位置可用。

还想提一下,不建议使用全局对象,它被认为是糟糕的设计和反模式。

将全局变量放在头文件中,并将其添加到命名强制包含文件。