0

フォームの拡張ガラス フレームに TextBox を描画しようとしています。この手法については説明しませんが、よく知られています。聞いたことがない人のために例を示します: http://www.danielmoth.com/Blog/Vista-Glass-In-C.aspx

問題は、このガラス フレームに描画するのは複雑なことです。黒は 0 アルファ カラーと見なされるため、黒はすべて消えます。

この問題に対抗する方法は明らかにあります: 複雑な GDI+ シェイプの描画は、このアルファネスの影響を受けません。たとえば、次のコードを使用して、ガラスにラベルを描画できます (注:恐ろしい ClearType 問題を回避するためにGraphicsPath代わりに使用されます)。DrawString

public class GlassLabel : Control
{
    public GlassLabel()
    {
        this.BackColor = Color.Black;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        GraphicsPath font = new GraphicsPath();

        font.AddString(
            this.Text,
            this.Font.FontFamily,
            (int)this.Font.Style,
            this.Font.Size,
            Point.Empty,
            StringFormat.GenericDefault);

        e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
        e.Graphics.FillPath(new SolidBrush(this.ForeColor), font);
    }
}

同様に、このようなアプローチを使用して、ガラス領域にコンテナーを作成できます。長方形の代わりに多角形を使用していることに注意してください。長方形を使用すると、その黒い部分がアルファと見なされます。

public class GlassPanel : Panel
{
    public GlassPanel()
    {
        this.BackColor = Color.Black;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Point[] area = new Point[]
            {
                new Point(0, 1),
                new Point(1, 0),
                new Point(this.Width - 2, 0),
                new Point(this.Width - 1, 1),
                new Point(this.Width -1, this.Height - 2),
                new Point(this.Width -2, this.Height-1),
                new Point(1, this.Height -1),
                new Point(0, this.Height - 2)
            };

        Point[] inArea = new Point[]
            {
                new Point(1, 1),
                new Point(this.Width - 1, 1),
                new Point(this.Width - 1, this.Height - 1),
                new Point(this.Width - 1, this.Height - 1),
                new Point(1, this.Height - 1)
            };

        e.Graphics.FillPolygon(new SolidBrush(Color.FromArgb(240, 240, 240)), inArea);
        e.Graphics.DrawPolygon(new Pen(Color.FromArgb(55, 0, 0, 0)), area);

        base.OnPaint(e);
    }
}

今私の問題は次のとおりです: TextBox を描画するにはどうすればよいですか? 多くのグーグル検索の後、次の解決策を思いつきました。

  • TextBox のOnPaintメソッドをサブクラス化します。これは可能ですが、正しく動作させることはできませんでした。まだ方法がわからない魔法のようなものを描く必要があります。
  • おそらく. TextBox_ TextBoxBase誰かが良い、有効で実用的な例を持っていて、これが良い全体的な解決策になると思うなら、教えてください。
  • を使用しBufferedPaintSetAlphaます。( http://msdn.microsoft.com/en-us/library/ms649805.aspx )。この方法の欠点は、テキスト ボックスの角が奇妙に見えることかもしれませんが、私はそれを受け入れることができます。Graphics オブジェクトからそのメソッドを適切に実装する方法を誰かが知っている場合は、教えてください。私は個人的にはしませんが、これはこれまでのところ最良の解決策のようです。正直なところ、素晴らしい C++ の記事を見つけましたが、変換するのが面倒です。http://weblogs.asp.net/kennykerr/archive/2007/01/23/controls-and-the-desktop-window-manager.aspx

注: BufferedPaint メソッドで成功した場合は、ガラスに描画可能なすべての一般的な Windows フォーム コントロールを備えた単純な DLL を作成することを誓います。

4

1 に答える 1

0

私はしばらく前にこのトピックに時間を費やしました。基本的に必要なのは透明なテキストボックスです。私の最初のアプローチは、 codeproject AlphaBlendTextBox - A transparent/translucent textbox for .NETを使用することでした。しかし、そのコントロールの問題を解決するのが難しいことがいくつかありました。しばらくして、必要な解決策を見つけましたが、それは Windows XP 以降でのみ機能します。同様に、このコントロールを単一行のテキスト ボックスのように動作させるには、RichTextBox.Multiline を false に設定します。

// Source:
// http://www.dotnetjunkies.com/WebLog/johnwood/archive/2006/07/04/transparent_richtextbox.aspx

// It seems there are 4 versions of the RichEdit control out there - when I'm talking about the 
// RichEdit control, I'm talking about the C DLL that either comes with Windows or some version 
// of Office. The files are named either RICHEDXX.DLL (XX is the version number), or MSFTEDIT.DLL 
// and they're in the System32 folder.

// .Net RichTextBox control is bound to version 2. The biggest problem with this version (at least 
// for me) is that it does not render properly if you try to make the window transparent. Later versions, 
// however, do.

// We can fix that. If you create a control deriving from the original RichTextBox control, but overriding 
// the CreateParams property, you can put in a new Windows class name (this is the window class name, 
// nothing to do with classes in the C# sense). This effectively gives us a free upgrade. When the .Net 
// RichTextBox control instantiates, it will now use the latest RichEdit control and not the old, archaic, 
// version 2.

// There are other benefits too - version 3 and beyond of the RichEdit control support quite an extensive 
// array of layout features, such as tables and full text justification. This is the version of the RichEdit 
// that WordPad uses in Windows XP. To really see what it's capable of displaying you can create documents in 
// Word and save them in RTF, load these into the new RichEdit and in a lot of cases it'll look identical, 
// it's that powerful. A full list of features can be found here:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/aboutricheditcontrols.asp

// There are a couple of caveats:
// 
// 1. The control that this is bound to was shipped with Windows XP, and so this code won't work in 
//    Windows 2000 or earlier. 
//
// 2. The RichTextBox control in C# only knows about version 2, so the interface doesn't include 
//    all the new features. You can wrap a few of the features yourself through new methods on the 
//    RichEdit class.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

internal class RichEdit : RichTextBox
{

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr LoadLibrary(string lpFileName);

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams parameters = base.CreateParams;
            if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
            {
                parameters.ExStyle |= 0x020; // transparent
                parameters.ClassName = "RICHEDIT50W";
            }
            return parameters;
        }
    }
}
于 2010-05-29T21:52:38.390 に答える