2017-06-14 68 views
0

我有一個wxToolBar裏面一個下拉項: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()?

謝謝!

+0

您使用的是'wxComboBox'類?如果是這樣:請嘗試'controlMenu.Popup()'([see here](http://docs.wxwidgets.org/trunk/classwx_combo_box.html#a325d7d2afc9e1e17f7b5fb13f4186d0c))而不是'PopupMenu(controlMenu)'。這是否符合您的預期? –

+0

Andre Kampling:不,不是wxComboBox,而是在wxToolBar中使用wxMenu創建下拉工具 – Elmi

回答

0

您可以嘗試撥打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()); 
+0

好的,但是這給了我工具欄的位置,而不是特定工具TOOLBAR_​​CMD_CONTROL_DROPDOWN的位置,下拉菜單應該在那裏出現在!? – Elmi

+0

我不確定,因爲我目前沒有安裝wxWidgets,所以無法快速測試。所以請測試並報告。 –

+0

@Elmi:它有效嗎? –