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

📄 asdk_menubar.cpp

📁 作工具条用 我的新程序
💻 CPP
字号:
// Asdk_MenuBar.cpp : 定义 DLL 的初始化例程。
//

#include "stdafx.h"
#include "Asdk_MenuBar.h"
#include "acadi.h"


#include "acadi_i.c"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

HINSTANCE _hdllInstance = NULL;

extern Adesk::Boolean acedPostCommand(const char *pCmd);

// Define the sole extension module object.
AC_IMPLEMENT_EXTENSION_MODULE(ThisDLL);

// Functions prototype
void LoadMenuFile();

// DLL Entry Point
extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
{
	if (dwReason == DLL_PROCESS_ATTACH)
	{
		// Extension DLL one time initialization
		ThisDLL.AttachInstance(hInstance);
		_hdllInstance = hInstance;
	}else if (dwReason == DLL_PROCESS_DETACH) {
		// Terminate the library before destructors are called
		ThisDLL.DetachInstance();
	}
	return TRUE;	// ok
}

bool getApplication(LPDISPATCH * pVal)
{
	LPDISPATCH pDispatch = acedGetAcadWinApp()->GetIDispatch(TRUE);
	if (pDispatch == NULL)
		return false;
	*pVal = pDispatch;
	return true;
}

bool getAcadMenuGroup(IAcadMenuGroup **pVal)
{
	IAcadApplication *acadApp = NULL;
	LPDISPATCH pDisp = NULL;

	if (!getApplication(&pDisp))
	{
		acadApp->Release();
		return false;
	}
	HRESULT hr = S_OK;
	hr = pDisp->QueryInterface(IID_IAcadApplication, (LPVOID*)&acadApp);
	if (FAILED(hr))
	{
		acadApp->Release();
		return false;
	}

	LPDISPATCH pTempDisp = NULL;
	IAcadMenuGroups *mnuGrps = NULL;
	long cnt = 0;
	//get the menu groups
	hr = acadApp->get_MenuGroups(&mnuGrps);
	if (FAILED(hr))
	{
		acadApp->Release();
		return false;
	}
	acadApp->Release();
	mnuGrps->get_Count(&cnt);
	//get AutoCAD menu group. say it is index 0.
	IAcadMenuGroup *mnuGrp = NULL;

	VARIANT vtName;
	vtName.vt = VT_I4;
	BSTR grpName;
	bool found = false;
	for (long i = 0; i < cnt; i++)
	{
		vtName.lVal = i;
		hr = mnuGrps->Item(vtName, &mnuGrp);
		if (FAILED(hr))
			return false;

		hr = mnuGrp->get_Name(&grpName);
		CString cgrpName(grpName);
		if (cgrpName.CompareNoCase("Acad") == 0)
		{
			found = true;
			*pVal = mnuGrp;
			break;
		}
	}
	acadApp->Release();
	return found;
}


//////////////////////////////////////////////////////////////////////////////
// Toolbar Operations

bool getToolBar(IAcadToolbars **pVal, CString pName)
{
	IAcadToolbars *tlbrs;
	tlbrs = *pVal;
	HRESULT hr = S_OK;
	long cnt = 0l;
	hr = tlbrs->get_Count(&cnt);

	IAcadToolbar *tlbr = NULL;

	BSTR tempName;
	VARIANT vtName;
	bool found = false;
	for (long i = 0; i < cnt; i++)
	{
		vtName.vt = VT_I4;
		vtName.lVal = i;
		hr = tlbrs->Item(vtName, &tlbr);
		if (FAILED(hr))
			return found;

		hr = tlbr->get_Name(&tempName);
		CString tlbName(tempName);
		if(tlbName.CompareNoCase(pName) == 0)
		{
			found = true;
			break;
		}
		tlbr->Release();
	}
	//tlbrs->Release();
	if (!found)
	{
		acutPrintf("\n没有发现[%s]工具条!", pName);
		return found;
	}
	return found;
}

