Minimize or restore a window via a small application

  • Thread starter Thread starter SteveT
  • Start date Start date
S

SteveT

I have an application that will look at other Windows applications that are
currently running. In a listbox I have the names of these applications and
their Windows ID. When the user clicks one of the application names in the
listbox I want to either minimize or restore that application's window to the
desktop. Can someone show me how this is done in VS2008 C# and .NET 3.5?
 
Hi Steve,

If you already have the handle to those windows (hwnd), then you can use
P/Invoke to call win32 api ShowWindow() to restore or minimize them:

#ShowWindow Function ()
http://msdn2.microsoft.com/en-us/library/ms633548(VS.85).aspx


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi. Thanks for the example. I've implemented the calls and the routines are
running, but my windows will not "restore" to the desktop. I start them via
StartInfo minimized and then my small app wants to display a list of these
minimized windows to open the selected one.

The example says this "The first time an application calls ShowWindow, it
should use the WinMain function's nCmdShow parameter as its nCmdShow
parameter."

What does this mean? How do I do this?
 
Hi Steve,

The comments about WinMain is for win32 (unmanaged) application that is
using CreateWindow and ShowWindow to create and show windows on startup. We
can ignore that part in this case.

I'm not sure about your existing code, but I created some example code for
your reference:

1) Run notepad and minimize it

2) Create a new C# winform application, add a button:

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);

const int SW_RESTORE = 9;

private void button1_Click(object sender, EventArgs e)
{
IntPtr hwndNotepad = FindWindow("Notepad", "Untitled -
Notepad");
if (hwndNotepad != IntPtr.Zero)
{
ShowWindow(hwndNotepad, SW_RESTORE);
}
}

(I'm assuming you're using English version of Notepad, otherwise the second
parameter to FindWindow need to be changed accordingly.)

This will restore the Notepad window that was minimized previously.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top