简体   繁体   中英

How to offer the user the possibility to paint consistently a custom control with additional controls on it

To make it simple, let's suppose to have a custom control with fully personalized graphics and that contains also inside a panel (with thin black borders in the picture):

在此处输入图片说明

Now, the custom control should provide the user with a mechanism to personalize it further. Let's suppose for instance that the user needs to paint the red vertical line on it (see picture). The line is partly on the user control background and partly on the panel. Like it is now, by using the control_paint event the user will end up on painting below the panel.

What would you suggest in similar cases to make the life easier to the user?

EDIT

The panel is just an example. Instead of that I have two other controls (one for instance is a ruler, which paints labels an ticks according to various parameters) whose logic is quite complex.

You can create a transparent control, and put the transparent control above your other controls for drawing purpose and make it invisible when you don't need.

Here is the code for Transparent control and a simple line, then you can put required logic for painting in OnMouseMove and OnMouseDown and OnMouseUp and draw what you need, simply like I did in OnPaint:

using System.Drawing;
using System.Windows.Forms;
public class TransparentControl : Control
{
    public TransparentControl()
    {
        SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor, true);
    }
    protected override void OnPaintBackground(PaintEventArgs e)
    {
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.DrawLine(Pens.Red, 0, 0, Width, Height);
    }
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x20;
            return cp;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM