在FontDialog中显示颜色选项,没有下划线和删除线选项

问题描述 投票:2回答:3

我在VB.NET应用程序中使用FontDialog控件。

当我将ShowColor属性设置为true时,它会在“效果”组下显示Strikeout,Underline和Color选项。我只需要这三种颜色。

有没有办法隐藏Strikeout和Underline效果,以便只有Color选项可见?

.net winforms colors
3个回答
0
投票

我不认为可以隐藏或更改默认的Windows字体对话框。

为此,您必须创建自己的自定义控件。


1
投票
  • 得出ColorDialog
  • 覆盖HookProc方法
  • 处理WM_INITDIALOG消息(272)
  • 隐藏相应的对话框项

这类似于ColorDialog类在设置ShowColor = falseShowEffects = true时的作用(默认值是什么):

protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
{
  switch (msg)
  {
    case 272:
      if (!this.showColor)
      {
        SafeNativeMethods.ShowWindow(new HandleRef((object) null, UnsafeNativeMethods.GetDlgItem(new HandleRef((object) null, hWnd), 1139)), 0);
        SafeNativeMethods.ShowWindow(new HandleRef((object) null, UnsafeNativeMethods.GetDlgItem(new HandleRef((object) null, hWnd), 1091)), 0);
        break;
      }
      else
        break;
  ...
}

以上隐藏了“颜色”标签和颜色组合(对话框ID 1139和1091)。

你想要相反,隐藏Strikeout和Underline。他们的对话框ID是1040和1041(在Wine代码中找到)。


@HansPassant全面实现“字体和颜色”对话框:

在项目中添加一个新类并粘贴下面显示的代码。编译。将新组件从工具箱顶部拖放到表单上,替换现有的FontDialog

Imports System.Runtime.InteropServices

Public Class MyFontDialog
    Inherits FontDialog

    Protected Overrides Function HookProc(hWnd As IntPtr, msg As Integer, wparam As IntPtr, lparam As IntPtr) As IntPtr
        If msg = 272 And Me.ShowColor Then  '' WM_INITDIALOG
            Dim strikeout = GetDlgItem(hWnd, &H410)
            If strikeout <> IntPtr.Zero Then ShowWindow(strikeout, 0)
            Dim underline = GetDlgItem(hWnd, &H411)
            If underline <> IntPtr.Zero Then ShowWindow(underline, 0)
        End If
        Return MyBase.HookProc(hWnd, msg, wparam, lparam)
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function GetDlgItem(hDlg As IntPtr, item As Integer) As IntPtr
    End Function
    <DllImport("user32.dll")> _
    Private Shared Function ShowWindow(hWnd As IntPtr, cmd As Integer) As Boolean
    End Function
End Class

我实际上需要这个用于WinAPI(C ++ Builder),所以虽然稍微偏离主题,但我也在共享C ++ WinAPI代码。它更加花哨,因为它甚至可以移动颜色框:

RECT Rect;
POINT Point;

HWND StrikeoutHandle = GetDlgItem(Handle, 1040);
GetWindowRect(StrikeoutHandle, &Rect);
POINT StrikeoutPoint = { Rect.left, Rect.top };
ScreenToClient(Handle, &StrikeoutPoint);

ShowWindow(StrikeoutHandle, SW_HIDE);
ShowWindow(GetDlgItem(Handle, 1041), SW_HIDE);

HWND LabelHandle = GetDlgItem(Handle, 1091);
GetWindowRect(LabelHandle, &Rect);
Point.x = Rect.left;
Point.y = Rect.top;
ScreenToClient(Handle, &Point);

int Shift = StrikeoutPoint.y - Point.y;

SetWindowPos(LabelHandle, NULL, Point.x, Point.y + Shift, 0, 0,
  SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);

HWND ComboHandle = GetDlgItem(Handle, 1139);
GetWindowRect(ComboHandle, &Rect);
Point.x = Rect.left;
Point.y = Rect.top;
ScreenToClient(Handle, &Point);
SetWindowPos(ComboHandle, NULL, Point.x, Point.y + Shift, 0, 0,
  SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);

结果如下:


0
投票

是的,不可能在fontdialog中隐藏下划线和删除线选项。您可以通过在目标文本上应用样式之前处理它来对文本显示此类效果来解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.