The following contains screens for general full screen PBCC applications.
Graphic screens have built-in support for keyboard and mouse.
As an extra, a function for mouse and keyboard support for an arbitrary graphic window has been included.
The code below is divided in two sections : an include file "PBScreen.inc", and a small program to demonstrate the various functions.
------------------------------------------------------------
Revision march 19, 2008 : Graphic Font style parameter corrected, with thanks to Chris Holbrook
Revision march 21, 2008 : #If Not %Def (%ExampleHandler).. block moved out of primary #If / #EndIf block
Revision march 31, 2008 : Mouse and keyboard support for arbitrary graphic window added
Presentation changed : Include file and demonstration program now presented separately
------------------------------------------------------------
Include file "PBScreen.inc"
Demonstration program for PBScreen functions
Graphic screens have built-in support for keyboard and mouse.
As an extra, a function for mouse and keyboard support for an arbitrary graphic window has been included.
The code below is divided in two sections : an include file "PBScreen.inc", and a small program to demonstrate the various functions.
------------------------------------------------------------
Revision march 19, 2008 : Graphic Font style parameter corrected, with thanks to Chris Holbrook
Revision march 21, 2008 : #If Not %Def (%ExampleHandler).. block moved out of primary #If / #EndIf block
Revision march 31, 2008 : Mouse and keyboard support for arbitrary graphic window added
Presentation changed : Include file and demonstration program now presented separately
------------------------------------------------------------
Include file "PBScreen.inc"
Code:
' ------------------------------------------------------------------------------- ' PBScreen.inc - screens for general keyboard based PBCC applications ' ' Arie Verheul ' ------------------------------------------------------------------------------- ' ' This file contains a set of screens for general keyboard based PBCC applications. ' Except for the arbitrary graphic window, the screens fill the whole desktop, ' taking into account the presence or absence of a system tray. ' ' The set includes the following : ' ' ------------------------------------------------------------------------------- ' a. Full screen console window ' ------------------------------------------------------------------------------- ' ' ' Function ConsoleScreen () As Dword ' ' ' The function takes no arguments and returns the console handle. ' ' ------------------------------------------------------------------------------- ' b. Split screen ' ------------------------------------------------------------------------------- ' ' The screen consists of a console window of the specified size, either along the ' full width of the bottom of the screen, or to the left of the screen across the ' full height, while the remainder of the screen is filled with a graphic window. ' The graphic screen has built-in Left and Right-click mouse support. ' ' ' Function SplitScreen ( Dimension As Long, ByVal RowsOrCols As Asciiz*2 ) As Dword ' ' ' The function returns the handle of the graphic window. ' ' Arguments : ' ' [Dimension] : the desired size of the console window. ' ' Windows may refuse very small console windows. ' If this is the case, or if the specified value ' is otherwise invalid, the value is ignored. ' ' [RowsOrCols] : valid values include "Rows", "Cols" or "Columns" ' (only the first character is processed) ' ' The routine directs the focus to the console, and it is recommended that the ' program does the same, to avoid improper closure of the screen. ' ' ------------------------------------------------------------------------------- ' c. Full screen graphic window ' ------------------------------------------------------------------------------- ' ' Installs a full screen graphic window, with or without title bar, and with ' built-in keyboard and Left and Right-click mouse support. ' ' ' Function GraphicScreen (Caption As String) As Dword ' ' ' The function returns the graphic handle ' ' Argument : [Caption] ' ' If a title is specified, a window with titlebar is installed, if [Caption] ' is an empty string the titlebar is omitted, exactly like in Powerbasic. ' ' The console becomes redundant and is turned off with the FreeConsole function. ' If needed again at a later stage, it has to be turned on again with AllocConsole. ' ' The graphic window with titlebar may be closed either with the Close-button or ' with Alt-F4, the window without titlebar may be closed with Alt-F4 or with means ' provided by the main program. ' ' To terminate the program after the window has been closed, a global variable ' [WindowClosed] is provided, which receives the value -1 at window closure, ' and should be processed in a suitable way. ' ' ------------------------------------------------------------------------------- ' d. Arbitrary graphic window with mouse and keyboard support ' ------------------------------------------------------------------------------- ' ' Installs an arbitrary graphic window, with or without title bar, and with ' built-in keyboard and Left and Right-click mouse support. ' ' ' Function GraphicWindow (Caption As String, Xo As Long, Yo As Long,_ ' WindowWidth As Long, WindowHeight As Long) As Dword ' ' ' The parameterlist is identical to that of the PBCC function, and the resulting ' graphic window is exactly the same, but with added built-in support for mouse ' and keyboard. Also supported is the global variable [WindowClosed], to terminate ' the program after the window has been closed. ' The console is turned off with the Windows function FreeConsole. ' ' ------------------------------------------------------------------------------- ' Mouse support ' ------------------------------------------------------------------------------- ' ' The Splitscreen and GraphicScreen graphic windows have Left-click and ' Right-click mouse support. ' ' For this purpose the global DWord variables [LMouse_HA] and [RMouse_HA] are ' provided. The screen calls with a Call DWord procedure the handlers whose ' addresses are placed in these variables. The handlers must take as argument ' one DWord variable that holds the mouse coordinates as follows: ' ' x = Hi(Word,lparam) ' y = Lo(Word,lparam) ' ' If [LMouse_HA] and/or [RMouse_HA] are set to, or remain zero, the mouse support ' for the respective button(s) is in effect turned off. ' ' Other types of mouse support may be added, if desired, by changing the Select Case ' block accordingly (see also list of Windows equates). Mouse support for the console ' window may be obtained with the regular means provided by PowerBasic compiler. ' ' ------------------------------------------------------------------------------- ' Keyboard support ' ------------------------------------------------------------------------------- ' ' The GraphicScreen routine features keyboard support in a similar way. ' Two global DWord variables [Char_HA] and [Key_HA] are provided. ' If the address of an appropriate handler routine is placed in these variables, ' the handler will be called if a key in the specified class is pressed. ' ' Support for two classes of keys is provided : ' ' [Char_HA] is associated with all text characters + Enter, Escape, Tab, Backspace ' ' [Key_HA] is associated with a list of auxiliar keys, see list of equates below. ' ' If [Char_HA] and/or [Key_HA] are set to, or remain zero, the keyboard support ' for the respective class of keys is in effect turned off. ' ' ------------------------------------------------------------------------------- ' Avoiding redundant code ' ------------------------------------------------------------------------------- ' ' To compile only relevant code, the following equates may be defined : ' ' %ConsoleScreen = 1 ' %Splitscreen = 1 ' %GraphicScreen = 1 ' ' It is sufficient to define just the equate for the desired section. ' The equate(s) should be placed above the include call. ' Omitting the equates does not affect the functionality of the routines, ' but may cause up to 17.5 kBytes of redundant code to be compiled. ' ' The include file contains two example handlers : ExampleMouseHandler ' and ExampleCharHandler, which will only be included if an equate ' ' %ExampleHandler = 1 has been defined. ' ' ------------------------------------------------------------------------------- ' Restoring the console ' ------------------------------------------------------------------------------- ' ' Some screens modify the properties of the console window. ' The Splitscreen and ConsoleScreen functions turn the resize and/or minimize ' buttons off. The graphic screen turns the console completely off. These changes ' remain of course in effect as long as the program is running. Therefore, in the ' case that the same program needs at a later stage a regular console window, a ' new console window must be installed using the FreeConsole and AllocConsole ' functions. The respective Windows declarations are included in this file. ' ' ------------------------------------------------------------------------------- ' Acknowledgement ' ------------------------------------------------------------------------------- ' ' Many features of the described routines were freely borrowed from posts on the ' PBForum, for which i want to thank those who made the contributions. ' ' ------------------------------------------------------------------------------- ' START OF PBScreen CODE ' ------------------------------------------------------------------------------- ' Includes for conditional compilation #If Not (%Def (%ConsoleScreen)) Or (%Def (%SplitScreen)) Or (%Def (%GraphicScreen)) ' Turn everything on if no equates defined %ConsoleScreen = 1 %SplitScreen = 1 %GraphicScreen = 1 #Else ' Set unused equates to zero #If Not %Def (%ConsoleScreen) %ConsoleScreen = 0 #EndIf #If Not %Def (%SplitScreen) %SplitScreen = 0 #EndIf #If Not %Def (%GraphicScreen) %GraphicScreen = 0 #EndIf #EndIf #If Not %Def (%ExampleHandler) ' Include ExampleHandlers only if specified %ExampleHandler = 0 #EndIf ' ------------------------------------------------------------------------------- ' User defined settings, may be edited %PBS_TextColor = &H50FF50 ' Default graphic colors (BGR) %PBS_Background = &H200000 $PBS_Font = "Courier New" ' Default graphic font %PBS_FontSize = 16 ' ------------------------------------------------------------------------------- ' Key code equates to be used in handlers %Key_Bsp = 8 %Key_Tab = 9 %Key_Enter = 13 %Key_Shift = 16 %Key_Ctl = 17 %Key_Break = 19 %Key_CapsLck = 20 %Key_Esc = 27 %Key_PgUp = 33 %Key_PgDwn = 34 %Key_End = 35 %Key_Home = 36 %Key_CsrLeft = 37 %Key_CsrUp = 38 %Key_CsrRight = 39 %Key_CsrDwn = 40 %Key_Ins = 45 %Key_Del = 46 %Key_Menu = 93 %Key_F1 = 112 %Key_F2 = 113 %Key_F3 = 114 %Key_F4 = 115 %Key_F5 = 116 %Key_F6 = 117 %Key_F7 = 118 %Key_F8 = 119 %Key_F9 = 120 %Key_F10 = 121 %Key_F11 = 122 %Key_F12 = 123 ' ------------------------------------------------------------------------------- ' Windows system equates %GW_CHILD = 5 %GWL_WNDPROC = -4 %WM_DESTROY = &H2 %WM_MOUSEMOVE = &H200 %WM_LBUTTONDOWN = &H201 %WM_LBUTTONUP = &H202 %WM_LBUTTONDBLCLK = &H203 %WM_RBUTTONDOWN = &H204 %WM_RBUTTONUP = &H205 %WM_RBUTTONDBLCLK = &H206 %WM_KEYDOWN = &H100 %WM_CHAR = &H102 %SM_CXDLGFRAME = 7 %SM_CYCAPTION = 4 %SC_MINIMIZE = &HF020& %SC_RESTORE = &HF120& ' ------------------------------------------------------------------------------- ' Windows declarations Declare Function AllocConsole Lib "KERNEL32.DLL" Alias "AllocConsole" () As Long Declare Function CallWindowProc Lib "USER32.DLL" Alias "CallWindowProcA" (_ ByVal lpPrevWndFunc As Dword,_ ByVal hWnd As Dword,_ ByVal uMsg As Dword,_ ByVal wParam As Dword,_ ByVal lParam As Long) As Long Declare Function DeleteMenu Lib "USER32.DLL" Alias "DeleteMenu"(_ ByVal hMenu As Dword,_ ByVal nPosition As Long,_ ByVal wFlags As Dword) As Long Declare Function FreeConsole Lib "KERNEL32.DLL" Alias "FreeConsole" () As Long Declare Function GetLargestConsoleWindowSize Lib "KERNEL32.DLL"_ Alias "GetLargestConsoleWindowSize"(_ ByVal hConsoleOutput As Dword) As Dword Declare Function GetSystemMenu Lib "USER32.DLL" Alias "GetSystemMenu"(_ ByVal hWnd As Dword,_ ByVal bRevert As Long) As Long Declare Function GetSystemMetrics Lib "USER32.DLL" Alias "GetSystemMetrics"(_ ByVal nIndex As Long) As Long Declare Function GetWindow Lib "USER32.DLL" Alias "GetWindow" (_ ByVal hWnd As Dword,_ ByVal wCmd As Dword) As Long Declare Function SetWindowLong Lib "USER32.DLL" Alias "SetWindowLongA" (_ ByVal hWnd As Dword,_ ByVal nIndex As Long,_ ByVal lNewLong As Long) As Long Declare Function ShowWindow Lib "USER32.DLL" Alias "ShowWindow" (_ ByVal hWnd As Dword, ByVal nCmdShow As Long) As Long ' ------------------------------------------------------------------------------- #If %ConsoleScreen Function ConsoleScreen () As Dword 'Installs a full screen console window Local WindowSize As Dword Local Rows, Columns As Long Console Set Loc 0,0 WindowSize = GetLargestConsoleWindowSize (GetStdOut) Rows = (Hi(Word, WindowSize)) Columns = (Lo(Word, WindowSize)) Console Set Screen Rows, Columns ShowWindow ConsHndl, 3 DeleteMenu GetSystemMenu(ConsHndl, 0), %SC_Restore, 0 ' Turn resize button off Cursor Off Function = ConsHndl End Function #EndIf ' ------------------------------------------------------------------------------- #If %SplitScreen Function SplitScreen ( Dimension As Long, ByVal RowsOrCols As Asciiz*2 ) As Dword ' Installs a console window + graphic window on the full screen Global OldProc As Long Global LMouse_HA, RMouse_HA As Dword Local hWin, hStatic, ConsoleSize As Dword Local ClientX, ClientY, ClientWidth, ClientHeight As Long Local ConX, ConY, Rows, Cols, ConWidth, ConHeight As Long Local GraphicX, GraphicY, GraphicWidth, GraphicHeight As Long ' Determine Clientsize and location Desktop Get Client To ClientWidth, ClientHeight Desktop Get Loc To ClientX, ClientY ' Determine maximum console size ConsoleSize = GetLargestConsoleWindowSize (GetStdOut) Select Case UCase$ (RowsOrCols) Case "R" ' Graphic screen above console Rows = Dimension Cols = (Lo(Word, ConsoleSize)) ' Contains max. Cols Console Set Screen Rows, Cols ' Set up console window Console Get Size To ConWidth, ConHeight ' Determine size in pixels GraphicWidth = ClientWidth - 2 ' Two pixels border line GraphicHeight = ClientHeight - ConHeight - 1 ConX = ClientX + (ClientWidth - ConWidth) \ 2 ' Distribute the difference ConY = ClientY + GraphicHeight + 1 GraphicX = ClientX GraphicY = ClientY Case "C" ' Graphic screen next to console Rows = (Hi(Word, ConsoleSize)) ' Contains max. Rows Cols = Dimension Console Set Screen Rows, Cols ' Set up console window Console Get Size To ConWidth, ConHeight ' Determine size in pixels GraphicWidth = ClientWidth - ConWidth - 2 GraphicHeight = ClientHeight - 2 ConX = ClientX ConY = ClientY + (ClientHeight - ConHeight) \ 2 ' Distribute the difference GraphicX = ConWidth + ClientX GraphicY = ClientY Case Else : Print Dimension; RowsOrCols; "...What ?" End Select ShowWindow (ConsHndl, 3) Console Set Loc ConX, ConY DeleteMenu GetSystemMenu(ConsHndl, 0), %SC_Minimize, 0 ' Turn mimimize button off DeleteMenu GetSystemMenu(ConsHndl, 0), %SC_Restore, 0 ' Turn resize button off Graphic Window "", GraphicX, GraphicY, GraphicWidth, GraphicHeight To hWin hStatic = GetWindow(hWin, %GW_CHILD) OldProc = SetWindowLong(hStatic, %GWL_WNDPROC, CodePtr (Process_SS_Message)) Graphic Attach hWin, 0 Graphic Color %PBS_TextColor, %PBS_Background Graphic Font $PBS_Font, %PBS_FontSize, 0 Graphic Clear Console Set Focus Cursor Off Function = hWin End Function ' ------------------------------------------------------------------------------- Sub Process_SS_Message( ByVal hWnd As Dword, ByVal wMsg As Dword,_ ByVal wParam As Dword, ByVal lParam As Long) Graphic Attach hWnd,0 Select Case wMsg Case %WM_LBUTTONDOWN If LMouse_HA Then Call Dword LMouse_HA BDecl (lparam) Case %WM_RBUTTONDOWN If RMouse_HA Then Call Dword RMouse_HA BDecl (lparam) End Select Console Set Focus CallWindowProc(OldProc, hWnd, wMsg, wParam, lParam) End Sub #EndIf ' ------------------------------------------------------------------------------- #If %GraphicScreen Function GraphicWindow (Caption As String, Xo As Long, Yo As Long,_ WindowWidth As Long, WindowHeight As Long) As Dword ' Installs an arbitrary graphic window with or without titlebar ' and with support for mouse and keyboard Global OldProc, WindowClosed As Long Global LMouse_HA, RMouse_HA, Char_HA, Key_HA As Dword Local hWin, hStatic As Dword WindowClosed = 0 FreeConsole Graphic Window Caption, Xo, Yo, WindowWidth, WindowHeight To hWin hStatic = GetWindow(hWin, %GW_CHILD) OldProc = SetWindowLong(hStatic, %GWL_WNDPROC, CodePtr(Process_GS_Message)) Graphic Attach hWin, 0 Graphic Set Focus Graphic Color %PBS_TextColor, %PBS_Background Graphic Font $PBS_Font, %PBS_FontSize, 0 Graphic Clear Function = hWin End Function ' ------------------------------------------------------------------------------- Function GraphicScreen (Caption As String) As Dword ' Determines the parameters for a full screen graphic window Local ClientX, ClientY, ClientWidth, ClientHeight As Long Local CaptionHeight, FrameWidth, WindowWidth, WindowHeight As Long Desktop Get Client To ClientWidth, ClientHeight Desktop Get Loc To ClientX, ClientY If Caption = "" Then FrameWidth = 2 CaptionHeight = 0 Else FrameWidth = 2 * GetSystemMetrics (%SM_CXDLGFRAME) CaptionHeight = GetSystemMetrics (%SM_CYCAPTION) End If WindowWidth = ClientWidth - FrameWidth WindowHeight = ClientHeight - FrameWidth - CaptionHeight Function = GraphicWindow (Caption, ClientX, ClientY, WindowWidth, WindowHeight) End Function ' ------------------------------------------------------------------------------- Sub Process_GS_Message( ByVal hWnd As Dword, ByVal wMsg As Dword,_ ByVal wParam As Dword, ByVal lParam As Long) Graphic Attach hWnd,0 Select Case wMsg Case %WM_DESTROY : WindowClosed = -1 ' To be properly processed ' after window has been closed Case %WM_LBUTTONDOWN If LMouse_HA Then Call Dword LMouse_HA BDecl (lparam) Case %WM_RBUTTONDOWN If RMouse_HA Then Call Dword RMouse_HA BDecl (lparam) Case %WM_CHAR If Char_HA Then Call Dword Char_HA BDecl (wparam) Case %WM_KEYDOWN If Key_HA Then Call Dword Key_HA BDecl (wparam) End Select CallWindowProc(OldProc, hWnd, wMsg, wParam, lParam) End Sub #EndIf ' ------------------------------------------------------------------------------- #If %ExampleHandler Sub ExampleMouseHandler (MousePos As Dword) Local x, y As Single x = Lo (Word,MousePos) y = Hi (Word,MousePos) Graphic Line (x - 3, y) - (x + 4, y), %White Graphic Line (x, y - 3) - (x, y + 4), %White Print x, y End Sub ' ------------------------------------------------------------------------------- Sub ExampleCharHandler (Character As Dword) %PBS_TopMargin = 10 %PBS_LeftMargin = 10 Static x, y As Single Local CharWidth, CharHeight As Single Graphic Chr Size To CharWidth, CharHeight x = Max (x, %PBS_LeftMargin) y = Max (y, %PBS_TopMargin) Select Case Character Case %Key_Bsp x = x - CharWidth Graphic Set Pos (x,y) Graphic Print Chr$(32) Case %Key_Enter x = %PBS_LeftMargin y = y + CharHeight Case > 31 Graphic Set Pos (x,y) Graphic Print Chr$(Character); x = x + Charwidth End Select End Sub #EndIf ' ------------------------------------------------------------------------------- ' END OF PBScreen INCLUDE FILE ' -------------------------------------------------------------------------------
Code:
'------------------------------------------------------------------------ ' Demonstration of PBScreen.inc ' ' Arie Verheul '------------------------------------------------------------------------ #Compile Exe #Dim All %ExampleHandler = 1 #Include "PBScreen.inc" '------------------------------------------------------------------------ Sub DoSomething (Capture As String) ' In case no console or graphic window is present ' the respective instructions are ignored. Locate 3,5 Print "Console window" Print Graphic Set Pos (50,200) Graphic Print Capture Graphic Box (350,350)-(500,500),0,%Blue Graphic Box (300,300)-(450,450),0,%Yellow Sleep 5000 End Sub '------------------------------------------------------------------------ Function PBMain () As Long ConsoleScreen Console Name "Full screen console window" DoSomething ("") SplitScreen (30, "Cols") Console Name "Split screen" LMouse_HA = CodePtr (ExampleMouseHandler) ' Install mouse handler RMouse_HA = CodePtr (ExampleMouseHandler) DoSomething ("Graphic window, mouse click is supported") Graphic Window End SplitScreen (10, "Rows") DoSomething ("Graphic window, mouse click is supported") Graphic Window End GraphicWindow "Graphic window with built-in mouse and keyboard support",100,100,800,550 Char_HA = CodePtr (ExampleCharHandler) ' Install keyboard handler DoSomething ("Graphic window with mouse click and keyboard support") Graphic Window End GraphicScreen ("") DoSomething ("Graphic screen without titlebar, keyboard and mouse click are supported") Graphic Window End GraphicScreen ("Graphic screen") DoSomething ("Graphic screen with titlebar, keyboard and mouse click are supported") Graphic Set Pos (50,230) Graphic Print "Close window when done ..." Do : Sleep 1000 : Loop Until WindowClosed End Function '------------------------------------------------------------------------
Comment