ソースを参照

双层窗体添加鼠标位置记录,Http工具新增带状态码的请求

yuzhengyang 8 年 前
コミット
a5485893a9

+ 5 - 19
Fork.Net/Y.Skin/YoForm/Irregular/IrregularForm.cs

@@ -14,6 +14,7 @@ namespace Y.Skin.YoForm.Irregular
     public partial class IrregularForm : Form
     {
         private IrregularFormSkin Skin;
+        public  Point MouseLocation { get { return Skin.MouseLocation; } }
         public IrregularForm()
         {
             InitializeComponent();
@@ -26,12 +27,12 @@ namespace Y.Skin.YoForm.Irregular
         {
             if (!DesignMode)
             {
-                Opacity = 0;
                 Skin = new IrregularFormSkin(this);//创建皮肤层
                 BackgroundImage = null;//去除控件层背景
-                Skin.Show();//显示皮肤层 
-                AnimateShow();
-                if (ContextMenuStrip != null) Skin.ContextMenuStrip = ContextMenuStrip;//设置右键菜单
+                Skin.Show();//显示皮肤层
+
+                //设置右键菜单
+                if (ContextMenuStrip != null) Skin.ContextMenuStrip = ContextMenuStrip;
             }
         }
 
@@ -81,21 +82,6 @@ namespace Y.Skin.YoForm.Irregular
             base.AutoScaleMode = AutoScaleMode.None;
         }
         #endregion
-        private void AnimateShow()
-        {
-            Task.Factory.StartNew(() =>
-            {
-                for (int i = 0; i <= 10; i++)
-                {
-                    BeginInvoke(new Action(() =>
-                    {
-                        Opacity = i / 10.0;
-                        Skin.SetBits();
-                    }));
-                    Thread.Sleep(25);
-                }
-            });
-        }
         /// <summary>
         /// 窗体显示状态
         /// </summary>

+ 7 - 6
Fork.Net/Y.Skin/YoForm/Irregular/IrregularFormSkin.cs

@@ -13,6 +13,8 @@ namespace Y.Skin.YoForm.Irregular
 {
     partial class IrregularFormSkin : Form
     {
+        private Point _MouseLocation;
+        internal Point MouseLocation { get { return _MouseLocation; } }
         private IrregularForm Main;
         public IrregularFormSkin(IrregularForm main)
         {
@@ -32,7 +34,10 @@ namespace Y.Skin.YoForm.Irregular
             SetBits();//绘制半透明不规则皮肤
             Location = new Point(Main.Location.X, Main.Location.Y);//统一控件层和皮肤层的位置
         }
+        private void IrregularFormSkin_Load(object sender, EventArgs e)
+        {
 
+        }
         #region 减少闪烁
         private void SetStyles()
         {
@@ -63,7 +68,7 @@ namespace Y.Skin.YoForm.Irregular
             {
                 //绘制绘图层背景
                 Bitmap bitmap = new Bitmap(BackgroundImage, base.Width, base.Height);
-                if (!Bitmap.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Bitmap.IsAlphaPixelFormat(bitmap.PixelFormat))
+                if (!Image.IsCanonicalPixelFormat(bitmap.PixelFormat) || !Image.IsAlphaPixelFormat(bitmap.PixelFormat))
                     throw new ApplicationException("图片必须是32位带Alhpa通道的图片。");
                 IntPtr oldBits = IntPtr.Zero;
                 IntPtr screenDC = FormStyleAPI.GetDC(IntPtr.Zero);
@@ -221,16 +226,12 @@ namespace Y.Skin.YoForm.Irregular
         protected override void OnMouseMove(MouseEventArgs e)
         {
             base.OnMouseMove(e);
+            _MouseLocation = e.Location;
             if (e.Button == MouseButtons.Left)
             {
                 FormStyleAPI.ReleaseCapture();
                 FormStyleAPI.SendMessage(Handle, FormStyleAPI.WM_NCLBUTTONDOWN, FormStyleAPI.HTCAPTION, 0);
             }
         }
-
-        private void IrregularFormSkin_Load(object sender, EventArgs e)
-        {
-
-        }
     }
 }

+ 6 - 3
Fork.Net/Y.Utils/DataUtils/JsonUtils/JsonTool.cs

@@ -1,7 +1,10 @@
-//############################################################
+//************************************************************************
 //      https://github.com/yuzhengyang
-//      author:yuzhengyang
-//############################################################
+//      author:     yuzhengyang
+//      date:       2017.3.29 - 2017.8.24
+//      desc:       Json转换工具类(需要Newtonsoft.Json支持)
+//      Copyright (c) yuzhengyang. All rights reserved.
+//************************************************************************
 using Newtonsoft.Json;
 using System;
 using Y.Utils.IOUtils.TxtUtils;

+ 123 - 39
Fork.Net/Y.Utils/NetUtils/HttpUtils/HttpTool.cs

@@ -4,6 +4,7 @@
 //############################################################
 using Newtonsoft.Json;
 using System;
+using System.Diagnostics;
 using System.IO;
 using System.Net;
 using System.Text;
@@ -32,27 +33,6 @@ namespace Y.Utils.NetUtils.HttpUtils
             catch (Exception e) { }
             return result;
         }
-        public static T Get<T>(string url, string encoding = "utf-8")
-        {
-            try
-            {
-                Encoding myEncoding = Encoding.GetEncoding(encoding);
-                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
-                req.Method = "GET";
-                using (WebResponse wr = req.GetResponse())
-                {
-                    //在这里对接收到的页面内容进行处理
-                    string response = new StreamReader(wr.GetResponseStream(), myEncoding).ReadToEnd();
-                    if (!StringTool.IsNullOrWhiteSpace(response))
-                    {
-                        T result = JsonConvert.DeserializeObject<T>(response);
-                        return result;
-                    }
-                }
-            }
-            catch (Exception e) { }
-            return default(T);
-        }
         public static string Post(string url, string param, string encoding = "utf-8")
         {
             string result = string.Empty;
@@ -78,6 +58,27 @@ namespace Y.Utils.NetUtils.HttpUtils
             { }
             return result;
         }
+        public static T Get<T>(string url, string encoding = "utf-8")
+        {
+            try
+            {
+                Encoding myEncoding = Encoding.GetEncoding(encoding);
+                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
+                req.Method = "GET";
+                using (WebResponse wr = req.GetResponse())
+                {
+                    //在这里对接收到的页面内容进行处理
+                    string response = new StreamReader(wr.GetResponseStream(), myEncoding).ReadToEnd();
+                    if (!StringTool.IsNullOrWhiteSpace(response))
+                    {
+                        T result = JsonConvert.DeserializeObject<T>(response);
+                        return result;
+                    }
+                }
+            }
+            catch (Exception e) { }
+            return default(T);
+        }
         public static T Post<T>(string url, string param, string encoding = "utf-8")
         {
             try
@@ -109,24 +110,106 @@ namespace Y.Utils.NetUtils.HttpUtils
             catch (Exception ex) { }
             return default(T);
         }
-        //public static string PostJson(string url, string param)
-        //{
-        //    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
-        //    request.Method = "POST";
-        //    request.ContentType = "application/json";
-        //    request.ContentLength = Encoding.UTF8.GetByteCount(param);
-        //    Stream myRequestStream = request.GetRequestStream();
-        //    StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
-        //    myStreamWriter.Write(param);
-        //    myStreamWriter.Close();
-        //    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
-        //    Stream myResponseStream = response.GetResponseStream();
-        //    StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
-        //    string retString = myStreamReader.ReadToEnd();
-        //    myStreamReader.Close();
-        //    myResponseStream.Close();
-        //    return retString;
-        //}
+        /// <summary>
+        /// Http Get(返回值:>=0正常,-100编码异常,-200创建web请求异常,-300网络异常,-400返回内容为空)
+        /// </summary>
+        /// <typeparam name="T"></typeparam>
+        /// <param name="url"></param>
+        /// <param name="what"></param>
+        /// <param name="encoding"></param>
+        /// <returns></returns>
+        public static int Get<T>(string url, out T what, string encoding = "utf-8")
+        {
+            DateTime beginTime = DateTime.Now;
+            what = default(T);
+            //设置编码
+            Encoding myEncoding;
+            try { myEncoding = Encoding.GetEncoding(encoding); } catch { return -100; }//编码异常
+
+            //创建web请求
+            HttpWebRequest req;
+            try
+            {
+                req = (HttpWebRequest)WebRequest.Create(url);
+                req.Method = "GET";
+            }
+            catch { return -200; }//创建web请求异常
+
+            //请求数据
+            string txt;
+            try
+            {
+                using (WebResponse wr = req.GetResponse())
+                {
+                    txt = new StreamReader(wr.GetResponseStream(), myEncoding).ReadToEnd();
+                }
+            }
+            catch { return -300; }//网络异常
+
+            //转换模型
+            if (StringTool.IsNullOrWhiteSpace(txt))
+            {
+                return -400;//返回内容为空
+            }
+            else
+            {
+                what = JsonConvert.DeserializeObject<T>(txt);
+                return (int)Math.Ceiling((DateTime.Now - beginTime).TotalSeconds);//操作成功
+            }
+        }
+        public static int Post<T>(string url, string param, out T what, string encoding = "utf-8")
+        {
+            DateTime beginTime = DateTime.Now;
+            what = default(T);
+            //设置编码
+            Encoding myEncoding;
+            try { myEncoding = Encoding.GetEncoding(encoding); } catch { return -100; }//编码异常
+
+            //创建web请求
+            HttpWebRequest webReq;
+            byte[] byteArray;
+            try
+            {
+                byteArray = myEncoding.GetBytes(param); //转化参数
+                webReq = (HttpWebRequest)WebRequest.Create(new Uri(url));
+                webReq.Method = "POST";
+                webReq.ContentType = "application/x-www-form-urlencoded";
+                webReq.ContentLength = byteArray.Length;
+            }
+            catch { return -200; }//创建web请求异常
+
+            //请求数据
+            string txt;
+            try
+            {
+                using (Stream newStream = webReq.GetRequestStream())
+                {
+                    newStream.Write(byteArray, 0, byteArray.Length);//写入参数
+                    newStream.Close();
+                    using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse())
+                    {
+                        using (StreamReader sr = new StreamReader(response.GetResponseStream(), myEncoding))
+                        {
+                            txt = sr.ReadToEnd();
+                        }
+                    }
+                }
+            }
+            catch { return -300; }//网络异常
+
+            //转换模型
+            if (StringTool.IsNullOrWhiteSpace(txt))
+            {
+                return -400;//返回内容为空
+            }
+            else
+            {
+                what = JsonConvert.DeserializeObject<T>(txt);
+                return (int)Math.Ceiling((DateTime.Now - beginTime).TotalSeconds);//操作成功
+            }
+        }
+
+        [Obsolete]
         public static string PostJson(string url, string param)
         {
             string rs = null;
@@ -227,6 +310,7 @@ namespace Y.Utils.NetUtils.HttpUtils
             }
             return rs;
         }
+        [Obsolete]
         public static T PostJson<T>(string url, string param, string encoding = "utf-8")
         {
             ServicePointManager.DefaultConnectionLimit = 300;