2006-05-28
Calctor 1.1
I've updated my calculator tool to version 1.1:
- Optimized the parser
- Changed the meaning of the % operator (now percentage instead of modulo)
- Added synonyms for functions (abbreviations)
- Added some functions: modulo, sign, exp, secans, cosecans, arcsinh, arccosh, arctanh, arccotanh
- Added the option to auto scroll the output region
- The output of an invalid expression now looks less threatening
- Added license information to the Info-window
- Calctor is now prejitted after installation, which should improve performance
2006-05-27
Calctor
Calctor is a new tool I created. It's an advanced calculator with a simple user interface. It uses the free Krypton Toolkit from Component Factory for some of the user interface elements. You can download it from the Sandbox at Channel 9.
2006-05-20
Getting a list of all the open windows
Here's some code you can use to get a list of all the open windows. Actually, you get a dictionary where each item is a KeyValuePair where the key is the handle (hWnd) of the window and the value is its title.
using HWND = IntPtr;And here's some code that uses it:/// <summary>Contains a method to get all the open windows.</summary>public static class OpenWindowGetter {/// <summary>Returns a dictionary that contains the handle and title of all the open windows.</summary> /// <returns>A dictionary that contains the handle and title of all the open windows.</returns>public static IDictionary<HWND, string> GetOpenWindows() { HWND lShellWindow = GetShellWindow(); Dictionary<HWND, string> lWindows = new Dictionary<HWND, string>(); EnumWindows(delegate(HWND hWnd, int lParam) { if (hWnd == lShellWindow) return true; if (!IsWindowVisible(hWnd)) return true; int lLength = GetWindowTextLength(hWnd); if (lLength == 0) return true; StringBuilder lBuilder = new StringBuilder(lLength); GetWindowText(hWnd, lBuilder, lLength + 1); lWindows[hWnd] = lBuilder.ToString(); return true; }, 0); return lWindows; } delegate bool EnumWindowsProc(HWND hWnd, int lParam); [DllImport("USER32.DLL")] static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam); [DllImport("USER32.DLL")] static extern int GetWindowText(HWND hWnd, StringBuilder lpString, int nMaxCount); [DllImport("USER32.DLL")] static extern int GetWindowTextLength(HWND hWnd); [DllImport("USER32.DLL")] static extern bool IsWindowVisible(HWND hWnd); [DllImport("USER32.DLL")] static extern IntPtr GetShellWindow(); }
foreach(KeyValuePair<IntPtr, string> lWindow in OpenWindowGetter.GetOpenWindows()) { IntPtr lHandle = lWindow.Key; string lTitle = lWindow.Value; Console.WriteLine("{0}: {1}", lHandle, lTitle); }
Reading data from an embedded resource
Here's some code to read data from an embedded resource:
Basically, you can read any kind of data from an embedded resource, using the method GetManifestResourceStream. The resource name you have to pass as an argument should be the default namespace of the assembly, appended with the filename. Here's an example how to use it:/// <summary>Contains some methods to read data from an embedded resource.</summary>public static class EmbeddedResourceReader {/// <summary>Returns an embedded resource of the specified assembly as a string.</summary> /// <param name="assembly">The assembly to read the resource from.</param> /// <param name="resourceName">The name of the resource.</param> /// <returns>A string read from an embedded resource.</returns>public static string ReadString(Assembly assembly, string resourceName) { if (assembly == null) throw new ArgumentNullException("assembly"); if (resourceName == null) throw new ArgumentNullException("resourceName"); using(Stream lStream = assembly.GetManifestResourceStream(resourceName)) using(StreamReader lReader = new StreamReader(lStream)) return lReader.ReadToEnd(); }/// <summary>Returns an embedded resource of the specified assembly as an icon.</summary> /// <param name="assembly">The assembly to read the resource from.</param> /// <param name="resourceName">The name of the resource.</param> /// <returns>An icon read from an embedded resource.</returns>public static Icon ReadIcon(Assembly assembly, string resourceName) { if (assembly == null) throw new ArgumentNullException("assembly"); if (resourceName == null) throw new ArgumentNullException("resourceName"); using(Stream lStream = assembly.GetManifestResourceStream(resourceName)) return new Icon(lStream); } }
Assembly assembly = this.GetType().Assembly; string resourceName = "MyCompany.MyApplication.file.txt"; string content = EmbeddedResourceReader.ReadString(assembly, resourceName);
2006-05-19
Screen Capture 2.2
I've updated my screen capture tool to version 2.2. I fixed a few bugs, and added these features:- The size of the captured image is displayed in the statusbar.
- The captured image is stretched when resizing the window.
- A new function is added to capture the content of a selected window.
2006-05-05
Format a file size
Here you can find a method that creates a human-readable string of a file-size:
/// <summary>Returns a file size as a formatted string with a unit.</summary> /// <param name="fileSize">The file size to format.</param> /// <returns>The formatted string.</returns>public static string FormatFileSize(long fileSize) { if (fileSize < 0) throw new ArgumentOutOfRangeException("fileSize"); if (fileSize >= 1024 * 1024 * 1024) return string.Format("{0:########0.00} GB", ((double)fileSize) / (1024 * 1024 * 1024)); else if (fileSize >= 1024 * 1024) return string.Format("{0:####0.00} MB", ((double)fileSize) / (1024 * 1024)); else if (fileSize >= 1024) return string.Format("{0:####0.00} KB", ((double)fileSize) / 1024); else return string.Format("{0} bytes", fileSize); }
Filter numbers out of a string
Here you find a method that filters the numbers out of a string:
public static string FilterNumbers(string mightContainNumbers) { if (mightContainNumbers == null || mightContainNumbers.Length == 0) return ""; StringBuilder builder = new StringBuilder(mightContainNumbers.Length); foreach (char c in mightContainNumbers) if (Char.IsNumber(c)) builder.Append(c); return builder.ToString(); }