0
点赞
收藏
分享

微信扫一扫

winform窗体(拖动、透明、圆角、最小化、最大化、关闭、日志)

1.移动窗体

1 #region 移动窗体
2 /// <summary>
3 /// 重写WndProc方法,实现窗体移动和禁止双击最大化
4 /// </summary>
5 /// <param name="m">Windows 消息</param>
6 protected override void WndProc(ref Message m)
7 {
8 switch (m.Msg)
9 {
10 case 0x4e:
11 case 0xd:
12 case 0xe:
13 case 0x14:
14 base.WndProc(ref m);
15 break;
16 case 0x84://鼠标点任意位置后可以拖动窗体
17 this.DefWndProc(ref m);
18 if (m.Result.ToInt32() == 0x01)
19 {
20 m.Result = new IntPtr(0x02);
21 }
22 break;
23 case 0xA3://禁止双击最大化
24 break;
25 default:
26 base.WndProc(ref m);
27 break;
28 }
29 }
30 #endregion

View Code

2.设置无边窗的winform窗体透明?

1  public Form1()
2 {
3 InitializeComponent();
4 //初始化调用 参数为200
5 this.SetWindowTransparent(200);
6 }
7 #region 窗体透明
8 protected override CreateParams CreateParams
9 {
10 get
11 {
12 CreateParams cp = base.CreateParams;
13
14 cp.Parent = DesktopWinAPI.GetDesktopWindow();
15 cp.ExStyle = 0x00000080 | 0x00000008;//WS_EX_TOOLWINDOW | WS_EX_TOPMOST
16
17 return cp;
18 }
19 }
20 private void SetWindowTransparent(byte bAlpha)
21 {
22 try
23 {
24 DesktopWinAPI.SetWindowLong(this.Handle, (int)DesktopWinAPI.WindowStyle.GWL_EXSTYLE,
25 DesktopWinAPI.GetWindowLong(this.Handle, (int)DesktopWinAPI.WindowStyle.GWL_EXSTYLE) | (uint)DesktopWinAPI.ExWindowStyle.WS_EX_LAYERED);
26
27 DesktopWinAPI.SetLayeredWindowAttributes(this.Handle, 0, bAlpha, DesktopWinAPI.LWA_COLORKEY | DesktopWinAPI.LWA_ALPHA);
28 }
29 catch (Exception ex)
30 {
31
32 }
33 }
34 #endregion

