Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Progmatically minimizing an application

2 views
Skip to first unread message

Simon Hart

unread,
Jan 2, 2007, 1:14:01 PM1/2/07
to
Looking back over the archives, there doesn't seem to be a definitive answer
to this problem.

I have tried everything I can think of to achieve this from a desirable
point of view for the lack of a simple Minimize call.

Has anyone achieved this?

I have tried the following:

Placing a Hide() call in the Activated event - Problem with this is you can
no longer activate the application via the Control Panel\Memory applet. Also
it doesn't seem to hide the actual Window, rather just the Client visable
area.

Calling the native SetWindowPos API which sets the window on the bottom of
the Z-Order. Doesn't really work properly, part of the Window is missing and
also if an exact replication of hitting the "X" minimize button was required,
placing the Window on the bottom of the Z queue will not hide it anyway, it
would simply place it under all other Windows.

I've tried a bunch of other bodgey ways each fail....

Any ideas would be great. CF 2 - SP1, Pocket PC 2003 SE onwards.

Cheers
Simon.

Paul G. Tobey [eMVP]

unread,
Jan 2, 2007, 3:21:36 PM1/2/07
to
I don't get it. You want to "programmatically minimize an application"?
Just hide it when you want to do that. I don't understand trying to do that
from the Activated() event. That doesn't match what you say you're trying
to do. Therefore, either you are not trying to do what you say you're
trying to do or you've forgotten what it was that you were trying to do.

It looks to me like the following will do what you want (simulate the user
clicking on the X button):

SetWindowPos( this.MyhWnd, (IntPtr)1, 0, 0, 0, 0,
SetWindowPosFlags.SWP_NOMOVE );

assuming that MyhWnd is the handle to the form window and that SWP_NOMOVE is
set based on the native code header file value...

Paul T.

"Simon Hart" <srha...@yahoo.com> wrote in message
news:7D0D1ADC-C806-43EF...@microsoft.com...

Simon Hart

unread,
Jan 3, 2007, 4:51:01 AM1/3/07
to
I should have made this clearer. I want to programmatically mimic the
minimize function. Essentially I want to mimic the behaviour of clicking the
"X" button on the form, BUT at startup. This is the reason of placing the
Hide() etc into the Activated event. Sorry reading my post I can see the
confusion.

It seems the Application.Run method shows the form after it starts the
message pump. This Application class is sealed so cannot be derived from.

Calling Hide() in the Activated event does not mimic the Minimize
functionality.

Simon.

<ctacke/>

unread,
Jan 3, 2007, 9:09:57 AM1/3/07
to
Set the Form's Visible property to false in it's constructor.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


"Simon Hart" <srha...@yahoo.com> wrote in message

news:4FF5763B-4922-4680...@microsoft.com...

Paul G. Tobey [eMVP]

unread,
Jan 3, 2007, 10:38:55 AM1/3/07
to
I sent you the code to do a smart minimize. I suppose that it would work
just as well in form.Load as it does anywhere else...

Paul T.

"Simon Hart" <srha...@yahoo.com> wrote in message

news:4FF5763B-4922-4680...@microsoft.com...

Simon Hart

unread,
Jan 3, 2007, 11:09:01 AM1/3/07
to
I've tried this but it doesn't work.

Simon.

<ctacke/>

unread,
Jan 3, 2007, 11:28:56 AM1/3/07
to
I can't see how that's possible. If the Form's Visible property is False,
then it can't be visible. Something in _your_ code is making it visible
after that point.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--

"Simon Hart" <srha...@yahoo.com> wrote in message

news:A23825ED-F013-4D37...@microsoft.com...

Simon Hart

unread,
Jan 3, 2007, 12:21:01 PM1/3/07
to
Nothing in my code is making it visible. I have just tried creating a new
blank PPC 2003 app and set the Visible property to false in the ctor and it
still shows. I believe this is the way the Application.Run method is
implemented.

Simon.

Paul G. Tobey [eMVP]

unread,
Jan 3, 2007, 12:38:08 PM1/3/07
to
Sorry to keep jumping in, but have you tried my suggestion?

Paul T.

"Simon Hart" <srha...@yahoo.com> wrote in message

news:F29DE1DB-BE0A-4734...@microsoft.com...

