1

I need to call ShowDialog() on my Form without it actually displaying the dialog (keep Visible set to false). Unfortunately, there is no VisibleChanged event like there is on the full framework. I also can't override the the Visible property. The closest I can come up with is to override OnLoad and call Hide in a new thread (since the form's visibility is set after it is loaded). This is obviously a crazy hack (not to mention is looks really bad since you can see a form being drawn and then hidden on the screen) but I really can't seem to figure out another way to do this. Any ideas?

Edit: I need to call ShowDialog() because I'm working with a buggy third party library which only works when invoked within a form like this and in my scenario I have no need or desire for any UI. I've confirmed the bug with the third party but they don't currently have any resources to fix the issue so I'm stuck with some crazy workaround this.

Edit2: Here's some more specific info about my issue:

This works:

MyForm_OnLoad(...)
{
    thirdPartyLib.StartUp(MyCallback);
}

private void MyCallback(...)
{
    // Do some work with the data passed in.
}

This does not:

public static void Main()
{
    thirdPartyLib.StartUp(MyCallback);
    // Sleep for a bit to allow the library to fire the callback.
    // Normally, the callback is triggered several times a second.
    Thread.Sleep(20000);
}

private void MyCallback(...)
{
    // This callback is never invoked by the library.
}

So the only way I can get things to work is by using the library in a Form. Unfortunately I don't want to display a form in my application so I'm trying to use the form to appease the library but not display anything to accommodate my application. I'm open to suggestions.

Note that the compact framework winforms API does not support opacity nor does it have an OnShown event (nor VisibleChanged).

Edit3: Sorry guys, I'm not intending to be vague I just didn't want to get lost in details that didn't seem relevant. The third party library captures images from a special camera hooked up via USB. The callback function gets fired with a couple different parameters to indicate the current status and image data from the camera.

Dennis
  • 532
  • 7
  • 21
  • 4
    Can you explain why you need to call ShowDialog without it showing? It might be helpful for someone to give you an alternative approach. – msergeant Jul 13 '10 at 19:29
  • @msergeant agreed. It seems like there is underlying problem you are trying to solve and this is one solution. But there might be something better because the whole point of having something show in dialog mode is so that they can't do anything else. – spinon Jul 13 '10 at 19:31
  • Yes. Instead of telling us how you've decided to solve the problem (which is almost certainly wrong), tell us what the actual issue you're trying to solve is. – ctacke Jul 13 '10 at 19:45
  • Any chance you can set the window to entirely transparent before you call ShowDialog()? – Stuart Matheson Jul 13 '10 at 21:14
  • I don't believe the compact framework supports transparent forms. – Dennis Jul 14 '10 at 21:31
  • Do you need to invoke `ShowDialog()`, or is it enough to instantiate the form -- `new LibraryContainerForm()`, and execute your library code after that, e.g. `libraryContainerForm.DoSthWithLibrary()`? – stakx - no longer contributing Jul 14 '10 at 21:32
  • I actually need to call `ShowDialog()`, if I simply called `MyForm_OnLoad(...)` manually, it doesn't work which is incredibly puzzling. The library must somehow depend upon a window handle (which gets created when the form is becomes visible for the first time) or it needs to be invoked from the UI thread. I've tried invoking it from the UI thread (and various other threads for that matter) but that still doesn't work. – Dennis Jul 15 '10 at 13:24

2 Answers2

1

It sounds like the third-party library is using Windows Messages for dispatching, though you're still not being terribly clear on what the actual problem is and seem to be too focused on the approach you have decided on, which I still think is wrong.

If the reason you need the control in a Window is becasue it is using Windows Messages for dispatching, then you can probably get around the issue with a MessageWindow to sink the messages or through your own calls to GetMessage/TranslateMessage/DispatchMessage.

Again, tell us what the actual root problem is, not the difficulties you're having with the solution you're trying to implement.

ctacke
  • 66,480
  • 18
  • 94
  • 155
  • It is not very clear what the issue with the third party library is (they're not very forth coming about the problem). Given the sample code I have in the main question now, it doesn't seem like they'd be depending upon the window messages since they don't hook into any window handles or anything like that. I've tried invoking the library on different threads thinking there is some type of threading issue with the library that is causing the problem but this didn't do anything. – Dennis Jul 14 '10 at 15:47
  • I can think of no reason that a Window would be neededunless it needed to dispatch windows messages. It might be doing it internally, it might be getting OS messages through them. Again, you're being very vague about what this control is or what it's supposed to do, so all we can do is guess. Why don't you tell us what this control is and what it's supposed to do? – ctacke Jul 14 '10 at 16:55
  • never underestimate the power of a 3rd party to do something totally crazy. Those of us forced to deal primarily with other's code have had to deal with plenty of things third parties have done that fit the form "there is no reason that X is needed, but we do it anyway, even though X is an antipattern. – Peter Recore Jul 14 '10 at 21:43
0

Here's a way to minimize the form since you don't have FormWindowState.Minimized available in compact framework.

http://christian-helle.blogspot.com/2007/06/programmatically-minimize-application.html

jdot
  • 801
  • 1
  • 6
  • 9
  • There is no `OnShown` on compact framework nor is there a `FormWindowState.Minimized` enum value. – Dennis Jul 14 '10 at 15:45
  • I can only call `ShowWindow` after a window handle has been created which is done in `ShowDialog`. But then I could just use `Hide()` which means I'm back to what I'm doing now. :( – Dennis Jul 14 '10 at 21:36
  • I wish there was a way to create my own `ShowDialog()` but I have a feeling .NET uses a bunch of internal and native stuff to make this work for the real version so I don't know how easy it would be to re-implement this. – Dennis Jul 14 '10 at 21:39