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

时间:2010-01-07 11:32:28

标签: .net winforms colors

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

当我将ShowColor属性设置为true时,它会显示 Strikeout 下划线颜色选项在效果组下。我只需要这三个中的 Color

有没有办法隐藏 Strikeout 下划线效果,以便只显示 Color 选项?

3 个答案:

答案 0 :(得分: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代码。它更加花哨,因为它甚至可以移动 Color 框:

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);

结果如下:

Font and color dialog

答案 1 :(得分:0)

我认为无法隐藏或更改默认的Windows字体对话框。

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

答案 2 :(得分:0)

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

相关问题