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

📄 plug_in_2.cpp

📁 这是一本学习 window编程的很好的参考教材
💻 CPP
字号:
// Plug_In_2.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "Plug_In_2.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#include "resource.h"		// main symbols

//
//	Note!
//
//		If this DLL is dynamically linked against the MFC
//		DLLs, any functions exported from this DLL which
//		call into MFC must have the AFX_MANAGE_STATE macro
//		added at the very beginning of the function.
//
//		For example:
//
//		extern "C" BOOL PASCAL EXPORT ExportedFunction()
//		{
//			AFX_MANAGE_STATE(AfxGetStaticModuleState());
//			// normal function body here
//		}
//
//		It is very important that this macro appear in each
//		function, prior to any calls into MFC.  This means that
//		it must appear as the first statement within the 
//		function, even before any object variable declarations
//		as their constructors may generate calls into the MFC
//		DLL.
//
//		Please see MFC Technical Notes 33 and 58 for additional
//		details.
//

#define PLUG_API __declspec(dllexport)

// The one and only CPlug_In_2App object
CPlug_In_2App theApp;

// ********************************************************************************
// These need to be changed by different DLLs toi use there own buttons and bitmaps
#define ID_BUTTON ID_BUTTON_PLUGIN_2
#define ID_BITMAP IDB_PLUGIN_2
const char szName[] = "Calculator";
// ********************************************************************************

// Exported Plug-In methods
extern "C" PLUG_API UINT InitPlugIn(CToolBar *pwndToolbar);
extern "C" PLUG_API void DestroyPlugIn();
extern "C" PLUG_API bool PlugInEventHandler (UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo);
extern "C" PLUG_API void GetPlugInResources(HICON *hIcon, CString *szLabel);
extern "C" PLUG_API void GetPlugInName(CString *szPlugInName);

// Used to get the name of the Plug-In
PLUG_API void GetPlugInName(CString *szPlugInName)
{
	*szPlugInName = szName;
}

// Used to initlialize the Plug-In and various resources
PLUG_API UINT InitPlugIn(CToolBar *pwndToolBar)
{
#ifdef DEBUG
	TRACE("Called DLL's InitPlugIn() method\n");
#endif

	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	theApp.SetToolBarPointer(pwndToolBar);
	theApp.AddButtonToToolBar();

#ifdef DEBUG
	TRACE("Plug_In_1_Funtion_INIT_PLUG_IN() called...\n");
#endif

	return ID_BUTTON;
}

// Used to destroy the Plug-In and free resources help by it
extern "C" PLUG_API void DestroyPlugIn()
{
#ifdef DEBUG
	TRACE("Called DLL's DestroyPlugIn() method\n");
#endif

	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	theApp.RemoveButtonFromToolBar();

#ifdef DEBUG
	TRACE("Plug_In_1_Funtion_DESTROY_PLUG_IN() called...\n");
#endif
}

// Event handler function. Redirects call to CPlug_In_1App::OnCmdMsg()
PLUG_API bool PlugInEventHandler (UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
#ifdef DEBUG
	TRACE("Called DLL's PlugInEventHandler() method\n");
#endif

	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	if (nID == ID_BUTTON)
		theApp.SendCmdMsgToPlugIn(nID, nCode, pExtra, pHandlerInfo);

	return true;
}

// Used to get the various resources from the Plug-In. Not used till now.
PLUG_API void GetPlugInResources(HICON *hIcon, CString *szLabel)
{
	hIcon = theApp.GetPlugInIcon();
	*szLabel = "Plug-In 1 Icon";
}

// CPlug_In_2App
BEGIN_MESSAGE_MAP(CPlug_In_2App, CWinApp)
END_MESSAGE_MAP()

// CPlug_In_2App construction
CPlug_In_2App::CPlug_In_2App()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
#ifdef DEBUG
	TRACE("Called CPlug_In_2App::CPlug_In_2App() method\n");
#endif

	m_pwndToolBar = NULL;
}

CPlug_In_2App::~CPlug_In_2App()
{
#ifdef DEBUG
	TRACE("Called CPlug_In_2App::~CPlug_In_2App() method\n");
#endif

	// Remove button added by this DLL from the main app's default toolbar
//	RemoveButtonFromToolBar();

	// Set toolbar pointer to point to NULL;
	m_pwndToolBar = NULL;
}

// CPlug_In_2App initialization
BOOL CPlug_In_2App::InitInstance()
{
#ifdef DEBUG
	TRACE("Called CPlug_In_2App::InitInstance() method\n");
#endif

	CWinApp::InitInstance();

	return TRUE;
}

// Used to set the toolbar object pointer
void CPlug_In_2App::SetToolBarPointer(CToolBar *pwndToolBar)
{
#ifdef DEBUG
	TRACE("Called CPlug_In_2App::SetToolBarpointer() method\n");
#endif

	m_pwndToolBar = pwndToolBar;
}

