c# RichTextBox怎样设置背景透明?

时间:2021-11-07 16:03:06
RichTextBox 的BackColor属性不能为Color.Transparent
想让RichTextBox透明显示FORM的背景图片

在网上找到以下方法,有BUG,输入内容和删除的时候显示不正常
class TransparentRichTextBox : RichTextBox
{
   public TransparentRichTextBox()
   {
      base.ScrollBars = RichTextBoxScrollBars.None;
   }

   override protected CreateParams CreateParams
   {
      get
      {
         CreateParams cp = base.CreateParams;
         cp.ExStyle |= 0x20;
         return cp;
      }
   }

   override protected void OnPaintBackground( PaintEventArgs e )
   {
   }


还有什么可用的方法大家帮帮忙吧,急用,非常感谢!

13 个解决方案

#1


帮你定哈,现在大家都喜欢搞这些问题,界面美化。。。

#2


在pictureBox的Paint事件中写下如下代码

private void pictureBox1_Paint(object sender, PaintEventArgs e)

        {

            foreach (Control C in this.Controls)

            {

                if (C is Label)

                {

                    Label L = (Label)C;

                   L.Visible = false;

                    e.Graphics.DrawString(L.Text, L.Font, new

          SolidBrush(L.ForeColor), L.Left - pictureBox1.Left, L.Top - pictureBox1.Top);

                }

            }

        }

或者在载入页面的时候在LOAD中写入下面代码:

pictureBox1.SendToBack();

            label1.BackColor = Color.Transparent;

            label1.Parent = pictureBox1;

            label1.BringToFront();

和你要的类似的。。修改下就好了

#3


是RichTextBox
不是PictureBox

#4


顶哈哦,网上实在太多这样的美化控件的方法了,应该很多的哦!

#5


大家帮帮忙吧

#6


http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2620835&SiteID=1
http://dotnetjunkies.com/WebLog/johnwood/archive/2006/07/04/transparent_richtextbox.aspx

For how and why it works (only for Windows XP and newer), check this link:

 

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

 

The code is as follows:

public class RichEdit50 : RichTextBox
{
 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
 static extern IntPtr LoadLibrary(string lpFileName);

 protected override CreateParams CreateParams
 {
  get
  {
   CreateParams prams = base.CreateParams;
   if (LoadLibrary("msftedit.dll")!=IntPtr.Zero)
   {
    prams.ExStyle |= 0x020; // transparent
    prams.ClassName = "RICHEDIT50W";
   }
   return prams;
  }
 }
}

Just replace you references to RichTextBox with RichEdit50 and you'll be rolling. Because it's transparent you will need to create some kind of background on the control behind the RichTextBox - draw a gradient or something to get some cool effects. Of course if you just want the features of the RichEdit without transparency, comment out the line that ends with '// transparent'. 

#7


ls 的方法管用? 以前找到过这个说法, 不过最后没有采纳, 具体原因, 忘了...

#8


6楼的方法可以实现效果,但是偶尔会出现“死机”现象,
比如CTRL+A全选或拖动滚动条的时候,偶尔会出现死机,大概持续十几秒
占用CPU100%连任务管理器都打不开。不知道是什么原因

还有谁有更好的办法或者能解决上边问题的吗?
满意再追加100分

#9


VB的代码,我不懂VB,会的把它翻译成C#吧,如下:

Private Declare Func呵呵tion SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long 
Private Declare Func呵呵tion GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long 
Const GWL_EXSTYLE = (-20) 
Const WS_EX_TRANSPARENT = &H20& 

Private Sub Form_Load() 
 Dim rtn As Long 
 rtn = GetWindowLong(RichTextBox1.hwnd, GWL_EXSTYLE) 
 SetWindowLong RichTextBox1.hwnd, GWL_EXSTYLE, rtn Or WS_EX_TRANSPARENT 
End Sub


FROM:http://tieba.baidu.com/f?kz=527787768

#10


我也来看看,对这么蛮有兴趣~

#11


我也来看看

#12


我也在关注这个

#13


有没有找到合适的方法是richtextbox透明呢,发一个学习一下啊

#1


帮你定哈,现在大家都喜欢搞这些问题,界面美化。。。

#2


在pictureBox的Paint事件中写下如下代码

private void pictureBox1_Paint(object sender, PaintEventArgs e)

        {

            foreach (Control C in this.Controls)

            {

                if (C is Label)

                {

                    Label L = (Label)C;

                   L.Visible = false;

                    e.Graphics.DrawString(L.Text, L.Font, new

          SolidBrush(L.ForeColor), L.Left - pictureBox1.Left, L.Top - pictureBox1.Top);

                }

            }

        }

或者在载入页面的时候在LOAD中写入下面代码:

pictureBox1.SendToBack();

            label1.BackColor = Color.Transparent;

            label1.Parent = pictureBox1;

            label1.BringToFront();

和你要的类似的。。修改下就好了

#3


是RichTextBox
不是PictureBox

#4


顶哈哦,网上实在太多这样的美化控件的方法了,应该很多的哦!

#5


大家帮帮忙吧

#6


http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2620835&SiteID=1
http://dotnetjunkies.com/WebLog/johnwood/archive/2006/07/04/transparent_richtextbox.aspx

For how and why it works (only for Windows XP and newer), check this link:

 

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

 

The code is as follows:

public class RichEdit50 : RichTextBox
{
 [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
 static extern IntPtr LoadLibrary(string lpFileName);

 protected override CreateParams CreateParams
 {
  get
  {
   CreateParams prams = base.CreateParams;
   if (LoadLibrary("msftedit.dll")!=IntPtr.Zero)
   {
    prams.ExStyle |= 0x020; // transparent
    prams.ClassName = "RICHEDIT50W";
   }
   return prams;
  }
 }
}

Just replace you references to RichTextBox with RichEdit50 and you'll be rolling. Because it's transparent you will need to create some kind of background on the control behind the RichTextBox - draw a gradient or something to get some cool effects. Of course if you just want the features of the RichEdit without transparency, comment out the line that ends with '// transparent'. 

#7


ls 的方法管用? 以前找到过这个说法, 不过最后没有采纳, 具体原因, 忘了...

#8


6楼的方法可以实现效果,但是偶尔会出现“死机”现象,
比如CTRL+A全选或拖动滚动条的时候,偶尔会出现死机,大概持续十几秒
占用CPU100%连任务管理器都打不开。不知道是什么原因

还有谁有更好的办法或者能解决上边问题的吗?
满意再追加100分

#9


VB的代码,我不懂VB,会的把它翻译成C#吧,如下:

Private Declare Func呵呵tion SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long 
Private Declare Func呵呵tion GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long 
Const GWL_EXSTYLE = (-20) 
Const WS_EX_TRANSPARENT = &H20& 

Private Sub Form_Load() 
 Dim rtn As Long 
 rtn = GetWindowLong(RichTextBox1.hwnd, GWL_EXSTYLE) 
 SetWindowLong RichTextBox1.hwnd, GWL_EXSTYLE, rtn Or WS_EX_TRANSPARENT 
End Sub


FROM:http://tieba.baidu.com/f?kz=527787768

#10


我也来看看,对这么蛮有兴趣~

#11


我也来看看

#12


我也在关注这个

#13


有没有找到合适的方法是richtextbox透明呢,发一个学习一下啊