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

📄 plug_in_1.cpp

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

#include "stdafx.h"
#include "Plug_In_1.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_1App object
CPlug_In_1App theApp;

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

// 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 initialize 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
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_1App
BEGIN_MESSAGE_MAP(CPlug_In_1App, CWinApp)
END_MESSAGE_MAP()

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

	m_pwndToolBar = NULL;
}

CPlug_In_1App::~CPlug_In_1App()
{
	// TODO: Add your specialized code here and/or call the base class
#ifdef DEBUG
	TRACE("Called CPlug_In_1App::~CPlug_In_1App() 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_1App initialization
BOOL CPlug_In_1App::InitInstance()
{
#ifdef DEBUG
	TRACE("Called CPlug_In_1App::InitInstance() method\n");
#endif

	CWinApp::InitInstance();

	return TRUE;
}

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

	m_pwndToolBar = pwndToolBar;
}

// Used to add button to main app's toolbar
void CPlug_In_1App::AddButtonToToolBar()
{
#ifdef DEBUG
	TRACE("Called CPlug_In_1App::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_1App::RemoveButtonFromToolBar()
{
#ifdef DEBUG
	TRACE("Called CPlug_In_1App::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);
	*/
				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_1App::ExitInstance()
{
	// TODO: Add your specialized code here and/or call the base class
#ifdef DEBUG
	TRACE("Called CPlug_In_1App::ExitInstance() method\n");
#endif

	return CWinApp::ExitInstance();
}

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

	return ID_BUTTON;
}

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

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

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

	if (WinExec("Notepad.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 1: 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_1App::GetPlugInIcon(void)
{
	return (HICON *) LoadIcon(IDI_PLUGIN_1_CLOCK);
}

// *********************** Temporary *************************
// Used to modify the Main app's toolbar
/*void UpdateToolBar(CToolBar *pwndToolbar)
{
	int		nIndex = 0;
	TBBUTTON	tb;

	// Get toolbar control
	CToolBarCtrl& bar = pwndToolbar->GetToolBarCtrl();

	for (nIndex = bar.GetButtonCount() -1; nIndex >= 0; nIndex--)
	{
		ZeroMemory(&tb, sizeof(TBBUTTON));
		bar.GetButton(nIndex, &tb);

		// Do we have a separator?
		if ((tb.fsStyle & TBSTYLE_SEP) ==  TBSTYLE_SEP)
			continue;

		// Have we got a valid command id?
		if (tb.idCommand == 0)
			continue;

		// Get the resource string if there is one.
		CString strText;
		LPCTSTR lpszButtonText = NULL;
		CString	strButtonText(_T(""));
		_TCHAR	seps[] = _T("\n");

		strText.LoadString(tb.idCommand);

		if (!strText.IsEmpty())
		{
			lpszButtonText = _tcstok((LPTSTR)(LPCTSTR)strText, seps);
			while(lpszButtonText)
			{
				strButtonText = lpszButtonText;
				lpszButtonText = _tcstok(NULL, seps);
			}
		}

		if (!strButtonText.IsEmpty())
			pwndToolbar->SetButtonText(nIndex, strButtonText);
	}

	// Resize the buttons so that the text will fit.
	CRect rc(0, 0, 0, 0);
	CSize sizeMax(0, 0);

	for (nIndex = bar.GetButtonCount() - 1; nIndex >= 0; nIndex--)
	{
		bar.GetItemRect(nIndex, rc);
		rc.NormalizeRect();
		sizeMax.cx = __max(rc.Size().cx, sizeMax.cx);
		sizeMax.cy = __max(rc.Size().cy, sizeMax.cy);
	}

	pwndToolbar->SetSizes(sizeMax, CSize(16,15));
}
*/

⌨️ 快捷键说明

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