在哪里可以在 MFC 应用程序中定义全局变量?
Posted
技术标签:
【中文标题】在哪里可以在 MFC 应用程序中定义全局变量?【英文标题】:Where I can define a global variable in an MFC application? 【发布时间】:2012-09-23 15:00:39 【问题描述】:我想在 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()
【问题讨论】:
与您在常规应用程序中的定义完全相同。 全局变量的具体用途是什么?避免使用全局变量是向任何学习编程的人发出的常见建议。 - 我建议阅读并理解这个问题的答案***.com/questions/484635/… 然后退后一步看看替代方案 我想声明一个字符串变量,在许多动作之间共享, 【参考方案1】:如果您的项目使用预编译头文件(有 99.9999% 的可能性将其用作默认 MFC 项目设置),您可以在预编译头文件中声明此变量,通常其名称为 stdafx.h
和 在任何适当的翻译单元(.cpp
文件)的全局范围内定义它。通常这可能是 YourProjectName.cpp
文件,其中包含从 CWinApp
派生的应用程序类。
// 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
将在您的项目代码中随处可用。
另外还要提一下,不推荐使用全局对象,认为是bad design and anti-pattern。
【讨论】:
我在 stdafx.h 中添加了变量,但它没有编译 @Eslam 什么是变量类型?你把它放在stdafx.h的什么地方?最合适的地方是 stdafx.h 的末尾。【参考方案2】:将全局变量放在头文件中,并添加到Name Forced Include File。
【讨论】:
以上是关于在哪里可以在 MFC 应用程序中定义全局变量?的主要内容,如果未能解决你的问题,请参考以下文章