More on closing windows

New to FreeBASIC? Post your questions here.
Carlos Herrera
Posts: 62
Joined: Nov 28, 2011 13:29
Location: Dictatorship

More on closing windows

Postby Carlos Herrera » Aug 20, 2018 11:14

Hi All,
I know that we had better open the windows than close, but consider the following code:

Code: Select all

' uses WinGUI(12) by Lothar Schirm
#Include "WinGUI.bi"
#Include "fbgfx.bi"
Using FB
Dim As HWND Window_Main, Graphic_Screen, Button_Open, Button_Close
Dim As MSG msg
dim as string driver
Window_Main = Window_New   (100, 100, 300, 300, "This window controls graphics")
Button_Open = Button_New      (50, 100, 60, 20, "Open",, Window_Main)
Button_Close = Button_New      (50, 200, 60, 20, "Close",, Window_Main)
Do
   WaitEvent(Window_Main, msg)
   Select Case msg.hwnd
      Case Button_Open
         If msg.message = WM_LBUTTONDOWN Then
            ScreenRes 320, 200, 32
            Print "Graphics Open"
         End If
      Case Button_Close   
         If msg.message = WM_LBUTTONDOWN Then
            ScreenControl GET_WINDOW_HANDLE, Graphic_Screen
                DestroyWindow(Graphic_Screen)
                Print "Graphics Closed"
         End If      
   End Select
Loop Until Window_Event_Close(Window_Main, msg)
DestroyWindow(Window_Main)
Print "Window Closed"
Sleep
End

The idea was to close a graphic screen by pressing the button, however, compiler reports
"error 98: No matching overloaded function, SCREENCONTROL() in 'ScreenControl GET_WINDOW_HANDLE, Graphic_Screen'".
Probably I have missed something. Any clues appreciated.
Carlos
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: More on closing windows

Postby fxm » Aug 20, 2018 12:09

ScreenControl GET_WINDOW_HANDLE, Graphic_Screen
DestroyWindow(Graphic_Screen)

Screen 0

It was not worth opening a new thread (it was enough to continue on the previous one).
How to close a window:
- Close a GUI API window.
- Close a FBGFX window.
- Close a console window.
dodicat
Posts: 5251
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: More on closing windows

Postby dodicat » Aug 20, 2018 15:05

I'll try.
Although the help file says the handle via screencontrol is an hwnd, it must be a different flavour of hwnd, because it won't work.
The help file says that gfx_null is a bridge between fb screen and api hwnd.
So why not use this instead?
I've tried to simulate Lothar's subs, but I have never used his gui.

Code: Select all

' uses WinGUI(12) by Lothar Schirm
'#Include "WinGUI.bi"
#include "windows.bi"
#Include "fbgfx.bi"
function Window_Event_Close(h as hwnd, msg as msg) as boolean
  if msg.message=273 then return DestroyWindow(h)
    end function

sub WaitEvent(p as any ptr,m as msg)
    GetMessage(@m,0,0,0)
    TranslateMessage(@m)
    DispatchMessage(@m)
end sub

function window_new(x as integer ,y as integer, wdth as integer, hght as integer,message as string) as hwnd
    return CreateWindowEx(NULL,"#32770", message, WS_OVERLAPPEDWINDOW Or WS_VISIBLE, x, y, wdth, hght, NULL, NULL, NULL, NULL)
end function

function button_new(x as integer ,y as integer, wdth as integer, hght as integer,message as string,d as byte=0,win as hwnd) as hwnd
    dim as hwnd btno,btnc
    if message="Open" then
 btno= CreateWindowEx(NULL,"Button", message, WS_VISIBLE Or WS_CHILD, x, y, wdth,hght,win, NULL, NULL, NULL)
 return btno
else
    btnc= CreateWindowEx(NULL,"Button", message, WS_VISIBLE Or WS_CHILD, x, y, wdth,hght,win, NULL, NULL, NULL)
    return btnc
        end if
   end function
   
Using FB
Dim As HWND Window_Main, Graphic_Screen, Button_Open, Button_Close

Dim As MSG msg

dim as string driver
Window_Main = Window_New(100, 100, 300, 300, "This window controls graphics")
Button_Open = Button_New (50, 100, 60, 20, "Open",, Window_Main)
Button_Close =Button_New (50, 200, 60, 20, "Close",, Window_Main)
Do

   WaitEvent(Window_Main,msg)
 
   Select Case msg.hwnd
   Case  Button_Open
         If msg.message = WM_LBUTTONDOWN Then
            ScreenRes 320, 200, 32
            Print "Graphics Open"
         End If
      Case Button_Close
         If msg.message = WM_LBUTTONDOWN Then
             
            ' dim as integer I
            'ScreenControl GET_WINDOW_HANDLE,I
           ' Graphic_Screen=cast(HWND,I)
            'print DestroyWindow(Graphic_Screen) '' print 0, error
              Print "Graphics Closed"
             
            screenres 320,200,32,,GFX_NULL
             
         End If
         
   End Select
 
Loop Until Window_Event_Close(Window_Main, msg)

end
   
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: More on closing windows

