_Giải thích chi tiết:
_Các hàm window và các biến cần thiết đều đc declare trong class Win32:
+ Một số hàm chính tạo nên aero cho form 1 :
Mã:
public struct BLENDFUNCTION { public byte BlendOp; public byte BlendFlags; public byte SourceConstantAlpha; public byte AlphaFormat; } public const int ULW_COLORKEY = 1; public const int ULW_ALPHA = 2; public const int ULW_OPAQUE = 4; public const byte AC_SRC_OVER = 0; public const byte AC_SRC_ALPHA = 1; [DllImport("user32.dll")] public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags); [DllImport("user32.dll")] public static extern IntPtr GetDC(IntPtr hWnd); [DllImport("user32.dll")] public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC); [DllImport("gdi32.dll")] public static extern IntPtr CreateCompatibleDC(IntPtr hDC); [DllImport("gdi32.dll")] public static extern bool DeleteDC(IntPtr hdc); [DllImport("gdi32.dll")] public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); [DllImport("gdi32.dll")] public static extern bool DeleteObject(IntPtr hObject);
_Các hằng số quan trọng:
Mã:
protected const int cst_frmExtraHeight = 50; //Phần thêm vào bên trên của form để draw icon và title protected const int cst_iconSize = 50; public const int cst_imgWidth = 284; //Width và Height của image glassform.png public const int cst_imgHeight = 249; public const int cst_frmWidth = 248; //Width và Height của form thực trong hình không kể shadow public const int cst_frmHeight = 213; public const int cst_lFrm = 12; //left, right, top, bottom của form thực trong hình public const int cst_rFrm = 24; public const int cst_tFrm = 12; public const int cst_bFrm = 24; public const int cst_xBtnX = -44-5; //Position của close button public const int cst_xBtnY = -5;
_Các biến quan trọng:
Mã:
public int frmWidth; //The size of window with extra height public int frmHeight; //The size of window with extra height public int clientWidth; //The size of window without extra height public int clientHeight; //The size of window without extra height public int clientX; //Distance between this.Left and form.left public int clientY; ////Distance between this.Top and form.top protected Bitmap bitmap; //bitmap chứa hình ảnh của form 1 protected Graphics g; public delegate void dPaint(Graphics g); //Delegate & Event của hàm cần vẽ thêm trên form 1 public event dPaint ePaint; // protected int posButtonX = 0; //Position của image closebutton (có 3 hình closebutton trong file image) protected Rectangle recButtonX; protected bool moveOutButtonX = true; //Chuột có ở trong closebutton kô để thay đổi hình closebutton protected Form f; //Form 2 protected int arrRecNext = 0; //Số lượng textglow protected Rectangle[] arrRec = new Rectangle[100]; //rec của các textglow (add vào bằng addTextGlow)
_Hàm calculate(int formHeight): hàm này là rối nhất trong project, mình viết lâu rùi nên giờ quên, để khi nào nhớ lại thì post chú thích lên
Mã:
public void calculate(int formHeight) { //Modify the form //this.Height = formHeight + cst_frmExtraHeight; //Increase the form height to draw icon and title //Caculate frmWidth = this.Width - (int)(this.Width*cst_lFrm/cst_frmWidth) - (int)(this.Width*cst_rFrm/cst_frmWidth); frmHeight = this.Height - (int)(this.Height*cst_tFrm/cst_frmHeight) - (int)(this.Height*cst_bFrm/cst_frmHeight); clientWidth = frmWidth; clientHeight = frmHeight - cst_frmExtraHeight; clientX = (int)(this.Width*cst_lFrm/cst_frmWidth); clientY = (this.Height*cst_tFrm/cst_frmHeight) + cst_frmExtraHeight; }
_Hàm addTextGlow(int x,int y,int width,int height) : x, y, width, height của control
addTextGlow(Form frm,Control ctrl) : thường xài cái này, chừng nào kô được kết quả mong muốn thì xài cái trên
Mã:
public void addTextGlow(int x,int y,int width,int height){arrRec[arrRecNext] = new Rectangle(x,y,width,height); arrRecNext += 1;}public void addTextGlow(Form frm,Control ctrl){arrRec[arrRecNext] = new Rectangle(ctrl.Left - frm.Left,ctrl.Top - frm.Top,ctrl.Width,ctrl.Height); arrRecNext += 1;}
_Hàm setFormMain(Form frm): hàm này cũng rối nốt, nhưng đại khái là attach cái form 2 vào rùi tính tóan khoảng cách, width height của form 1 sao cho form 1 bao quát hết form 2
Mã:
public void setFormMain(Form frm){ this.Width = frm.Width + (int)(frm.Width*cst_lFrm/cst_frmWidth) + (int)(frm.Width*cst_rFrm/cst_frmWidth);frm.Height += cst_frmExtraHeight; //Increase the form height to draw icon and titlethis.Height = frm.Height + (int)(frm.Height*cst_tFrm/cst_frmHeight) + (int)(frm.Height*cst_bFrm/cst_frmHeight);frm.Height -= cst_frmExtraHeight; //Calculatecalculate(this.Height); this.TopMost = true; f = frm;f.TopMost = true; this.Show();f.Show(); this.frmGlass_onMouseDown(this,new MouseEventArgs(MouseButtons.Left,1,clientX,clientY,0)); beginPaint(); drawFrm(); if (ePaint != null) ePaint(g); applyTrans();endPaint(); f.Left = this.Left + (int)(this.Width*cst_lFrm/cst_imgWidth);f.Top = this.Top + (int)(this.Height*cst_tFrm/cst_imgHeight) + cst_frmExtraHeight;}
_Hàm beginPaint và endPaint: như đã giới thiệu ở trên
Mã:
public virtual void beginPaint(){ bitmap = new Bitmap(frmWidth,frmHeight); g = Graphics.FromImage(bitmap); g.SmoothingMode = SmoothingMode.AntiAlias;}public virtual void endPaint(){ g.Dispose(); bitmap.Dispose();}
_Hàm drawForm: hàm này khá dễ hiểu, đại khái là vẽ cái form, closebutton, title, mấy cái textglow lên
Mã:
public virtual void drawFrm(){ if (bitmap == null || g == null) return; g.DrawImage(new Bitmap("GlassFormImages\\GlassForm.png"),this.ClientRectangle); //g.DrawLine(new Pen(Color.Black),clientX,clientY,clientX + clientWidth,clientY + clientHeight); //g.DrawLine(new Pen(Color.Red),this.Left,this.Left,this.Left + frmWidth,this.Left + frmHeight); g.DrawImage(new Bitmap("GlassFormImages\\TextGlow.png"), new Rectangle(25,20,clientWidth - (int)(clientWidth*30/100),25)); string title = this.Text; if (f != null) title = f.Text; g.DrawString(title, new Font(FontFamily.GenericSansSerif,12,FontStyle.Bold,GraphicsUnit.Pixel), new SolidBrush(Color.Black),new Point(30+20,20+5)); g.DrawImage(new Bitmap("GlassFormImages\\Icon.png"),0,0,cst_iconSize,cst_iconSize); recButtonX = new Rectangle((clientX + clientWidth) + cst_xBtnX,(int)( frmHeight - clientHeight + cst_xBtnY),44,18); if (posButtonX == 0) g.DrawImage(new Bitmap("GlassFormImages\\CloseButton.png"), recButtonX, posButtonX,0,44,18,GraphicsUnit.Pixel); //Draw TextGlow if (arrRecNext == 0) return; for (int i = 0; i != arrRecNext; i++) { g.DrawImage(new Bitmap("GlassFormImages\\TextGlow.png"), new Rectangle(clientX + arrRec[i].X -25,clientY + arrRec[i].Y -10, arrRec[i].Width +40,arrRec[i].Height +20)); }}
_Hàm applyTrans: hàm quyết định aero, hơi khó hiểu, xem thêm phần tham khảo để hiểu rõ, và phải call hàm này sau khi drawForm(). Ex:
Mã:
beginPaint(); drawFrm(); if (ePaint != null) //Call các hàm vẽ lên form 1 (cái này do form 2 làm) ePaint(g); applyTrans(); endPaint();
Mã:
public virtual void applyTrans(){ if (bitmap == null) return; IntPtr dcScreen = Win32.GetDC(IntPtr.Zero); IntPtr dcMem = Win32.CreateCompatibleDC(dcScreen); IntPtr hBmp = IntPtr.Zero; IntPtr oldBmp = IntPtr.Zero; try { hBmp = bitmap.GetHbitmap(Color.FromArgb(0)); oldBmp = Win32.SelectObject(dcMem,hBmp); Win32.Size size = new Win32.Size(bitmap.Width,bitmap.Height); Win32.Point pSource = new Win32.Point(0,0); Win32.Point pTop = new Win32.Point(this.Left,this.Top); Win32.BLENDFUNCTION blendF = new Win32.BLENDFUNCTION(); blendF.BlendOp = Win32.AC_SRC_OVER; blendF.BlendFlags = 0; blendF.SourceConstantAlpha = 255; blendF.AlphaFormat = Win32.AC_SRC_ALPHA; Win32.UpdateLayeredWindow(Handle,dcScreen,ref pTop,ref size,dcMem,ref pSource,0,ref blendF,Win32.ULW_ALPHA); } finally { Win32.ReleaseDC(IntPtr.Zero,dcScreen); if (!(hBmp.Equals(IntPtr.Zero))) { Win32.SelectObject(dcMem,oldBmp); Win32.DeleteObject(hBmp); } Win32.DeleteDC(dcMem); }}
_Example về ePaint (nơi vẽ lên form 1)
Ví dụ trong form 2 muốn vẽ thêm một đường thẳng vào form 1 thì:
Mã:
glassF.ePaint += new GlassApp.dPaint(exPaint); void exPaint(Graphics g){ g.DrawLine(...);}
Những cái còn lại khá dễ hiểu, nếu có thắc mắc thì các bác cứ nêu lên
+++Tham khảo:
+ Per Pixel Alpha Blend in C# : http://www.codeproject.com/KB/GDI-pl...pha_sharp.aspx
Ngọc Bích xanh bản chất thuộc dòng đá đá hoa (jade). Và cẩm thạch là tên gọi chung của ngọc bích. Vì thực chất chúng thuộc dòng đá đa khoáng được hình thành từ chất Silicat dưới dạng dioxy. Ngọc bích...
Chia sẻ Vòng tay ngọc bích xanh là...