c# - 重叠的半透明矩形

标签 c# winforms alphablending

是否可以绘制不会累积 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;
    }
}

最佳答案

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

尝试自定义Windows富文本控件是可以的,但最后总是收场不好。这就是为什么有这么多第 3 方文本编辑控件的原因。

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

或者获取一个第 3 方文本编辑器,它将为您提供此功能以及许多其他功能。

关于c# - 重叠的半透明矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5051334/

相关文章:

c# - 如何在我的 ASP.NET MVC 5 项目中获取 “Add Controller” 和 “Add View” 菜单选项?

c# - 通过 AJAX 编辑 NHibernate 记录

c# - C# TextBox 中的负数

graphics - 如何在 DirectX 中以恒定的 alpha 混合两个 Sprite 的颜色?

c# - 从映射网络驱动器读取目录

c# - Task.Run 什么时候流 SynchronizationContext 和 ExecutionContext?

c# - 在 .NET 7 和 WinForms 应用程序中使用 SqlDependency

c# - 如何在调用 window.showdialog 后以编程方式关闭窗口?

colors - WebGL 中的混合如何与 GLSL ES 片段着色器一起工作?

c++ - Win32 C++ Alphablend 位图部分透明