?? plugin_one.cpp
字號:
// PlugIn_one.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "PlugIn_one.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////
// CPlugIn_oneApp
//定義按鈕標識
#define ID_BUTTON_PLUGIN_CALC 12001
//定義插件名稱
const TCHAR szPlugName[] = _T("計算器");
#define PLUG_EXPORT __declspec(dllexport)
//初始化插件,這里主要設置主程序ToolBar指針
extern "C" PLUG_EXPORT UINT InitPlugIn(CToolBar *pWndToolbar);
//釋放插件,主要是刪除掉ToolBar上的按鈕
extern "C" PLUG_EXPORT void DestroyPlugIn();
//當點擊ToolBar上的按鈕事件
extern "C" PLUG_EXPORT void HandleOnClick();
//得到插件圖標和插件標題
extern "C" PLUG_EXPORT void GetPlugInResources(HICON *hIcon, CString *szLabel);
//得到插件名
extern "C" PLUG_EXPORT void GetPlugInName(CString *szPlugInName);
BEGIN_MESSAGE_MAP(CPlugIn_oneApp, CWinApp)
//{{AFX_MSG_MAP(CPlugIn_oneApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// The one and only CPlugIn_oneApp object
CPlugIn_oneApp theApp;
/*
*函數介紹:初始化插件,這里主要設置主程序ToolBar指針
*入口參數:pWndToolBar : 指向主程序的ToolBar指針
*出口參數:無
*返回值: 無
*/
PLUG_EXPORT UINT InitPlugIn(CToolBar *pWndToolBar)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
theApp.SetToolBarPointer(pWndToolBar);
theApp.AddButtonToToolBar();
return ID_BUTTON_PLUGIN_CALC;
}
/*
*函數介紹:釋放插件,主要是刪除掉ToolBar上的按鈕
*入口參數:無
*出口參數:無
*返回值: 無
*/
PLUG_EXPORT void DestroyPlugIn()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
theApp.RemoveButtonFromToolBar();
}
/*
*函數介紹:當點擊ToolBar上的按鈕事件
*入口參數:無
*出口參數:無
*返回值: 無
*/
PLUG_EXPORT void HandleOnClick()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (!theApp.WinExec(_T("calc")))
{
AfxMessageBox(_T("計算器程序未被執行"));
}
}
/*
*函數介紹:得到插件圖標和插件標題
*入口參數:無
*出口參數:hIcon : 插件圖標句柄
szLabel : 插件標題
*返回值: 無
*/
PLUG_EXPORT void GetPlugInResources(HICON *hIcon, CString *szLabel)
{
hIcon = theApp.GetPlugInIcon();
*szLabel = _T("計算器插件");
}
/*
*函數介紹:得到插件名
*入口參數:無
*出口參數:szPlugInName : 插件名
*返回值: 無
*/
PLUG_EXPORT void GetPlugInName(CString *szPlugInName)
{
*szPlugInName = szPlugName;
}
/////////////////////////////////////////////////////////////////////////////
// CPlugIn_oneApp construction
CPlugIn_oneApp::CPlugIn_oneApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/*
*函數介紹:用于設置主窗體工具欄指針
*入口參數:pWndToolBar : 指向主程序的ToolBar指針
*出口參數:無
*返回值: 無
*/
void CPlugIn_oneApp::SetToolBarPointer(CToolBar *pwndToolBar)
{
m_pwndToolBar = pwndToolBar;
}
/*
*函數介紹:用于添加按鈕到主窗體工具欄上
*入口參數:無
*出口參數:無
*返回值: 無
*/
void CPlugIn_oneApp::AddButtonToToolBar()
{
CToolBarCtrl& pToolbarCtrl = m_pwndToolBar->GetToolBarCtrl();
//得到工具欄已有的按鈕數
int nButCount = pToolbarCtrl.GetButtonCount();
//將插件按鈕位圖添加到工具欄上
int nImageCount = pToolbarCtrl.AddBitmap(1, IDB_PLUGIN_CALC);
//定義一個新按鈕
TBBUTTON tb;
//設置按鈕按鈕圖片
tb.iBitmap = nImageCount;
//設置按鈕標識
tb.idCommand = ID_BUTTON_PLUGIN_CALC;
//設置按鈕狀態
tb.fsState = TBSTATE_ENABLED;
//設置按鈕演示
tb.fsStyle = TBSTYLE_BUTTON;
tb.dwData = 0;
tb.iString = NULL;
//將按鈕添加到工具欄上
pToolbarCtrl.InsertButton(nImageCount, &tb);
}
/*
*函數介紹:用于從主窗體上刪除掉添加的按鈕
*入口參數:無
*出口參數:無
*返回值: 無
*/
void CPlugIn_oneApp::RemoveButtonFromToolBar()
{
if (m_pwndToolBar != NULL)
{
CToolBarCtrl& pToolbarCtrl = m_pwndToolBar->GetToolBarCtrl();
TBBUTTON tbButton;
int nButtonCount = pToolbarCtrl.GetButtonCount();
for (int i=0; i<nButtonCount; ++i)
{
pToolbarCtrl.GetButton(i, &tbButton);
//當按鈕是本插件按鈕時,則刪除之。
if (tbButton.idCommand == ID_BUTTON_PLUGIN_CALC)
{
pToolbarCtrl.DeleteButton(i);
break;
}
}
}
}
/*
*函數介紹:用于得到插件按鈕的標識
*入口參數:無
*出口參數:無
*返回值: 插件按鈕標識
*/
UINT CPlugIn_oneApp::GetToolBarButtonInfo(void)
{
return ID_BUTTON_PLUGIN_CALC;
}
/*
*函數介紹:用于得到插件的圖標
*入口參數:pWndToolBar : 指向主程序的ToolBar指針
*出口參數:無
*返回值: 圖標句柄
*/
HICON *CPlugIn_oneApp::GetPlugInIcon(void)
{
return (HICON *)LoadIcon(IDI_PLUGIN_CALC);
}
/*
*函數介紹:用于執行外部程序
*入口參數:FileName : 表示可執行文件名
*出口參數:無
*返回值: TRUE:執行成功,FALSE:執行失敗
*/
bool CPlugIn_oneApp::WinExec(LPCTSTR FileName)
{
PROCESS_INFORMATION processInfo; //進程相關信息
if (!CreateProcess(FileName, NULL, NULL, NULL, NULL
, CREATE_NEW_CONSOLE
, NULL, NULL, NULL, &processInfo))
{
return false;
}
CloseHandle(processInfo.hThread);
CloseHandle(processInfo.hProcess);
return true ;
}