如何在嵌套按钮上方的TabPage中绘制图像

时间:2015-12-10 07:09:22

标签: c# forms draw

如何在tabPage重叠按钮上绘制图像

黑色圆圈(tabPage8_Paint上的DrawImage)应该 按钮

http://rghost.ru/6sVBl8mkh/image.png

一定是这样的: http://rghost.ru/6BgDM77pq/image.png

我的代码

public SibModem() {

    InitializeComponent();

    tabPage8.Paint += new PaintEventHandler(tabPage8_Paint);
    gettime();

    this.SizeChanged += new EventHandler(this.SibModem_Resize);
}

protected void tabPage8_Paint(object sender, PaintEventArgs e) {

    GraphicsUnit units = GraphicsUnit.Pixel;
    base.OnPaint(e);

    Graphics g = e.Graphics;
    g.DrawImage(bg, 0, 0);

    Rectangle srcRect = new Rectangle(offsetant, 0, w, h);
    g.DrawImage(anten, x, y, srcRect, units);

    Rectangle ussdwaitRect = new Rectangle(offsetussd, 0, 64, 64);
    g.DrawImage(ussdwait, usx, usy, ussdwaitRect, units);
}

2 个答案:

答案 0 :(得分:0)

尝试使用BringToFront方法,它将控件带到z顺序的前面。

在MSDN上查看此参考:https://msdn.microsoft.com/en-us/library/system.windows.forms.control.bringtofront(v=vs.110).aspx

答案 1 :(得分:0)

您无法绘制上方嵌套控件,因此您需要将图像的一部分绘制到那些按钮上。

所以将绘图合并到标签页并将绘制到您需要装饰的按钮上!

以下是仅使用一张图片的简单示例:

enter image description here

一些班级变量:

Point iLoc = Point.Empty;
Image img = null;
List<Button> overlaidButtons = new List<Button>();

准备图像,位置和可能重叠的按钮列表:

public Form1()
{
    InitializeComponent();

    string imgN = @"d:\scrape\gears\gear_12x4hand.png";
    img = Image.FromFile(imgN);
    iLoc = new Point(100, 100);
    overlaidButtons.AddRange(new []{button10,button11,button12,button13 });
    // each button calls the same paint event
    foreach (Button btn in overlaidButtons) btn.Paint += btn_Paint;
}

常见Paint事件。我们计算图像的相对位置..

void btn_Paint(object sender, PaintEventArgs e)
{
    Button btn = sender as Button;
    e.Graphics.DrawImage(img, new Point(iLoc.X - btn.Left, iLoc.Y - btn.Top));
}

请注意,如果Buttons嵌套得更深,则需要调整计算以包含所有级别的嵌套!

TabPage Paint事件:

private void tabPage5_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImage(img, iLoc);
}