首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在没有阴影的情况下绘制自己的工具提示?

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

Stack Overflow用户
提问于 2018-06-15 15:54:29
回答 1查看 657关注 0票数 3

我试着画我自己的工具提示。但我无法摆脱标准的阴影。

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

代码语言:javascript
复制
 Application.EnableVisualStyles();

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

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

Minimal WinForm应用程序在这里:

代码语言:javascript
复制
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);
      }
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-15 17:44:18

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

代码语言:javascript
复制
//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);
    }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50879056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档