Page 1 of 2

Hide / Show a Console?

Posted: Sat Oct 01, 2016 2:44 pm
by Zimbo
Hi all,

I hope it is ok if I pick your brains again?

I am writing a small utility program which I am using to "dump" the contents of a multi-dimensional array to screen (a bit like print_r() in PHP). The simplest way to do this is for me in this situation is to use a console screen.

My question: is it possible to hide the console screen until I am ready to display text on it? The screen shot below shows the program when it starts running.

1. On line 2 of the source code I all REQUEST HB_GT_WIN_DEFAULT - when the program starts, the console screen (I added a red box around it in the image) appears before the main window (the one with a "Show Array" button). * I WANT TO HIDE THIS CONSOLE *

2. When the "Show Array" button is clicked, it calls the function on line 54 (ShowArray). This is when I want to *SHOW THE CONSOLE *

Maybe I have missed something simple? I tried SET CONSOLE OFF and SET CONSOLE ON but that did not work.

Thanks!

Zim


Code: Select all

#include "hmg.ch"
REQUEST HB_GT_WIN_DEFAULT

FUNCTION Main()

/*
    Sample program for Chapter 11 - Advanced Data Types
    This sample creates a multi-dimensional array which contains several different data types.
    Users can add more elements and / or change values as the learn about arrays. When rthe click
    on the "Show Array" button, the entire contents of the array, including all nested arrays, will
    be displayed on a console screen showing the element number, data type and value in
    that element.
    
*/

LOCAL nlblRow  :=  16
LOCAL nlblCol  :=  16

// Create a memory variable that will be an array
LOCAL aBigArray1 := {  }

/*
    Add the first two elements using the AADD array function. These are both arrays and each contains 5 elements
*/
AADD(aBigArray1, {.T., "Smith", Nil, "Married", "Customer"} )
AADD(aBigArray1, {".F.", "Jones", Nil, DATE(),  "Employee"} )

/*
    Add the next two elements using the AADD array function. The first one is an array that also contains another array
    at element position 3. The second one is also an array that also contains another array but this time at element
    position 4
*/
AADD(aBigArray1, { "Paul", "Brown", {"Mary", "Bob", "Billy"}, "Married",   "Employee" } )
AADD(aBigArray1, { "Mike", "White", Nil,                      {16, 5, 22}, "Customer"} )
AADD(aBigArray1, "Customer" )

SET FONT TO "Arial", 10
SET DATE BRITISH
SET CONSOLE OFF

DEFINE WINDOW Win_1 ; 
    AT 0,0 ;
    WIDTH  704 ;
    HEIGHT 480 ;
    TITLE "Array Test Three" ;
    MAIN

    DEFINE BUTTON btn_01
        ROW nlblRow * 3
        COL nlblCol
        CAPTION "Show Array"
        AUTOSIZE .T.
        ONCLICK ShowArray(aBigArray1)
    END BUTTON


END WINDOW

CENTER WINDOW Win_1
ACTIVATE WINDOW Win_1
    
Return Nil    /* -- End of FUNCTION Main() -- */

FUNCTION ShowArray(aArray)

LOCAL nRow := 1
LOCAL nCol := 2
LOCAL cText := ""

SetMode(75, 100)
CLS

@ ++nRow, nCol SAY "Number of elements in this array: " + ALLTRIM(STR(LEN(aArray)))
++nRow

ShowElement(aArray, @nRow, nCol)

RETURN Nil  /* -- End of FUNCTION ShowArray() -- */

FUNCTION ShowElement(aArray, nRow, nCol)

LOCAL i := 0

FOR i := 1 TO LEN(aArray)

    IF VALTYPE(aArray[i]) == "U"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - * NIL *"
        ELSEIF VALTYPE(aArray[i]) == "B"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - * CODE BLOCK *"
        ELSEIF VALTYPE(aArray[i]) == "C"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'CHARACTER' - value: " + aArray[i]
        ELSEIF VALTYPE(aArray[i]) == "N"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'NUMERIC' - value: " + STR(aArray[i])
        ELSEIF VALTYPE(aArray[i]) == "L"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'LOGICAL' - value: " + IIF(aArray[i], "True", "False")
        ELSEIF VALTYPE(aArray[i]) == "D"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'DATE'  - value: " + DTOC(aArray[i])
        ELSE
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'ARRAY' "
            ShowElement(aArray[i], @nRow, nCol + 5)
    ENDIF
    
NEXT i

Return Nil  /* -- End of FUNCTION ShowElement() -- */

/* -- End of file -- */

Re: Hide / Show a Console?

Posted: Sat Oct 01, 2016 2:47 pm
by Zimbo
This is how it looks with the code after I clicked "Show Array", by the way!


Re: Hide / Show a Console?

Posted: Sat Oct 01, 2016 3:52 pm
by Rathinagiri
Why don't you use a GRID for this?

Re: Hide / Show a Console?

Posted: Sat Oct 01, 2016 4:19 pm
by KDJ
Zimbo

To show/hide a console window you can use ShowWindow() and HideWindow() functions.

Code: Select all

#include "hmg.ch"
REQUEST HB_GT_WIN_DEFAULT

FUNCTION Main()

/*
    Sample program for Chapter 11 - Advanced Data Types
    This sample creates a multi-dimensional array which contains several different data types.
    Users can add more elements and / or change values as the learn about arrays. When rthe click
    on the "Show Array" button, the entire contents of the array, including all nested arrays, will
    be displayed on a console screen showing the element number, data type and value in
    that element.
   
*/

LOCAL nlblRow  :=  16
LOCAL nlblCol  :=  16

// Create a memory variable that will be an array
LOCAL aBigArray1 := {  }


LOCAL nHConsole := GetForegroundWindow()

IF (nHConsole != 0)
  IF (GetClassName(nHConsole) == 'ConsoleWindowClass')
    HideWindow(nHConsole)
  ELSE
    nHConsole := 0
  ENDIF
ENDIF


/*
    Add the first two elements using the AADD array function. These are both arrays and each contains 5 elements
*/
AADD(aBigArray1, {.T., "Smith", Nil, "Married", "Customer"} )
AADD(aBigArray1, {".F.", "Jones", Nil, DATE(),  "Employee"} )

/*
    Add the next two elements using the AADD array function. The first one is an array that also contains another array
    at element position 3. The second one is also an array that also contains another array but this time at element
    position 4
*/
AADD(aBigArray1, { "Paul", "Brown", {"Mary", "Bob", "Billy"}, "Married",   "Employee" } )
AADD(aBigArray1, { "Mike", "White", Nil,                      {16, 5, 22}, "Customer"} )
AADD(aBigArray1, "Customer" )

SET FONT TO "Arial", 10
SET DATE BRITISH
//SET CONSOLE OFF

DEFINE WINDOW Win_1 ;
    AT 0,0 ;
    WIDTH  704 ;
    HEIGHT 480 ;
    TITLE "Array Test Three" ;
    MAIN

    DEFINE BUTTON btn_01
        ROW nlblRow * 3
        COL nlblCol
        CAPTION "Show Array"
        AUTOSIZE .T.
        ONCLICK (ShowWindow(nHConsole), ShowArray(aBigArray1))
    END BUTTON
END WINDOW

CENTER WINDOW Win_1
ACTIVATE WINDOW Win_1
   
Return Nil    /* -- End of FUNCTION Main() -- */

FUNCTION ShowArray(aArray)

LOCAL nRow := 1
LOCAL nCol := 2
LOCAL cText := ""

SetMode(75, 100)
CLS

@ ++nRow, nCol SAY "Number of elements in this array: " + ALLTRIM(STR(LEN(aArray)))
++nRow

ShowElement(aArray, @nRow, nCol)

RETURN Nil  /* -- End of FUNCTION ShowArray() -- */

FUNCTION ShowElement(aArray, nRow, nCol)

LOCAL i := 0

FOR i := 1 TO LEN(aArray)

    IF VALTYPE(aArray[i]) == "U"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - * NIL *"
        ELSEIF VALTYPE(aArray[i]) == "B"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - * CODE BLOCK *"
        ELSEIF VALTYPE(aArray[i]) == "C"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'CHARACTER' - value: " + aArray[i]
        ELSEIF VALTYPE(aArray[i]) == "N"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'NUMERIC' - value: " + STR(aArray[i])
        ELSEIF VALTYPE(aArray[i]) == "L"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'LOGICAL' - value: " + IIF(aArray[i], "True", "False")
        ELSEIF VALTYPE(aArray[i]) == "D"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'DATE'  - value: " + DTOC(aArray[i])
        ELSE
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'ARRAY' "
            ShowElement(aArray[i], @nRow, nCol + 5)
    ENDIF
   
NEXT i

Return Nil  /* -- End of FUNCTION ShowElement() -- */

/* -- End of file -- */

Re: Hide / Show a Console?

Posted: Sun Oct 02, 2016 10:43 am
by Zimbo
Brilliant, KDJ. That works perfectly for the purpose I intended!

Rathinagiri, I have avoided GRID on this occasion as the program is part of my guide and I am trying to keep it as simple as possible at this stage. I do intend to cover GRID but at a much later on in the guide!

Zim

Re: Hide / Show a Console?

Posted: Tue Oct 04, 2016 10:01 am
by chrisjx2002
Very interesting example!

How to close the console window and go back to the main window?

Re: Hide / Show a Console?

Posted: Tue Oct 04, 2016 2:35 pm
by serge_girard
Zimbo:

ON RELEASE Form_xxx.Show


Serge

Re: Hide / Show a Console?

Posted: Tue Oct 04, 2016 3:02 pm
by chrisjx2002
I try ON KEY ESCAPE ACTION Win_1.show but it does not work in console mode

Re: Hide / Show a Console?

