설명에서 구한 내용을 100 % 확신 할 수는 없지만 캡션이 마우스로 가리키고있는 컨트롤 부분에 따라 도구 설명이있는 컨트롤 하나가 필요하다고 생각하십니까?
나는 이것을 "좋은"방법으로 생각할 수 없다. 아래의 코드는 Label에서 작동하지만 추악합니다. 나는 개인적으로 여러 개의 레이블이있는 UserControl을 만들고 수동으로 간격을 두지 않을까 걱정합니다. 하지만 여기에 내가 가지고있는 게있어 :
Label lbl = new Label() { Left = 6, Top = 6, AutoSize = true };
this.Controls.Add(lbl);
// Determine the width of each of the "sections" of the Label Text.
// Use the fact that AutoSize = true will increase the Width of the
// Label as you increase the Text Length.
int width = lbl.Width;
lbl.Width = 0;
lbl.Text = string.Empty;
lbl.Text = "x";
int delta = lbl.Width;
lbl.Text += "x";
delta = lbl.Width - 2 * (lbl.Width - delta);
lbl.Text = "ToolTip1";
int txt1Width = lbl.Width - delta;
delta = lbl.Width;
lbl.Text += " NoToolTip ";
int txt2Width = lbl.Width - delta;
delta = lbl.Width;
lbl.Text += "ToolTip2";
int txt3Width = lbl.Width - delta;
// Use a System.Windows.Forms.ToolTip and set the caption on
// MouseHover, depending on the Position of the Cursor.
var tip = new ToolTip();
lbl.MouseHover += delegate(object sender, EventArgs e)
{
tip.RemoveAll();
// Find the Point for the ToolTip (relative to the Label) based
// on the Position of the Cursor.
this.Cursor = new Cursor(Cursor.Current.Handle);
Point lblScreenPos = lbl.PointToScreen(Point.Empty);
Point tipPoint = new Point(Cursor.Position.X, Cursor.Position.Y + lbl.Height);
tipPoint = new Point(tipPoint.X - lblScreenPos.X, tipPoint.Y - lblScreenPos.Y);
// Determine the location of the "sections" of Label text.
int x = tipPoint.X;
int txt1Left = 3;
int txt1Right = txt1Left + txt1Width;
int txt3Left = txt1Right + txt2Width;
int txt3Right = txt3Left + txt3Width;
// Show the ToolTip with the correct caption.
if (x >= txt1Left && x <= txt1Right)
{
tip.Show("TOOLTIPA", lbl, tipPoint, tip.AutoPopDelay);
}
else if (x >= txt3Left && x <= txt3Right)
{
tip.Show("TOOLTIPB", lbl, tipPoint, tip.AutoPopDelay);
}
};
lbl.MouseLeave += delegate(object sender, EventArgs e) { tip.RemoveAll(); };
툴팁에는 밑줄이없고 시스템 기본 글꼴이 자동으로 사용됩니다. 소유자가 도구 설명을 그릴 때만 재정의 할 수 있습니다. 파란색으로 표시하고 밑줄을 긋아야하는 경우 OwnerDraw 속성을 확인하십시오.
만족스러운 결과를 얻었습니까? 소유자가 색상 표를 그려주는 사용자 정의 컨트롤을 만들고 있습니다. 각 색상 위에 다른 툴팁을 표시하는 것이 좋습니다. Tooltip.Show()를 호출 할 수는 있지만 일단 숨기면 마우스가 닫히고 다시 제어 할 때까지 다시 표시되지 않습니다. –
나는 그것을 끝내고있다. TextRenderer를 사용하여 원하는 문자열을 그리고 다른 툴팁 위치를 추적했습니다. 그것은 당신의 사용을 위해 그것을 수정하는 것은 나쁘지 않아야합니다. 코드를 공유하는 좋은 방법은 무엇입니까? – TrevDev