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

iroyal 2008-07-11 11:33:36
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 )
{
}


还有什么可用的方法大家帮帮忙吧,急用,非常感谢!
...全文
1660 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
sp0602 2011-09-21
  • 打赏
  • 举报
回复
有没有找到合适的方法是richtextbox透明呢,发一个学习一下啊
yunfan2009 2010-07-22
  • 打赏
  • 举报
回复
我也在关注这个
xmjj1983 2010-06-28
  • 打赏
  • 举报
回复
我也来看看
cckkcckk 2010-05-06
  • 打赏
  • 举报
回复
我也来看看,对这么蛮有兴趣~
benbenRH 2010-01-09
  • 打赏
  • 举报
回复
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
iroyal 2008-07-12
  • 打赏
  • 举报
回复
6楼的方法可以实现效果,但是偶尔会出现“死机”现象,
比如CTRL+A全选或拖动滚动条的时候,偶尔会出现死机,大概持续十几秒
占用CPU100%连任务管理器都打不开。不知道是什么原因

还有谁有更好的办法或者能解决上边问题的吗?
满意再追加100分
iroyal 2008-07-11
  • 打赏
  • 举报
回复
大家帮帮忙吧
beancurd005 2008-07-11
  • 打赏
  • 举报
回复
顶哈哦,网上实在太多这样的美化控件的方法了,应该很多的哦!
iroyal 2008-07-11
  • 打赏
  • 举报
回复
是RichTextBox
不是PictureBox
viki117 2008-07-11
  • 打赏
  • 举报
回复
在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();

和你要的类似的。。修改下就好了
Tll_W 2008-07-11
  • 打赏
  • 举报
回复
帮你定哈,现在大家都喜欢搞这些问题,界面美化。。。
halve 2008-07-11
  • 打赏
  • 举报
回复
ls 的方法管用? 以前找到过这个说法, 不过最后没有采纳, 具体原因, 忘了...
vivianfdlpw 2008-07-11
  • 打赏
  • 举报
回复
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'.

110,825

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