2010-11-19 41 views
1

我的程序有一個圖片框,並且我想要點擊鼠標,或者在ContextMenuStrip選項上使某個東西出現在點擊的相同位置。如何添加標籤apon鼠標點擊C#中的相同位置?

所看到的畫面,我想補充某種說明的具體日期點擊區域(可能是添加一個用戶控件)

我如何去它?我怎樣才能發送點擊座標(x,y)並使某些東西出現在這些相同的座標上?

謝謝!

alt text

+0

也許一個工具提示是一個更好的選擇... – munissor 2010-11-19 10:00:20

+0

我basicly想要做的是記下(如待辦事項)堅持在同一天。 (堅持和留下)你能否詳細說明我該如何使用工具提示? – RanH 2010-11-19 10:06:10

回答

1

我想創建一個類,它可以提供菜單項和捕捉x,y座標,讓他們準備好被點擊的項目時。或者您可以在匿名代理中捕獲這些座標。

事情是這樣的:

public Form1() 
{ 
    InitializeComponent(); 
    MouseClick += new MouseEventHandler(Form1_MouseClick); 
} 

private void Form1_MouseClick (object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Right) 
    { 
     ContextMenuStrip ctxMenu = new ContextMenuStrip(); 

     // following line creates an anonymous delegate 
     // and captures the "e" MouseEventArgs from 
     // this method 
     ctxMenu.Items.Add(new ToolStripMenuItem(
      "Insert info", null, (s, args) => InsertInfoPoint(e.Location))); 

     ctxMenu.Show(this, e.Location); 
    } 
} 

private void InsertInfoPoint(Point location) 
{ 
    // insert actual "info point" 
    Label lbl = new Label() 
    { 
     Text = "new label", 
     BorderStyle = BorderStyle.FixedSingle, 
     Left = location.X, Top = location.Y 
    }; 
    this.Controls.Add(lbl); 
} 
0

您可以使用工具提示或使用鼠標移動事件。此事件將爲您提供鼠標當前位置,然後您可以通過在該位置顯示真/假顯示您的內容,或者採用標籤並設置其文本,然後根據鼠標的xy設置其xy位置。然後在鼠標離開事件動議標籤關閉屏幕或隱藏

1

滿足您需求的示例代碼,在我下面的代碼我加入鼠標點擊按鈕控制。您可以根據需要修改代碼。

int xValue=0, yValue=0; 
    private void Form1_MouseClick(object sender, MouseEventArgs e) 
    { 
     xValue = e.X; 
     yValue = e.Y; 
     Button btn = new Button(); 
     btn.Name = "Sample Button"; 
     this.Controls.Add(btn); 
     btn.Location = new Point(xValue, yValue); 
    }