⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 singleinstance.cpp

📁 本程序是VC为平台开发的股票资讯系统
💻 CPP
字号:
// SingleInstance.cpp : implementation file
//

#include "stdafx.h"
#include "StockRefer.h"
#include "SingleInstance.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSingleInstance

CSingleInstance::CSingleInstance()
{
	m_hMutex = NULL;
}

CSingleInstance::~CSingleInstance()
{
	if(m_hMutex != NULL) 
		ReleaseMutex(m_hMutex);
}

/////////////////////////////////////////////////////////////////////////////
// CSingleInstance message handlers

BOOL CSingleInstance::Create( UINT nID )
{
	CString strFullString;

	// Create our class name string
	if ( strFullString.LoadString( nID ) ) {
		// Extract the first sub-string
		AfxExtractSubString( m_strClassName, strFullString, 0 );
	}

	// Add the word 'Class' to the end
	m_strClassName += _T(" Class");

	// Create the mutex
	m_hMutex = CreateMutex( NULL, FALSE, m_strClassName );
	// Check for errors
	if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
		// Reset our mutext handle (just in case)
		m_hMutex = NULL;
		// The mutex already exists, which means an instance is already
		// running. Find the app and pop it up
		HWND hWnd = FindWindowEx( NULL, NULL, m_strClassName, NULL );
		if ( hWnd != NULL ) {
			ShowWindow( hWnd, SW_RESTORE );
			BringWindowToTop( hWnd );
			SetForegroundWindow( hWnd );
		}
		// Return failure
		return FALSE;
	}

	// Register the unique window class name so others can find it.
	WNDCLASS wndcls;    memset(&wndcls, 0, sizeof(WNDCLASS));
	wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
	wndcls.lpfnWndProc = AfxWndProc;
	wndcls.hInstance = AfxGetInstanceHandle();
	wndcls.hIcon = LoadIcon( wndcls.hInstance, MAKEINTRESOURCE( nID ) );//or AFX_IDI_STD_FRAME; 
	wndcls.hCursor = LoadCursor( wndcls.hInstance, IDC_ARROW );
	wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
	wndcls.lpszMenuName = NULL;//You may need to fix this
	wndcls.lpszClassName = m_strClassName; // my class name
	// Register name, exit if it fails
	if(!AfxRegisterClass(&wndcls)) {
		AfxMessageBox( _T("Failed to register window class!"), MB_ICONSTOP | MB_OK );
		return FALSE;
	}

	// Return success
	return TRUE;
}

CString CSingleInstance::GetClassName( void ) const
{
	return m_strClassName;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -