Tuesday, December 30, 2014

I'd Cap That

A buddy suggested I do a Vim how-to.  The only problem I have is that, on Windows, I don't have an animated window capture utility to provide proof of the power of this programmer's editor.  So, in preparation, I decided to see if I could build one.  Here's how.


Darn skippy I copied all of this -- and in just three queries:

Thank you, information super-highway. Now, on to what I found...


The relevant pages I found were:

I reviewed the code at developerfusion.com, found it to be straightforward, and incorporated it into a C# program.  Then I wrote this code to go with it:

 public class Cap
  // http://www.pinvoke.net/default.aspx/user32.findwindow
  // Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.
  [DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
  static public extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

  static public int Main(string [] args)
   if (args.Length != 3)
    Console.WriteLine("Usage: cap windowtitle gifbasename delayms");
    return 1;

   IntPtr hWnd = FindWindowByCaption(IntPtr.Zero, args[0]);

   if (hWnd == IntPtr.Zero)
    Console.WriteLine("No window by the title - {0}", args[0]);
    return 1;


   // stackoverflow.com/questions/1163761/capture-screenshot-of-active-window
   ScreenCapture sc = new ScreenCapture();

   int i = 0;
   StringBuilder sb = new StringBuilder();

   while (true)
    sb.AppendFormat("{0}{1}.gif", args[1], i.ToString("D8"));
    sc.CaptureWindowToFile(hWnd, sb.ToString(), ImageFormat.Gif);

   return 0;

I saved the 171-line file as screenshot.cs, and then wrote a compiler script:

%WINDIR%\Microsoft.NET\Framework\v4.0.30319\csc.exe *.cs

Then, I double-clicked that, sifted through some warnings, and came up with an executable.

Next, I built a script to use ImageMagick's convert.exe to stitch those gifs into an animation:


IF "%3" == "" GOTO Usage

 convert -loop 0 -delay %3 -resize %4 %1 %2

 ECHO Usage: %~nx0 fileswildcard outfile dly reduce%%
 ECHO Note: fileswildcard ought not match outfile, else: Inception!
 ECHO Note: dly in units of msec / 10

I saved that as makegif.cmd.  Finally, I decided to test it:

Now I'm ready to do that Vim demo.  Tomorrow, that is.

'nite ;-)

No comments:

Post a Comment