我想看一级黄色大片_久久亚洲国产精品一区二区_久久精品免视看国产明星_91久久青青青国产免费

您的位置:網站首頁 > CAD新聞

Hook技術在CAD中的使用方法

時間:2012-03-02 10:23:44 來源:未知

本實例主要是講Hook技術在CAD中的使用方法。
Hook技術就是所謂鉤子技術,在CAD的二次開發過程中,有很多的情況,是需要使用鉤子技術的(盡管CAD已經提供了強大的反應器技術),由于CAD也是基于MFC開發的應用程序,我們也可以像HOOK其它應用程序一樣的方法來HOOKCAD,但有些時候,一些消息不知道怎么回事,會HOOK不到。比如:按下SHIFT鍵。 不管怎么樣,CAD還是把HOOKCAD消息的處理方法封裝了一下。很好用,主要有以下幾種:

// Define callback function for apps that want windows messages
typedef void (* AcedWatchWinMsgFn)(const MSG*); //定義監視消息的函數指針
typedef BOOL (* AcedFilterWinMsgFn)(MSG*);   //定義過濾消息的函數指針
typedef void (* AcedOnIdleMsgFn) ();     //定義監視CAD空閑時間的函數指針

// Windows messages hooks
/* Register a filter message hook into AutoCAD's Windows message loop.
* The message passed to your application can be changed and can be blocked out.
* If the function returns TRUE, the message WON'T be passed to other hook
* functions or AutoCAD at all. The message is terminated.
* Returns TRUE if successfully registers the hook. Otherwise FALSE.
*/
以上的意思大概是,在CAD的WINDOWS 消息循環中定義一個hook,你可以在其中處理這個消息,這個處理函數是必須有返回值的,返回TRUE,這個消息不會傳遞給其它的HOOK和CAD了,這個消息就中止了,如果返回FALSE,這個消息將可以被任何其它的HOOK和CAD處理。
不過個人做了一個實驗,返回TRUE時,CAD好像還是可以對這個消息進行一個處理。這個地方還有待研究。
BOOL acedRegisterFilterWinMsg(const AcedFilterWinMsgFn pfn);

/* acedRemoveFilterWinMsg
* Takes a message hook function pointer and remove it
* Returns TRUE if successfully registers the hook. Otherwise FALSE.
*/
很簡單,移除上面注冊的過濾函數
BOOL acedRemoveFilterWinMsg(const AcedFilterWinMsgFn pfn);

/* acedRegisterWatchWinMsg
* Register a hook function into AutoCAD message loop.
* The function can only look up AutoCAD's Windows message.
* It can't change or block message.
* Returns TRUE if successfully registers the hook. Otherwise FALSE.
*/
以上的意思大概是,在CAD的WINDOWS 消息循環中定義一個hook,你可以在其中查看CAD的任何消息,但不能中止其消息的傳遞,當然,你可以監控到一些消息后,做一些你自己要做的事,但不會中斷消息的傳遞。
BOOL acedRegisterWatchWinMsg(const AcedWatchWinMsgFn pfn);

/* acedRemoveWatchWinMsg
* Takes a message hook function pointer and remove it
* Returns TRUE if successfully registers the hook. Otherwise FALSE.
*
*/
很簡單,移除上面注冊的監控函數
BOOL acedRemoveWatchWinMsg(const AcedWatchWinMsgFn pfn);

/* acedRegisterOnIdleWinMsg
* Register a hook function to be notified when AutoCAD is on idle
* Returns TRUE if successfully registers the hook. Otherwise FALSE.
*/
以上的意思大概是,在CAD的WINDOWS 消息循環中定義一個hook,當CAD的空閑時間時,就會激活這個定義的HOOK,然后你就可以做一些處理了。
BOOL acedRegisterOnIdleWinMsg(const AcedOnIdleMsgFn pfn);

/* acedRemoveOnIdleWinMsg
* Takes a message on idle hook function pointer and remove it
* Returns TRUE if successfully registers the hook. Otherwise FALSE.
*
*/
簡單,移除上面注冊的監控空閑時間函數
BOOL acedRemoveOnIdleWinMsg(const AcedOnIdleMsgFn pfn);


