http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 17025|回复: 5

[其他] 关于C#listBox控件的背景透明,和设置控件背景图片

[复制链接]
发表于 2013-4-25 13:56:50 | 显示全部楼层 |阅读模式
关于C#listBox控件的背景颜色透明问题,还有可不可以设置控件的背景图片,,,这问题纠结了很久。。


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-4-25 14:00:32 | 显示全部楼层
在listbox的属性选项里设forecolor为:web选项卡里的第一个Transparent.
发表于 2013-4-25 14:04:15 | 显示全部楼层
背景图片和透明安说是不支持的,建议你使用其它控件
 楼主| 发表于 2013-4-25 14:52:44 | 显示全部楼层
站长苏飞 发表于 2013-4-25 14:00
在listbox的属性选项里设forecolor为:web选项卡里的第一个Transparent.

我想把BackColor属性设为透明,,好像要重绘什么的。。是可以实现这效果的。
发表于 2013-4-25 15:01:15 | 显示全部楼层
Qiū_yongjin 发表于 2013-4-25 14:52
我想把BackColor属性设为透明,,好像要重绘什么的。。是可以实现这效果的。

没这样整过,你试试吧,
发表于 2013-4-25 15:07:06 | 显示全部楼层
C#中希望搞一个背景透明的ListBox,且文本居中,却发现VS里面自带的ListBox控件不支持背景透明,怎么办?
我们可以做一个自定义控件,重载里面的OnPaint函数,使ListBox支持背景透明
[code=csharp]
    /// <summary>
    /// 实现背景透明的ListBox,且文本居中
    /// </summary>
    public class TransParentListBox : ListBox
    {
        public TransParentListBox()
        {
            //如果为 true,控件将自行绘制,而不是通过操作系统来绘制。
            //如果为 false,将不会引发 Paint 事件。此样式仅适用于派生自 Control 的类。
            this.SetStyle(ControlStyles.UserPaint, true);

            //如果为 true,控件接受 alpha 组件小于 255 的 BackColor 以模拟透明。
            //仅在 UserPaint 位设置为 true 并且父控件派生自 Control 时才模拟透明。
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            this.Invalidate();
            base.OnSelectedIndexChanged(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (this.Focused && this.SelectedItem != null)
            {
                //设置选中行背景颜色
                Rectangle itemRect = this.GetItemRectangle(this.SelectedIndex);
                //e.Graphics.FillRectangle(Brushes.Red, itemRect);
                e.Graphics.FillRectangle(Brushes.LightBlue, itemRect);
            }
            for (int i = 0; i < Items.Count; i++)
            {
                //设置绘制文字的格式
                StringFormat strFmt = new System.Drawing.StringFormat();
                strFmt.Alignment = StringAlignment.Center; //文本垂直居中
                strFmt.LineAlignment = StringAlignment.Center; //文本水平居中
                e.Graphics.DrawString(this.GetItemText(this.Items), this.Font, new SolidBrush(this.ForeColor), this.GetItemRectangle(i), strFmt);

                //e.Graphics.DrawString(this.GetItemText(this.Items), this.Font, new SolidBrush(this.ForeColor), this.GetItemRectangle(i));
            }
            base.OnPaint(e);
        }
    }[/code]
使用方法是项目--新建一个组件类,把代码放进去就行了
未命名.jpg
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-3 22:36

© 2014-2021

快速回复 返回顶部 返回列表