BCB-用右鍵選擇 TreeNode 並顯示右鍵選單


假設你有一個 TreeView 叫做 tvArticle,有個 PopupMenu 叫做 PopupMenu1,你希望用右鍵可以點選 tvArticle 的節點,然後再顯示右鍵選單。
首先你要把 PopupMenu1 的 AutoPupop 屬性設為 false,然後在 tvArticle 的 OnMouseDown 事件加入以下程式碼。


void __fastcall TForm1::tvArticleMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TPoint p;
GetCursorPos(&p); // 取得游標在螢幕的真實座標
if (Button == mbRight && tvArticle->GetNodeAt(X, Y) != NULL){
tvArticle->GetNodeAt(X, Y)->Selected = true;
PopupMenu1->Popup(p.x, p.y); // 顯示右鍵選單
}
}

以上的程式碼會在你選中 TreeNode 時才會出現右鍵選單,若你希望不選中  TreeNode 也能出現選單的話,就改成以下這樣:


void __fastcall TForm1::tvArticleMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TPoint p;
GetCursorPos(&p); // 取得游標在螢幕的真實座標
if (Button == mbRight){
if (
tvArticle->GetNodeAt(X, Y) != NULL)
tvArticle->GetNodeAt(X, Y)->Selected = true;
PopupMenu1->Popup(p.x, p.y); // 顯示右鍵選單
}
}
arrow
arrow
    文章標籤
    BCB TreeNode popup popupmenu
    全站熱搜

    yao67 發表在 痞客邦 留言(0) 人氣()