如何在ActiveX控件中创建工具栏 [英] How to create a toolbar within an ActiveX Control
问题描述
大家好,
我尝试创建自己的ActiveX控件,显示为工具栏。为此,我使用VS2010向导创建一个MFC ActiveX控件。
自动生成一些类后我添加成员
< pre lang =c ++> CToolBar m_toolbar
和WM_CREATE消息的消息处理程序,它创建方法OnCreate(...)。
int MyCtrl :: OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl :: OnCreate(lpCreateStruct)== - 1 )
return - 1 ;
// TODO:在此处添加专门的创作代码
m_toolbar.Create( this ,WS_CHILD | WS_VISIBLE,IDC_MYCTRL_TOOLBAR);
m_toolbar.LoadBitmap(IDB_TEXTCOMPLEMENT_TOOLBAR);
m_toolbar.SetButtons(NULL, 5 );
m_toolbar.SetButtonInfo( 0 ,IDC_MYCTRL_NEW,TBBS_BUTTON, 0 );
m_toolbar.SetButtonInfo( 1 ,IDC_MYCTRL_DELETE,TBBS_BUTTON, 1 );
m_toolbar.SetButtonInfo( 2 ,IDC_MYCTRL_MODIFY,TBBS_BUTTON, 2 );
m_toolbar.SetButtonInfo( 3 ,IDC_MYCTRL_PREVIOUS,TBBS_BUTTON, 3 );
m_toolbar.SetButtonInfo( 4 ,IDC_MYCTRL_NEXT,TBBS_BUTTON, 4 );
return 0 ;
}
IDB_TEXTCOMPLEMENT_TOOLBAR
是5的位图按钮。
我在编译或运行时没有收到任何错误。 Create()和所有其他功能成功完成。
现在我创建一个带有Doc / View架构的MFC项目(也使用VS 2010向导)。对于这个项目,我使用类向导添加我的控件,生成一个包装器。
在View-class'OnCreate()函数中,我执行以下操作:
int CMFC_OCXView :: OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView :: OnCreate(lpCreateStruct)== - 1 )
返回 - 1 ;
// TODO:在此处添加专门的创作代码
m_toolbarCtrl.Create(NULL,WS_CHILD | WS_VISIBLE,CRect( 0 , 0 , 200 , 200 ),此, 1278 跨度>);
return 0 ;
}
其中m_toolbarCtrl是我的控件包装器类型的成员。
当我启动MFC应用程序时,视图中没有显示任何内容。
我已经尝试在视图上显示其他组件并且它们工作正常。例如。我在ActiveX控件项目中创建了一些按钮,就像我对CToolBar一样。
我找不到为什么我的工具栏从未显示的错误。
您的MFX应用资源应该是位图和工具栏资源,使用您的ID为auf 1278.
概述: http://msdn.microsoft.com/en-us/library/f9hbax0b.aspx [ ^ ]
工具栏编辑器: http://msdn.microsoft.com/en-us/library/kfb33f94 .aspx [ ^ ]
所以,在玩了新的和干净的项目后,我发现了问题。
如上所述,我的工具栏的创建总是s成功但我从来没有看到过什么。原因是我的工具栏被绘制在我无法看到的地方。
您必须将以下粗体标记的行添加到ActiveX控件的OnCreate方法中:
int MyCtrl :: OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl :: OnCreate(lpCreateStruct)== - 1 )
return - 1 ;
// TODO:在此处添加专门的创作代码
m_toolbar.Create( this ,WS_CHILD | WS_VISIBLE,IDC_MYCTRL_TOOLBAR);
m_toolbar.LoadBitmap(IDB_TEXTCOMPLEMENT_TOOLBAR);
m_toolbar.SetButtons(NULL, 5 );
m_toolbar.SetButtonInfo( 0 ,IDC_MYCTRL_NEW,TBBS_BUTTON, 0 );
m_toolbar.SetButtonInfo( 1 ,IDC_MYCTRL_DELETE,TBBS_BUTTON, 1 );
m_toolbar.SetButtonInfo( 2 ,IDC_MYCTRL_MODIFY,TBBS_BUTTON, 2 );
m_toolbar.SetButtonInfo( 3 ,IDC_MYCTRL_PREVIOUS,TBBS_BUTTON, 3 );
m_toolbar.SetButtonInfo( 4 ,IDC_MYCTRL_NEXT,TBBS_BUTTON, 4 );
CRect rc;
GetClientRect(& rc);
rc.bottom = rc.top + 30 ;
m_toolbar.MoveWindow(& rc);
return 0 跨度>;
}
这会将工具栏向下移动到CView的可视区域,或者您想要添加工具栏的区域。
Hi all,
I try to create my own ActiveX Control that is shown as a toolbar. For that I used the VS2010 Wizard to create a MFC ActiveX Control.
After the automatic generation of some classes I add the member
CToolBar m_toolbar
and the message handler for the "WM_CREATE" message which creates the method "OnCreate(...)".
int MyCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (COleControl::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_toolbar.Create(this, WS_CHILD | WS_VISIBLE, IDC_MYCTRL_TOOLBAR);
m_toolbar.LoadBitmap(IDB_TEXTCOMPLEMENT_TOOLBAR);
m_toolbar.SetButtons(NULL, 5);
m_toolbar.SetButtonInfo(0, IDC_MYCTRL_NEW, TBBS_BUTTON, 0);
m_toolbar.SetButtonInfo(1, IDC_MYCTRL_DELETE, TBBS_BUTTON, 1);
m_toolbar.SetButtonInfo(2, IDC_MYCTRL_MODIFY, TBBS_BUTTON, 2);
m_toolbar.SetButtonInfo(3, IDC_MYCTRL_PREVIOUS, TBBS_BUTTON, 3);
m_toolbar.SetButtonInfo(4, IDC_MYCTRL_NEXT, TBBS_BUTTON, 4);
return 0;
}
IDB_TEXTCOMPLEMENT_TOOLBAR
is a bitmap for the 5 buttons.
I do not get any error during compilation or runtime. The Create() and all other function finishing successfully.
Now I create a MFC Project with a Doc/View Architecture (also with the VS 2010 Wizard). To this project I add my control with the class wizard which generates a wrapper.
In the View-class' "OnCreate()" function I do the following:
int CMFC_OCXView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_toolbarCtrl.Create(NULL, WS_CHILD|WS_VISIBLE, CRect(0, 0, 200, 200), this, 1278);
return 0;
}
where m_toolbarCtrl is a member of the type of my control's wrapper.
When I start the MFC application nothing is shown in the view.
I tried already to show other components on the view and they worked fine. E.g. I created some buttons in my ActiveX Control project the same way I did with the CToolBar.
I don't find the mistake why my toolbar is never shown.
your MFX app resources should be a bitmap and a toolbar resource, with your used ID auf 1278.
Overview: http://msdn.microsoft.com/en-us/library/f9hbax0b.aspx[^]
Toolbar Editor: http://msdn.microsoft.com/en-us/library/kfb33f94.aspx[^]
So, after playing around with new and clean projects I found the problem.
As described, the creation of my toolbar was always successful but I never was able to see something. The cause of this was that my toolbar was drawn where I could not see it.
You have to add the following bold marked lines to the "OnCreate"-method of the ActiveX Control:
int MyCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (COleControl::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here m_toolbar.Create(this, WS_CHILD | WS_VISIBLE, IDC_MYCTRL_TOOLBAR); m_toolbar.LoadBitmap(IDB_TEXTCOMPLEMENT_TOOLBAR); m_toolbar.SetButtons(NULL, 5); m_toolbar.SetButtonInfo(0, IDC_MYCTRL_NEW, TBBS_BUTTON, 0); m_toolbar.SetButtonInfo(1, IDC_MYCTRL_DELETE, TBBS_BUTTON, 1); m_toolbar.SetButtonInfo(2, IDC_MYCTRL_MODIFY, TBBS_BUTTON, 2); m_toolbar.SetButtonInfo(3, IDC_MYCTRL_PREVIOUS, TBBS_BUTTON, 3); m_toolbar.SetButtonInfo(4, IDC_MYCTRL_NEXT, TBBS_BUTTON, 4); CRect rc; GetClientRect(&rc); rc.bottom = rc.top + 30; m_toolbar.MoveWindow(&rc); return 0; }
This will move the toolbar down to the viewable area of the CView or whereever you want to add the toolbar.
这篇关于如何在ActiveX控件中创建工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!