我开发了一个控件,并将其用作工具提示。
我需要在ToolTip中添加一个控件。所以我在工具提示控件的OnLayout中添加了要添加到工具提示中的控件。控制正在增加罚款。但添加的控件不是第一次渲染。仅从第二次渲染才显示。
public ToolTipControl : Control
{
NativeToolTip.TOOLINFO toolInfo;
public ToolTipControl()
{
toolInfo = new NativeToolTip.TOOLINFO();
toolInfo.cbSize = (uint)Marshal.SizeOf(toolInfo);
toolInfo.uFlags = WindowMessages.TTF_IDISHWND;
toolInfo.hwnd = IntPtr.Zero;
toolInfo.uId = IntPtr.Zero;
toolInfo.rect = new NativePaint.RECT();
toolInfo.hinst = IntPtr.Zero;
toolInfo.lpszText = (IntPtr)Marshal.StringToHGlobalAuto("tooltip");
toolInfo.lParam = IntPtr.Zero;
}
public void SetToolTip(Control control)
{
if (control == null)
return;
control.HandleCreated += OnControlHandleCreated;
if (control.IsHandleCreated)
OnControlHandleCreated(control, EventArgs.Empty);
}
private void OnControlHandleCreated(object sender, EventArgs e)
{
Control control = sender as Control;
if (control == null)
return;
toolInfo.hwnd = this.Handle;
toolInfo.uId = control.Handle;
NativeToolTip.SendMessage(this.Handle, WindowMessages.TTM_ADDTOOLW, 0, ref toolInfo);
}
protected override CreateParams CreateParams
{
get
{
CreateParams param = base.CreateParams;
param.ClassName = "tooltips_class32";
param.Style = unchecked(WindowMessages.WS_POPUP) | WindowMessages.TTS_ALWAYSTIP;
param.ExStyle |= WindowMessages.WS_EX_TOPMOST;
return param;
}
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case (int)WindowMessages.WM_NOTIFY:
{
NativeToolTip.NMHDR hdr = (NativeToolTip.NMHDR)Marshal.PtrToStructure(m.LParam, typeof(NativeToolTip.NMHDR));
switch (hdr.code)
{
case (int)WindowMessages.TTN_SHOW:
OnPopup(GetControl(hdr.hwndFrom, (IntPtr)hdr.idFrom), ref m);
return;
}
}
}
void OnPopup(Control control, ref Message m)
{
Info info = this.GetInfo(control);
if (info == null)
return;
this.Info = info;
this.PerformLayout();
}
public Info Info
{
get;
set;
}
protected override OnLayout(LayoutEventArgs e)
{
if(this.Info==null)
{
this.Size= Size.Empty;
return;
}
Control control= this.Info.Control;
this.Size= control.Size;
this.Controls.Add(control);
}
}
有谁请让我知道,为什么第一次没有渲染添加的控件?
此致