Posted: Tue Oct 04, 2016 6:40 pm
by KDJ
chrisjx2002 wrote: How to close the console window and go back to the main window?
Example:

Code: Select all

#include "hmg.ch"
REQUEST HB_GT_WIN_DEFAULT

STATIC nHConsole

FUNCTION Main()

/*
    Sample program for Chapter 11 - Advanced Data Types
    This sample creates a multi-dimensional array which contains several different data types.
    Users can add more elements and / or change values as the learn about arrays. When rthe click
    on the "Show Array" button, the entire contents of the array, including all nested arrays, will
    be displayed on a console screen showing the element number, data type and value in
    that element.
   
*/

LOCAL nlblRow  :=  16
LOCAL nlblCol  :=  16

// Create a memory variable that will be an array
LOCAL aBigArray1 := {  }


nHConsole := GetForegroundWindow()

IF (nHConsole != 0)
  IF (GetClassName(nHConsole) == "ConsoleWindowClass")
    HideWindow(nHConsole)
  ELSE
    nHConsole := 0
  ENDIF
ENDIF

IF (nHConsole == 0)
  MsgBox("Console window not found!")
  QUIT
ENDIF

/*
    Add the first two elements using the AADD array function. These are both arrays and each contains 5 elements
*/
AADD(aBigArray1, {.T., "Smith", Nil, "Married", "Customer"} )
AADD(aBigArray1, {".F.", "Jones", Nil, DATE(),  "Employee"} )

/*
    Add the next two elements using the AADD array function. The first one is an array that also contains another array
    at element position 3. The second one is also an array that also contains another array but this time at element
    position 4
*/
AADD(aBigArray1, { "Paul", "Brown", {"Mary", "Bob", "Billy"}, "Married",   "Employee" } )
AADD(aBigArray1, { "Mike", "White", Nil,                      {16, 5, 22}, "Customer"} )
AADD(aBigArray1, "Customer" )

SET FONT TO "Arial", 10
SET DATE BRITISH
//SET CONSOLE OFF

DEFINE WINDOW Win_1 ;
    AT 0,0 ;
    WIDTH  704 ;
    HEIGHT 480 ;
    TITLE "Array Test Three" ;
    MAIN

    DEFINE BUTTON btn_01
        ROW nlblRow * 3
        COL nlblCol
        WIDTH 120
        CAPTION "Show Array"
        ONCLICK ShowArray(aBigArray1)
    END BUTTON

    DEFINE BUTTON btn_02
        ROW nlblRow * 5
        COL nlblCol
        WIDTH 120
        CAPTION "Show/hide console"
        ONCLICK ShowHideConsole()
    END BUTTON
END WINDOW

IF (nHConsole == 0)
  Win_1.btn_02.ENABLED := .F.
ENDIF

CENTER WINDOW Win_1
ACTIVATE WINDOW Win_1
   
Return Nil    /* -- End of FUNCTION Main() -- */

FUNCTION ShowHideConsole()

  IF IsWindowVisible(nHConsole)
    HideWindow(nHConsole)
  ELSE
    ShowWindow(nHConsole)
  ENDIF

  Win_1.SETFOCUS

RETURN NIL

FUNCTION ShowArray(aArray)

LOCAL nRow := 1
LOCAL nCol := 2
LOCAL cText := ""

IF ! IsWindowVisible(nHConsole)
  ShowWindow(nHConsole)
ENDIF

SetMode(75, 100)
CLS

@ ++nRow, nCol SAY "Number of elements in this array: " + ALLTRIM(STR(LEN(aArray)))
++nRow

ShowElement(aArray, @nRow, nCol)

RETURN Nil  /* -- End of FUNCTION ShowArray() -- */

FUNCTION ShowElement(aArray, nRow, nCol)

LOCAL i := 0

FOR i := 1 TO LEN(aArray)

    IF VALTYPE(aArray[i]) == "U"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - * NIL *"
        ELSEIF VALTYPE(aArray[i]) == "B"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - * CODE BLOCK *"
        ELSEIF VALTYPE(aArray[i]) == "C"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'CHARACTER' - value: " + aArray[i]
        ELSEIF VALTYPE(aArray[i]) == "N"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'NUMERIC' - value: " + STR(aArray[i])
        ELSEIF VALTYPE(aArray[i]) == "L"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'LOGICAL' - value: " + IIF(aArray[i], "True", "False")
        ELSEIF VALTYPE(aArray[i]) == "D"
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'DATE'  - value: " + DTOC(aArray[i])
        ELSE
            @ ++nRow, nCol SAY "-> Element " + ALLTRIM(STR(i)) + " - 'ARRAY' "
            ShowElement(aArray[i], @nRow, nCol + 5)
    ENDIF
   
NEXT i

Return Nil  /* -- End of FUNCTION ShowElement() -- */

/* -- End of file -- */

Re: Hide / Show a Console?

Posted: Wed Oct 05, 2016 4:29 pm
by chrisjx2002
Thanks for this example.

When I close the console window(click on the X), it closes the main window too. How to prevent that?