c# - 如何在没有阴影的情况下绘制我自己的工具提示?

标签 c# winforms tooltip

我正在尝试绘制自己的工具提示。但我无法摆脱标准阴影。

它是一个标准的 WinForm 应用程序,有很多表单。因此是

 Application.EnableVisualStyles();

在应用程序启动时调用并需要。如果我注释掉这一行,它就起作用了。我在下面制作了一个最小的 WinForm 应用程序。如果 EnableVisualStyles 被注释掉,它只绘制一个红色矩形。当我取消注释时,它会绘制一个带阴影的红色矩形。

有谁知道如何解决这个问题?如何拥有 Application.EnableVisualStyles(),并拥有 100% OwnerDrawn 的工具提示,没有任何标准阴影?

enter image description here

enter image description here

最小的 WinForm 应用程序在这里:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ToolTipExample
{
  public class MainForm : Form
  {
      [STAThread]
      static void Main()
      {
          // Comment out below line and it works.
          Application.EnableVisualStyles();
          Application.Run(new MainForm());
      }

      private ToolTip toolTip;
      private Button button;

      public MainForm()
      {
          toolTip = new ToolTip();
          toolTip.OwnerDraw = true;
          toolTip.Draw += new DrawToolTipEventHandler(toolTip1_Draw);
          toolTip.Popup += new PopupEventHandler(toolTip1_Popup);

          button = new Button();
          button.Location = new Point(25, 25);
          button.Text = "Button";

          toolTip.SetToolTip(button, "Button tip text");

          Controls.AddRange(new Control[] { button });
      }

      private void toolTip1_Popup(object sender, PopupEventArgs e)
      {
          e.ToolTipSize = new Size(100, 100);
      }

      private void toolTip1_Draw(System.Object sender, DrawToolTipEventArgs e)
      {
          e.Graphics.FillRectangle(new SolidBrush(Color.Red), e.Bounds);
      }
  }
}

最佳答案

您可以使用 GetClassLong 获取 ToolTip 的类样式,然后从中删除 CS_DROPSHADOW 样式并为 设置类样式code>ToolTip 再次:

//using System.Runtime.InteropServices;

public const int GCL_STYLE = -26;
public const int CS_DROPSHADOW = 0x20000;

[DllImport("user32.dll", EntryPoint = "GetClassLong")]
public static extern int GetClassLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", EntryPoint = "SetClassLong")]
public static extern int SetClassLong(IntPtr hWnd, int nIndex, int dwNewLong);

private void toolTip1_Popup(object sender, PopupEventArgs e) 
{
    e.ToolTipSize = new Size(100, 100);
    var hwnd = (IntPtr)typeof(ToolTip).GetProperty("Handle",
        System.Reflection.BindingFlags.NonPublic |
        System.Reflection.BindingFlags.Instance).GetValue(toolTip);
    var cs = GetClassLong(hwnd, GCL_STYLE);
    if ((cs & CS_DROPSHADOW) == CS_DROPSHADOW)
    {
        cs = cs & ~CS_DROPSHADOW;
        SetClassLong(hwnd, GCL_STYLE, cs);
    }
}

关于c# - 如何在没有阴影的情况下绘制我自己的工具提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50879056/

相关文章:

c# - 带命令模式的 DI

c# - 如何在不访问表的情况下查询数据库,更正数据库而不是表

c# - LinearGradientBrush 无法正确呈现

javascript - 根据搜索字段值触发按钮上的工具提示

c# - 是否可以解决 .Net 3.5 中的 "Default parameter specifiers are not permitted"错误?

c# - 如何将 List<object> 转换为 Json 格式并在 C# 中填充 <tbody>?

c# - 将多个字符串分成每个

c# - 我怎么知道没有 Textchanged 事件的文本框的文本更改

javascript - jQuery 工具提示脚本

javascript - 如何将工具提示绑定(bind)到此 html 元素