C# 重叠半透明矩形

C# 重叠半透明矩形,c#,winforms,alphablending,C#,Winforms,Alphablending,是否可以绘制不会累积alpha值的重叠半透明(具有低alpha值)?当我现在这样做时,如果在重叠区域中有一个alpha为50的矩形,那么重叠区域的alpha值将为100 下面是一个示例程序,演示了该问题。 我添加了一个按钮,在同一位置绘制矩形,并增加alpha值 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawi

是否可以绘制不会累积alpha值的重叠半透明(具有低alpha值)?当我现在这样做时,如果在重叠区域中有一个alpha为50的矩形,那么重叠区域的alpha值将为100

下面是一个示例程序,演示了该问题。
我添加了一个按钮,在同一位置绘制矩形,并增加alpha值

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms;

namespace Sample {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richerTextBox1.MarkLine();
        }
    }


    public partial class RicherTextBox : RichTextBox
    {
        private const int WM_PAINT = 0x000F;
        private const int WM_VSCROLL = 277;
        private Graphics m_graphics = null;

        public void MarkLine()
        {
            if (m_graphics == null)
                m_graphics = this.CreateGraphics();

            int firstCharOfLine = this.GetFirstCharIndexFromLine(0);
            Rectangle marker = new Rectangle(0, 0, ClientSize.Width - 1, this.Font.Height);
            m_graphics.FillRectangle(new SolidBrush(Color.FromArgb(60, Color.Red)), marker);

        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_PAINT:
                    MarkLine();
                    break;
                case WM_VSCROLL:
                    MarkLine();
                    break;
            }

            base.WndProc(ref m);
        }
    } }
设计师:

namespace Sample
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.richerTextBox1 = new Sample.RicherTextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // richerTextBox1
            // 
            this.richerTextBox1.Location = new System.Drawing.Point(22, 12);
            this.richerTextBox1.Name = "richerTextBox1";
            this.richerTextBox1.Size = new System.Drawing.Size(239, 183);
            this.richerTextBox1.TabIndex = 1;
            this.richerTextBox1.Text = "";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(168, 210);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(76, 26);
            this.button1.TabIndex = 2;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.richerTextBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }
        #endregion

        private RicherTextBox richerTextBox1;
        private System.Windows.Forms.Button button1;
    }
}
名称空间示例
{
部分类Form1
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.richerTextBox1=新样本.RicherTextBox();
this.button1=new System.Windows.Forms.Button();
这个.SuspendLayout();
// 
//richerTextBox1
// 
this.richerTextBox1.Location=新系统.图纸.点(22,12);
this.richerTextBox1.Name=“richerTextBox1”;
this.richerTextBox1.Size=新系统.Drawing.Size(239183);
this.richerTextBox1.TabIndex=1;
this.richerTextBox1.Text=“”;
// 
//按钮1
// 
this.button1.Location=新系统图纸点(168,210);
this.button1.Name=“button1”;
this.button1.Size=新系统图纸尺寸(76,26);
this.button1.TabIndex=2;
this.button1.Text=“button1”;
this.button1.UseVisualStyleBackColor=true;
this.button1.Click+=新系统.EventHandler(this.button1\u Click);
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(292266);
this.Controls.Add(this.button1);
this.Controls.Add(this.richerTextBox1);
this.Name=“Form1”;
this.Text=“Form1”;
此选项为.resume布局(false);
}
#端区
专用RicherTextBox RicherTextBox 1;
private System.Windows.Forms.Button按钮1;
}
}

仍然不完全清楚发生了什么,特别是您的
颜色是否恒定等,但您可以尝试将
m\u graphics.CompositingMode
属性设置为
SourceCopy


不能以这种方式对windows控件进行透明绘制。合成只能在BeginPaint和EndPaint之间进行(或在从位图创建的DC上)

尝试自定义Windows富文本控件是可能的,但最终结果总是很糟糕。这就是为什么有这么多第三方文本编辑控件的原因

我的建议是你问一个不同的问题,“我如何在RichTextBox上以不同的颜色绘制线条的背景?”


或者找一个第三方文本编辑器,它将为您提供此功能和更多功能。

一些显示如何实际绘制的代码可能会有所帮助什么是m_图形?设备上下文——图形——永远不应该被缓存。您是响应子文本框的绘制事件还是覆盖OnPaint?您在什么时候执行绘图?我正在重写“WndProc”并在“WM_paint”事件上绘制它(在RichTextBox中重写时不会调用OnPaint。您可以向我们显示更完整的代码吗?或者至少创建一个演示该行为的示例吗?好的,我添加了示例程序:)颜色是一种可变颜色。假设它是红色的。我试图改变合成模式,但矩形完全变黑。