0

很长一段时间以来,我一直在尝试找到解决“per-appllication cleartype fonts”问题的方法。

我想为 a 或 a 或任何其他非自定义/库存控件实现TextBoxcleartypeButton字体ComboBox

好吧,我知道有一种简单的方法可以实现这一点,那就是覆盖 OnPaint 方法:

protected override void OnPaint(PaintEventArgs pevent)
{
    pevent.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

    base.OnPaint(pevent);
}

这工作得很好,但问题是我想在不继承任何这些控件的情况下做到这一点,并以某种方式从类外部更改字体属性。

到目前为止,我使用的方法没有多大成功,是安装一个“挂钩程序”并监听发送到控件的消息。下面的代码是以下代码的一部分Form1 class

// the delegate with the same signature as the callback function
private delegate int HookMessages(int nCode, IntPtr wParam, IntPtr lParam);

// when the Load event of the Form takes place the controls are created
// and the method that sets the hooks is called
private void Form1_Load(object sender, EventArgs e)
{
    Button btn = new Button();
    btn.Name = "btn";
    btn.Parent = this;
    btn.Size = new Size(this.ClientRectangle.Width - 10, 20);
    btn.Location = new Point(5, tbx.Top + tbx.Height + 10);
    btn.BackColor = Color.FromArgb(100, 1, 1, 1);
    btn.Text = "Click ME";

    HookControlMessages();
}

// the hook that monitors control's events is set for the current thread
private void HookControlMessages()
{
    if (hookWndHandle == 0)
    {
        HookWndProcedure = new HookMessages(HookWndProcMessages);

        hookWndHandle = SetWindowsHookEx(
            WindowHookTypes.WH_CALLWNDPROC,
            HookWndProcedure,
            IntPtr.Zero,
            (int)GetCurrentThreadId());

        if (hookWndHandle == 0)
        {
            MessageBox.Show("Window`s messages hooking failed.");

            return;
        }
    }
}

// the callback function
private static int HookWndProcMessages(int nCode, IntPtr wParam, IntPtr lParam)
{
    int nextHook = 0;

    try
    {
        if (nCode >= 0)
        {
            CWPSTRUCT m = (CWPSTRUCT)Marshal.PtrToStructure(lParam, typeof(CWPSTRUCT));

            _thisInstance.CheckWndProcMsgs(m);
        }

        nextHook = CallNextHookEx(hookWndHandle, nCode, wParam, lParam);
    }
    catch
    {
        nextHook = 0;
    }

    return nextHook;
}

private void CheckWndProcMsgs(CWPSTRUCT m)
{
    if (m.hwnd == ((this.Controls.Find("btn", true) as Control[])[0] as Button).Handle)
    {
        #region BUTTON

        switch (m.message)
        {
            case (int)WindowsMessages.WM_CREATE:
                {
                    // when the handle of the control is created also a new font is created
                    // and a WM_SETFONT message sent along with the specification that a redraw must be performed
                    Rect rc = new Rect(new Rectangle(0, 0, ((this.Controls.Find("btn", true) as Control[])[0] as UButton).Width, ((this.Controls.Find("btn", true) as Control[])[0] as UButton).Height));
                    hFontNew = CreateFont(
                        25, 5, 2, 0, 900, 0, 0, 0,
                        (byte)FontCharSet.OEM_CHARSET,
                        (byte)FontPrecision.OUT_DEFAULT_PRECIS,
                        (byte)FontClipPrecision.CLIP_DEFAULT_PRECIS,
                        (byte)FontQuality.DEFAULT_QUALITY,
                        (byte)(FontPitchAndFamily.DEFAULT_PITCH | FontPitchAndFamily.FF_DONTCARE),
                        "Verdana");

                    SendMessage(m.hwnd, (uint)WindowsMessages.WM_SETFONT, hFontNew, (IntPtr)1);
                }
                break;
            case (int)WindowsMessages.WM_SETFONT:
                {
                    // the purpose of this block is to verify if the new font is set correct
                    LOGFONT lf = new LOGFONT();
                    IntPtr lf0 = Marshal.AllocCoTaskMem(Marshal.SizeOf(lf));

                    GetObject((IntPtr)m.wparam, Marshal.SizeOf(lf), lf0);
                    lf = (LOGFONT)Marshal.PtrToStructure(lf0, lf.GetType());

                    Marshal.FreeCoTaskMem(lf0);
                }
                break;
            case (int)WindowsMessages.WM_DESTROY:
                {
                    // when the control is destroyed the new font is also deleted
                    if (hFontNew != IntPtr.Zero)
                    {
                        DeleteObject(hFontNew);
                    }
                }
                break;
        }

        #endregion
    }
}

这件作品非常适用于使用 CreateWindow/CreateWindowEx 函数创建的窗口,或者如果Paint event将由用户处理自定义绘画,但我希望将所有绘画留给系统,只更改字体而无需任何其他干预到控制。

这种方法应该有效吗?如果没有,有人可以解释我为什么吗?我知道当系统绘制控件时,它使用由Control.Font类的属性设置的字体,但是,它不应该也使用窗口消息中的字体呈现吗?当类在方法中处理系统消息时,它不是在绘画WndProc时传递进来的数据Message.WParam & Message.LParam吗?WndProc

例如。绘制文本时,系统不应该使用 WM_GETFONT 来使用适当的字体吗?我不知道任何将字体渲染设置在类之外的 Control 属性。

这个问题还有其他解决方案吗?

4

1 回答 1

0

我看到回答我自己的问题开始成为一种习惯:D

好吧..没问题,只要我找到答案,

无论如何,我的问题的解决方案非常简单:在类之外呈现按钮、标签或组框文本(不继承基类并覆盖 OnPaint 方法),应该将控件的FaltStyle属性设置为系统(例如。myControl.FlatStyle = FlatStyle.System;),因此系统将处理绘图。

然后,安装一个全局窗口过程并覆盖该WM_PAINT消息。这是我发现创建新逻辑字体并将 WM_SETFONT 消息发送到控件的一个好地方。

效果很好,但是,有一个更简单的方法!

我也玩过Font.FromLogFont(LOGFONT)一段时间但没有成功;字体质量尚未设置,但我不知道为什么不设置。

如果有人可以解释原因,请这样做。

于 2011-03-19T14:23:06.760 回答