WinForm各种API---时时更新

 

好文要顶 关注我 收藏该文
徐淳
关注 - 1
粉丝 - 3
 
 
 
0
0
 
 

本文原文地址:http://www.cnblogs.com/hqxc/p/6160685.html 

 

 

 

 

 

 

徐淳
 1 [DllImport("user32.dll")]
 2 public static extern bool ReleaseCapture();
 3 [DllImport("user32.dll")]
 4 public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
 5 public const int WM_SYSCOMMAND = 0x0112;
 6 public const int SC_MOVE = 0xF010;
 7 public const int HTCAPTION = 0x0002;
 8 [DllImport("user32")]
 9 private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);
10 private const int WM_SETREDRAW = 0xB;
11 
12 
13 
14 private void Form1_MouseDown(object sender, MouseEventArgs e)
15 {
16     if (this.WindowState == FormWindowState.Normal)
17     {
18         ReleaseCapture();
19         SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
20     }
21 }
窗体移动API
徐淳
 1         const int CS_DropSHADOW = 0x20000;
 2         const int GCL_STYLE = (-26);
 3         [DllImport("user32.dll", CharSet = CharSet.Auto)]
 4         public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
 5         [DllImport("user32.dll", CharSet = CharSet.Auto)]
 6         public static extern int GetClassLong(IntPtr hwnd, int nIndex);
 7 
 8 
 9         public Form1()
10         {
11             InitializeComponent();
12             SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW);
13         }
窗体阴影API
徐淳
 1         const int WM_NCHITTEST = 0x0084;
 2         const int HT_LEFT = 10;
 3         const int HT_RIGHT = 11;
 4         const int HT_TOP = 12;
 5         const int HT_TOPLEFT = 13;
 6         const int HT_TOPRIGHT = 14;
 7         const int HT_BOTTOM = 15;
 8         const int HT_BOTTOMLEFT = 16;
 9         const int HT_BOTTOMRIGHT = 17;
10         const int HT_CAPTION = 2;
11         #region 调大小
12         protected override void WndProc(ref Message Msg)
13         {
14             if (Msg.Msg == WM_NCHITTEST)
15             {
16                 //获取鼠标位置
17                 int nPosX = (Msg.LParam.ToInt32() & 65535);
18                 int nPosY = (Msg.LParam.ToInt32() >> 16);
19                 //右下角
20                 if (nPosX >= this.Right - 6 && nPosY >= this.Bottom - 6)
21                 {
22                     Msg.Result = new IntPtr(HT_BOTTOMRIGHT);
23                     return;
24                 }
25                 //左上角
26                 else if (nPosX <= this.Left + 6 && nPosY <= this.Top + 6)
27                 {
28                     Msg.Result = new IntPtr(HT_TOPLEFT);
29                     return;
30                 }
31                 //左下角
32                 else if (nPosX <= this.Left + 6 && nPosY >= this.Bottom - 6)
33                 {
34                     Msg.Result = new IntPtr(HT_BOTTOMLEFT);
35                     return;
36                 }
37                 //右上角
38                 else if (nPosX >= this.Right - 6 && nPosY <= this.Top + 6)
39                 {
40                     Msg.Result = new IntPtr(HT_TOPRIGHT);
41                     return;
42                 }
43                 else if (nPosX >= this.Right - 4)
44                 {
45                     Msg.Result = new IntPtr(HT_RIGHT);
46                     return;
47                 }
48                 else if (nPosY >= this.Bottom - 4)
49                 {
50                     Msg.Result = new IntPtr(HT_BOTTOM);
51                     return;
52                 }
53                 else if (nPosX <= this.Left + 4)
54                 {
55                     Msg.Result = new IntPtr(HT_LEFT);
56                     return;
57                 }
58                 else if (nPosY <= this.Top + 4)
59                 {
60                     Msg.Result = new IntPtr(HT_TOP);
61                     return;
62                 }
63                 else
64                 {
65                     Msg.Result = new IntPtr(HT_CAPTION);
66                     return;
67                 }
68             }
69             base.WndProc(ref Msg);
70         }
71         #endregion
无边框拖动大小API
系统热键API
徐淳
 1         System.Timers.Timer time = new System.Timers.Timer();
 2         [System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数
 3         public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, Keys vk);
 4         [System.Runtime.InteropServices.DllImport("user32.dll")] //申明API函数         
 5         public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
 6         [System.Runtime.InteropServices.DllImport("user32")]
 7         private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
 8         const int MOUSEEVENTF_LEFTDOWN = 0x0002;
 9         const int MOUSEEVENTF_LEFTUP = 0x0004;//模拟鼠标左键抬起 