<ctacke/>

unread,
Jan 3, 2007, 1:32:43 PM1/3/07
to
Application.Run is not implemented that way. I can guarantee that. Run
does nothing but set up a message pump - it doesn't do anything with window
visibility or style.

Where in you ctor are you setting it? Before or after InitializeComponent?
If it's before, then that's expected, as your InitializeComponent code is
probably setting it back to true.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


"Simon Hart" <srha...@yahoo.com> wrote in message

news:F29DE1DB-BE0A-4734...@microsoft.com...

Simon Hart

unread,
Jan 4, 2007, 4:32:00 AM1/4/07
to
Yes thanks Paul, I have tried your suggestion but it doesn't seem to mimic
the minimize functionality. It seems to do just what calling Hide() does
except I am able to re-activate the application from Settings\Memory applet.

Simon Hart

unread,
Jan 4, 2007, 4:37:01 AM1/4/07
to
According to the documentation it does:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.run.aspx

I am setting it after the InitializeComponent call. InitializeComponent (by
designer) doesn't set its Visible property anyway.

Simon.

Paul G. Tobey [eMVP]

unread,
Jan 4, 2007, 11:29:05 AM1/4/07
to
It mimics it perfectly on my WM5 device. Items are shown in the Memory
applet. Trying to start another instance of the EXE pops up the existing
one. When it's minimized, it disappears from the screen.

What aspect of "smart minimize" is it not copying for you?

Paul T.

"Simon Hart" <srha...@yahoo.com> wrote in message

news:ECF12B6A-6E87-4D14...@microsoft.com...

<ctacke/>

unread,
Jan 4, 2007, 12:29:12 PM1/4/07
to
Simon,

Wow, look at that. I was blantantly wrong! And I did a test app and get
the same behavior. Trying to start the app with Run(myForm) by having the
Form _not_ appear is not so simple. IMHO, this is a classic case of the
framework trying to do too much for the developer and ending up screwing us.

Since I wronged you, I tried to set it right. I've tried several P/Invoke
hacks this morning and all of them are failing. I can get the Form to hide,
but the Form title still shows at the top of the screen (that's actually a
different app in PPC/WM, BTW, which is why it's doing this).

This is what I did so far:

private bool m_firstShow = true;
public Form1()
{
InitializeComponent();

this.Activated += new EventHandler(Form1_Activated);
}

void Form1_Activated(object sender, EventArgs e)
{
if (m_firstShow)
{
SetWindowPos(this.Handle, WindowPos.Bottom, 0, 0, 0, 0,
WindPosFlag.NoMove | WindPosFlag.Hide);
m_firstShow = false;
}
}

enum WindowPos
{
Top = 0,
TopMost = -1,
NoTopMost = -2,
Bottom = 1
}

[Flags]
enum WindPosFlag
{
NoSize = 0x01,
NoMove = 0x02,
NoZOrder = 0x04,
NoActivate = 0x10,
Show = 0x40,
Hide = 0x80
}
[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr SetWindowPos(IntPtr hWnd, WindowPos
hWndInsertAfter, int X, int Y, int cx, int cy, WindPosFlag uFlags);


Adding calls to get the desktop hWnd, invalidating it and updating it don't
help. Tapping the screen _does_ get rid of the title. The next "hack" I'd
try (and this really is a hack) is to P/Invoke mouse_event to
programmatically "tap" the upper right of the screen where the minimize
button is. It's a kluge that I don't like, but for now it's all I can think
of.

The other option is to use the OpenNETCF's ApplicationEx.Run method and
modify the code so it's doesn't show your window. That's a far cleaner
solution.


--
Chris Tacke
OpenNETCF Consulting
Managed Code in the Embedded World
www.opennetcf.com
--


"Simon Hart" <srha...@yahoo.com> wrote in message

news:E5EB0AE6-3422-4931...@microsoft.com...

Simon Hart

unread,
Jan 5, 2007, 5:46:01 AM1/5/07
to
Thankyou for the time you've spent on this Chris.

I have modified the ApplicationEx (in 1.4) to receive another parameter and
the class uses this to determine whether to show the form or not and it works
very well.

Thanks again for your advice.

Simon.

0 new messages