Is it possible to draw overlapping semi-transparent (with a low alpha value) which will not accumulate the alpha values? When I do it now, if in the overlapping area I have a rectangle with alpha of 50, the alpha value of开发者_运维百科 the overlapping area will be 100.
Here's a sample program which demonstrate the problem.
I added a button which draws the rectangle in the same place and that increases the alpha value.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);
}
} }
and the designer:
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;
}
}
You cannot paint with transparency to a windows control this way. Compositing can only happen between BeginPaint and EndPaint (or on a DC created from a bitmap).
Attempting to customizing the Windows rich text control is possible, but in the end always ends badly. That is why there are so many 3rd party text editing controls out there.
My suggestion is that you ask a different question, "How do I draw the background of a line on a RichTextBox in a different color?"
Or get a 3rd party text editor which will give you this feature and many, many more.
It is still not that completely clear what is going on, especially whether your color
is constant etc but you can try setting m_graphics.CompositingMode
property to SourceCopy
value.
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.compositingmode.aspx
精彩评论