c# winform 应用编程代码总结 1
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 联系作者本人。
作者:BuildNewApp
出处:http://syxchina.cnblogs.com、 BuildNewApp.com
本文版权归作者、博客园和百度空间共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则作者会诅咒你的。
如果您阅读了我的文章并觉得有价值请点击此处,谢谢您的肯定1。