Postby fxm » Aug 20, 2018 15:29

GFX_NULL removes the current graphics window, but also creates a new graphic buffer without window.
So, if we are working with a console window in the background, all input / output instructions will be disabled on it, and furthermore, an input instruction will induce a blocked program.
dodicat
Posts: 5251
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: More on closing windows

Postby dodicat » Aug 20, 2018 16:17

Thanks fxm.
In that case perhaps, preserving the console:

Code: Select all

' uses WinGUI(12) by Lothar Schirm
'#Include "WinGUI.bi"
#include "windows.bi"
#Include "fbgfx.bi"
function Window_Event_Close(h as hwnd, msg as msg) as boolean
  if msg.message=273 then return DestroyWindow(h)
    end function

sub WaitEvent(p as any ptr,m as msg)
    GetMessage(@m,0,0,0)
    TranslateMessage(@m)
    DispatchMessage(@m)
end sub

function window_new(x as integer ,y as integer, wdth as integer, hght as integer,message as string) as hwnd
    return CreateWindowEx(NULL,"#32770", message, WS_OVERLAPPEDWINDOW Or WS_VISIBLE, x, y, wdth, hght, NULL, NULL, NULL, NULL)
end function

function button_new(x as integer ,y as integer, wdth as integer, hght as integer,message as string,d as byte=0,win as hwnd) as hwnd
    dim as hwnd btno,btnc
    if message="Open" then
 btno= CreateWindowEx(NULL,"Button", message, WS_VISIBLE Or WS_CHILD, x, y, wdth,hght,win, NULL, NULL, NULL)
 return btno
else
    btnc= CreateWindowEx(NULL,"Button", message, WS_VISIBLE Or WS_CHILD, x, y, wdth,hght,win, NULL, NULL, NULL)
    return btnc
        end if
   end function
   
Using FB
Dim As HWND Window_Main, Graphic_Screen, Button_Open, Button_Close

Dim As MSG msg

dim as string driver
Window_Main = Window_New(100, 100, 300, 300, "This window controls graphics")
Button_Open = Button_New (50, 100, 60, 20, "Open",, Window_Main)
Button_Close =Button_New (50, 200, 60, 20, "Close",, Window_Main)
Do

   WaitEvent(Window_Main,msg)
 
   Select Case msg.hwnd
   Case  Button_Open
         If msg.message = WM_LBUTTONDOWN Then
            ScreenRes 320, 200, 32
            Print "Graphics Open"
         End If
      Case Button_Close
         If msg.message = WM_LBUTTONDOWN Then
             
            ' dim as integer I
            'ScreenControl GET_WINDOW_HANDLE,I
           ' Graphic_Screen=cast(HWND,I)
            'print DestroyWindow(Graphic_Screen) '' print 0, error
             
                SCREEN 0, 0, 0, &h80000000   
            'screenres 320,200,32,,GFX_NULL  'nope
              Print "Graphics Closed"
         End If
         
   End Select
 
Loop Until Window_Event_Close(Window_Main, msg)

end
 
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: More on closing windows

Postby fxm » Aug 20, 2018 16:33

Yes.
So the console window is preserved.

@admin,
Flag "[GFX_]SCREEN_EXIT" (&h80000000) to be added in documentation and in fbgfx.bi?
Flag only significant when used with Screen 0 (closing any graphics window and preserving the console window content):
SCREEN 0, , , &h80000000

See Fbgfx , not erase txt console when closing graphic mode
fxm
Posts: 8424
Joined: Apr 22, 2009 12:46
Location: Paris (suburbs), FRANCE

Re: More on closing windows

Postby fxm » Aug 21, 2018 5:34

Documentation update:
KeyPgScreengraphics → fxm [Added note on using 'Screen 0, , , SCREEN_EXIT' (with SCREEN_EXIT=&h80000000) to close any graphics window while preserving the console window if it exists]
(this capacity has always existed since at least the FreeBASIC 0.12 Beta version)
coderJeff
Site Admin
Posts: 2717
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: More on closing windows

Postby coderJeff » Aug 25, 2018 0:59

This is on my TODO list to investigate more:

Currently, this flag exists only internally, when the EXE is ending to indicate -- closing GFX screen, don't mess with the console.

I tested a little, and seems like should be OK except:
- if VIEW PRINT statement was called before graphics SCREENRES/SCREEN (non-zero), then the graphics screen gets the same VIEW PRINT range, which is wrong. gfxlib2 doesn't store separate contexts for the console and gfx screen.

Looks like we should be able to add the flag for gfxlib2 that works in a reliable way. If added to fbgfx.bi though, should probably should be GFX_SCREEN_EXIT, or GFX_PRESERVE_CONSOLE, or some such thing to be consistent with other constant names.
jj2007
Posts: 895
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: More on closing windows

Postby jj2007 » Aug 25, 2018 8:10

The bigger problem here is that WaitEvent does not process the WM_CLOSE and WM_DESTROY messages. Instead, it relies on ExitProcess to kill the window. Therefore, once you exit the WaitEvent loop, the window remains alive until you exit the process. Wrong behaviour...

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 5 guests