PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/SSHelper/MainWindow.xaml.cs

https://bitbucket.org/system8bit/sharpshooterhelper
C# | 280 lines | 238 code | 39 blank | 3 comment | 24 complexity | 9db37ae014b6170ad8d0a69ab45c2375 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Xml.Linq;
  15. using System.IO;
  16. using System.Runtime.InteropServices;
  17. using System.Diagnostics;
  18. namespace SSHelper
  19. {
  20. /// <summary>
  21. /// MainWindow.xaml の相互作用ロジック
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. [StructLayout(LayoutKind.Sequential)]
  26. private struct RECT
  27. {
  28. public int Left;
  29. public int Top;
  30. public int Right;
  31. public int Bottom;
  32. }
  33. [DllImport("user32.dll")]
  34. [return: MarshalAs(UnmanagedType.Bool)]
  35. private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
  36. [DllImport("user32.dll")]
  37. [return: MarshalAs(UnmanagedType.Bool)]
  38. private static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
  39. [DllImport("user32.dll")]
  40. static extern int GetSystemMetrics(SystemMetric smIndex);
  41. public enum SystemMetric : int
  42. {
  43. SM_CXSCREEN = 0,
  44. SM_CYSCREEN = 1,
  45. SM_CYVSCROLL = 2,
  46. SM_CXVSCROLL = 3,
  47. SM_CYCAPTION = 4,
  48. SM_CXBORDER = 5,
  49. SM_CYBORDER = 6,
  50. SM_CXDLGFRAME = 7,
  51. SM_CYDLGFRAME = 8,
  52. SM_CYVTHUMB = 9,
  53. SM_CXHTHUMB = 10,
  54. SM_CXICON = 11,
  55. SM_CYICON = 12,
  56. SM_CXCURSOR = 13,
  57. SM_CYCURSOR = 14,
  58. SM_CYMENU = 15,
  59. SM_CXFULLSCREEN = 16,
  60. SM_CYFULLSCREEN = 17,
  61. SM_CYKANJIWINDOW = 18,
  62. SM_MOUSEWHEELPRESENT = 75,
  63. SM_CYHSCROLL = 20,
  64. SM_CXHSCROLL = 21,
  65. SM_DEBUG = 22,
  66. SM_SWAPBUTTON = 23,
  67. SM_RESERVED1 = 24,
  68. SM_RESERVED2 = 25,
  69. SM_RESERVED3 = 26,
  70. SM_RESERVED4 = 27,
  71. SM_CXMIN = 28,
  72. SM_CYMIN = 29,
  73. SM_CXSIZE = 30,
  74. SM_CYSIZE = 31,
  75. SM_CXFRAME = 32,
  76. SM_CYFRAME = 33,
  77. SM_CXMINTRACK = 34,
  78. SM_CYMINTRACK = 35,
  79. SM_CXDOUBLECLK = 36,
  80. SM_CYDOUBLECLK = 37,
  81. SM_CXICONSPACING = 38,
  82. SM_CYICONSPACING = 39,
  83. SM_MENUDROPALIGNMENT = 40,
  84. SM_PENWINDOWS = 41,
  85. SM_DBCSENABLED = 42,
  86. SM_CMOUSEBUTTONS = 43,
  87. SM_CXFIXEDFRAME = SM_CXDLGFRAME,
  88. SM_CYFIXEDFRAME = SM_CYDLGFRAME,
  89. SM_CXSIZEFRAME = SM_CXFRAME,
  90. SM_CYSIZEFRAME = SM_CYFRAME,
  91. SM_SECURE = 44,
  92. SM_CXEDGE = 45,
  93. SM_CYEDGE = 46,
  94. SM_CXMINSPACING = 47,
  95. SM_CYMINSPACING = 48,
  96. SM_CXSMICON = 49,
  97. SM_CYSMICON = 50,
  98. SM_CYSMCAPTION = 51,
  99. SM_CXSMSIZE = 52,
  100. SM_CYSMSIZE = 53,
  101. SM_CXMENUSIZE = 54,
  102. SM_CYMENUSIZE = 55,
  103. SM_ARRANGE = 56,
  104. SM_CXMINIMIZED = 57,
  105. SM_CYMINIMIZED = 58,
  106. SM_CXMAXTRACK = 59,
  107. SM_CYMAXTRACK = 60,
  108. SM_CXMAXIMIZED = 61,
  109. SM_CYMAXIMIZED = 62,
  110. SM_NETWORK = 63,
  111. SM_CLEANBOOT = 67,
  112. SM_CXDRAG = 68,
  113. SM_CYDRAG = 69,
  114. SM_SHOWSOUNDS = 70,
  115. SM_CXMENUCHECK = 71,
  116. SM_CYMENUCHECK = 72,
  117. SM_SLOWMACHINE = 73,
  118. SM_MIDEASTENABLED = 74,
  119. SM_MOUSEPRESENT = 19,
  120. SM_XVIRTUALSCREEN = 76,
  121. SM_YVIRTUALSCREEN = 77,
  122. SM_CXVIRTUALSCREEN = 78,
  123. SM_CYVIRTUALSCREEN = 79,
  124. SM_CMONITORS = 80,
  125. SM_SAMEDISPLAYFORMAT = 81,
  126. SM_IMMENABLED = 82,
  127. SM_CXFOCUSBORDER = 83,
  128. SM_CYFOCUSBORDER = 84,
  129. SM_TABLETPC = 86,
  130. SM_MEDIACENTER = 87,
  131. SM_CMETRICS_OTHER = 76,
  132. SM_CMETRICS_2000 = 83,
  133. SM_CMETRICS_NT = 88,
  134. SM_REMOTESESSION = 0x1000,
  135. SM_SHUTTINGDOWN = 0x2000,
  136. SM_REMOTECONTROL = 0x2001,
  137. }
  138. List<string> figures = new List<string>{
  139. "小×",
  140. "大×",
  141. "小+",
  142. "大+",
  143. "●",
  144. };
  145. private string targetName = string.Empty;
  146. private Window targetWindow = null;
  147. public MainWindow()
  148. {
  149. InitializeComponent();
  150. LoadConfig();
  151. figures.AddRange((Directory.GetFiles(Directory.GetCurrentDirectory()).Where(x => System.IO.Path.GetExtension(x) == ".png")).Select(x => System.IO.Path.GetFileName(x)));
  152. comboBoxFigure.ItemsSource = figures;
  153. comboBoxFigure.SelectedIndex = 0;
  154. comboBoxColor.SelectedIndex = 0;
  155. }
  156. private void LoadConfig()
  157. {
  158. var xml = XElement.Load("Config.xml");
  159. this.targetName = xml.Element("TargetName").Value;
  160. var rgbs = xml.Descendants("RGB").Select(x => x.Value)
  161. .Select(x => x.Split(',').Select(y => byte.Parse(y)).ToArray())
  162. .Select(x => Color.FromRgb(x[0], x[1], x[2]));
  163. comboBoxColor.ItemsSource = rgbs;
  164. }
  165. private void Window_Loaded(object sender, RoutedEventArgs e)
  166. {
  167. }
  168. private void Button_Click(object sender, RoutedEventArgs e)
  169. {
  170. var ps = Process.GetProcessesByName(targetName).FirstOrDefault();
  171. if (ps == null)
  172. {
  173. MessageBox.Show(targetName + "が見つかりません");
  174. }
  175. else
  176. {
  177. if (targetWindow != null)
  178. {
  179. targetWindow.Close();
  180. targetWindow = null;
  181. }
  182. var isSmall = false;
  183. var item = comboBoxFigure.SelectedItem as string;
  184. var color = (Color)comboBoxColor.SelectedItem;
  185. var transparency = (100.0 - sliderTransparency.Value) / 100.0;
  186. if (item == "小×" || item == "大×")
  187. {
  188. targetWindow = new CrossWindow(color, transparency);
  189. if (item.StartsWith("小")) isSmall = true;
  190. }
  191. else if (item == "小+" || item == "大+")
  192. {
  193. targetWindow = new PlusWindow(color, transparency);
  194. if (item.StartsWith("小")) isSmall = true;
  195. }
  196. else if (item == "●")
  197. {
  198. targetWindow = new DotWindow(color, transparency);
  199. }
  200. else if (File.Exists(System.IO.Path.Combine(Directory.GetCurrentDirectory(), comboBoxFigure.SelectedItem as string)))
  201. {
  202. targetWindow = new ImageWindow(System.IO.Path.Combine(Directory.GetCurrentDirectory(), comboBoxFigure.SelectedItem as string), transparency);
  203. isSmall = true;
  204. }
  205. targetWindow.Owner = this;
  206. RECT rect;
  207. GetClientRect(ps.MainWindowHandle, out rect);
  208. var xOffset = 0;
  209. var yOFfset = 0;
  210. if (isSmall)
  211. {
  212. var size = Math.Min(rect.Right, rect.Bottom) / 20;
  213. targetWindow.Width = size;
  214. targetWindow.Height = size;
  215. xOffset = (rect.Right - size) / 2;
  216. yOFfset = (rect.Bottom - size) / 2;;
  217. }
  218. else
  219. {
  220. targetWindow.Width = rect.Right;
  221. targetWindow.Height = rect.Bottom;
  222. }
  223. GetWindowRect(ps.MainWindowHandle, out rect);
  224. int cxSizeFrame = GetSystemMetrics(SystemMetric.SM_CXSIZEFRAME); // 境界線幅X方向
  225. int cySizeFrame = GetSystemMetrics(SystemMetric.SM_CYSIZEFRAME); // 境界線幅Y方向
  226. int cyCaption = GetSystemMetrics(SystemMetric.SM_CYCAPTION); // タイトルバーの高さ
  227. targetWindow.Left = rect.Left + cxSizeFrame + xOffset;
  228. targetWindow.Top = rect.Top + cyCaption + cySizeFrame + yOFfset;
  229. targetWindow.Show();
  230. }
  231. }
  232. private void Button_Click_1(object sender, RoutedEventArgs e)
  233. {
  234. if (targetWindow != null)
  235. {
  236. targetWindow.Close();
  237. targetWindow = null;
  238. }
  239. }
  240. }
  241. }