如何通过控件在窗体上绘图? [英] How to draw on a windows form over controls ?

查看:71
本文介绍了如何通过控件在窗体上绘图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力做到这里解释的内容:social.msdn.microsoft [dot] com /论坛/ vstudio / en-US / e0f686fe-5fbf-467a-9589-9000088c177f / how-to -draw-on-a-windows-form-over-controls-?forum = csharpgeneral

I am trying to do just what is explained in this here: social.msdn.microsoft[dot]com/Forums/vstudio/en-US/e0f686fe-5fbf-467a-9589-9000088c177f/how-to-draw-on-a-windows-form-over-controls-?forum=csharpgeneral

只有我得到不同的结果。

Only I get different results.

我的行是不在表单上的控件之上。

My line is not on top of controls on the form.

一旦表单上的随机用户控件被重新绘制(当拖动表单时,某些用户控件的一部分在屏幕外,然后向后拖动,暴露了更多的控制权)线条与所有内容重叠,就像预期一样。

Once a random user control on the form gets repainted (when dragging the form so part of some user control is outside the screen and then dragging back, exposing a bit more of the control) the line overlaps everything, like intended.

当鼠标悬停在菜单条上的按钮上时,该线条将从此按钮消失并显示 再次仅在重新绘制一些用户控件时,如上所述。

When the mouse is hovered over a button on a menu strip the line disappears from this button and will appear again only when redrawing some user control as explained above.

当表单被重新绘制(最小化和最大化)时,该行再次"落后"。所有控件。

When the form is repainted(minimized and maximized) the line is yet again "behind" all the controls.

此外,当我的线条没有涂在窗口边框或某些控件上时,我有时会出现不一致的行为。

Also, I sometimes get inconsistent behavior when my line is not painted over the window border or over some of the controls.

我已经尝试在表单上覆盖OnPaint,调用base.Paint然后绘制该行,并且还没有调用base.Paint,也尝试订阅表单Paint事件,但没有任何帮助,我得到了这一行为。

I have tried overriding OnPaint on the form, calling base.Paint and then drawing the line, and also not calling base.Paint, also tried subscribing to the form Paint event, but nothing helps and everything I get this behavior.

private void MainForm_Paint(object sender, PaintEventArgs e)
    {
        IntPtr hdc = GetWindowDC(this.Handle);
        Graphics g = Graphics.FromHdc(hdc);
        Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0));
        g.DrawLine(pen, 20, 10, 3000, 1000);
        g.Dispose();
        ReleaseDC(this.Handle, hdc);
    }

我问过stackoverflow :  stackoverflow [dot] com / questions / 47624872 / drawing-on- a-windows-form-over-controls-not-working-proper

I have asked on stackoverflow : stackoverflow[dot]com/questions/47624872/drawing-on-a-windows-form-over-controls-not-working-properly

为什么会发生这种情况 并且可以在< g class =" gr_之上画一条线gr_1300 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling"数据-GR-ID =" 1300" id =" 1300"> winform< / g> 或自定义用户
控件重叠窗口上的每个子控件而不触及子控件?

Why this happens and is it possible to draw a line on top of a <g class="gr_ gr_1300 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" data-gr-id="1300" id="1300">winform</g> or custom user control that overlaps every child control on the window without touching the child controls?

推荐答案

尝试下一个间接方法。处理表单的
加载事件:

private void Form1_Load( object sender, EventArgs e )
{
   var f = new Form
      {
         FormBorderStyle = FormBorderStyle.None,
         ShowInTaskbar = false,
         BackColor = Color.Magenta,
         TransparencyKey = Color.Magenta,
      };

   Move += ( s, a ) => f.Bounds = RectangleToScreen( ClientRectangle );

   Resize += ( s, a ) => f.Bounds = RectangleToScreen( ClientRectangle );

   f.Paint += DrawOverControls;

   f.Show( this );
   f.Bounds = RectangleToScreen( ClientRectangle );
}

private void DrawOverControls( object sender, PaintEventArgs e )
{
   Graphics g = e.Graphics;
   g.DrawLine( Pens.Black, 20, 10, 3000, 1000 );
}

请注意,单击绘制的线条不会传播到主窗体。


这篇关于如何通过控件在窗体上绘图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