Date: 02-16-2022
Return to Index
created by gbSnippets
'Programmers often need to check to see which messages are received by their
'application, where the message text is provided, not just the message number.
'Adding controls to the application is sometimes undesirable, so this approach
'opens a console Window and lists incoming messages there.
'Note: this requires the use of the WinMsg.dll (part of PowerBASIC distribution)
'Primary Code:
'This code is typically called from within a Callback function to list
'the various message the callback function receives.
'Credit: Semen Matusovski
Static iMsgCount As Long
CPrint Str$(iMsgCount&)+ " " + WinMsg(CB.Msg) 'Console message code
Incr iMsgCount&
'And the function it calls is this:
Sub CPrint (SOut As String)
Static hConsole As Long, cWritten As Long
If hConsole = 0 Then AllocConsole: hConsole = GetStdHandle(-11&)
WriteConsole hConsole, ByCopy sOut + $CrLf, Len(sOut) + 2, cWritten, ByVal 0&
End Sub
'And this declaration is needed:
Declare Function WinMsg LIB "WINMSG.DLL" ALIAS "WindowMessageA" (ByVal MsgNum As Long) AS String
'To clear the console, use this:
Sub ClearConsole
Local hWrittenChars As Long
Local tConsoleInfo As CONSOLE_SCREEN_BUFFER_INFO
GetConsoleScreenBufferInfo(hConsole, tConsoleInfo)
FillConsoleOutputCharacter(hConsole, 32, tConsoleInfo.dwSize.x * tConsoleInfo.dwSize.y, Mak(DWord,0,0), hWrittenChars)
SetConsoleCursorPosition(hConsole, Mak(DWord,0,0))
End Sub
cPrint LCase$(FuncName$)
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc"
Declare Function WinMsg Lib "WINMSG.DLL" Alias "WindowMessageA" (ByVal MsgNum As Long) As String
Global hConsole, hDlg As DWord
Function PBMain () As Long
Dialog New Pixels, 0, "Test",500,550,200,200, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, 200, "Print", 10,10,45,25
Control Add Button, hDlg, 300, "Clear", 60,10,45,25
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Static iCount As Long
If CB.Msg = %WM_Command AND CB.Ctl = 200 Then Incr iCount : Cprint Str$(iCount)
If CB.Msg = %WM_Command AND CB.Ctl = 300 Then ClearConsole
End Function
Sub CPrint (SOut As String)
Static cWritten As Long
If hConsole = 0 Then AllocConsole: hConsole = GetStdHandle(-11&)
WriteConsole hConsole, ByCopy sOut + $CrLf, Len(sOut) + 2, cWritten, ByVal 0&
End Sub
Sub ClearConsole
Local hWrittenChars As Long
Local tConsoleInfo As CONSOLE_SCREEN_BUFFER_INFO
GetConsoleScreenBufferInfo(hConsole, tConsoleInfo)
FillConsoleOutputCharacter(hConsole, 32, tConsoleInfo.dwSize.x * tConsoleInfo.dwSize.y, Mak(DWord,0,0), hWrittenChars)
SetConsoleCursorPosition(hConsole, Mak(DWord,0,0))
End Sub
'gbs_00114
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm