| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C-Sharp Programming Post questions, answers, and comments about C#. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello,
I'm trying to minimize all open windows. I have tried to get a few api's to find all open windows and close them, but i cant manage to do it... first i thought i had a working app, i was getting a list of processes and close their open windows. altho that didnt work wid a few windows, like explorer (windows 7). anyway i can use EnumWindow to loop throw all windows and minimize them? a little example would be very helpful. Thanks in advance, Peter |
|
#2
|
||||
|
||||
|
Re: Minimize all open windows
Include this ontop of your class :
Code:
using System.Runtime.InteropServices; //For APIs Code:
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName); //Find A Window
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam); //Send System Message
const int WM_COMMAND = 0x111;
const int MIN_ALL = 419;
const int MIN_ALL_UNDO = 416;
Code:
IntPtr lHwnd = FindWindow("Shell_TrayWnd", null); //Get Access To TaskBar Window
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero); //Minimize All
System.Threading.Thread.Sleep(2000); //Let It Do Its Stuff
SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero);
I hope it helps!
__________________
My Latest Articles : Changing Mouse Settings with VB.NET || Creating Your Own Encryption / Decryption Program Using VB.NET 2005 || Autorun Menu Creator in VB.NET || Creating Your Own Tetris Game With VB.NET Part 1 Find All My Articles : Here FAQs : .NET FrameWork FAQs || Visual Basic.NET FAQs || C# FAQs Be The Change That You Want To See In The World - Michael Scofield, Prison Break; Season 1 Episode 1 Read This Before You Post || Acceptable Use Policy
|
|
#3
|
||||
|
||||
|
Re: Minimize all open windows
Hannes, you may want to use SendMessageTimeout instead.
If you don't, SendMessage will cause your app to hang if any target app (window) is not responding.
__________________
Arjay Need a little help with Win32 thread synchronization? Check out the following CG articles and posts: Sharing a thread safe std::queue between threads w/progress bar updating Simple Thread: Part I Simple Thread: Part II Win32 Thread Synchronization, Part I: Overview Win32 Thread Synchronization, Part 2: Helper Classes www.iridyn.com
|
|
#4
|
|||
|
|||
|
Re: Minimize all open windows
Quote:
Code:
// somewhere in your main form: Form SomeForm = new Form(); SomeForm.Owner = this; |
|
#5
|
||||
|
||||
|
Re: Minimize all open windows
Good call!
I'll see how I can modify this
__________________
My Latest Articles : Changing Mouse Settings with VB.NET || Creating Your Own Encryption / Decryption Program Using VB.NET 2005 || Autorun Menu Creator in VB.NET || Creating Your Own Tetris Game With VB.NET Part 1 Find All My Articles : Here FAQs : .NET FrameWork FAQs || Visual Basic.NET FAQs || C# FAQs Be The Change That You Want To See In The World - Michael Scofield, Prison Break; Season 1 Episode 1 Read This Before You Post || Acceptable Use Policy
|
|
#6
|
|||
|
|||
|
Re: Minimize all open windows
couldn't you also use postmessage and the broadcast handle to accomplish the same thing.
__________________
/bb|[^b]{2}/ my deeply padded corner of the internet |
|
#7
|
||||
|
||||
|
Re: Minimize all open windows
OK, here's the Modified code, now using SendMessageTimeOut :
Code:
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
// [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
// static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr SendMessageTimeout(
IntPtr windowHandle,
uint Msg,
IntPtr wParam,
IntPtr lParam,
SendMessageTimeoutFlags flags,
uint timeout,
out IntPtr result);
[Flags]
enum SendMessageTimeoutFlags : uint
{
SMTO_NORMAL = 0x0,
SMTO_BLOCK = 0x1,
SMTO_ABORTIFHUNG = 0x2,
SMTO_NOTIMEOUTIFNOTHUNG = 0x8
}
const int WM_COMMAND = 0x111;
const int MIN_ALL = 419;
const int MIN_ALL_UNDO = 416;
private void button1_Click(object sender, EventArgs e)
{
IntPtr OutResult;
IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
SendMessageTimeout(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 2000, out OutResult);
System.Threading.Thread.Sleep(2000);
SendMessageTimeout(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_ABORTIFHUNG, 2000, out OutResult);
}
}
Quote:
__________________
My Latest Articles : Changing Mouse Settings with VB.NET || Creating Your Own Encryption / Decryption Program Using VB.NET 2005 || Autorun Menu Creator in VB.NET || Creating Your Own Tetris Game With VB.NET Part 1 Find All My Articles : Here FAQs : .NET FrameWork FAQs || Visual Basic.NET FAQs || C# FAQs Be The Change That You Want To See In The World - Michael Scofield, Prison Break; Season 1 Episode 1 Read This Before You Post || Acceptable Use Policy
|
|
#8
|
||||
|
||||
|
Re: Minimize all open windows
Easiest way yet, without using APIs, is to make use of the Shell32 class directly
![]() Set a reference to Microsoft Shell Controls And Automation, to do this follow these steps : Click Project Click Add Reference Click the COM tab Scroll down and select Microsoft Shell Controls And Automation Click OK Add a Button and give it the Text : Show Desktop Switch to code View, add the following using statement : Code:
using Shell32; Code:
private static Shell32.Shell shellInterface = new Shell32.Shell(); Code:
private void button1_Click(object sender, EventArgs e)
{
shellInterface.MinimizeAll();
}
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Shell32;
namespace ShowDesktop
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static Shell32.Shell shellInterface = new Shell32.Shell();
private void button1_Click(object sender, EventArgs e)
{
shellInterface.MinimizeAll();
}
}
}
![]() I hope this also helps!
__________________
My Latest Articles : Changing Mouse Settings with VB.NET || Creating Your Own Encryption / Decryption Program Using VB.NET 2005 || Autorun Menu Creator in VB.NET || Creating Your Own Tetris Game With VB.NET Part 1 Find All My Articles : Here FAQs : .NET FrameWork FAQs || Visual Basic.NET FAQs || C# FAQs Be The Change That You Want To See In The World - Michael Scofield, Prison Break; Season 1 Episode 1 Read This Before You Post || Acceptable Use Policy
|
|
#9
|
|||
|
|||
|
Re: Minimize all open windows
Thank you all for your help!
I managed to create my little app that can minimize all windows on just one screen in my 4 screen setup. mapped to Ctrl + 1, 2, 3 and 4. makes it a quite usefull app. attaching source if someone wants to take a look at it. Definitely badly written but it does what it's suppose to. Thanks again! |
|
#10
|
||||
|
||||
|
Re: Minimize all open windows
Thanx for sharing!
Everyone starts with writing "badly written programs" ![]() With experience, it imporves, keep on keeping on !
__________________
My Latest Articles : Changing Mouse Settings with VB.NET || Creating Your Own Encryption / Decryption Program Using VB.NET 2005 || Autorun Menu Creator in VB.NET || Creating Your Own Tetris Game With VB.NET Part 1 Find All My Articles : Here FAQs : .NET FrameWork FAQs || Visual Basic.NET FAQs || C# FAQs Be The Change That You Want To See In The World - Michael Scofield, Prison Break; Season 1 Episode 1 Read This Before You Post || Acceptable Use Policy
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|