从 png 创建透明表单

发布于 2024-12-07 15:05:08 字数 77 浏览 1 评论 0原文

我有一个具有透明部分的 png 图像,如何将此 png 图像设置为我的 WinForms 表单的背景图像而不丢失透明度?我使用 C#。谢谢!

I have a png image that have transparent parts, how to set this png image like background image for my WinForms form and not lose transparency? i Use C#. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

·深蓝 2024-12-14 15:05:08

这里我写了一个绘制表单的代码。您可以根据我们的要求更改颜色和透明度。我使用颜色作为表单的背景。您可以根据您的要求将其更改为图片。这是一个示例代码。

首先,您需要创建一个包含这些函数的静态类

public enum FormType
        {
            MDI,
            Child
        }

        public static void PaintFrom(Form frm, PaintEventArgs e, FormType formType)
        {
            if (formType == FormType.MDI)
            {
                Graphics mGraphics = e.Graphics;
                Pen pen1 = new Pen(Color.FromArgb(96, 155, 173), 1);

                Rectangle Area1 = new Rectangle(0, 0, frm.Width - 1, frm.Height - 1);
                LinearGradientBrush LGB = new LinearGradientBrush(Area1, Color.FromArgb(96, 155, 173), Color.FromArgb(245, 251, 251), LinearGradientMode.Vertical);
                mGraphics.FillRectangle(LGB, Area1);
                mGraphics.DrawRectangle(pen1, Area1);
                PictureBox picBox = new PictureBox();
                Color backColor = Color.Transparent;
                Bitmap bm = new Bitmap(ImagePath + "title_bar.png");
                //frm.Controls.Add(picBox);
                Point pt = new Point(0, 0);
                picBox.Location = pt;
                picBox.Image = bm;
                picBox.Width = frm.Width - 1;
                picBox.Height = 24;//frm.Height - 1;
                picBox.BackColor = backColor;
                picBox.BackgroundImageLayout = ImageLayout.Stretch;


                PictureBox closeBox = new PictureBox();
                //frm.Controls.Add(closeBox);
                bm = new Bitmap(ImagePath + "close.gif");
                pt = new Point(frm.Width - (bm.Width), -1);
                closeBox.Location = pt;
                closeBox.Image = bm;
                closeBox.Width = bm.Width + 1;
                closeBox.Height = bm.Width + 1;
                closeBox.BackColor = backColor;
                closeBox.BackgroundImageLayout = ImageLayout.Stretch;

                PictureBox minBox = new PictureBox();
                //frm.Controls.Add(closeBox);
                bm = new Bitmap(ImagePath + "close.gif");
                pt = new Point(frm.Width - (2*(bm.Width))-1, bm.Width);
                minBox.Location = pt;
                minBox.Image = bm;
                minBox.Width = bm.Width + 1;
                minBox.Height = bm.Width + 1;
                minBox.BackColor = backColor;
                minBox.BackgroundImageLayout = ImageLayout.Stretch;

                frm.Controls.Add(picBox);
                picBox.Controls.Add(closeBox);
                picBox.Controls.Add(minBox);
                minBox.Click+=new EventHandler(minBox_Click);
                closeBox.Click += new EventHandler(closeBox_Click);
            }
            else
            {
                PaintForm(frm, e);
            }
        }
        public static void PaintForm(Form frm, PaintEventArgs e)
        {

            Graphics mGraphics = e.Graphics;
            Pen pen1 = new Pen(Color.FromArgb(96, 155, 173), 1);

            Rectangle Area1 = new Rectangle(0, 0, frm.Width - 1, frm.Height - 1);
            LinearGradientBrush LGB = new LinearGradientBrush(Area1, Color.FromArgb(96, 155, 173), Color.FromArgb(245, 251, 251), LinearGradientMode.Vertical);
            mGraphics.FillRectangle(LGB, Area1);
            mGraphics.DrawRectangle(pen1, Area1);
            PictureBox picBox=new PictureBox();
            Color backColor = Color.Transparent;
            Bitmap bm=new Bitmap(ImagePath+"title_bar.png");
            //frm.Controls.Add(picBox);
            Point pt=new Point(0,0);
            picBox.Location = pt;
            picBox.Image = bm;
            picBox.Width = frm.Width - 1;
            picBox.Height = 24;//frm.Height - 1;
            picBox.BackColor = backColor;
            picBox.BackgroundImageLayout = ImageLayout.Stretch;


            PictureBox closeBox = new PictureBox();
            //frm.Controls.Add(closeBox);
            bm = new Bitmap(ImagePath + "close.gif");
            pt = new Point(frm.Width - (bm.Width), -1);
            closeBox.Location = pt;
            closeBox.Image = bm;
            closeBox.Width = bm.Width + 1;
            closeBox.Height = bm.Width + 1;
            closeBox.BackColor = backColor;
            closeBox.BackgroundImageLayout = ImageLayout.Stretch;

            foreach (Control ctr in frm.Controls)
            {
                if (ctr.HasChildren)
                {
                    if (ctr is DataGridView)
                    {
                        DataGridView dtg = ctr as DataGridView;
                        DataGridViewCellStyle dtstyle=new DataGridViewCellStyle();
                        dtstyle.BackColor = Color.FromArgb(96, 155, 173);
                        dtg.ColumnHeadersDefaultCellStyle = dtstyle;
                    }
                    else if (ctr is TextBox)
                    {
                    }
                    else if (ctr is TabControl)
                    {

                    }
                    else
                    {
                        ctr.BackColor = backColor;
                    }

                }
                if (ctr is Label)
                {
                    ctr.BackColor = backColor;
                }

            }


            frm.Controls.Add(picBox);
            picBox.Controls.Add(closeBox);
            closeBox.Click+=new EventHandler(closeBox_Click);

        }
        static void closeBox_Click(object sender, EventArgs e)
        {
            PictureBox close = sender as PictureBox;
            PictureBox pic = close.Parent as PictureBox;
            Form fm = pic.Parent as Form;
            fm.Close();
        }
        static void minBox_Click(object sender, EventArgs e)
        {
            PictureBox min = sender as PictureBox;
            PictureBox pic = min.Parent as PictureBox;
            Form fm = pic.Parent as Form;
            fm.WindowState = FormWindowState.Minimized;
        }

