加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何引用一些Windows元素(滚动条,推入选项按钮等)使用的BG

发布时间:2020-12-15 21:30:22 所属栏目:百科 来源:网络整理
导读:有谁知道我如何在C#中引用系统背景颜色,用于滚动条或推入选项按钮或标签等元素(作为下图中“tabPage1”的背景)? 或者,如果不存在预定义的const,那么任何算法如何用这种颜色创建一个Brush?谢谢! 我直观的第一选择SystemColors.ScrollBar与.Control或.Butto
有谁知道我如何在C#中引用系统背景颜色,用于滚动条或推入选项按钮或标签等元素(作为下图中“tabPage1”的背景)?
或者,如果不存在预定义的const,那么任何算法如何用这种颜色创建一个Brush?谢谢!

我直观的第一选择SystemColors.ScrollBar与.Control或.ButtonFace(‘tabPage2’的BG)相同,这实际上是滚动条的“面部”颜色,而不是我称之为“背景”的颜色.
SystemColors.ControlLight或SystemColors.ControlLightLight不再接近..

当放大时,控件的那个区域看起来像带有.Control和.Window像素的棋盘格,所以对我来说暗示这种颜色可能会抖动;也许这表明标准SystemColors枚举没有定义它的原因?我如何将它应用于另一个控件?

PS:我正在尝试增强自定义TabControl(它支持可禁用的标签 – std控件中缺少的另一个好东西,但这是另一个已经解决过的故事),所以当它的.Appearance设置为Buttons或FlatButtons时,它看起来很相似到原件(如上图所示).选定的选项卡由推入按钮指示,但其背景颜色设置为Control或ButtonFace:

解决方法

好吧,事实证明这比我想象的要简单得多 – 它只是用于填充BG-rect的Brush:

protected override void OnDrawItem( DrawItemEventArgs e )
{
    base.OnDrawItem( e );

    int         i=  e.Index;
    TabPage     p=  this.TabPages[ i ];
    Rectangle   r=  GetTabRect( i );
    Brush       br= null;

    if(  this.Appearance != TabAppearance.Normal  &&  i == this.SelectedIndex  )
        br= new HatchBrush( HatchStyle.Percent50,SystemColors.Control,SystemColors.Window );
    else
        br= new SolidBrush( p.BackColor );

    try                     // fill the BG-rectangle
    {   e.Graphics.FillRectangle( br,r );      }
    finally
    {   br.Dispose( );  }   // make sure brush is disposed of

    ..  // the rest of the event-handler
}

唯一的技巧是无法应用using(Brush br = new ..){..}块,因为初始化与结果对象类型不同.因此,一个有点丑陋的尝试 – 终结构造.充当魅力!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读