How to minimize command mode window in console program?

  • Thread starter Thread starter James Wong
  • Start date Start date
J

James Wong

Dear all,

I'd like to know if there is any method to minimize command mode window when
a console program is running. In my case, there are several console programs
which run periodically in server. Now, every console program instance will
open a command mode window and they occupy the whole screen. I want to
minimize all of them and maximize it if neccessary by manual. Is it possible
and how to do it? I'm using VB.NET 2005.

Thanks for your attention and kindly advice!

Regards,
James Wong
 
Dear all,

I'd like to know if there is any method to minimize command mode window when
a console program is running. In my case, there are several console programs
which run periodically in server. Now, every console program instance will
open a command mode window and they occupy the whole screen. I want to
minimize all of them and maximize it if neccessary by manual. Is it possible
and how to do it? I'm using VB.NET 2005.

Thanks for your attention and kindly advice!

Regards,
James Wong

Hi James,
If you want to minimize them on application launch, it's pretty
simple:

' Assuming you have a console app
Dim info As New System.Diagnostics.ProcessStartInfo("c:\process.exe")
info.WindowStyle = ProcessWindowStyle.Minimized
Dim process As New Process
process.Start(info)

And i suppose you want to minimize *all* of your apps while they're
running, so why don't use just click on "show desktop" on quick launch
bar to minimize all to open spare space on screen?

Thanks,

Onur Güzel
 
Hi Onur,

Thanks for your reply! But I may not describe my question clearly. The
console programs are either launched by Windows scheduler or manual
double-click in Windows Explorer, but not run by other VB application. My
question is if it is possible to write code to minimize command window within
my console application.

Thanks again for your help!

Regards,
James Wong
 
Hi Onur,

Thanks for your reply! But I may not describe my question clearly. The
console programs are either launched by Windows scheduler or manual
double-click in Windows Explorer, but not run by other VB application. My
question is if it is possible to write code to minimize command window within
my console application.

Thanks again for your help!

Regards,
James Wong

Hi again,
I tested this and you can minimize a console window using ShowWindow
function API via P/Invoke.

Here is a full simple snippet to minimize a console window:
(Save and compile application as "myconsole")


Imports System.Diagnostics

Module Module1
Private Declare Function ShowWindow Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal nCmdShow As SHOW_WINDOW _
) As Boolean

<Flags()> _
Private Enum SHOW_WINDOW As Integer
SW_HIDE = 0
SW_SHOWNORMAL = 1
SW_NORMAL = 1
SW_SHOWMINIMIZED = 2
SW_SHOWMAXIMIZED = 3
SW_MAXIMIZE = 3
SW_SHOWNOACTIVATE = 4
SW_SHOW = 5
SW_MINIMIZE = 6
SW_SHOWMINNOACTIVE = 7
SW_SHOWNA = 8
SW_RESTORE = 9
SW_SHOWDEFAULT = 10
SW_FORCEMINIMIZE = 11
SW_MAX = 11
End Enum

Sub Main()
Console.Write("Press Any Key to minimize window")
Console.ReadLine()
' Minimize function
minimize()
' See it minimized, then exit
Console.ReadLine()
End Sub

Private Sub minimize()
For Each p As Process In
Process.GetProcessesByName("myconsole")
ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_MINIMIZE)
Next p
End Sub

End Module

.... And you can change "myconsole" which is your orginal console
application's name.(Don't add .exe extension, be careful!) to get
process. Then call the sub named "minimize" (even you can change this
sub name, you know) to invoke ShowWindow function API.


Hope this helps,

Onur Güzel
 
Hi Onur,

Thanks a lot for your help! What I thought in the past is that I only need
to set some property to minimize the console window. I never imagine that I
need to write code involving API. I'll never make it without your help!

Regards,
James Wong
 
Hi Onur,

Thanks a lot for your help!  What I thought in the past is that I only need
to set some property to minimize the console window.  I never imagine that I
need to write code involving API.  I'll never make it without your help!

Regards,
James Wong
















- Show quoted text -

Thanks for the feedback, if it was a form then minimizing would be
done with a single codeline as you stated by setting windowState
property, but console window is minimized using API.

Glad you solved it :)

Thanks,

Onur Güzel
 
Back
Top