再看主窗体
窗体加载
base.OnLoad(e); try { if (Settings.Default.skinColor != null) { this.BackColor = Settings.Default.skinColor; MyFormTemp.CurrentBackgroundColor = Settings.Default.skinColor; } if (Settings.Default.skinPic != null) { if (File.Exists(Settings.Default.skinPic)) { FileStream fs=new FileStream(Settings.Default.skinPic,FileMode.Open); this.BackgroundImage = Image.FromStream(fs); fs.Close(); MyFormTemp.CurrentBackgroundImage = this.BackgroundImage; } } } catch { } skinForm.ColorChanged += new SkinForm.ColorChangedEventHandler(skinPanel_ColorChanged); skinForm.SkinFileChanged += new SkinForm.SkinFileChangedEventHandler(skinPanel_SkinFileChanged); Control c = this.Controls.Find("ButtonMin",true)[0]; this.ButtonMenu.Left = c.Left - ButtonMenu.Width; this.ButtonSkin.Left = ButtonMenu.Left - ButtonSkin.Width; skinForm.OpacityFileChanged += new SkinForm.OpacityChangedEventHandler(skinForm_OpacityFileChanged);
里面有一些事件,是换肤的,在之后的章节中给出
方法
private void ButtonSkin_Click(object sender, EventArgs e) { if (skinForm.Visible == false) { skinForm.Visible = true; skinForm.Left =this.Left+ this.ButtonSkin.Left + this.ButtonSkin.Width - 365; skinForm.Top =this.Top+ this.ButtonSkin.Top + this.ButtonSkin.Height; skinForm.BringToFront(); skinForm.Focus(); } else { skinForm.Visible = false; } } public virtual void skinPanel_SkinFileChanged(string filePath) { if (File.Exists(filePath)) { FileStream fs = new FileStream(filePath,FileMode.Open); Image img = Image.FromStream(fs); fs.Close(); this.BackgroundImage = img; this.BackgroundImageLayout = MyFormTemp.CurrentImageLayout; MyFormTemp.CurrentBackgroundImage = this.BackgroundImage; // this.childForm.BackgroundImage = this.BackgroundImage; try { Settings.Default.skinPic = filePath; Settings.Default.Save(); } catch { } } } public virtual void skinPanel_ColorChanged(HSLColor hsl, Color color) { this.BackgroundImage = null; this.BackColor = color; try { Settings.Default.skinColor = color; Settings.Default.skinPic = null; Settings.Default.Save(); } catch { } // this.childForm.BackColor = color; //this.childForm.BackgroundImage = null; MyFormTemp.CurrentBackgroundColor = color; MyFormTemp.CurrentBackgroundImage = null; }
重绘
protected override void DrawBackground(PaintEventArgs e) { BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current; BufferedGraphics myBuffer = currentContext.Allocate(e.Graphics, e.ClipRectangle); Graphics g = myBuffer.Graphics; g.Clear(this.BackColor); if (this.BackgroundImage != null) { g.DrawImage(this.BackgroundImage,new Rectangle(0,0,this.Width,this.Height)); } int titleHeight = 130; Bitmap Bmp = global::FYJ.Winform.Properties.Resources.main_form_bg; g.DrawImage(Bmp, new Rectangle(0, 0, 5, titleHeight), 0, 0, 5, titleHeight, GraphicsUnit.Pixel); g.DrawImage(Bmp, new Rectangle(5, 0, this.Width - 10, titleHeight), 5, 0, Bmp.Width - 10, titleHeight, GraphicsUnit.Pixel); g.DrawImage(Bmp, new Rectangle(this.Width - 5, 0, 5, titleHeight), Bmp.Width - 5, 0, 5, titleHeight, GraphicsUnit.Pixel); g.DrawImage(Bmp, new Rectangle(0, titleHeight, 2, 25), 0, titleHeight, 2, 25, GraphicsUnit.Pixel); g.DrawImage(Bmp, new Rectangle(2, titleHeight, this.Width - 4, 25), 2, titleHeight, 5, 25, GraphicsUnit.Pixel); g.DrawImage(Bmp, new Rectangle(this.Width - 2, titleHeight, 2, 25), Bmp.Width - 2, titleHeight, 2, 25, GraphicsUnit.Pixel); g.DrawImage(Bmp, new Rectangle(0, titleHeight + 25, 5, Height - titleHeight - 34 - 25), 0, titleHeight + 25, 5, Bmp.Height - 34 - 25 - titleHeight - 20, GraphicsUnit.Pixel); g.DrawImage(Bmp, new Rectangle(5, titleHeight + 25, this.Width - 10, Height - titleHeight - 34 - 25), 5, titleHeight + 25, Bmp.Width - 10, Bmp.Height - 34 - 25 - titleHeight - 20, GraphicsUnit.Pixel); g.DrawImage(Bmp, new Rectangle(this.Width - 5, titleHeight + 25, 5, Height - titleHeight - 34 - 25), Bmp.Width - 5, titleHeight + 25, 5, Bmp.Height - 34 - 25 - titleHeight - 20, GraphicsUnit.Pixel); //底部 g.DrawImage(Bmp, new Rectangle(0, this.Height - 34, 5, 34), 0, Bmp.Height - 34, 5, 34, GraphicsUnit.Pixel); g.DrawImage(Bmp, new Rectangle(5, this.Height - 34, this.Width - 10, 34), 5, Bmp.Height - 34, Bmp.Width - 10, 34, GraphicsUnit.Pixel); g.DrawImage(Bmp, new Rectangle(this.Width - 5, this.Height - 34, 5, 34), Bmp.Width - 5, Bmp.Height - 34, 5, 34, GraphicsUnit.Pixel); int strX = 10; if (this.ShowIcon) { strX = 30; g.DrawIcon(Icon, new Rectangle(8, 7, 16, 16)); } if (!String.IsNullOrEmpty(this.Text)) { g.DrawString(this.Text, new Font("宋体", 11F, FontStyle.Regular), Brushes.WhiteSmoke, strX, 8); } Bmp.Dispose(); myBuffer.Render(e.Graphics); g.Dispose(); myBuffer.Dispose();//释放资源 }
窗体还实现了一个功能,就是拖放一张图片到窗体上会成为它的背景
protected override void OnDragEnter(DragEventArgs drgevent) { base.OnDragEnter(drgevent); drgevent.Effect = DragDropEffects.Move; } protected override void OnDragDrop(DragEventArgs drgevent) { base.OnDragDrop(drgevent); if (drgevent.Data.GetDataPresent(DataFormats.FileDrop, false)) { String[] files = (String[])drgevent.Data.GetData(DataFormats.FileDrop); List tempList = new List(); foreach (String s in files) { if (File.Exists(s)) { if (s.EndsWith(".png", StringComparison.CurrentCultureIgnoreCase) || s.EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase) || s.EndsWith(".bmp", StringComparison.CurrentCultureIgnoreCase)) { this.BackgroundImage = Image.FromFile(s); MyFormTemp.CurrentBackgroundImage = this.BackgroundImage; } } } } }