View Code

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Runtime.InteropServices;
5
6 namespace WindowsFormsApplication1
7 {
8 public class DesktopWinAPI
9 {
10 [DllImport("user32.dll")]
11 public extern static IntPtr GetDesktopWindow();
12
13 [DllImport("user32.dll")]
14 public extern static bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
15 public static uint LWA_COLORKEY = 0x00000001;
16 public static uint LWA_ALPHA = 0x00000002;
17
18 [DllImport("user32.dll")]
19 public extern static uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
20 [DllImport("user32.dll")]
21 public extern static uint GetWindowLong(IntPtr hwnd, int nIndex);
22
23 public enum WindowStyle : int
24 {
25 GWL_EXSTYLE = -20
26 }
27
28 public enum ExWindowStyle : uint
29 {
30 WS_EX_LAYERED = 0x00080000
31 }
32
33 }
34

View Code

3. 设置无边窗的winform窗体圆角?

1 #region 窗体圆角
2 protected override void OnSizeChanged(EventArgs e)
3 {
4 base.OnSizeChanged(e);
5 RenderHelper.SetFormRoundRectRgn(this, 5);
6 }
7 #endregion

View Code

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows.Forms;
6 using System.Drawing;
7 using System.Drawing.Drawing2D;
8
9 namespace WindowsFormsApplication1
10 {
11 /// <summary>
12 /// 窗体自绘辅助类
13 /// </summary>
14 public class RenderHelper
15 {
16 /// <summary>
17 /// 设置窗体的圆角矩形
18 /// </summary>
19 /// <param name="form">需要设置的窗体</param>
20 /// <param name="rgnRadius">圆角矩形的半径</param>
21 public static void SetFormRoundRectRgn(Form form, int rgnRadius)
22 {
23 int hRgn = 0;
24 hRgn = Win32.CreateRoundRectRgn(0, 0, form.Width + 1, form.Height + 1, rgnRadius, rgnRadius);
25 Win32.SetWindowRgn(form.Handle, hRgn, true);
26 Win32.DeleteObject(hRgn);
27 }
28
29 /// <summary>
30 /// 移动窗体
31 /// </summary>
32 public static void MoveWindow(Form form)
33 {
34 Win32.ReleaseCapture();
35 Win32.SendMessage(form.Handle, Win32.WM_NCLBUTTONDOWN, Win32.HTCAPTION, 0);
36 }
37
38 /// <summary>
39 /// 取低位 X 坐标
40 /// </summary>
41 public static int LOWORD(int value)
42 {
43 return value & 0xFFFF;
44 }
45
46 /// <summary>
47 /// 取高位 Y 坐标
48 /// </summary>
49 public static int HIWORD(int value)
50 {
51 return value >> 16;
52 }
53
54 /// <summary>
55 /// 绘制窗体边框
56 /// </summary>
57 /// <param name="destForm">需要绘制边框的窗体</param>
58 /// <param name="g">绘制边框所用的绘图对象</param>
59 /// <param name="fringeImg">边框图片</param>
60 /// <param name="radius">边框的圆角矩形</param>
61 public static void DrawFormFringe(Form destForm, Graphics g, Image fringeImg, int radius)
62 {
63 DrawNineRect(
64 g,
65 fringeImg,
66 new Rectangle(-radius, -radius, destForm.ClientSize.Width + 2 * radius, destForm.ClientSize.Height + 2 * radius),
67 new Rectangle(0, 0, fringeImg.Width, fringeImg.Height));
68 }
69
70 /// <summary>
71 /// 画九宫图
72 /// </summary>
73 /// <param name="g">绘图对象</param>
74 /// <param name="img">所需绘制的图片</param>
75 /// <param name="DestRect">目标矩形</param>
76 /// <param name="SrcRect">来源矩形</param>
77 public static void DrawNineRect(Graphics g, Image img, Rectangle DestRect, Rectangle SrcRect)
78 {
79 int offset = 5;
80 Rectangle NineRect = new Rectangle(img.Width / 2 - offset, img.Height / 2 - offset, 2 * offset, 2 * offset);
81 int x = 0, y = 0, nWidth, nHeight;
82 int xSrc = 0, ySrc = 0, nSrcWidth, nSrcHeight;
83 int nDestWidth, nDestHeight;
84 nDestWidth = DestRect.Width;
85 nDestHeight = DestRect.Height;
86 // 左上-------------------------------------;
87 x = DestRect.Left;
88 y = DestRect.Top;
89 nWidth = NineRect.Left - SrcRect.Left;
90 nHeight = NineRect.Top - SrcRect.Top;
91 xSrc = SrcRect.Left;
92 ySrc = SrcRect.Top;
93 g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nWidth, nHeight, GraphicsUnit.Pixel);
94 // 上-------------------------------------;
95 x = DestRect.Left + NineRect.Left - SrcRect.Left;
96 nWidth = nDestWidth - nWidth - (SrcRect.Right - NineRect.Right);
97 xSrc = NineRect.Left;
98 nSrcWidth = NineRect.Right - NineRect.Left;
99 nSrcHeight = NineRect.Top - SrcRect.Top;
100 g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
101 // 右上-------------------------------------;
102 x = DestRect.Right - (SrcRect.Right - NineRect.Right);
103 nWidth = SrcRect.Right - NineRect.Right;
104 xSrc = NineRect.Right;
105 g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nWidth, nHeight, GraphicsUnit.Pixel);
106 // 左-------------------------------------;
107 x = DestRect.Left;
108 y = DestRect.Top + NineRect.Top - SrcRect.Top;
109 nWidth = NineRect.Left - SrcRect.Left;
110 nHeight = DestRect.Bottom - y - (SrcRect.Bottom - NineRect.Bottom);
111 xSrc = SrcRect.Left;
112 ySrc = NineRect.Top;
113 nSrcWidth = NineRect.Left - SrcRect.Left;
114 nSrcHeight = NineRect.Bottom - NineRect.Top;
115 g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
116 // 中-------------------------------------;
117 x = DestRect.Left + NineRect.Left - SrcRect.Left;
118 nWidth = nDestWidth - nWidth - (SrcRect.Right - NineRect.Right);
119 xSrc = NineRect.Left;
120 nSrcWidth = NineRect.Right - NineRect.Left;
121 g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
122
123 // 右-------------------------------------;
124 x = DestRect.Right - (SrcRect.Right - NineRect.Right);
125 nWidth = SrcRect.Right - NineRect.Right;
126 xSrc = NineRect.Right;
127 nSrcWidth = SrcRect.Right - NineRect.Right;
128 g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
129
130 // 左下-------------------------------------;
131 x = DestRect.Left;
132 y = DestRect.Bottom - (SrcRect.Bottom - NineRect.Bottom);
133 nWidth = NineRect.Left - SrcRect.Left;
134 nHeight = SrcRect.Bottom - NineRect.Bottom;
135 xSrc = SrcRect.Left;
136 ySrc = NineRect.Bottom;
137 g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nWidth, nHeight, GraphicsUnit.Pixel);
138 // 下-------------------------------------;
139 x = DestRect.Left + NineRect.Left - SrcRect.Left;
140 nWidth = nDestWidth - nWidth - (SrcRect.Right - NineRect.Right);
141 xSrc = NineRect.Left;
142 nSrcWidth = NineRect.Right - NineRect.Left;
143 nSrcHeight = SrcRect.Bottom - NineRect.Bottom;
144 g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nSrcWidth, nSrcHeight, GraphicsUnit.Pixel);
145 // 右下-------------------------------------;
146 x = DestRect.Right - (SrcRect.Right - NineRect.Right);
147 nWidth = SrcRect.Right - NineRect.Right;
148 xSrc = NineRect.Right;
149 g.DrawImage(img, new Rectangle(x, y, nWidth, nHeight), xSrc, ySrc, nWidth, nHeight, GraphicsUnit.Pixel);
150 }
151
152
153 /// <summary>
154 /// 绘制窗体主体部分白色透明层
155 /// </summary>
156 /// <param name="form">需要绘制的窗体</param>
157 /// <param name="g">绘图对象</param>
158 public static void DrawFromAlphaMainPart(Form form, Graphics g)
159 {
160 Color[] colors =
161 {
162 Color.FromArgb(5, Color.White),
163 Color.FromArgb(30, Color.White),
164 Color.FromArgb(150, Color.White),
165 Color.FromArgb(180, Color.White),
166 Color.FromArgb(30, Color.White),
167 Color.FromArgb(5, Color.White)
168 };
169
170 float[] pos =
171 {
172 0.0f,
173 0.05f,
174 0.15f,
175 0.85f,
176 0.99f,
177 1.0f
178 };
179
180 ColorBlend colorBlend = new ColorBlend(6);
181 colorBlend.Colors = colors;
182 colorBlend.Positions = pos;
183
184 RectangleF destRect = new RectangleF(0, 0, form.Width, form.Height);
185 using (LinearGradientBrush lBrush = new LinearGradientBrush(destRect, colors[0], colors[5], LinearGradientMode.Vertical))
186 {
187 lBrush.InterpolationColors = colorBlend;
188 g.FillRectangle(lBrush, destRect);
189 }
190 }
191 }
192