// Used to add button to main app's toolbar
void CPlug_In_2App::AddButtonToToolBar()
{
#ifdef DEBUG
	TRACE("Called CPlug_In_2App::AddButtonToToolBar() method\n");
#endif

	CToolBarCtrl& pToolbarCtrl = m_pwndToolBar->GetToolBarCtrl();
//	ASSERT(pToolbarCtrl != NULL);

	// Get the number of buttons in existing 
	// toolbar control.
	int nButCount = pToolbarCtrl.GetButtonCount();

	// Add a new button in it. 
	// IDB_NEW_BUTTON is a bitmap resource
	int nImageCount = pToolbarCtrl.AddBitmap(1, ID_BITMAP);

	// get the number of images in existing toolbar
	//int nImageCount = pToolbarCtrl.GetImageList()->GetImageCount();

	// Define a new button
	TBBUTTON tb;

	// Index of new button image.
	tb.iBitmap = nImageCount;

	// Command associated with toolbar button
	tb.idCommand = ID_BUTTON;

	// Setting button state
	tb.fsState = TBSTATE_ENABLED;

	// Setting button style
	tb.fsStyle = TBSTYLE_BUTTON;
	tb.dwData = 0;
	tb.iString = NULL;

	// Insert it in existing toolbar control
		TRACE ("BUTTON ID: %d\n", ID_BUTTON);
		TRACE ("BUTTON BITMAP: %d\n", tb.iBitmap);
	pToolbarCtrl.InsertButton(nButCount, &tb);
}

// Used to remove button from main app's toolbar
void CPlug_In_2App::RemoveButtonFromToolBar()
{
#ifdef DEBUG
	TRACE("Called CPlug_In_2App::RemoveButtonFromToolBar() method\n");
#endif

	if (m_pwndToolBar != NULL)
	{
		CToolBarCtrl& pToolbarCtrl = m_pwndToolBar->GetToolBarCtrl();

		TBBUTTON tbButton;

		int nButtonCount = pToolbarCtrl.GetButtonCount();
		// Get info for the button added by this DLL
		for (int i=0; i<nButtonCount; ++i)
		{
			pToolbarCtrl.GetButton(i, &tbButton);
			if (tbButton.idCommand == ID_BUTTON)
			{
				TRACE ("BUTTON ID: %d\n", ID_BUTTON);
				TRACE ("BUTTON BITMAP: %d\n", tbButton.iBitmap);

	/*			CString s;
				s.Format("PREV: %d", pToolbarCtrl.GetImageList()->GetImageCount());
				AfxMessageBox(s);
				pToolbarCtrl.GetImageList()->Remove(tbButton.iBitmap);
				s.Format("AFTER: %d", pToolbarCtrl.GetImageList()->GetImageCount());
				AfxMessageBox(s);*/

	//			pToolbarCtrl.GetImageList()->SetImageCount(pToolbarCtrl.GetImageList()->GetImageCount() - 1);

				if (pToolbarCtrl.DeleteButton(i) == 0)
				{
	#ifdef DEBUG
					TRACE("ERROR: Toolbar button NOT deleted\n");
	#endif
				}
				else
				{
	#ifdef DEBUG
					TRACE("Toolbar button deleted\n");
	#endif
				}
			}
		}
	}
}

int CPlug_In_2App::ExitInstance()
{
	// TODO: Add your specialized code here and/or call the base class
#ifdef DEBUG
	TRACE("Called CPlug_In_2App::ExitInstance() method\n");
#endif

	return CWinApp::ExitInstance();
}

// Used by main app to get info about the button added my this DLL
UINT CPlug_In_2App::GetToolBarButtonInfo(void)
{
#ifdef DEBUG
	TRACE("Called CPlug_In_2App::GetToolBarButtonInfo() method\n");
#endif

	return ID_BUTTON;
}

void CPlug_In_2App::SendCmdMsgToPlugIn(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
#ifdef DEBUG
	TRACE("Called CPlug_In_2App::SendCmdMsgToPlugIn() method\n");
#endif

	OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

BOOL CPlug_In_2App::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
#ifdef DEBUG
	TRACE("Called CPlug_In_2App::OnCmdMsg() method\n");
#endif

	if (WinExec("Calc.exe", SW_SHOW) <= 31)
	{
		AfxMessageBox("Caculator.exe could not be executed");
	}

	// TODO: Add your specialized code here and/or call the base class
//	AfxMessageBox("PLUGIN 2: Event caught by handler\n");
	return CWinApp::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}

// Used to get the handle to the icon dor the Plug-In
HICON *CPlug_In_2App::GetPlugInIcon(void)
{
	return (HICON *) LoadIcon(IDI_PLUGIN_2_CALCULATOR);
}

⌨️ 快捷键说明

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