void CreateToolbar()
{
	IAcadMenuGroup *mnuGrp = NULL;
	if (!getAcadMenuGroup(&mnuGrp))
		return;
	//取得所有Toolbar
	IAcadToolbars *tlbrs = NULL;
	HRESULT hr = S_OK;
	hr = mnuGrp->get_Toolbars(&tlbrs);
	mnuGrp->Release();

	if (getToolBar(&tlbrs, "地形图缩编"))
	{
		tlbrs->Release();
		acutPrintf("\n[地形图缩编]工具条已经存在!");
		return;
	}
	//创建Toolbar
	IAcadToolbar *tlbr = NULL;
	hr = tlbrs->Add(L"地形图缩编", &tlbr);
	if (FAILED(hr))
		return;

	tlbrs->Release();
	//添加Button
	IAcadToolbarItem *button1 = NULL;
	IAcadToolbarItem *button2 = NULL;
	IAcadToolbarItem *button3 = NULL;
	IAcadToolbarItem *button4, *button5, *button6, *button7, *button8 = NULL;

	long cnt;
	hr = tlbr->get_Count(&cnt);

	VARIANT index;
	index.vt = VT_I4;
	index.lVal = cnt + 1;

	VARIANT vtFalse;
	vtFalse.vt = VT_BOOL;
	vtFalse.boolVal = VARIANT_FALSE;

	//取得ARX模块路径
	TCHAR appFullPath[MAX_PATH];
	int len = GetModuleFileName(_hdllInstance, appFullPath, MAX_PATH);

	CString strBmpPath = appFullPath;
	strBmpPath = strBmpPath.Left(strBmpPath.ReverseFind('\\')) + "\\bmp";

	CComBSTR smallIconName;
	CComBSTR largeIconName;

	hr = tlbr->AddToolbarButton(index, L"PALETTEON", L"打开工具选项板", L"_PALETTEON ", vtFalse, &button1);
	smallIconName = strBmpPath + "\\paletteon.bmp";
	largeIconName = strBmpPath + "\\paletteon.bmp";

	button1->SetBitmaps(smallIconName, largeIconName);
	button1->Release();

    index.lVal = cnt + 2;
	hr = tlbr->AddToolbarButton(index, L"PALETTEOFF", L"关闭工具选项板", L"_PALETTEOFF ", vtFalse, &button2);
	smallIconName = strBmpPath + "\\paletteoff.bmp";
	largeIconName = strBmpPath + "\\paletteoff.bmp";
	button2->SetBitmaps(smallIconName, largeIconName);
	button2->Release();

	//先加载菜单,再在其前面加载Separator
	index.lVal = cnt + 3;
	hr = tlbr->AddToolbarButton(index, L"OTHER", L"OTHER", L"_OTHER", vtFalse, &button3);
	button3->SetBitmaps(smallIconName, largeIconName);
	button3->Release();

	index.lVal = cnt + 3;
	hr = tlbr->AddSeparator(index, &button4);
	button4->Release();

	index.lVal = cnt + 4;
	hr = tlbr->AddToolbarButton(index, L"OTHER2", L"OTHER2", L"_OTHER2", vtFalse, &button5);
	button5->SetBitmaps(smallIconName, largeIconName);
	button5->Release();

	index.lVal = cnt + 4;
	hr = tlbr->AddSeparator(index, &button6);
	button6->Release();

	//定义菜单浮动(停泊)位置	
	//tlbr->Dock(acToolbarDockTop);
	hr = tlbr->Float(120,720,1);
	int nLeft, nTop;
	hr = tlbr->get_Left(&nLeft);
	hr = tlbr->get_Top(&nTop);
	if (FAILED(hr))
		acutPrintf("\n浮动位置不确定!");
	else if(SUCCEEDED(hr))
		acutPrintf("\n%浮动位置:( %d, %d )", nTop, nLeft);
	//设置可见性
	VARIANT_BOOL bFlag = VARIANT_TRUE;
	tlbr->put_Visible(bFlag);

	tlbr->Release();
	//
	acutPrintf("\n[地形图缩编]工具条加载成功!");
	acutPrintf("\n");

	return;
}


static void InitApp()
{
	CreateToolbar();
	LoadMenuFile();
}

static void UnloadApp()
{
}

void LoadMenuFile()
{
	// 取得AutoCAD应用程序对象
	IAcadApplication *acadApp = NULL;
	LPDISPATCH pDisp = NULL;

	if (!getApplication(&pDisp))
	{
		acadApp->Release();
		return;
	}
	HRESULT hr = S_OK;
	hr = pDisp->QueryInterface(IID_IAcadApplication, (LPVOID*)&acadApp);
	if (FAILED(hr))
	{
		acadApp->Release();
		return;
	}
	//
	// 取得所有菜单组
	IAcadMenuGroups *mnuGrps = NULL;
	long cnt = 0;
	hr = acadApp->get_MenuGroups(&mnuGrps);
	if (FAILED(hr))
	{
		acadApp->Release();
		return;
	}
	acadApp->Release();
	// 菜单组个数
	mnuGrps->get_Count(&cnt);

	IAcadMenuGroup *mnuGrp = NULL;

	// 循环所有菜单组,寻找[ACAD_MAP]菜单组,if false, load "acad_map.mnu".
	VARIANT vtName;
	vtName.vt = VT_I4;
	BSTR grpName;
	bool found = false;
	for (long i = 0; i < cnt; i++)
	{
		vtName.llVal = i;
		hr = mnuGrps->Item(vtName, &mnuGrp);
		if (FAILED(hr))
			return;
		hr = mnuGrp->get_Name(&grpName);
		CString cgrpName(grpName);
		if (cgrpName.CompareNoCase("ACAD_MAP") == 0)
		{
			found = true;
			break;
		}
	}
	mnuGrps->Release();

	if (found == false)
	{
		CString mac = "(COMMAND \"_MENULOAD\" \"acad_map.mnu\")"; 
		if (acDocManager->curDocument()) {
			acedPostCommand( mac );
			mac = "(menucmd \"P10=+ACAD_MAP.地形图缩编\")";		//如果P10不存在,则新载入的菜单加到当前菜单最后面
			acedPostCommand( mac );
		}
	}

	mnuGrp->Release();

	return;
}

// ObjectARX Entry Point.
extern "C" AcRx::AppRetCode 
acrxEntryPoint( AcRx::AppMsgCode msg, void* appId)
{
	switch (msg)
	{
	case AcRx::kInitAppMsg:
		acrxDynamicLinker->unlockApplication(appId);
		acrxDynamicLinker->registerAppMDIAware(appId);
		InitApp();
		break;
	case AcRx::kUnloadAppMsg:
		UnloadApp();
		break;
	default:
		break;
	}
	return AcRx::kRetOK;
}

⌨️ 快捷键说明

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