diff --git a/ClientsLibrary/AccountSupport/FormAccountDetails.cs b/ClientsLibrary/AccountSupport/FormAccountDetails.cs index 484ab96..b13d568 100644 --- a/ClientsLibrary/AccountSupport/FormAccountDetails.cs +++ b/ClientsLibrary/AccountSupport/FormAccountDetails.cs @@ -70,43 +70,48 @@ namespace ClientsLibrary public UserPortrait UserPortrait { get; } - - private void LoadPortraitByFileName(string fileName) - { - if(IsHandleCreated) - { - if(InvokeRequired) - { - BeginInvoke(new Action(() => - { - LoadPortraitByFileName(fileName); - })); - return; - } - - try - { - pictureBox_UserPortrait.LoadAsync(fileName); - } - catch(Exception ex) - { - UserClient.LogNet?.WriteException("加载图片异常!", ex); - } - } - } - + private void pictureBox_UserPortrait_Click(object sender, EventArgs e) { - UserPortrait.ChangePortrait(); - ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadPoolLoadLargePortrait), null); + UserPortrait.ChangePortrait(LoadLargeProtrait,UnloadLargeProtrait); } private void ThreadPoolLoadLargePortrait(object obj) { - UserPortrait.LoadUserLargePortraint(LoadPortraitByFileName); + Thread.Sleep(200); + UserPortrait.LoadUserLargePortraint(LoadLargeProtrait, UnloadLargeProtrait); } + private void UnloadLargeProtrait() + { + if (IsHandleCreated && InvokeRequired) + { + BeginInvoke(new Action(() => + { + UnloadLargeProtrait(); + })); + return; + } + + pictureBox_UserPortrait.Image = null; + } + + private void LoadLargeProtrait(string fileName) + { + if (IsHandleCreated && InvokeRequired) + { + BeginInvoke(new Action(() => + { + LoadLargeProtrait(fileName); + })); + return; + } + + pictureBox_UserPortrait.ImageLocation = fileName; + } + + #endregion #region 个人文件管理块 @@ -240,6 +245,9 @@ namespace ClientsLibrary #endregion + #region 文件拖拽上传块 + + private void treeView1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) @@ -298,6 +306,7 @@ namespace ClientsLibrary } } - + + #endregion } } diff --git a/ClientsLibrary/AccountSupport/UserPortrait.cs b/ClientsLibrary/AccountSupport/UserPortrait.cs index 6e8ebc3..f75883a 100644 --- a/ClientsLibrary/AccountSupport/UserPortrait.cs +++ b/ClientsLibrary/AccountSupport/UserPortrait.cs @@ -35,17 +35,17 @@ namespace ClientsLibrary /// 实例化一个新的头像管理类对象 /// /// 头像存储的文件夹路径 - /// 加载图片的方法 - /// 加载前的操作 - public UserPortrait(string filePath, Action loadPic, Action disPic) + /// 卸载小头像的委托 + public UserPortrait(string filePath,Action loadPicSmall, Action unloadPicSmall) { if (!System.IO.Directory.Exists(filePath)) { System.IO.Directory.CreateDirectory(filePath); } FileSavePath = filePath; - LoadPic = loadPic; - DisPic = disPic; + + m_UnloadPicSmall = unloadPicSmall; + m_LoadPicSmall = loadPicSmall; } #endregion @@ -55,7 +55,9 @@ namespace ClientsLibrary /// /// 点击更改头像后的操作,打开头像选择对话框,获取到2种分辨率的头像,然后进行上传 /// - public void ChangePortrait() + /// 加载大头像的委托 + /// 卸载大头像的委托 + public void ChangePortrait(Action loadPic, Action unloadPic) { using (FormPortraitSelect fps = new FormPortraitSelect()) { @@ -64,14 +66,31 @@ namespace ClientsLibrary string path300 = FileSavePath + @"\" + PortraitSupport.LargePortrait; string path32 = FileSavePath + @"\" + PortraitSupport.SmallPortrait; - DisPic?.Invoke(); + // 先卸载图片 + unloadPic?.Invoke(); + m_UnloadPicSmall?.Invoke(); Bitmap bitmap300 = fps.GetSpecifiedSizeImage(300); - bitmap300.Save(path300); Bitmap bitmap32 = fps.GetSpecifiedSizeImage(32); - bitmap32.Save(path32); - bitmap300.Dispose(); - bitmap32.Dispose(); + + try + { + bitmap300.Save(path300); + bitmap32.Save(path32); + bitmap300.Dispose(); + bitmap32.Dispose(); + } + catch(Exception ex) + { + // 文件被占用的时候无法进行覆盖 + UserClient.LogNet?.WriteException("头像保存失败!", ex); + MessageBox.Show("头像保存失败,原因:" + ex.Message); + + // 加载回旧的文件 + loadPic?.Invoke(path300); + m_LoadPicSmall?.Invoke(path32); + return; + } // 传送服务器 @@ -123,8 +142,6 @@ namespace ClientsLibrary { UserClient.UserAccount.SmallPortraitMD5 = SmallPortraitMD5; UserClient.UserAccount.LargePortraitMD5 = LargePortraitMD5; - // 成功上传MD5码 - LoadUserSmallPortraint(); } else { @@ -136,6 +153,10 @@ namespace ClientsLibrary MessageBox.Show("上传头像失败!原因:" + result.Message); } + // 先显示信息 + loadPic?.Invoke(path300); + m_LoadPicSmall?.Invoke(path32); + }), null); } @@ -149,7 +170,7 @@ namespace ClientsLibrary #region Load Portraint /// - /// 加载小尺寸的头像操作,可以放到主线程 + /// 加载小尺寸的头像操作,需要放置到线程池中 /// public void LoadUserSmallPortraint() { @@ -165,18 +186,20 @@ namespace ClientsLibrary string fileName = FileSavePath + @"\" + PortraitSupport.SmallPortrait; if (System.IO.File.Exists(fileName)) { + // 先进行加载 + m_LoadPicSmall?.Invoke(fileName); // 本地存在文件 string currentMd5 = SoftBasic.CalculateFileMD5(fileName); // 对比验证 if (fileName == currentMd5) { - // 加载本地文件 - LoadPic?.Invoke(fileName); return; } } + m_UnloadPicSmall?.Invoke(); + // 本地不存在文件或校验失败,需要重新下载 OperateResult result = UserClient.Net_File_Client.DownloadFile(PortraitSupport.SmallPortrait, "Files", @@ -188,7 +211,7 @@ namespace ClientsLibrary if(result.IsSuccess) { // 下载成功 - LoadPic?.Invoke(fileName); + m_LoadPicSmall?.Invoke(fileName); } else { @@ -201,7 +224,7 @@ namespace ClientsLibrary /// 加载大尺寸的头像方法,需要放到线程池中操作 /// /// - public void LoadUserLargePortraint(Action largeLoadAction) + public void LoadUserLargePortraint(Action largeLoadAction, Action largeUnloadAction) { // 先获取服务器端的MD5码 string fileMd5 = UserClient.UserAccount.LargePortraitMD5; @@ -218,6 +241,8 @@ namespace ClientsLibrary // 本地存在文件,先进行加载,如果运算不一致,再重新加载 largeLoadAction?.Invoke(fileName); string currentMd5 = null; + + try { currentMd5 = SoftBasic.CalculateFileMD5(fileName); @@ -235,6 +260,8 @@ namespace ClientsLibrary } } + largeUnloadAction?.Invoke(); + // 本地不存在文件或校验失败,需要重新下载 OperateResult result = UserClient.Net_File_Client.DownloadFile(PortraitSupport.LargePortrait, "Files", @@ -253,13 +280,14 @@ namespace ClientsLibrary MessageBox.Show("头像从服务器下载失败,错误原因:" + result.Message); } + } #endregion - + #region Download Portraint - + /// /// 下载大尺寸的头像并打开它,该方法适合线程池 /// @@ -335,15 +363,16 @@ namespace ClientsLibrary /// 文件的路径 /// private string FileSavePath { get; set; } - /// - /// 加载图片的操作 - /// - private Action LoadPic = null; - /// - /// 加载图片前的操作 - /// - private Action DisPic = null; + /// + /// 卸载小头像的委托 + /// + private Action m_UnloadPicSmall { get; set; } + + /// + /// 加载小头像的委托 + /// + private Action m_LoadPicSmall { get; set; } #endregion } diff --git a/CommonLibrary/ProtocolSupport/CommonProtocol.cs b/CommonLibrary/ProtocolSupport/CommonProtocol.cs index 5867e6b..52c4abf 100644 --- a/CommonLibrary/ProtocolSupport/CommonProtocol.cs +++ b/CommonLibrary/ProtocolSupport/CommonProtocol.cs @@ -55,7 +55,7 @@ namespace CommonLibrary * **************************************************************************/ - SoftBasic.FrameworkVersion = new SystemVersion("1.4.2"); + SoftBasic.FrameworkVersion = new SystemVersion("1.4.3"); } @@ -65,7 +65,7 @@ namespace CommonLibrary * * 注意:您在准备二次开发时,应该重新生成一个自己的GUID码 * - **************************************************************************************************/ + ************************************************************************************************/ /// @@ -75,9 +75,13 @@ namespace CommonLibrary - //====================================================================================== - // 此处的所有的网络端口应该重新指定,防止其他人的项目连接到你的程序上 - // 假设你们的多个项目服务器假设在一台电脑的情况,就绝对要替换下面的端口号 + /************************************************************************************************ + * + * 注意:此处的所有的网络端口应该重新指定,防止其他人的项目连接到你的程序上 + * 假设你们的多个项目服务器假设在一台电脑的情况,就绝对要替换下面的端口号 + * + ************************************************************************************************/ + /// /// 主网络端口,此处随机定义了一个数据 @@ -92,10 +96,6 @@ namespace CommonLibrary /// public static int Port_Update_Net { get; } = 17538; /// - /// 用于软件远程更新的端口,此处随机定义了一个数据 - /// - public static int Port_Update_Remote { get; } = 26435; - /// /// 共享文件的端口号 /// public static int Port_Share_File { get; } = 34261; diff --git a/软件系统客户端Wpf/MainWindow.xaml.cs b/软件系统客户端Wpf/MainWindow.xaml.cs index e6218dc..07a8505 100644 --- a/软件系统客户端Wpf/MainWindow.xaml.cs +++ b/软件系统客户端Wpf/MainWindow.xaml.cs @@ -621,23 +621,32 @@ namespace 软件系统客户端Wpf { SoftUserPortrait = new UserPortrait(AppDomain.CurrentDomain.BaseDirectory + @"Portrait\" + UserClient.UserAccount.UserName, m => { - byte[] content = System.IO.File.ReadAllBytes(m); - BitmapImage bi = new BitmapImage(); - bi.BeginInit(); - bi.StreamSource = new System.IO.MemoryStream(content); - bi.EndInit(); - AccountPortrait.Source = bi; + + if (IsWindowShow) Dispatcher.Invoke(new Action(() => + { + byte[] content = File.ReadAllBytes(m); + BitmapImage bi = new BitmapImage(); + bi.BeginInit(); + bi.StreamSource = new MemoryStream(content); + bi.EndInit(); + + AccountPortrait.Source = bi; + })); }, ()=> { - AccountPortrait.Source = null; + if (IsWindowShow) Dispatcher.Invoke(new Action(() => { + AccountPortrait.Source = null; + })); }); } + + private void AccountChip_Click(object sender, RoutedEventArgs e) { - //点击了头像,请求下载高清版本头像 + // 点击了头像,请求查看高清版本头像 using (FormMatterRemind fmr = new FormMatterRemind("正在下载图片", SoftUserPortrait.ThreadPoolDownloadSizeLarge)) { fmr.ShowDialog(); diff --git a/软件系统客户端模版/FormMainWindow.cs b/软件系统客户端模版/FormMainWindow.cs index 74544f6..f98b0f1 100644 --- a/软件系统客户端模版/FormMainWindow.cs +++ b/软件系统客户端模版/FormMainWindow.cs @@ -18,9 +18,9 @@ using HslCommunication.LogNet; /*************************************************************************************** * - * 模版日期 2017-09-02 - * 创建人 胡少林 - * 版权所有 胡少林 + * 模版日期 2017-09-20 + * 创建人 Richard.Hu + * 版权所有 Richard.Hu * 授权说明 模版仅授权个人使用,如需商用,请联系hsl200909@163.com洽谈 * 说明一 JSON组件引用自james newton-king,遵循MIT授权协议 * 说明二 文件的图标来源于http://fileicons.chromefans.org/,感谢作者的无私分享 @@ -301,11 +301,7 @@ namespace 软件系统客户端模版 } } - - private void 更换头像ToolStripMenuItem_Click(object sender, EventArgs e) - { - SoftUserPortrait.ChangePortrait(); - } + private void 我的信息ToolStripMenuItem_Click(object sender, EventArgs e) { @@ -653,11 +649,39 @@ namespace 软件系统客户端模版 private void SoftUserPortraitInitialization() { - SoftUserPortrait = new UserPortrait(Application.StartupPath + @"\Portrait\" + UserClient.UserAccount.UserName, m => pictureBox1.LoadAsync(m),null); + SoftUserPortrait = new UserPortrait( + Application.StartupPath + @"\Portrait\" + UserClient.UserAccount.UserName, + LoadSmallProtraitAsync, UnloadSmallProtrait); } - - + + private void LoadSmallProtraitAsync(string fileName) + { + if (IsHandleCreated && InvokeRequired) + { + Invoke(new Action(() => + { + LoadSmallProtraitAsync(fileName); + })); + return; + } + + pictureBox1.LoadAsync(fileName); + } + + private void UnloadSmallProtrait() + { + if (IsHandleCreated && InvokeRequired) + { + Invoke(new Action(() => + { + UnloadSmallProtrait(); + })); + return; + } + + pictureBox1.Image = null; + } private void pictureBox1_Click(object sender, EventArgs e) {