GradeCount.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Forms;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17. namespace AIRS
  18. {
  19. /// <summary>
  20. /// GradeCount.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class GradeCount : Window
  23. {
  24. //委托事件
  25. public delegate void SelectGrade(List<String> grade_name,Boolean state);
  26. public static event SelectGrade SelectGradeEvent;
  27. public List<string> list_grade = new List<string>();
  28. public Dictionary<String, int> gradesum = new Dictionary<String, int>();
  29. public Dictionary<String, String> aiRES = new Dictionary<String, String>();
  30. public String gradename = "";
  31. public String gradetitle = "";
  32. public GradeCount()
  33. {
  34. InitializeComponent();
  35. }
  36. private void BtnLogin_Click(object sender, RoutedEventArgs e)
  37. {
  38. list_grade.Clear();
  39. SelectGradeEvent(list_grade,true);
  40. }
  41. private void BtnExport_Click(object sender, RoutedEventArgs e)
  42. {
  43. if (gradesum.Keys.Count > 0)
  44. {
  45. string filename = "";
  46. //申明保存对话框
  47. SaveFileDialog dlg = new SaveFileDialog();
  48. //默然文件后缀
  49. dlg.DefaultExt = "xls";
  50. //文件后缀列表
  51. dlg.Filter = "Excel Files|*.xls";
  52. //默然路径是系统当前路径
  53. dlg.InitialDirectory = Directory.GetCurrentDirectory();
  54. //保存对话框是否bai记忆上次du打开的目录
  55. dlg.RestoreDirectory = false;
  56. dlg.FileName = gradename + " - " + gradetitle + ".xls";
  57. dlg.Title = "导出Excel文件到";
  58. //打开保存对话框
  59. if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  60. {
  61. filename = dlg.FileName;
  62. if (filename.Trim() != "")
  63. {
  64. if (IsFileInUse(filename) && System.IO.File.Exists(filename))
  65. {
  66. System.Windows.Forms.MessageBox.Show("无法导出!选择导出的文件正在被使用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  67. return;
  68. }
  69. StreamWriter sw = new StreamWriter(filename, false, Encoding.GetEncoding("gb2312"));
  70. StringBuilder sb = new StringBuilder();
  71. //zm:
  72. sb.Append("图片" + "\t");
  73. sb.Append("级别" + "\t");
  74. sb.Append(Environment.NewLine);
  75. foreach (var res in aiRES)
  76. {
  77. //文件名+级别+其它
  78. sb.Append(res.Key+"\t");
  79. sb.Append(res.Value + "\t");
  80. sb.Append(Environment.NewLine);
  81. }
  82. sb.Append(gname.Content.ToString() + "\t");
  83. sb.Append(gcount.Content.ToString() + "\t");
  84. sb.Append(Environment.NewLine);
  85. int sum = 0;
  86. foreach(string key in gradesum.Keys)
  87. {
  88. System.Windows.Forms.Application.DoEvents();
  89. sb.Append(key + "\t");
  90. sb.Append(gradesum[key].ToString() + "\t");
  91. sb.Append(Environment.NewLine);
  92. sum += gradesum[key];
  93. }
  94. sb.Append("合计" + "\t");
  95. sb.Append(sum.ToString() + "\t");
  96. sw.Write(sb.ToString());
  97. sw.Flush();
  98. sw.Close();
  99. }
  100. if (System.Windows.Forms.MessageBox.Show("导出成功!\r\n是否打开Excel文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
  101. {
  102. System.Diagnostics.Process.Start(filename);
  103. }
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// 检测文件是否被其他程序占用
  109. /// </summary>
  110. /// <param name="fileName"></param>
  111. /// <returns></returns>
  112. public static bool IsFileInUse(string fileName)
  113. {
  114. bool inUse = true;
  115. FileStream fs = null;
  116. try
  117. {
  118. fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
  119. inUse = false;
  120. }
  121. catch { }
  122. finally
  123. {
  124. if (fs != null)
  125. fs.Close();
  126. }
  127. return inUse;//true表示正在使用,false没有使用
  128. }
  129. private void Window_Loaded(object sender, RoutedEventArgs e)
  130. {
  131. double _top = this.gname.Height + 14;
  132. gradesum = gradesum.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  133. foreach(string key in gradesum.Keys)
  134. {
  135. System.Windows.Controls.Label g_select = new System.Windows.Controls.Label();
  136. g_select.Width = 70;
  137. g_select.Height = 37;
  138. g_select.Content = "";
  139. g_select.Tag = key;
  140. g_select.BorderThickness = new System.Windows.Thickness(1);
  141. g_select.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
  142. g_select.SetValue(Canvas.TopProperty, _top);
  143. g_select.SetValue(Canvas.LeftProperty, 15.0);
  144. g_select.SetValue(Canvas.BottomProperty, double.NaN);
  145. g_select.SetValue(Canvas.RightProperty, double.NaN);
  146. g_select.FontSize = 20;
  147. g_select.FontWeight = FontWeights.Bold;
  148. g_select.BorderBrush = new SolidColorBrush(Colors.Black);
  149. canshow.Children.Add(g_select);
  150. System.Windows.Controls.CheckBox checkBox = new System.Windows.Controls.CheckBox();
  151. checkBox.Tag = key;
  152. checkBox.IsChecked = false;
  153. checkBox.Checked += new RoutedEventHandler(check_checked);
  154. checkBox.Unchecked += new RoutedEventHandler(check_unchecked);
  155. checkBox.SetValue(System.Windows.Controls.CheckBox.StyleProperty, System.Windows.Application.Current.Resources["CheckBoxStyle"]);
  156. checkBox.SetValue(Canvas.TopProperty, _top - 13.5);
  157. checkBox.SetValue(Canvas.LeftProperty, 37.5);
  158. checkBox.SetValue(Canvas.BottomProperty, double.NaN);
  159. checkBox.SetValue(Canvas.RightProperty, double.NaN);
  160. canshow.Children.Add(checkBox);
  161. System.Windows.Controls.Label g_name = new System.Windows.Controls.Label();
  162. g_name.Width = 200;
  163. g_name.Height = 37;
  164. g_name.Content = key;
  165. g_name.Tag = "";
  166. //g_name.PreviewMouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(selectgrade);
  167. g_name.BorderThickness = new System.Windows.Thickness(1);
  168. g_name.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
  169. g_name.SetValue(Canvas.TopProperty, _top);
  170. g_name.SetValue(Canvas.LeftProperty, 85.0);
  171. g_name.SetValue(Canvas.BottomProperty, double.NaN);
  172. g_name.SetValue(Canvas.RightProperty, double.NaN);
  173. g_name.FontSize = 20;
  174. g_name.FontWeight = FontWeights.Bold;
  175. g_name.BorderBrush = new SolidColorBrush(Colors.Black);
  176. canshow.Children.Add(g_name);
  177. System.Windows.Controls.Label g_count = new System.Windows.Controls.Label();
  178. g_count.Width = 150;
  179. g_count.Height = 37;
  180. g_count.Content = gradesum[key].ToString();
  181. g_count.BorderThickness = new System.Windows.Thickness(1);
  182. g_count.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
  183. g_count.SetValue(Canvas.TopProperty, _top);
  184. g_count.SetValue(Canvas.LeftProperty, 284.5);
  185. g_count.SetValue(Canvas.BottomProperty, double.NaN);
  186. g_count.SetValue(Canvas.RightProperty, double.NaN);
  187. g_count.FontSize = 20;
  188. g_count.FontWeight = FontWeights.Bold;
  189. g_count.BorderBrush = new SolidColorBrush(Colors.Black);
  190. canshow.Children.Add(g_count);
  191. _top += 36;
  192. list_grade.Add(key);
  193. }
  194. this.canshow.Height = _top + 14;
  195. lbltitle.Content = gradename;
  196. ggrade.Content = gradetitle + ":";
  197. }
  198. private void check_checked(object sender,RoutedEventArgs e)
  199. {
  200. System.Windows.Controls.CheckBox cb = (System.Windows.Controls.CheckBox)sender;
  201. list_grade.Remove(cb.Tag.ToString());
  202. SelectGradeEvent(list_grade, false);
  203. }
  204. private void check_unchecked(object sender, RoutedEventArgs e)
  205. {
  206. System.Windows.Controls.CheckBox cb = (System.Windows.Controls.CheckBox)sender;
  207. list_grade.Add(cb.Tag.ToString());
  208. SelectGradeEvent(list_grade, false);
  209. }
  210. private void selectgrade(object sender, MouseButtonEventArgs e)
  211. {
  212. System.Windows.Controls.Label lbl = (System.Windows.Controls.Label)sender;
  213. if(lbl.Tag.ToString()=="")
  214. {
  215. lbl.Tag = "s";
  216. lbl.Foreground = new SolidColorBrush(Colors.Red);
  217. list_grade.Add(lbl.Content.ToString());
  218. }
  219. else
  220. {
  221. lbl.Tag = "";
  222. lbl.Foreground = new SolidColorBrush(Colors.Black);
  223. list_grade.Remove(lbl.Content.ToString());
  224. }
  225. SelectGradeEvent(list_grade,false);
  226. }
  227. private void Window_MouseDown(object sender, MouseButtonEventArgs e)
  228. {
  229. if (e.LeftButton == MouseButtonState.Pressed) { DragMove(); }
  230. }
  231. private void imgmin_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  232. {
  233. imgmin.Visibility = Visibility.Hidden;
  234. imgmin1.Visibility = Visibility.Visible;
  235. }
  236. private void imgmin1_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
  237. {
  238. imgmin.Visibility = Visibility.Visible;
  239. imgmin1.Visibility = Visibility.Hidden;
  240. }
  241. private void imgmin1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  242. {
  243. this.WindowState = WindowState.Minimized;
  244. }
  245. private void imgclosed_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
  246. {
  247. imgclosed.Visibility = Visibility.Hidden;
  248. imgclosed1.Visibility = Visibility.Visible;
  249. }
  250. private void imgclosed1_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
  251. {
  252. imgclosed.Visibility = Visibility.Visible;
  253. imgclosed1.Visibility = Visibility.Hidden;
  254. }
  255. private void imgclosed1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  256. {
  257. list_grade.Clear();
  258. SelectGradeEvent(list_grade, true);
  259. }
  260. }
  261. }