Show color option in FontDialog with no underline and strikeout options
I am using FontDialog
control in VB.NET application.
When I set its ShowColor
property to true
, it shows me Strikeout, 开发者_StackOverflow中文版Underline and Color option under Effects group. I need only Color from these three.
Is there any way to hide Strikeout and Underline effects, so that only Color option will be visible?
- Derive
ColorDialog
class - Override
HookProc
method - Handle
WM_INITDIALOG
message (272) - Hide respective dialog items
This is similar to what the ColorDialog
class does when you set ShowColor = false
and ShowEffects = true
(what is default):
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;
...
}
The above hides the "Color" label and color combo (dialog IDs 1139 and 1091).
You want the opposite, hide the Strikeout and Underline. Their dialog IDs are 1040 and 1041 (found in Wine code).
Full implementation of "font and color" dialog by @HansPassant:
Add a new class to your project and paste the code shown below. Compile. Drop the new component from the top of the toolbox onto your form, replacing the existing 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
I actually needed this for a WinAPI (C++ Builder), so while slightly off-topic, I'm sharing the C++ WinAPI code too. It is more fancy, as it does even shift the Color box up:
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);
The result is like:
I don't think it is possible to hide or change default Windows font dialog.
For that you have to create your own custom control.
Yes it's not possible to hide underline and strikeout option in fontdialog. You can solve your problem by not showing such effects on your text by handling it just before style apply on the target text.
精彩评论