首先你要把 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); // 顯示右鍵選單
}
}
訂閱最新文章
實用軟體介紹(11)