Like Tree2Likes
  • 2 Post By AndrewHunter

How to get command prompt to close and reopen after 5 seconds.

This is a discussion on How to get command prompt to close and reopen after 5 seconds. within the C++ Programming forums, part of the General Programming Boards category; Hello, I hope you are having a good day and thank you for taking the time to help me. I ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    8

    How to get command prompt to close and reopen after 5 seconds.

    Hello,

    I hope you are having a good day and thank you for taking the time to help me.

    I am trying to write a program that opens a command prompt, waits for a number and ENTER then displays a picture then the command prompt closes, waits 5 seconds and then reopens the command prompt and repeats.

    The problem is that I can't get the command prompt to close even though I have
    system("exit"); in my program.

    The reason why I need the command prompt to reopen is because I don't have the ability to select it (clink on it) before I enter the number and ENTER. ( a microcontroller is sending the number and ENTER and I can't select on the command prompt for the microcontroller).

    Here is my program:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <windows.h>
    using namespace std; 
    int main(void)
    {
    	while(true)
            {
    	        int i;  
    	        cout << "READY FOR INPUT: \n";
    	        cin  >> i;
    		if(i==0)
    		{
    			system("exit");
    			HINSTANCE hInst = ShellExecute(NULL,"open","explorer","C:\\one.png",0,SW_SHOWMAXIMIZED);
    			Sleep(5);
    		}
    		if(i==1)
    		{
    			system("exit");
    			HINSTANCE hInst = ShellExecute(NULL,"open","explorer","C:\\two.png",0,SW_SHOWMAXIMIZED);
    			Sleep(5);
    		}
    		if(i==2)
    		{
    			system("exit");
    			HINSTANCE hInst = ShellExecute(NULL,"open","explorer","C:\\three.png",0,SW_SHOWMAXIMIZED);
    			Sleep(5);
    
    		}
    		if(i==3)
    		{
    			system("exit");
    			HINSTANCE hInst = ShellExecute(NULL,"open","explorer","C:\\four.png",0,SW_SHOWMAXIMIZED);
    			Sleep(5);
    		}
    	}
    }

    My computer info:
    64-bit Dell running Windows 7 OS.
    Eventually, I am thinking of making an array when the number entered is the offset of the ShellExecute() command but that will come later.
    Last edited by +K.Flynn+; 07-13-2011 at 12:11 PM.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    ( a microcontroller is sending the number and ENTER and I can't select on the command prompt for the microcontroller).
    Then why try to close the prompt after some processing is completed?...you could just continue on the next line ..(or do a clear screen if cleanliness matters).
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Quote Originally Posted by manasij7479 View Post
    Then why try to close the prompt after some processing is completed?...you could just continue on the next line ..(or do a clear screen if cleanliness matters).
    Because, when the picture is opened it "goes above" the command prompt so that if the uC(micro-controler) types in a number then ENTER, nothing will happen because it will be typing that onto Windows Picture viewer which will then not do anything.

    Is there someway to get the command prompt to gain priorioty or be selected without clicking on it with the mouse or closing it or reopening it? Am I not using the correct system() command?

    Thank you for your help.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Am I not using the correct system() command?
    I'm not in any way knowledgeable with windows, but are you sure that the only way you have is to use system() commands?
    Shouldn't a Windows Api based solution be nicer ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    I don't think you are going to be able to do this with the shell() function. Take a look at the API function SetWindowPos(). You will have to include windows.h and also get the handle to the console window.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Quote Originally Posted by manasij7479 View Post
    I'm not in any way knowledgeable with windows, but are you sure that the only way you have is to use system() commands?
    Shouldn't a Windows Api based solution be nicer ?
    Well really all I am trying to do is close the current console window that is up. Is there another way to do this besides system("exit")?
    What would a windows Api solution be? I'm sorry I am so unknowledgeable.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Quote Originally Posted by AndrewHunter View Post
    I don't think you are going to be able to do this with the shell() function. Take a look at the API function SetWindowPos(). You will have to include windows.h and also get the handle to the console window.
    There is nothing wrong with the ShellExecute() function. I have edited the code to show what it actually looks like. I don't care what happens to the window that is opened when the ShellExecute() function executes, its that I want the console to close. How do I close the console window?

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Ok so I looked into the SetWindowPos Function and it looks really promising. I will investigate this further and let you know how I am coming along! Thank you for your help!!!!!!

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    So I guess now, the question is this:

    How do I get a command prompt to stay on top of the Z Order?

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Hmm, well I clicked on the link I provided and read and then saw this:
    Code:
    BOOL WINAPI SetWindowPos(
      __in      HWND hWnd,
      __in_opt  HWND hWndInsertAfter,
      __in      int X,
      __in      int Y,
      __in      int cx,
      __in      int cy,
      __in      UINT uFlags
    );
    And then I continued to read and saw:
    Code:
    HWND_TOP
    (HWND)0 Places the window at the top of the Z order.
    And then I looked at MSDN for how to get the console handle
    Sorry, I didn't read that one for you because I was so tired after the two seconds it took to look at the link I provided you a couple of posts ago.
    Last edited by AndrewHunter; 07-13-2011 at 06:33 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Jul 2011
    Posts
    8
    Do I declare the SetWindowPos like this:

    Code:
    BOOL WINAPI SetWindowPos(HWND,HWND,int X,int Y,int cx,int cy,UINT);
    ?

    I used it like this and got a lot of errors:
    Code:
    SetWindowPos(GetConsoleWindow(),HWND_BOTTOM,0,0,0,0,SWP_NOSIZE);
    but I did declare:
    Code:
    HWND WINAPI GetConsoleWindow(void);
    Thank you for your help.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    2,571
    You ONLY declare functions YOU are supplying to the Compiler!

    Did you write SetWindowPos and GetConsoleWindow functions?
    If not, do NOT lie to the compiler that you are going to supply them.

    Add the correct header to your source code and then link to the correct library (if a library is needed).

    Tim S.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do not pop up the command prompt?
    By ZaC in forum C Programming
    Replies: 4
    Last Post: 06-30-2009, 04:58 AM
  2. Command Prompt
    By Trafalgar Law in forum Tech Board
    Replies: 3
    Last Post: 10-09-2008, 06:00 PM
  3. command prompt++
    By l2u in forum Tech Board
    Replies: 13
    Last Post: 04-29-2007, 12:21 AM
  4. Command Prompt here
    By Salem in forum Tech Board
    Replies: 0
    Last Post: 04-11-2005, 11:10 AM
  5. No command prompt please!
    By Lurker in forum Windows Programming
    Replies: 3
    Last Post: 03-07-2003, 03:00 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21