mixConsoleGui on Release

Moderator: Rathinagiri

Post Reply
User avatar
hmgchang
Posts: 238
Joined: Tue Aug 13, 2013 4:46 am
Location: Indonesia
Has thanked: 1 time
Been thanked: 4 times

mixConsoleGui on Release

Post by hmgchang » Sat Jun 02, 2018 7:42 am

Dear Friends,

I tried a mixConsole Gui and DOS console,

1. start with a Main form
2. open a DOS console
3. open two child form, win1 and win2
4. at win1, i click to close both the win2 and win1
5. i expect to be back to the DOS console, but all forms closed

Code: Select all

#include <hmg.ch>

REQUEST HB_GT_WIN_DEFAULT  // allow console text mode

FUNCTION Main()
  DEFINE WINDOW Main ;
    AT 0, 0 ;
    WIDTH 400 HEIGHT 300 ;
    MAIN
    //
    @10, 10 BUTTON btn_RunDos CAPTION "Run DOS" ACTION Main_btn_RunDos_Action()
    //
  END WINDOW
  //
  CENTER WINDOW Main
  ACTIVATE WINDOW Main
  RETURN NIL
  
FUNCTION Main_btn_RunDos_Action()
  LOCAL hWndDos
  // THISWINDOW.HIDE
  IF VALTYPE( hWndDos) <> 'N'
    hWndDos := GetConsoleWindowHandle()
  ENDIF 
  SetForeGroundWindow( hWndDos )
  SetMode( 25, 80)
  MainDos()
  RETURN NIL  

FUNCTION MainDos()
  ? "Console mode"
  ? "Press Enter to Gui..."
  @9, 9 TO 11, 30 
  lDos := .F.
  OpenWin1()
  // OpenWin2()
  
  // @10, 10 GET lDos PUSHBUTTON CAPTION "Open Win1" STATE {|| OpenWin1()}
  // // @10, 30 GET lDos PUSHBUTTON CAPTION "Open Win2" STATE {|| OpenWin2()}
  // READ
  ? "i just want to get back here..."
  wait
  ? "After Enter... want to back to Main form..."
  TerminateProcess()
  DoMethod( "Main", "SetFocus")
  RETURN NIL
  
FUNCTION Win1_OnRelease()
  hWndDos := GetConsoleWindowHandle()
  SetForeGroundWindow( hWndDos )
  RETURN NIL  
  
FUNCTION CloseWin( cWin)
  // msgDebug( cWin, IsWindowDefined( cWin), IsWindowDefined( "Win2 "))
  //
  IF cWin == "Win1" .AND. IsWindowDefined( "Win1")
    DoMethod( cWin, "Release")
  ENDIF
  IF cWin == "Win2" .AND. IsWindowDefined( "Win2")
    DoMethod( cWin, "Release")
  ENDIF
  IF cWin == "All"
    IF IsWindowDefined( "Win2")
      DoMethod( "Win2", "Release")
    ENDIF
    // hWndDos := GetConsoleWindowHandle()
    // SetForeGroundWindow( hWndDos )
    DoMethod( "Win1", "Release")
    // DoEvents()
    // msgDebug( hWndDos)
    // ThisWindow.Release
  ENDIF
  RETURN NIL
  
FUNCTION WinOnInit( cWin)
  IF cWin == "Win1"
    SetProperty( "Win1", "Win1_btnCloseWin2", "Enabled", IsWindowDefined( "Win2"))
  ENDIF
  IF cWin == "Win2"
    SetProperty( "Win2", "Win2_btnCloseWin1", "Enabled", IsWindowDefined( "Win1"))
  ENDIF
  RETURN NIL  
  
FUNCTION OpenWin2()
  DEFINE WINDOW Win2 ;
    AT 10, 420 WIDTH 400 HEIGHT 300 TITLE "Win2" CHILD ON INIT WinOnInit( "Win2")
    @10, 10 BUTTON Win2_btnWin1 CAPTION "Open Win1" ACTION OpenWin1()
    @40, 10 BUTTON Win2_btnCloseWin1 CAPTION "Close Win1" ACTION CloseWin( "Win1")
  END WINDOW
  IF IsWindowDefined( "Win1")
    SetProperty( "Win1", "Win1_btnCloseWin2", "Enabled", IsWindowDefined( "Win2"))
  ENDIF
  ACTIVATE WINDOW Win2
  RETURN NIL  
  
FUNCTION OpenWin1()
  MEMVAR _HMG_SYSDATA
  DEFINE WINDOW Win1 ;
    AT 10, 10 WIDTH 400 HEIGHT 300 TITLE "Win1" CHILD ON INIT WinOnInit( "Win1")
    //
    @10, 10 BUTTON Win1_btnOpenWin2 CAPTION "Open Win2" ACTION OpenWin2()
    @40, 10 BUTTON Win1_btnCloseWin2 CAPTION "Close Win2" ACTION {|| CloseWin( "Win2")}
    @80, 10 LABEL Label_1 WIDTH 300 VALUE "Click OpenWin2..."
    @100, 10 LABEL Label_2 WIDTH 300 VALUE "I want to Click CloseWins button and back to console.. "
    @120, 10 BUTTON Win1_btnCloseAll CAPTION "Close Wins" ACTION {|| CloseWin( "All")}
  END WINDOW
  IF IsWindowDefined( "Win2")
    SetProperty( "Win2", "Win2_btnCloseWin1", "Enabled", IsWindowDefined( "Win1"))
  ENDIF
  ACTIVATE WINDOW Win1
  RETURN NIL  
  
//------------
#pragma BEGINDUMP
// #define WINVER 0x0600 // for Vista
// #define _WIN32_WINNT 0x0600 // for Vista

#include "windows.h"
#include "hbapi.h"

HB_FUNC( GETCONSOLEWINDOWHANDLE )
{
    HWND hwnd;
    AllocConsole();
    hwnd = FindWindowA("ConsoleWindowClass",NULL);
    hb_retnl( (LONG) hwnd );
}

/*
HB_FUNC ( TERMINATEPROCESS )
{
   DWORD ProcessID = HB_ISNUM (1) ? (DWORD) hb_parnl(1) : GetCurrentProcessId();
   UINT  uExitCode = (UINT) hb_parnl (2);
   HANDLE hProcess = OpenProcess ( PROCESS_TERMINATE, FALSE, ProcessID );
   if ( hProcess != NULL )
   {   if ( TerminateProcess (hProcess, uExitCode) == FALSE )
           CloseHandle (hProcess);
   }
}
*/
#pragma ENDDUMP  
Pls advise me on how to do it right

TIA

best rgds,
Chang
Just Hmg It !

Post Reply