I’ve recently needed to find what control currently has focus in my WinForms application. Google gave me two answers. The first depends on unmanaged code, and the second is fully managed.
Reference
[csharp]
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr GetFocus();
private Control GetFocusedControl()
{
Control ctl = FromHandle(CreateProjectPanel.GetFocus());
//MessageBox.Show(ctl.Name);
return ctl;
}
[/csharp]
I can’t remember where I found this second solution, but it is the one I use in my code. It uses the Control.Focused and Control.ContainsFocus properties to recursively find the focused control.
[csharp]
private System.Windows.Forms.Control GetFocusedControl(System.Windows.Forms.Control.ControlCollection Controls)
{
// store focused control…
foreach (System.Windows.Forms.Control clsControl in Controls)
{
if (clsControl.Focused)
{
return clsControl;
}
if (clsControl.ContainsFocus)
{
if (clsControl.Controls.Count == 0)
{
return clsControl;
}
else
{
return GetFocusedControl(clsControl.Controls);
}
}
}
// no focus…
return null;
}
// usage
Control hasFocus = GetFocusedControl(this.FindForm().Controls);
[/csharp]
Executable: Thumbnailer-0.2.zip
Source: Thumbnailer_src-0.2.zip
I have released a new version of my web page thumbnail creator. I modified the UI up a bit and included the thumbnail control within a web browser that I found on CodeProject. I thought this would be better than just typing the URL (or copy/paste) into a text field. Sometimes I found myself navigating around and then making a thumbnail.
Also, I believe I have fixed the “white page” thumbnail issue. I am not quite sure what causes it, but it seems to happen on more “dynamic” sites, such as espn.go.com and nytimes.com. I think it has to do with IFrames and how it is used on that particular site. Either way, I have fixed it so that this problem doesn’t occur as often as it did. You can look at the source if you want more information. (requires Visual Studio 2005).
Executable: Thumbnailer-0.2.zip
Source: Thumbnailer_src-0.2.zip
Samples
And for those of you who don’t want to download the .NET framework, I found Webshot, a free Windows app that does just about the same thing as my thumnail maker.
I don’t remember why, but for the longest time I have been searching for a tool or code that would allow me to create a screen shot and thumbnail of an entire web page. Lately, I have been using Cropper, as well as the old ”Print Screen” and cropping the image in Photoshop technique. This normally does the job. But what if the web page is rendered to be taller than the available screen height?
I am sure there is a tool that does what I am looking for, but I haven’t found it yet…until today. Well, I almost found it. What I found instead was two articles on Code Project. And then after a couple hours in Visual Studio, presto-chango, I give you the Web Page Thumber.
It’s actually just a prototype, built on top of dooskoobi’s “Webpage Thumbnailer“ source code. Credits also go to a reply comment by gimenezparera to Douglas M. Weems’ “Image Capture Whole Web Page using C#” article. This last one pointed me to the System.Windows.Forms.WebBrowser component, specifically using WebBrowser.Document.Body.ScrollRectangle.Height to find the rendered web page height.

