2011-10-02 73 views
0

我有一个PNG图像,有透明的部分,如何设置这个PNG图像像我的WinForms窗体的背景图像,并没有失去透明度?我使用C#。谢谢!从PNG创建透明窗体

+0

透明,因为在完全不可见或部分不可见的情况下应该与背景混合? –

+0

部分,应与背景混合,谢谢! – Kracken

+0

你不能混合透明背景,没有什么可以混合。 –

回答

0

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

首先,你需要创建一个包含这些功能

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事件的形式和在此事件中的静态类,你可以画的形式,这样

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

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