WinForms透明图像绘画绘制了旧的背景

时间:2019-03-05 09:17:39

标签: c# .net winforms

我想在透明的WinForms表单上绘制图像。

在第一幅绘画上,一切看起来都正确,但是一旦我开始移动表格,仍然会绘制“旧的”背景。

背景图片:https://i.stack.imgur.com/K5iH1.png

在启动程序后的第一次绘画中,它会被很好地绘制为透明的:

enter image description here

但是在移动表格之后:

enter image description here

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

namespace TransparentImage
{
    public class Form1 : Form
    {
        private Image _image;

        public Form1()
        {
            TopMost = true;            
            FormBorderStyle = FormBorderStyle.None;
            StartPosition = FormStartPosition.CenterScreen;
            _image = Image.FromFile(@"C:\temp\image.png");
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            //empty implementation
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            // base.OnPaint(e);

            var g = e.Graphics;
            g.DrawImage(_image, Point.Empty);
        }

        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();


        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); // drag event in window caption simulieren
            }
        }
    }
}

0 个答案:

没有答案