Archives for: March 2005, 23

23/03/05

Permalink 07:39:43 pm
Categories: Programming, .NET

WinForms controls and the red X

Most people working with WinForms have probably encountered that red X that is drawn over a control at some point and just doesn't go away as long as the application is running. Originally, I had a look at the source of this some months ago and now, when I saw a relating question again, I thought I might document my findings here.

Note that I did that research with .NET 1 and I haven't checked for .NET 2 yet, so in the latter case YMMV.

So where does the red X come from? Simple: The System.Windows.Forms.Control has an internal state flag for this that gets set when an exception is thrown in the control's drawing code. So if you've never seen the red X but you want to, just throw a panel on a form and create a Paint event handler like this:

private void panel1_Paint(object sender, System.Windows.Forms.EventArgs e) {
  throw new Exception("Boom");
}

Now, the really interesting thing about the red X is that you can't easily get rid of it once it's popped up. The only "official" way is to restart the application. Lucky though that .NET has powerful reflection... that makes it possible to use the following method to reset the state:

void ResetExceptionState(Control control) {
  typeof(Control).InvokeMember("SetState", BindingFlags.NonPublic |
    BindingFlags.InvokeMethod | BindingFlags.Instance, null,
    control, new object[] { 0x400000, false });
}

So you can get that panel in the example above to have another go at drawing itself by going

ResetExceptionState(panel1);
panel1.Invalidate(); // invoke redraw

Of course, if the same exception is still thrown from the paint handler, there won't be much to see as the state is immediately set back to show the X again.

Generally, of course, you should have a very close look at the reason why there's an exception thrown in the paint handling code at all. But there are situations where you might want to control the Control's behaviour in detail and in these cases it's nice to be able to handle that internal state yourself.

Two additional notes:

  1. You do need certain permissions to get that reflection code to run. If you want to configure your application for exact permission sets, you should use [ReflectionPermission(SecurityAction.Demand, MemberAccess=true)] in front of the ResetExceptionState method.
  2. As I got a request for this at the time, I have a translation of the method in VB.NET, too. I don't usually use VB, so there may be more elegant ways to do this, but here goes:
Private Sub ResetExceptionState(ByVal control As Control)
  Dim args() As [Object] = {&H400000, False}
  GetType(System.Windows.Forms.Control).InvokeMember("SetState", _
  BindingFlags.NonPublic Or BindingFlags.InvokeMethod Or _
  BindingFlags.Instance, _
  Nothing, control, args)
End Sub 
Permalink 02:59:41 pm
Categories: General, Software

Shooting your screen - a plug for SnagIt

How do you do screenshots? Is there anybody who doesn't do them these days? It just occurred to me that I never wrote about my program of choice for this task, SnagIt.

Obviously, some of us may be happy using the built-in Windows functions via the Print Screen key. But there's a lot more that can be done with proper screen capturing software. I went to evaluate quite a lot of them before deciding to use SnagIt, but most failed at the simple tasks... like working on a multi-head setup, for example.

I'm not saying SnagIt is the best program out there for everybody's purposes, but you get an awful lot in the package. The capturing system itself is very feature rich and stable. You get a great image editor that has just the right features and there's another imaging application included that can do advanced stuff with vectorized templates. Every filter that the image editor knows can be used in an automatic configuration, so you don't have to do anything to get a special border effect on all your shots, for example.

Apart from these things, there's a lot more in the package that makes SnagIt a useful allround application. There's a print-to-screenshot printer driver included, and plugins for all the MS Office apps. There's shell integration and there are browser plugins for Internet Explorer and Firefox. The editor can be used just as well to work on shots that you got from somebody else, and it has all the features you need to make these shots look just as good as if you'd done them yourself.

Is it cheap? No, it's $39.95. But it's well worth the money. Go have a look!

Disclaimer: I'm a satisfied customer with no other relations to TechSmith Corporation.

Enter your email address:

Search

Oliver
MVP logo
March 2005
Sun Mon Tue Wed Thu Fri Sat
 << < Current> >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31