c# – 在DWM玻璃下使用TextBox进行测试

我正在尝试处理DWM Glass下的TextBox文本的颜色.
我读了很多材料,还没有完美的解决方案.

几乎完美的结果代码我发现这里:http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/316a178e-252b-480d-8cc9-85814c2073d8/,但它有很多轻弹和事件特定的操作(例如:键入一些文本,并按下主页按钮).

我试图解决这些问题.

以下代码是原始代码的突变,但它不依赖于任何事件,只是WM_PAINT.它仍然闪烁,插入符号(文本光标)以某种方式消失!

如何防止闪烁,以及如何获取插入符号(文字光标)?

谢谢.

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;

namespace AeroWindowsFormsApplication
{
    public class AeroTextBox : TextBox
    {
        private const int WM_PAINT = 0xf;

        private bool _aeroFix;

        public AeroTextBox()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint,true);
        }

        protected override void WndProc(ref Message m)
        {
            if (_aeroFix)
            {
                switch (m.Msg)
                {
                    case WM_PAINT:
                        RedrawAsBitmap();
                        m.Result = new IntPtr(1);
                        break;

                    default:
                        base.WndProc(ref m);
                        break;
                }
            }
            else
            {
                base.WndProc(ref m);
            }
        }

        private void RedrawAsBitmap()
        {
            using (Bitmap bm = new Bitmap(this.Width,this.Height))
            using (Graphics g = this.CreateGraphics())
            {
                this.DrawToBitmap(bm,this.ClientRectangle);
                g.DrawImageUnscaled(bm,-1,-1);
            }
        }

        public bool AeroFix
        {
            get { return _aeroFix; }
            set 
            {
                if (_aeroFix != value)
                {
                    Invalidate();
                }

                _aeroFix = value;
            }
        }
    }
}

解决方法

如果将窗体中的TransparencyKey设置为玻璃区域中的背景颜色,则可以对其使用任何控件,但不能使用放置在其中的任何一个控件中的TransparencyKey中指定的颜色.

这种方法不方便您在后台点击窗口上的玻璃.但也可能有一种方法.

编辑:我一直在寻找这个很长一段时间,这一定是不可能的. Carret由Windows API本身管理,您不能强制它以您想要的方式显示.你可以做的是自己绘制整个文本框,但是这么多工作太少了.

我的总结:GDI和DWM并没有很好的结合.我放弃.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...