在CAD二次開發中,HOOK技術應該還是很有用的,比如,最近,我在做一個在CAD中定義右鍵菜單的功能。以前已經做過一個右鍵菜單,用CONTEXTMENU,但不能把CAD本身的菜單給干掉,早幾天看到BENTLY公司在CAD平臺上做的開發,他們的右鍵菜單完全沒有CAD的影子,所以,我下定決心,要嘗試著把CAD的右鍵給干掉,有人說,可以自己定義CUI文件搞定,但我們的軟件是要兼容低版本的CAD,所以不能用這種方法,想了想,HOOK的方法可能適合,自己試了試,感覺還行。以下是自己寫的核心代碼。
//pick the current selectset
BOOL HasEntityBeSelected()
{
// 如果AutoCAD關閉,則不執行任何任務。
//
if (!acdbHostApplicationServices()->workingDatabase())
   return FALSE;

// 取得剛打開的實體
//
ads_name sset;
int err = acedSSGet(_T("I"), NULL, NULL, NULL, sset);
if (err != RTNORM)
{
   /*acutPrintf(_T("n獲取選擇集錯誤n"));*/
   return FALSE;
}
/*actrTransactionManager->startTransaction();*/
long length;
acedSSLength(sset, &length);
acedSSFree(sset);
if( length > 0 )
{
   return TRUE;
}
else
{
   return FALSE;
}
}


//catch the message
int CatchRClick(MSG* msg)
{
CView* pView = acedGetAcadDwgView();//獲得程序主框架指針
HWND hActiveWnd = ::GetActiveWindow();
BOOL bInView = TRUE;
CPoint pt;
::GetCursorPos(&pt);
CRect rect;
::GetWindowRect( hActiveWnd , rect );
if( pt.x > rect.left &&
   pt.x < rect.right &&
   pt.y > rect.top &&
   pt.y < rect.bottom )
{
   bInView = FALSE;
}

CWnd* pMainWnd = acedGetAcadWinApp()->m_pMainWnd;
HWND hMainWnd = pMainWnd->m_hWnd;

if( hActiveWnd && hActiveWnd == hMainWnd )
{
   bInView = TRUE;
}

if( msg->message == WM_RBUTTONDOWN && bInView )
{
   CMenu* pMenu = NULL;
   if( HasEntityBeSelected() )
   {
    if( pObject )
    {
     pMenu = pObject->GetMainMenu(); //彈出選中了實體的右鍵菜單
    }
   }
   else
   {
    if( pDefault )
    {
     pMenu = pObject->GetMainMenu();//彈出未選中了實體的右鍵菜單
    }
   }
   CPoint pt;
   ::GetCursorPos(&pt);
   CRect rect;
   ::GetWindowRect(pView ->m_hWnd, rect );
   if( pt.x > rect.left &&
    pt.x < rect.right &&
    pt.y > rect.top &&
    pt.y < rect.bottom )
   {
    pMenu->TrackPopupMenu(TPM_LEFTALIGN,pt.x,pt.y, pView);//單擊右鍵菜單時彈出菜單指針
    return 1;
   }
   else
   {
    return 0;
   }
}
else
{
   return 0;
}
}

// Init this application. Register your
// commands, reactors...
void InitApplication(void *pkt)
{
// NOTE: DO NOT edit the following lines.
//{{AFX_ARX_INIT
//}}AFX_ARX_INIT

// TODO: add your initialization functions

pDefault = new DefaultContextMenu(_hdllInstance);
pObject = new ObjectContextMenu(_hdllInstance);

acedRegisterFilterWinMsg( CatchRClick );
}
// Unload this application. Unregister all objects
// registered in InitApplication.
void UnloadApplication()
{
// NOTE: DO NOT edit the following lines.
//{{AFX_ARX_EXIT
//}}AFX_ARX_EXIT

// TODO: clean up your application
If( pDefault )
   delete pDefault;
if( pObject )
   delete pObject;
acedRegisterFilterWinMsg( CatchRClick );
}