View Code

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Runtime.InteropServices;
6
7 namespace WindowsFormsApplication1
8 {
9 public class Win32
10 {
11 #region Window Const
12
13 public const int WM_ERASEBKGND = 0x0014;
14 public const int WM_LBUTTONDOWN = 0x0201;
15 public const int WM_LBUTTONUP = 0x0202;
16 public const int WM_LBUTTONDBLCLK = 0x0203;
17 public const int WM_WINDOWPOSCHANGING = 0x46;
18 public const int WM_PAINT = 0xF;
19 public const int WM_CREATE = 0x0001;
20 public const int WM_ACTIVATE = 0x0006;
21 public const int WM_NCCREATE = 0x0081;
22 public const int WM_NCCALCSIZE = 0x0083;
23 public const int WM_NCPAINT = 0x0085;
24 public const int WM_NCACTIVATE = 0x0086;
25 public const int WM_NCLBUTTONDOWN = 0x00A1;
26 public const int WM_NCLBUTTONUP = 0x00A2;
27 public const int WM_NCLBUTTONDBLCLK = 0x00A3;
28 public const int WM_NCMOUSEMOVE = 0x00A0;
29
30 public const int WM_NCHITTEST = 0x0084;
31
32 public const int HTLEFT = 10;
33 public const int HTRIGHT = 11;
34 public const int HTTOP = 12;
35 public const int HTTOPLEFT = 13;
36 public const int HTTOPRIGHT = 14;
37 public const int HTBOTTOM = 15;
38 public const int HTBOTTOMLEFT = 0x10;
39 public const int HTBOTTOMRIGHT = 17;
40 public const int HTCAPTION = 2;
41 public const int HTCLIENT = 1;
42
43 public const int WM_FALSE = 0;
44 public const int WM_TRUE = 1;
45
46
47
48 #endregion
49
50 #region Public extern methods
51
52 [DllImport("gdi32.dll")]
53 public static extern int CreateRoundRectRgn(int x1, int y1, int x2, int y2, int x3, int y3);
54
55 [DllImport("user32.dll")]
56 public static extern int SetWindowRgn(IntPtr hwnd, int hRgn, Boolean bRedraw);
57
58 [DllImport("gdi32.dll", EntryPoint = "DeleteObject", CharSet = CharSet.Ansi)]
59 public static extern int DeleteObject(int hObject);
60
61 [DllImport("user32.dll")]
62 public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
63
64 [DllImport("user32.dll")]
65 public static extern bool ReleaseCapture();
66
67 #endregion
68 }
69

View Code

4. 设置无边窗的winform窗体 最小化、最大化、关闭

1  private void button2_Click(object sender, EventArgs e)
2 {
3 this.WindowState = FormWindowState.Minimized;
4 }
5 private void pictureBox1_Click(object sender, EventArgs e)
6 {
7 if (this.WindowState == FormWindowState.Normal)
8 { this.WindowState = FormWindowState.Maximized; }
9 else if (this.WindowState == FormWindowState.Maximized)
10 { this.WindowState = FormWindowState.Normal; }
11 }
12 private void button2_Click(object sender, EventArgs e)
13 {
14 this.Close();
15

View Code

5.设置winform窗体无边窗

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

6.winform记录日志文件?

1 try
2 {
3 int f = 0;
4 int t = 1 / f;
5 }
6 catch (Exception ex)
7 {
8 Log.WriteFile(ex);
9

View Code

1 using System;
2 using System.IO;
3 using System.Windows.Forms;
4 using System.Diagnostics;
5
6 namespace WindowsFormsApplication1
7 {
8 public class Log
9 {
10 public static void WriteFile(Exception ex)
11 {
12 String sFileName;
13 String sFilePath = Path.Combine(Application.StartupPath, "Log");
14 if (Directory.Exists(sFilePath) == false)
15 Directory.CreateDirectory(sFilePath);
16 else
17 {
18 DirectoryInfo dInfo = new DirectoryInfo(sFilePath);
19 if (dInfo.GetFiles().Length > 100)
20 foreach (FileInfo fInfo in dInfo.GetFiles())
21 fInfo.Delete();
22 }
23 //用当前日期(年月日)作为文件名
24 sFileName = DateTime.Now.ToShortDateString().Replace("/", "-") + ".log"; //文件名不能包括:
25 sFilePath = Path.Combine(sFilePath, sFileName);
26
27 StreamWriter streamWriter;
28
29 if (File.Exists(sFilePath))
30 streamWriter = File.AppendText(sFilePath);
31 else
32 streamWriter = File.CreateText(sFilePath);
33
34 streamWriter.WriteLine();
35 streamWriter.WriteLine(DateTime.Now.ToString());
36 streamWriter.WriteLine(ex.ToString());
37 streamWriter.WriteLine(ex.Message);
38 streamWriter.WriteLine(ex.InnerException);
39 if (ex is DetailException)
40 {
41 streamWriter.Write(((DetailException)ex).additionalMsg);
42 streamWriter.WriteLine();
43 }
44 streamWriter.Close();
45 }
46 }
47

View Code

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace WindowsFormsApplication1
7 {
8 public class DetailException : Exception
9 {
10 public Exception exception;
11 public string additionalMsg;
12
13 public DetailException(Exception ex, string additionalMsg)
14 {
15 exception = ex;
16 this.additionalMsg = additionalMsg;
17 }
18 }
19
20 public class ExceptionHandler
21 {
22 public static StringBuilder strLog = new StringBuilder();
23
24 public static void handlingExcetion(Exception ex)
25 {
26 if (ex == null) return;
27
28 strLog.Append(DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + "||" + ex.Message);
29 strLog.Append("------------------" + ex.StackTrace + "\r\n\r\n");
30
31 Exception finalEx = ex;
32
33 while (ex.InnerException != null && !ex.InnerException.Equals(finalEx))
34 {
35 finalEx = ex.InnerException;
36 }
37 try
38 {
39 Log.WriteFile(finalEx);
40 }
41 catch (Exception e)
42 {
43 System.Diagnostics.Trace.Write(e.Message);
44 }
45 }
46 }
47
48
49

View Code

 

                  



举报

相关推荐

0 条评论