Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for marker.cs

(view source code of marker.cs as plain text)

  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5.  
  6.  
  7. namespace Marker
  8. {
  9. 	public partial class FormMain : Form
  10. 	{
  11. 		static readonly string progver = "0.10 beta";
  12.  
  13.  
  14. 		static bool helprequested = false;
  15. 		static Color ringcolor = Color.Red;
  16. 		static int ringthickness = 10;
  17. 		static int rotation = 0;
  18. 		static int windowheight;
  19. 		static int windowwidth;
  20. 		static int initialheight;
  21. 		static int initialwidth;
  22.  
  23.  
  24. 		public FormMain( )
  25. 		{
  26. 			InitializeComponent( );
  27. 			this.FormBorderStyle = FormBorderStyle.None;
  28. 			this.DoubleBuffered = true;
  29. 			this.SetStyle( ControlStyles.ResizeRedraw, true );
  30. 		}
  31.  
  32.  
  33. 		private void DrawEllipse( )
  34. 		{
  35. 			Application.DoEvents( );
  36. 			Pen pen = new Pen( ringcolor, (float)ringthickness );
  37. 			Graphics graphics = this.CreateGraphics( );
  38. 			if ( rotation == 0 )
  39. 			{
  40. 				graphics.DrawEllipse( pen, new Rectangle( ringthickness, ringthickness, windowwidth - 2 * ringthickness, windowheight - 2 * ringthickness ) );
  41. 			}
  42. 			else
  43. 			{
  44. 				// Though the following code DOES produce a rotated ellipse,
  45. 				// I haven't managed to make it fit within the window yet.
  46.  
  47. 				// Set world transform of graphics object to translate.
  48. 				graphics.TranslateTransform( 100.0F, 0.0F );
  49. 				// Then to rotate, prepending rotation matrix.
  50. 				graphics.RotateTransform( (float)rotation );
  51. 				this.Size = new Size( Math.Max( initialwidth, initialheight ), Math.Max( initialwidth, initialheight ) );
  52. 				graphics.DrawEllipse( pen, new Rectangle( ringthickness, ringthickness, initialwidth - 2 * ringthickness, initialheight - 2 * ringthickness ) );
  53. 			}
  54. 			pen.Dispose( );
  55. 			graphics.Dispose( );
  56. 		}
  57.  
  58.  
  59. 		private void FormMain_FormClosing( object sender, FormClosingEventArgs e )
  60. 		{
  61. 			if ( helprequested )
  62. 			{
  63. 				ShowHelp( true );
  64. 			}
  65. 		}
  66.  
  67.  
  68. 		private void FormMain_KeyUp( object sender, KeyEventArgs e )
  69. 		{
  70. 			if ( e.KeyCode == Keys.Escape )
  71. 			{
  72. 				Application.Exit( );
  73. 			}
  74. 			if ( e.KeyCode == Keys.F1 )
  75. 			{
  76. 				ShowHelp( );
  77. 			}
  78. 		}
  79.  
  80.  
  81. 		private void FormMain_Load( object sender, EventArgs e )
  82. 		{
  83. 			windowwidth = this.ClientRectangle.Width;
  84. 			windowheight = this.ClientRectangle.Height;
  85. 			initialheight = windowheight;
  86. 			initialwidth = windowwidth;
  87. 			string commandline = Environment.CommandLine;
  88. 			if ( commandline.Contains( "/?" ) )
  89. 			{
  90. 				ShowHelp( true );
  91. 				Application.Exit( );
  92. 			}
  93. 			if ( commandline.ToUpper( ).Contains( "/C:" ) )
  94. 			{
  95. 				string test = commandline.Substring( commandline.IndexOf( "/C:", StringComparison.OrdinalIgnoreCase ) + 3 );
  96. 				test = test.Split( ' ' )[0];
  97. 				ringcolor = Color.FromName( test );
  98. 			}
  99. 			if ( commandline.ToUpper( ).Contains( "/H:" ) )
  100. 			{
  101. 				string test = commandline.Substring( commandline.IndexOf( "/H:", StringComparison.OrdinalIgnoreCase ) + 3 );
  102. 				test = test.Split( ' ' )[0];
  103. 				if ( !int.TryParse( test, out windowheight ) )
  104. 				{
  105. 					windowheight = this.ClientRectangle.Height;
  106. 				}
  107. 			}
  108. 			/*
  109. 			if ( commandline.ToUpper( ).Contains( "/R:" ) )
  110. 			{
  111. 				string test = commandline.Substring( commandline.IndexOf( "/R:", StringComparison.OrdinalIgnoreCase ) + 3 );
  112. 				test = test.Split( ' ' )[0];
  113. 				if ( !int.TryParse( test, out rotation ) )
  114. 				{
  115. 					rotation = 0;
  116. 				}
  117. 			}
  118. 			*/
  119. 			if ( commandline.ToUpper( ).Contains( "/T:" ) )
  120. 			{
  121. 				string test = commandline.Substring( commandline.IndexOf( "/T:", StringComparison.OrdinalIgnoreCase ) + 3 );
  122. 				test = test.Split( ' ' )[0];
  123. 				if ( !int.TryParse( test, out ringthickness ) )
  124. 				{
  125. 					ringthickness = 10;
  126. 				}
  127. 			}
  128. 			if ( commandline.ToUpper( ).Contains( "/W:" ) )
  129. 			{
  130. 				string test = commandline.Substring( commandline.IndexOf( "/W:", StringComparison.OrdinalIgnoreCase ) + 3 );
  131. 				test = test.Split( ' ' )[0];
  132. 				if ( !int.TryParse( test, out windowwidth ) )
  133. 				{
  134. 					windowwidth = this.ClientRectangle.Width;
  135. 				}
  136. 			}
  137. 		}
  138.  
  139.  
  140. 		private void FormMain_Resize( object sender, EventArgs e )
  141. 		{
  142. 			windowheight = this.ClientRectangle.Height;
  143. 			windowwidth = this.ClientRectangle.Width;
  144. 			DrawEllipse( );
  145. 		}
  146.  
  147.  
  148. 		private void FormMain_Shown( object sender, EventArgs e )
  149. 		{
  150. 			DrawEllipse( );
  151. 		}
  152.  
  153.  
  154. 		private void FormMain_SizeChanged( object sender, EventArgs e )
  155. 		{
  156. 			windowheight = this.ClientRectangle.Height;
  157. 			windowwidth = this.ClientRectangle.Width;
  158. 			DrawEllipse( );
  159. 		}
  160.  
  161.  
  162. 		private static int ShowHelp( bool cli = false )
  163. 		{
  164. 			string message = string.Format( "Marker.exe,  Version {0}\n", progver );
  165. 			message += "Show a resizable elliptic ring with transparent background on screen\n\n";
  166. 			message += "Usage:  \tMarker\t[ options ]\n\n";
  167. 			message += "Options:\t      \t/?\t\tdisplay this help and exit\n";
  168. 			message += "        \t      \t/C:color    \tdefault Red\n";
  169. 			message += "        \t      \t/H:height   \tdefault 450\n";
  170. 			//message += "        \t      \t/R:rotation \tdefault 0\n";
  171. 			message += "        \t      \t/T:thickness\tdefault 10\n";
  172. 			message += "        \t      \t/W:width    \tdefault 800\n\n";
  173. 			message += "The ellipse can be resized bij \"grabbing\" its border at top center,\n";
  174. 			message += "bottom center, middle left or middle right.\n";
  175. 			message += "The ellipse can be moved by \"grabbing\" its border anywhere else.\n\n";
  176. 			message += "Keys:   \tPress F1 to display this help text, or Escape to exit.\n\n";
  177. 			message += "Credits:\tMake transparent window resizable by\n";
  178. 			message += "        \tuser1306322 on StackOverflow.com:\n";
  179. 			message += "        \thttps://stackoverflow.com/a/32261547\n";
  180. 			message += "        \tMake transparent window moveable by\n";
  181. 			message += "        \tFreewareFire on CodeProject.com\n";
  182. 			message += "        \thttps://www.codeproject.com/KB/cs/csharpmovewindow.aspx\n";
  183. 			message += "        \tOutput to parent console by Timm:\n";
  184. 			message += "        \tcsharp411.com/console-output-from-winforms-application/\n\n";
  185. 			message += "Written by Rob van der Woude\n";
  186. 			message += "https://www.robvanderwoude.com\n";
  187. 			if ( cli )
  188. 			{
  189. 				Console.Error.WriteLine( message );
  190. 				Environment.Exit( -1 );
  191. 				Application.Exit( );
  192. 			}
  193. 			else
  194. 			{
  195. 				helprequested = true;
  196. 				MessageBox.Show( message, "Help for Marker.exe, \u00A0 Version " + progver );
  197. 			}
  198. 			return -1;
  199. 		}
  200.  
  201.  
  202. 		#region Make Window Moveable
  203.  
  204. 		// Code to allow dragging the window
  205. 		// by FreewareFire on CodeProject.com
  206. 		// https://www.codeproject.com/KB/cs/csharpmovewindow.aspx
  207. 		public const int WM_NCLBUTTONDOWN = 0xA1;
  208. 		public const int HT_CAPTION = 0x2;
  209.  
  210. 		[DllImport( "user32.dll" )]
  211. 		public static extern int SendMessage( IntPtr hWnd, int Msg, int wParam, int lParam );
  212.  
  213. 		[DllImport( "user32.dll" )]
  214. 		public static extern bool ReleaseCapture( );
  215.  
  216. 		private void FormMain_MouseDown( object sender, MouseEventArgs e )
  217. 		{
  218. 			if ( e.Button == MouseButtons.Left )
  219. 			{
  220. 				ReleaseCapture( );
  221. 				SendMessage( Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0 );
  222. 			}
  223. 		}
  224.  
  225. 		#endregion Make Window Moveable
  226.  
  227.  
  228. 		#region Make Window Resizable
  229.  
  230. 		// Code to make borderless window resizable
  231. 		// by user1306322 on StackOverflow.com
  232. 		// https://stackoverflow.com/a/32261547
  233. 		private const int
  234. 			HTLEFT = 10,
  235. 			HTRIGHT = 11,
  236. 			HTTOP = 12,
  237. 			HTTOPLEFT = 13,
  238. 			HTTOPRIGHT = 14,
  239. 			HTBOTTOM = 15,
  240. 			HTBOTTOMLEFT = 16,
  241. 			HTBOTTOMRIGHT = 17;
  242.  
  243. 		const int _ = 10; // you can rename this variable if you like
  244.  
  245. 		Rectangle Top
  246. 		{
  247. 			get
  248. 			{
  249. 				return new Rectangle( 0, 0, this.ClientSize.Width, _ );
  250. 			}
  251. 		}
  252.  
  253. 		Rectangle Left
  254. 		{
  255. 			get
  256. 			{
  257. 				return new Rectangle( 0, 0, _, this.ClientSize.Height );
  258. 			}
  259. 		}
  260.  
  261. 		Rectangle Bottom
  262. 		{
  263. 			get
  264. 			{
  265. 				return new Rectangle( 0, this.ClientSize.Height - _, this.ClientSize.Width, _ );
  266. 			}
  267. 		}
  268.  
  269. 		Rectangle Right
  270. 		{
  271. 			get
  272. 			{
  273. 				return new Rectangle( this.ClientSize.Width - _, 0, _, this.ClientSize.Height );
  274. 			}
  275. 		}
  276.  
  277. 		Rectangle TopLeft
  278. 		{
  279. 			get
  280. 			{
  281. 				return new Rectangle( 0, 0, _, _ );
  282. 			}
  283. 		}
  284.  
  285. 		Rectangle TopRight
  286. 		{
  287. 			get
  288. 			{
  289. 				return new Rectangle( this.ClientSize.Width - _, 0, _, _ );
  290. 			}
  291. 		}
  292.  
  293. 		Rectangle BottomLeft
  294. 		{
  295. 			get
  296. 			{
  297. 				return new Rectangle( 0, this.ClientSize.Height - _, _, _ );
  298. 			}
  299. 		}
  300.  
  301. 		Rectangle BottomRight
  302. 		{
  303. 			get
  304. 			{
  305. 				return new Rectangle( this.ClientSize.Width - _, this.ClientSize.Height - _, _, _ );
  306. 			}
  307. 		}
  308.  
  309.  
  310. 		protected override void WndProc( ref Message message )
  311. 		{
  312. 			base.WndProc( ref message );
  313.  
  314. 			if ( message.Msg == 0x84 ) // WM_NCHITTEST
  315. 			{
  316. 				var cursor = this.PointToClient( Cursor.Position );
  317.  
  318. 				if ( TopLeft.Contains( cursor ) )
  319. 				{
  320. 					message.Result = (IntPtr)HTTOPLEFT;
  321. 				}
  322. 				else if ( TopRight.Contains( cursor ) )
  323. 				{
  324. 					message.Result = (IntPtr)HTTOPRIGHT;
  325. 				}
  326. 				else if ( BottomLeft.Contains( cursor ) )
  327. 				{
  328. 					message.Result = (IntPtr)HTBOTTOMLEFT;
  329. 				}
  330. 				else if ( BottomRight.Contains( cursor ) )
  331. 				{
  332. 					message.Result = (IntPtr)HTBOTTOMRIGHT;
  333. 				}
  334. 				else if ( Top.Contains( cursor ) )
  335. 				{
  336. 					message.Result = (IntPtr)HTTOP;
  337. 				}
  338. 				else if ( Left.Contains( cursor ) )
  339. 				{
  340. 					message.Result = (IntPtr)HTLEFT;
  341. 				}
  342. 				else if ( Right.Contains( cursor ) )
  343. 				{
  344. 					message.Result = (IntPtr)HTRIGHT;
  345. 				}
  346. 				else if ( Bottom.Contains( cursor ) )
  347. 				{
  348. 					message.Result = (IntPtr)HTBOTTOM;
  349. 				}
  350. 			}
  351. 		}
  352.  
  353. 		#endregion Make Window Resizable
  354. 	}
  355. }
  356.  

page last modified: 2024-04-16; loaded in 0.0105 seconds