1、漸顯的表單
放一個定時器Interval 設定為100 , 在響應的函數中添加
private void Form1_Load(object sender, System.EventArgs e) { this.timer1.Enabled=true; this.Opacity=0; } private void timer1_Tick(object sender, System.EventArgs e) { if(this.Opacity<1) { this.Opacity=this.Opacity+0.05; } else { this.timer1.Enabled=false; } }
2、設定最上層視窗
把TopMost 、 TopLevel 都設定為true就OK了!
3、設定一個多邊形的視窗
引入要使用的函數
[DllImport("gdi32")]private static extern IntPtr CreatePolygonRgn(Point[] lpPoint,int nCount,int nPolyFillMode);[DllImport("user32")]private static extern IntPtr SetWindowRgn(IntPtr hWnd,IntPtr hRgn,bool bRedraw);
在視窗load的時候設定形狀!
Point[] pt={ new Point(this.Width /2,0), new Point(0,this.Height/2), new Point(this.Width/2,this.Height), new Point(this.Width,this.Height/2), new Point(this.Width,0) }; IntPtr m_rgn; m_rgn=CreatePolygonRgn(pt,5,WINDING); SetWindowRgn(this.Handle,m_rgn,true);
4、設定圓形視窗
[System.Runtime.InteropServices.DllImport("gdi32")] private static extern IntPtr BeginPath(IntPtr hdc); [System.Runtime.InteropServices.DllImport("gdi32")] private static extern int SetBkMode(IntPtr hdc,int nBkMode); const int TRANSPARENT = 1; [System.Runtime.InteropServices.DllImport("gdi32")] private static extern IntPtr EndPath(IntPtr hdc); [System.Runtime.InteropServices.DllImport("gdi32")] private static extern IntPtr PathToRegion(IntPtr hdc); [System.Runtime.InteropServices.DllImport("gdi32")] private static extern int Ellipse(IntPtr hdc, int X1,int Y1, int X2,int Y2); [System.Runtime.InteropServices.DllImport("user32")] private static extern IntPtr SetWindowRgn(IntPtr hwnd,IntPtr hRgn,bool bRedraw); [System.Runtime.InteropServices.DllImport("user32")] private static extern IntPtr GetDC(IntPtr hwnd); private void Form1_Load(object sender, System.EventArgs e) { IntPtr dc; IntPtr region; dc=GetDC(this.Handle); BeginPath(dc); //根據路徑建立不規則表單 SetBkMode(dc,TRANSPARENT); //設定為透明模式 Ellipse(dc,20,20,220,220); EndPath(dc); region=PathToRegion(dc); SetWindowRgn(this.Handle,region,true); } const int WM_NCHITTEST = 0x0084; const int HTCLIENT = 0x0001; const int HTCAPTION = 0x0002; protected override void WndProc(ref System.Windows.Forms.Message m) { switch(m.Msg) { case WM_NCHITTEST: base.WndProc(ref m); if (m.Result==(IntPtr)HTCLIENT) m.Result=(IntPtr)HTCAPTION; break; default: base.WndProc(ref m); break; } } }
5 、漸層的視窗背景
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g=e.Graphics; Color FColor=Color.Blue; Color TColor=Color.Yellow; Brush b =new LinearGradientBrush(this.ClientRectangle, FColor, TColor, LinearGradientMode.ForwardDiagonal); g.FillRectangle(b,this.ClientRectangle); } private void Form1_Resize(object sender, System.EventArgs e) { this.Invalidate(); }
6、無標題視窗的拖動
[DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd,int wMsg,int wParam,int lParam); public const int WM_SYSCOMMAND=0x0112; public const int SC_MOVE=0xF010; public const int HTCAPTION=0x0002; private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle,WM_SYSCOMMAND,SC_MOVE+HTCAPTION, 0); }
本系列文章是作者學習《Visual C#.NET 應用編程150例》(源碼)心得筆記,歡迎轉載,請註明原文地址,如有疑問,可以通過 278250658@qq.com 聯絡作者本人。
作者:RyanDing