用ATL实现VC插件
作者: IUNKNOW 出处: vchelp
增加命令对应的方法:
1) 如果要增加一条命令,必须在Icopyright中增加相应的方法。如在示例程序中我增加了MiniWindow这个命令,那么我在Icopyright中也必须增加MiniWindow这样一个方法 (在ClassWizard中鼠标右键Icopyright,在弹出菜单中选择Add Method 即可增加方法)
示例程序程序中共增加了四个命令:ShowInfoDlg,MiniWindow,MaxWindow和Test,应此必须在Icopyright接口中实现此四个方法
7)实现工具条后,就是实现具体的方法了,具体的实现请参看Ccopyright中的四个方法
STDMETHODIMP CCopyright::ShowInfoDlg()
STDMETHODIMP CCopyright::MaxWindow()
STDMETHODIMP CCopyright::MiniWindow()
STDMETHODIMP CCopyright::Test()
STDMETHODIMP CCopyright::ShowInfoDlg()
{
// TODO: Add your implementation code here
USES_CONVERSION;
CUserInfoDlg dlg;
dlg.m_bstrName = m_bstrName;
dlg.m_bstrEmail = m_bstrEmail;
if(dlg.DoModal() == IDOK)
{
m_bstrName = dlg.m_bstrName;
m_bstrEmail = dlg.m_bstrEmail;
}
CRegKey reg;
reg.Create(HKEY_CURRENT_USER,_T("Software\\Evesoft\\AddIns"));
reg.SetValue(OLE2CT(m_bstrName),_T("NAME"));
reg.SetValue(OLE2CT(m_bstrEmail),_T("EMAIL"));
reg.Close();
return S_OK;
}
STDMETHODIMP CCopyright::MaxWindow()
{
// TODO: Add your implementation code here
CComPtr<IDispatch> pDispWindows;
CComQIPtr<IWindows, &IID_IWindows> pWindows;
m_spApplication->get_Windows(&pDispWindows);
pWindows = pDispWindows;
pDispWindows = NULL;
CComPtr<IUnknown> pUnk;
CComQIPtr<IEnumVARIANT, &IID_IEnumVARIANT> pNewEnum;
if (SUCCEEDED(pWindows->get__NewEnum(&pUnk)) && pUnk != NULL)
{
pNewEnum = pUnk;
VARIANT varWindow;
CComQIPtr<IGenericWindow, &IID_IGenericWindow> pWindow;
while (pNewEnum->Next(1, &varWindow, NULL) == S_OK)
{
pWindow = varWindow.pdispVal;
VariantClear(&varWindow);
CComBSTR bstrCaption;
pWindow->get_Caption(&bstrCaption);
//dsWindowStateMaximized ×î´ó»¯
pWindow->put_WindowState(dsWindowStateMaximized);
}
}
return S_OK;
}
STDMETHODIMP CCopyright::MiniWindow()
{
// TODO: Add your implementation code here
CComPtr<IDispatch> pDispWindows;
CComQIPtr<IWindows, &IID_IWindows> pWindows;
m_spApplication->get_Windows(&pDispWindows);
pWindows = pDispWindows;
pDispWindows = NULL;
CComPtr<IUnknown> pUnk;
CComQIPtr<IEnumVARIANT, &IID_IEnumVARIANT> pNewEnum;
if (SUCCEEDED(pWindows->get__NewEnum(&pUnk)) && pUnk != NULL)
{
pNewEnum = pUnk;
VARIANT varWindow;
CComQIPtr<IGenericWindow, &IID_IGenericWindow> pWindow;
while (pNewEnum->Next(1, &varWindow, NULL) == S_OK)
{
pWindow = varWindow.pdispVal;
VariantClear(&varWindow);
CComBSTR bstrCaption;
pWindow->get_Caption(&bstrCaption);
//dsWindowStateMaximized ×î´ó»¯
pWindow->put_WindowState(dsWindowStateMinimized);
}
}
return S_OK;
}
STDMETHODIMP CCopyright::Test()
{
// TODO: Add your implementation code here
MessageBox(NULL,"ллʹÓ
㬶¡Ó±ÉÏ","ÖÂл",MB_OK | MB_ICONINFORMATION);
return S_OK;
}
值的说明的是在ShowInfoDlg()方法中弹出了一个对话框。在ATL中类似于这种对话框很容易实现。在Class Wizard 中鼠标右键,在弹出菜单种选择New ATL Object…在ATL Object wizard中的Category中选择Miscellaneous ,在Object中选择Dialog,Next按钮后,在ATL Object wizard属性对话框中Short name 编辑框中输入UserInfoDlg即完成对话框的实现。另外,对话框中输入的用户信息是记载在注册表中的.ShowInfoDlg()方法弹出的界面如下图:
下载示例程序
评论