wx工具栏:获取工具的位置和大小



我有一个wxToolBar,里面有一个下拉项:

g_toolBar1->AddTool(TOOLBAR_CMD_CONTROL_DROPDOWN,_("Control elements"),MainWin::getBitmap(gearsXPM,"gears"),wxNullBitmap,wxITEM_DROPDOWN);
custParent->Connect(TOOLBAR_CMD_CONTROL_DROPDOWN,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(DrawCanvasSwitcher::OnToolbar),NULL,g_drawCanvas);
.... // add items to controlMenu here
g_toolBar1->SetDropdownMenu(TOOLBAR_CMD_CONTROL_DROPDOWN,controlMenu);

在某些情况下,我通过调用 PopupMenu(( 以编程方式打开下拉菜单。我的问题在这里:此功能在当前鼠标位置打开菜单,而不是像带有向下箭头的按钮那样在工具下方打开菜单。

那么:我怎样才能获得工具TOOLBAR_CMD_CONTROL_DROPDOWN的位置和大小,以便为下拉菜单计算合适的位置,以便将其交给 PopupMenu((?

谢谢!

您可以

尝试像我在下面所做的那样调用GetBestSize(),也不要忘记在使用AddTool()添加工具后调用Realize()

g_toolBar1->AddTool(TOOLBAR_CMD_CONTROL_DROPDOWN,_("Control elements"),MainWin::getBitmap(gearsXPM,"gears"),wxNullBitmap,wxITEM_DROPDOWN);
/* Added the next two lines. */
g_toolBar1->Realize();
wxSize toolBarPos = g_toolBar1->GetBestSize();
/* If you add more tools call Realize() after that again. */
custParent->Connect(TOOLBAR_CMD_CONTROL_DROPDOWN,wxEVT_COMMAND_TOOL_CLICKED,wxCommandEventHandler(DrawCanvasSwitcher::OnToolbar),NULL,g_drawCanvas);
.... // add items to controlMenu here
g_toolBar1->SetDropdownMenu(TOOLBAR_CMD_CONTROL_DROPDOWN,controlMenu);

然后,可以使用toolBarPos调用PopupMenu()方法,如下所示:

PopupMenu(controlMenu, toolBarPos.getWidth(), toolBarPos.getHeight());

最新更新