I have provided an executable download of my hacked version of the Webpage Thumbnailer. You will need .NET 2.0 Framework or redistributable package installed.
Executable: Thumbnailer.zip
The interface should be fairly straightforward, but here are a couple tips. ”Browser Width” is the width of the browser to render the web site at when the full size screen shot is taken. This is useful if you want to get a thumbnail/screen shot at different resolutions to see if the content shifts or not. To “Save a thumnail”, right-click on the image (or double-click on it). Currently, it only supports png as the output format.
One big note, it does NOT work with all web sites. I don’t know why, but it just doesn’t. I will take a look at it when I have more time.
I installed a newer version of VistaDB 2.x than what is in SourceSafe and in my project’s reference, and when I tried to run the app in Visual Studio 2005, it was asking me for the newer version of VistaDB.Provider.dll (2.20.25.0). And since my project only had reference to 2.20.22.0, it was a no go.
I spent an hour or two uninstalling the newer version, searching for and getting rid of all the new dlls, and verified all remaining VistaDB dlls on my hard drive to be the correct versions. It still didn’t work.
I finally fixed the problem when I searched the registry for the offending newer version, and removed references to it. Specifically the unwanted registry keys are found below:
HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Fusion \ PublisherPolicy \ Default \ policy.2.[11 or 20].vistadb.provider__7e75aae290f30672
Simply remove any reference to your unwanted versions. I deleted my exact keys before I wrote it down, so I can’t tell you what they were. Also, I guess you can just search the registry for the “bad” version of the VistaDB.Provider.dll using just the version number. Again, in my case, it was 2.20.25.0
I’ve downloaded this app from Code Project last night, and so I thought I would use it to make a post. Here’s a snippent of the what the app does.
RSS Feeder.NET is a free open source desktop RSS feed aggregator which downloads feeds from web sources and stores them locally for offline viewing, searching and processing. It is also a rich blogging tool which you can use to blog a variety of blog engines including WordPress, B2Evolution, .Text, Community Server etc. You can be fully MS Outlook® dependent or can run fully standalone. You can also use both at the same time whichever you find comfortable to work with. It does not increase the Outlook load time, nor does it make the Outlook slow or prevent it from closing properly. It is a Smart Client that makes best use of both Local Resource and Distributed Web Information sources.
I’m building a smart client of my own, and I hope this will be a decent example to follow. I am still going with the Compsite UI Application Block (CAB), so we’ll see if I can integrate what I learn from RSS Feeder.NET and what I have been doing with CAB for the last six months.
Also, I will look to improve this app so that I can use it to post to SurplusMeat without having to use a web browser. A very good feature set has already been implemented, but I think I have a few more ideas on how to improve it (spell check, live preview, post to multiple blogs).
I rolled my own dialog in WinForms 2.0 and wanted to use the images that is available when using the MessageBox by way of the MessageBoxIcon enum. To do what I wanted, I used the System.Drawing.SystemIcons class, which exposed static references to several system icons, of course. Here’s an example use of the Warning icon in a PictureBox:
[csharp]
System.Windows.Forms.PictureBox pb = new System.Windows.Forms.PictureBox();
pb.Location = new System.Drawing.Point(14, 7);
pb.Size = new System.Drawing.Size(48, 41);
pb.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
pb.BackgroundImage = System.Drawing.SystemIcons.Warning.ToBitmap();
[/csharp]
System.Drawing.SystemIcons also has Application, Asterisk, Error, Exclamation, Hand, Information, Question, Warning, and WinLogo.
The System.Drawing namespace also has SystemBrushes, SystemColors, SystemFonts, and SystemPens.
I was asked by our user interaction designer to make Enter key presses mimic a Tab key press by moving focus to the next control where TabStop=true. Sounds simple enough. But after several fruitless searches on Google, I thought I would have to resort to using unmanaged code. Luckly, I finally found a blog post with the answer that I was looking. Well, the post itself didn’t solve my problem, but the comments pointed me in the right direction.
The initial suggestion to override the form’s OnKeyUp event doesn’t apply to my case because my text fields were in a panel. ProcessKeyTab is a protected method that can only be called when you extend a control that is derived from ContainerControl, such as Form or Panel. Since I didn’t extend the Panel I was using, I couldn’t call ProcessKeyTab.
After reading the comments, I found that the answer lies in using the static SendKeys.Send method. In each TextBox that you want to treat Enter as Tab, simply handle the KeyPress event and call SendKeys.Send(”{TAB}”) if the key pressed is an Enter key.
Executive Summary
Great book on understanding and customizing Windows Forms! Quoting the book’s foreward given by Shawn Burke, Development Manager, Windows Forms Team, Microsoft Corporation:
Matthew MacDonald … has built a great resource for developers using Windows Forms to create great, rich applications. Whether the goal is to write components for internal use or a full application, this book will help you get there and deliver great results.
Mr. Burke goes on to say:
The practical, task-based approach, of Pro .NET 2.0 Windows Forms and Custom Controls in C# allows it to cover a wide range of Windows Forms topics, but still provide the technical depth to help developers deliver features. While many other resources read more like technical reference docs, Pro .NET 2.0 Windows Forms and Custom Controls in C# does an excellent job of filtering the information down to what developers really need to harness the power and innovations of Windows Forms 2.0 to deliver truly world-class client applications.
I couldn’t agree more with Mr. Burke. Since I’ve had the book, there hasn’t been a day gone by that I haven’t found at least one answer to my WinForms problems.
(more…)