這是在使用Control.GetChildAtPoint和Control.PointToClient修改後的代碼的摘錄中,通過在用戶點擊時定義的標籤遞歸搜索控件。
private void Form1_HelpRequested(object sender, HelpEventArgs hlpevent)
{
// Existing example code goes here.
// Use the sender parameter to identify the context of the Help request.
// The parameter must be cast to the Control type to get the Tag property.
Control senderControl = sender as Control;
//Recursively search below the sender control for the first control with a Tag defined to use as the help message.
Control controlWithTag = senderControl;
do
{
Point clientPoint = controlWithTag.PointToClient(hlpevent.MousePos);
controlWithTag = controlWithTag.GetChildAtPoint(clientPoint);
} while (controlWithTag != null && string.IsNullOrEmpty(controlWithTag.Tag as string));
// Existing example code goes here.
}
這是一個很好的開始,謝謝。 GetChildAtPoint是「相對於控件客戶區的左上角」,而HelpEventArgs.MousePos給出了鼠標指針的屏幕座標。所以我需要做一些轉換才能獲得可用的點,然後才能遞歸搜索特定的控件。 – 2011-01-23 21:05:13