10         const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
11         const int MOUSEEVENTF_RIGHTUP = 0x0010;
申明API、定义常量
徐淳
 1         protected override void WndProc(ref Message Msg)
 2         {
 3             const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键           
 4             switch (Msg.Msg)
 5             {
 6                 case WM_HOTKEY:
 7                     ProcessHotkey(Msg);//按下热键时调用ProcessHotkey()函数        
 8                     break;
 9             }
10             base.WndProc(ref Msg);
11         }
接收热键函数
徐淳
 1         int ijk = 0;//标记变量无意义
 2         private void ProcessHotkey(Message m) //按下设定的键时调用该函数        
 3         {
 4             IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型            
 5             //MessageBox.Show(id.ToString()); 
 6             string sid = id.ToString();
 7             switch (sid)
 8             {
 9                 case "100"://热键ID
10                     if (ijk == 0)
11                     {
12                         this.Size = new Size(1, 1);
13                         ijk = 1;
14                     }
15                     else
16                     {
17                         this.Size = new Size(819, 492);
18                         ijk = 0;
19                     }
20                     break;
21             }
22         }
热键调用的函数

  RegisterHotKey(this.Handle, 100, 2, Keys.D1);-----注册热键

  RegisterHotKey(本窗口句柄, 热键ID, 第一个键, 第二个键);

     第一个键:0---null

1---Alt

2---Ctrl

3---Shift

4---Windows

第二个键:Keys.键  Keys可以点出来

PS:热键设为同一个有奇效【手动斜眼】


 

  UnregisterHotKey(this.Handle, 100);-----移除热键

  UnregisterHotKey(本窗口句柄, 热键ID);
  PS:一定不要忘了移除啊!!!

徐淳
  1         #region 加密
  2 
  3 
  4 
  5 
  6 
  7 
  8         //加密
  9 
 10 
 11         public static string Encode(string data, string Key_64, string Iv_64)
 12         {
 13 
 14 
 15             string KEY_64 = Key_64;// "VavicApp";
 16 
 17 
 18             string IV_64 = Iv_64;// "VavicApp";
 19 
 20 
 21             try
 22             {
 23 
 24 
 25                 byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
 26 
 27 
 28                 byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
 29 
 30 
 31 
 32 
 33                 DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
 34 
 35 
 36                 int i = cryptoProvider.KeySize;
 37 
 38 
 39                 MemoryStream ms = new MemoryStream();
 40 
 41 
 42                 CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);
 43 
 44 
 45                 StreamWriter sw = new StreamWriter(cst);
 46 
 47 
 48 
 49 
 50                 sw.Write(data);
 51 
 52 
 53                 sw.Flush();
 54 
 55 
 56                 cst.FlushFinalBlock();
 57 
 58 
 59                 sw.Flush();
 60 
 61 
 62                 return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
 63 
 64 
 65             }
 66 
 67 
 68             catch (Exception x)
 69             {
 70 
 71 
 72                 return x.Message;
 73 
 74 
 75             }
 76 
 77 
 78 
 79 
 80         }
 81 
 82 
 83         #endregion
 84 
 85         #region 解密
 86 
 87 
 88 
 89 
 90 
 91         //解密
 92 
 93 
 94         public static string Decode(string data, string Key_64, string Iv_64)
 95         {
 96 
 97 
 98             string KEY_64 = Key_64;// "VavicApp";密钥
 99 
100 
101             string IV_64 = Iv_64;// "VavicApp"; 向量
102 
103 
104             try
105             {
106 
107 
108                 byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
109 
110 
111                 byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
112 
113 
114                 byte[] byEnc;
115 
116 
117                 byEnc = Convert.FromBase64String(data); //把需要解密的字符串转为8位无符号数组
118 
119 
120                 DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
121 
122 
123                 MemoryStream ms = new MemoryStream(byEnc);
124 
125 
126                 CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
127 
128 
129                 StreamReader sr = new StreamReader(cst);
130 
131 
132                 return sr.ReadToEnd();
133 
134 
135             }
136 
137 
138             catch (Exception x)
139             {
140 
141 
142                 return x.Message;
143 
144 
145             }
146 
147 
148         }
149 
150 
151         #endregion
C#简单的加密解密

 

转载于:https://www.cnblogs.com/hqxc/p/6160685.html

你可能感兴趣的:(WinForm各种API---时时更新)