将透明图像移到另一个透明图像上

时间:2012-08-05 14:34:26

标签: c# controls transparent

我定义SelecterControl用户控件(背景中的透明图片),其中包含带有透明图像的Roll控件。

我想将前控制移到SelecterControl上。

何时这样做,SelecterControl的透明区域不清楚。

enter image description here

我的完整代码如下:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace TestControl
{
public partial class SelecterControl : UserControl
{
    private Roll roll;

    public SelecterControl()
    {
        InitializeComponent();
        BackgroundImage = Image.FromStream(new MemoryStream(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAPsAAAAWCAYAAAAGqOr6AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3AgFDRwUWvPXdAAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAACxklEQVR42u3cv0sbYRwG8McgucnXpiBCThyCpUMqGa4RobhlKDjKBSLt0EU7tbQQmv4FjQj94aRZOhSSkiKdQqFNJxGkIUOKGQohFMmlBEHrOSVQ0yE5aYJW7jyVO54PhCwX3vDePfd+733vbgBn88G6fRCRWReSuYEzGlQABM7RaJ6BJzId9Mg5Al8FUDwpd4MnNXZj6Lby+uH7pPB7Fb3egvB7TbWm11uYXZ5Y5H4jMicXr+xZyZvwe6HXW9X7b2ZSe61fgW7gq/+Gvj/svsx8OSlkaWEsPITxaYFhWTL9hz8lqtxrRBbdeTRm+jcHWhM7W3rg3eON5PfPP/PPSxGjSjge5Qf7ziprwu9VrTRGRFdnWJYwOTeCybkRCL83kimUfbF0MNUNehEAPMbGG692njHoRO6oDMbCQ8qLUF5FZ97N1z+yJ2/Njdg90eBzWD9d46HS4ze7wJm6WY7gCfLdwOcHu6P6mpAlS9fn/6F0v500Gz/Dw6THBrvAuWW9kCVk5stqLB2sAvAZI7s6Pi1sbezpzbdqWJlShb0nELpc99gFzjU+LaBrTQXp3jLe9nI7rEzhbjJgd7VA5GoXuZLlATrrdAwlkftKeb3e6g07EbmfBwCE34sDrcneIHKRA61p3P3ac1PNvt3X7YXiNyABcIKO6Mrt5+KVzqpYKdtIlrKN9tGfI1s+uXilDWABzltnJ7pSuXjFthyWso12Zr78xcihBwBC0dGErjVZyhO5qITXtSZi6WDCKOOPJ+iELC1ur++yl4hcYHt9F7XC4RI6T76hJ+yh6GhKyFJqc6XGniJysM2VGvR660MsHVzCaY+4hqKji6Vso7i5UlsTsmT5EVdjUoDdTnR5ZfvOlo5a4RAAlmaXJxL925z6pppStpHVtaZ6jpdXXGfgiczJxSttqy+vAJD/uPo1+vLHA+aOiIjI9f4CBEpvEw5QVnUAAAAASUVORK5CYII=")));
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        BackColor = Color.Transparent;
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20;
            return cp;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (BackgroundImage != null)
            e.Graphics.DrawImage(BackgroundImage, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel);
    }

    #region Roll Horizontal Movement

    int strtX;

    private void roll_MouseDown(object sender, MouseEventArgs e)
    {
        strtX = e.X;
    }

    private void roll_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
            roll.Left += e.X - strtX;
    }

    #endregion

    #region Designer generated code

    private System.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SelecterControl));
        this.roll = new MapControl.Roll();
        this.SuspendLayout();
        // 
        // roll
        // 
        this.roll.BackColor = System.Drawing.Color.Transparent;
        //this.roll.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("roll.BackgroundImage")));
        this.roll.Location = new System.Drawing.Point(87, 1);
        this.roll.Name = "roll";
        this.roll.Size = new System.Drawing.Size(13, 18);
        this.roll.TabIndex = 0;
        this.roll.MouseDown += new System.Windows.Forms.MouseEventHandler(this.roll_MouseDown);
        this.roll.MouseMove += new System.Windows.Forms.MouseEventHandler(this.roll_MouseMove);
        // 
        // SelecterControl
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        //this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
        this.Controls.Add(this.roll);
        this.Name = "SelecterControl";
        this.Size = new System.Drawing.Size(251, 22);
        this.ResumeLayout(false);

    }

    #endregion
}


class Roll : Control
{
    public Roll()
    {
        BackgroundImage = Image.FromStream(new MemoryStream(Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAA0AAAASCAYAAACAa1QyAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3AgFDR0RM4ISugAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAA5UlEQVQoz2NkQAWCUMyARMPAexjNAhORjTul5GEhd5cBC1jc4h/249nJ9zCNjAwMDAwaufeVbDU57zIwMLyfnCKKYkPunNcwjek/np28x8DAcI+JgYGBAVnDozd/4BoevfnDMDlFlIGBgYEhtmbjTJizmWAKYBrkROAuZpATYUHRCANM2PygU/iIQafwEQMuwMRABhjVRIkmFmyCV/rl8Ol5z4SUxt7DUgByMpITYYGnP1iChWkSYmBgEIRpRE5GMA3LJ2abQDW9Z4QpSJ32QpCBgeEdNvesnt+p/OF0/3ukPEU6AADyU1AuXNEg+gAAAABJRU5ErkJggg==")));
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        BackColor = Color.Transparent;
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20;
            return cp;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        if (BackgroundImage != null)
            e.Graphics.DrawImage(BackgroundImage, e.ClipRectangle, e.ClipRectangle, GraphicsUnit.Pixel);
    }
}
}

这个问题的原因是什么? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

    this.roll.Size = new System.Drawing.Size(13, 18);

这是代码中的核心问题,“滚动”控件太小了。当您在CreateParams属性中使用WS_EX_TRANSPARENT标志时,您要求Windows确保控件后面的窗口始终首先绘制。它设置了“roll”控件的背景像素,因此它可以在它们之上绘制自己的东西。

麻烦的是,你的滚动控制是如此之小,以至于你移动之后,它不足以重叠先前绘制的杂散像素。它们不再位于滚动控制窗口内。所以他们会留在那里,产生你看到的涂抹效果。

避免这种情况的唯一方法是使您的滚动控制更大,以便始终捕获这些像素。实际上,与滑块的范围一样大。问题是,你现在失去了使“滚动”控制自己的所有好处。像命中测试这样的事情变得复杂,你需要知道拇指的位置才能知道用户点击它。

你也可以创建一个控件来绘制滑块和拇指。所需代码完全相同。由于您使用较少的系统资源,窗口是昂贵的对象,因此效率会更高。