C++ (MFC) 单程序运行(防止多开程序)

C++ (MFC) 单程序运行(防止多开程序)

项目文件名:MFCAppTest

在 C*****App.cpp 文件中

CMFCAppTestApp::InitInstance 函数中
添加以下代码

	//避免程序的多开  xxxx为信号量的名字 可随意
	CreateMutex(NULL, TRUE, TEXT("MFCAppTest")); 
	if (GetLastError() == ERROR_ALREADY_EXISTS)
	{
		//windowname为你的主窗体的标题,当然你也可以通过进程来找到主窗体。
		CWnd* cwnd = CWnd::FindWindow(NULL, TEXT("MFCAppTest"));
		if (cwnd)//显示原先的主界面
		{
			cwnd->ShowWindow(SW_SHOWNORMAL);
			cwnd->SetForegroundWindow();
		}
		return FALSE;
	}

完整代码


// MFCAppTest.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "MFCAppTest.h"
#include "MFCAppTestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCAppTestApp

BEGIN_MESSAGE_MAP(CMFCAppTestApp, CWinApp)
	ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()


// CMFCAppTestApp construction

CMFCAppTestApp::CMFCAppTestApp()
{
	// support Restart Manager
	m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;

	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}


// The one and only CMFCAppTestApp object

CMFCAppTestApp theApp;


// CMFCAppTestApp initialization

BOOL CMFCAppTestApp::InitInstance()
{
	//避免程序的多开  xxxx为信号量的名字 可随意
	CreateMutex(NULL, TRUE, TEXT("MFCAppTest")); 
	if (GetLastError() == ERROR_ALREADY_EXISTS)
	{
		//windowname为你的主窗体的标题,当然你也可以通过进程来找到主窗体。
		CWnd* cwnd = CWnd::FindWindow(NULL, TEXT("MFCAppTest"));
		if (cwnd)//显示原先的主界面
		{
			cwnd->ShowWindow(SW_SHOWNORMAL);
			cwnd->SetForegroundWindow();
		}
		return FALSE;
	}

	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	INITCOMMONCONTROLSEX InitCtrls;
	InitCtrls.dwSize = sizeof(InitCtrls);
	// Set this to include all the common control classes you want to use
	// in your application.
	InitCtrls.dwICC = ICC_WIN95_CLASSES;
	InitCommonControlsEx(&InitCtrls);

	CWinApp::InitInstance();


	AfxEnableControlContainer();

	// Create the shell manager, in case the dialog contains
	// any shell tree view or shell list view controls.
	CShellManager *pShellManager = new CShellManager;

	// Activate "Windows Native" visual manager for enabling themes in MFC controls
	CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	// of your final executable, you should remove from the following
	// the specific initialization routines you do not need
	// Change the registry key under which our settings are stored
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	CMFCAppTestDlg dlg;
	m_pMainWnd = &dlg;
	INT_PTR nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}
	else if (nResponse == -1)
	{
		TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n");
		TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
	}

	// Delete the shell manager created above.
	if (pShellManager != NULL)
	{
		delete pShellManager;
	}

#ifndef _AFXDLL
	ControlBarCleanUp();
#endif

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}


注意:

如果出现 CreateMutex 未定义
使用:CreateMutexW(Unicode)或CreateMutexA替换

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
c++中的MFC基础知识十分必要,理解MFC的实现原理将有助于更好地掌握它的使用方法。一名程序员需要熟悉并掌握MFC中的各种类和方法,这样才能够写出自己需要的程序。在MFC中,要编写计算器程序需要涉及到很多的类,在设计时要充分考虑程序的结构和设计。计算器程序需要有一些基本的功能,如加减乘除、等于、退格、清空等。要实现这些功能,还需要处理数字的输入和显示,以及错误提示等问题。 在MFC中,可以利用CWnd类来设计计算器的用户界面,利用CEdit类处理数字的输入和显示,利用CButton类来处理各个按钮的操作,如加减乘除、等于、退格、清空等。同时,还需要利用CString类来处理计算的结果,利用CException类来处理错误的提示。 在编写计算器程序时,需要充分考虑程序的可读性和可复用性。程序应该按照逻辑分析和功能分区的原则来组织代码,采用模块设计和面向对象的方法实现各个操作和功能。程序的可读性和可复用性不仅可以提高程序的效率,而且还能够减少代码的冗余程度,使代码更加简洁易懂。 总的来说,编写MFC计算器程序需要充分掌握MFC的基础知识,理解窗口、消息、控件、消息映射等概念。在程序设计时要考虑好程序的结构和设计,使用面向对象的方法来实现各个功能。这样才能够写出高效、简洁、易读的程序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

廷益--飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值