,然后您需要为表单调用一个 Paint Event,在这个事件中,您可以像这样绘制表单,

 private void frmComplaints_Paint(object sender, PaintEventArgs e)
    {
        UI.Common.PaintForm(this, e);
    }

我使用了一个静态类 UI.Common 来实现这个函数,并且我使用了一个图像对于标题栏。在您的情况下,您可以使用 png 图像作为背景。代码中的ImagePath是一个常量变量,可以设置保存图片的目录路径

Here I have written a code for painting the form. You can change the color and transparency according to our requirement. I used colors as background of the form. You can change it as picture as per your requirement. It is a sample code.

First you need to create a static class containing these functions

public enum FormType
        {
            MDI,
            Child
        }

        public static void PaintFrom(Form frm, PaintEventArgs e, FormType formType)
        {
            if (formType == FormType.MDI)
            {
                Graphics mGraphics = e.Graphics;
                Pen pen1 = new Pen(Color.FromArgb(96, 155, 173), 1);

                Rectangle Area1 = new Rectangle(0, 0, frm.Width - 1, frm.Height - 1);
                LinearGradientBrush LGB = new LinearGradientBrush(Area1, Color.FromArgb(96, 155, 173), Color.FromArgb(245, 251, 251), LinearGradientMode.Vertical);
                mGraphics.FillRectangle(LGB, Area1);
                mGraphics.DrawRectangle(pen1, Area1);
                PictureBox picBox = new PictureBox();
                Color backColor = Color.Transparent;
                Bitmap bm = new Bitmap(ImagePath + "title_bar.png");
                //frm.Controls.Add(picBox);
                Point pt = new Point(0, 0);
                picBox.Location = pt;
                picBox.Image = bm;
                picBox.Width = frm.Width - 1;
                picBox.Height = 24;//frm.Height - 1;
                picBox.BackColor = backColor;
                picBox.BackgroundImageLayout = ImageLayout.Stretch;


                PictureBox closeBox = new PictureBox();
                //frm.Controls.Add(closeBox);
                bm = new Bitmap(ImagePath + "close.gif");
                pt = new Point(frm.Width - (bm.Width), -1);
                closeBox.Location = pt;
                closeBox.Image = bm;
                closeBox.Width = bm.Width + 1;
                closeBox.Height = bm.Width + 1;
                closeBox.BackColor = backColor;
                closeBox.BackgroundImageLayout = ImageLayout.Stretch;

                PictureBox minBox = new PictureBox();
                //frm.Controls.Add(closeBox);
                bm = new Bitmap(ImagePath + "close.gif");
                pt = new Point(frm.Width - (2*(bm.Width))-1, bm.Width);
                minBox.Location = pt;
                minBox.Image = bm;
                minBox.Width = bm.Width + 1;
                minBox.Height = bm.Width + 1;
                minBox.BackColor = backColor;
                minBox.BackgroundImageLayout = ImageLayout.Stretch;

                frm.Controls.Add(picBox);
                picBox.Controls.Add(closeBox);
                picBox.Controls.Add(minBox);
                minBox.Click+=new EventHandler(minBox_Click);
                closeBox.Click += new EventHandler(closeBox_Click);
            }
            else
            {
                PaintForm(frm, e);
            }
        }
        public static void PaintForm(Form frm, PaintEventArgs e)
        {

            Graphics mGraphics = e.Graphics;
            Pen pen1 = new Pen(Color.FromArgb(96, 155, 173), 1);

            Rectangle Area1 = new Rectangle(0, 0, frm.Width - 1, frm.Height - 1);
            LinearGradientBrush LGB = new LinearGradientBrush(Area1, Color.FromArgb(96, 155, 173), Color.FromArgb(245, 251, 251), LinearGradientMode.Vertical);
            mGraphics.FillRectangle(LGB, Area1);
            mGraphics.DrawRectangle(pen1, Area1);
            PictureBox picBox=new PictureBox();
            Color backColor = Color.Transparent;
            Bitmap bm=new Bitmap(ImagePath+"title_bar.png");
            //frm.Controls.Add(picBox);
            Point pt=new Point(0,0);
            picBox.Location = pt;
            picBox.Image = bm;
            picBox.Width = frm.Width - 1;
            picBox.Height = 24;//frm.Height - 1;
            picBox.BackColor = backColor;
            picBox.BackgroundImageLayout = ImageLayout.Stretch;


            PictureBox closeBox = new PictureBox();
            //frm.Controls.Add(closeBox);
            bm = new Bitmap(ImagePath + "close.gif");
            pt = new Point(frm.Width - (bm.Width), -1);
            closeBox.Location = pt;
            closeBox.Image = bm;
            closeBox.Width = bm.Width + 1;
            closeBox.Height = bm.Width + 1;
            closeBox.BackColor = backColor;
            closeBox.BackgroundImageLayout = ImageLayout.Stretch;

            foreach (Control ctr in frm.Controls)
            {
                if (ctr.HasChildren)
                {
                    if (ctr is DataGridView)
                    {
                        DataGridView dtg = ctr as DataGridView;
                        DataGridViewCellStyle dtstyle=new DataGridViewCellStyle();
                        dtstyle.BackColor = Color.FromArgb(96, 155, 173);
                        dtg.ColumnHeadersDefaultCellStyle = dtstyle;
                    }
                    else if (ctr is TextBox)
                    {
                    }
                    else if (ctr is TabControl)
                    {

                    }
                    else
                    {
                        ctr.BackColor = backColor;
                    }

                }
                if (ctr is Label)
                {
                    ctr.BackColor = backColor;
                }

            }


            frm.Controls.Add(picBox);
            picBox.Controls.Add(closeBox);
            closeBox.Click+=new EventHandler(closeBox_Click);

        }
        static void closeBox_Click(object sender, EventArgs e)
        {
            PictureBox close = sender as PictureBox;
            PictureBox pic = close.Parent as PictureBox;
            Form fm = pic.Parent as Form;
            fm.Close();
        }
        static void minBox_Click(object sender, EventArgs e)
        {
            PictureBox min = sender as PictureBox;
            PictureBox pic = min.Parent as PictureBox;
            Form fm = pic.Parent as Form;
            fm.WindowState = FormWindowState.Minimized;
        }

Then you need to call a Paint Event for the form and in this event you can paint the form like this

 private void frmComplaints_Paint(object sender, PaintEventArgs e)
    {
        UI.Common.PaintForm(this, e);
    }

I have used a static class UI.Common for this function and i used an image for titlebar. In your case you can use a png image for background. The ImagePath in the code is a constant variable where you can set the path for directory where the image is saved

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文