Dialog Example does not run

Windows specific questions.
Post Reply
Lothar Schirm
Posts: 430
Joined: Sep 28, 2013 15:08
Location: Germany

Dialog Example does not run

Post by Lothar Schirm »

I use FreeBASIC 1.07.1 64 bit. The following example from the FreeBASIC examples folder gui\win32\dialogres\dialog.bas does not run, I believe.

Code: Select all

'' 
'' Dialog Example, by fsw
'' 
'' compile with:  fbc -s gui dialog.rc dialog.bas 
'' 
'' 
 

#include once "windows.bi" 
#include "dialog.bi"

declare function DlgProc (byval hwnd as HWND, byval umsg as UINT, byval wparam as WPARAM, byval lparam as LPARAM) as BOOL
                                

''' 
''' Program start 
'''      

    '' 
    '' Create the Dialog 
    '' 
    DialogBoxParam( GetModuleHandle( NULL ), cast( LPCSTR, IDD_DLG1 ), NULL, @DlgProc, NULL ) 
        
    '' 
    '' Program has ended 
    '' 
    end 

''' 
''' Program end 
'''      

                                  

function DlgProc (byval hwnd as HWND, byval umsg as UINT, byval wparam as WPARAM, byval lparam as LPARAM) as BOOL
    dim as long id, event
    
    '' 
    '' Process message 
    '' 
    select case uMsg
    case WM_INITDIALOG
        
    '' 
    '' Window was closed 
    '' 
    case WM_CLOSE
    	EndDialog( hwnd, 0 ) 

    case WM_COMMAND
		id    = loword( wParam )
		event = hiword( wParam )

        select case id
        case IDC_BTN1
        	EndDialog( hwnd, 0 )
    	end select
        
    case else
    	return FALSE
    
    end select
    
   return TRUE
end function 
When I compile it from my IDE (Geany), I get the following compiler output:
C:\FreeBASIC\fbc -w pedantic -exx "dialog.bas" (im Verzeichnis: C:\FreeBASIC\examples\GUI\win32\dialogres)
dialog.bas(22) warning 3(1): Passing different pointer types, at parameter 4 of DIALOGBOXPARAM()
Kompilierung erfolgreich beendet
.
So it compiles successfully with a warning, but when I run the compiled code I get no dialog box, the program ends immediately.

When I compile from the command prompt as described at the beginning of the code, dialog.bas is not found:
C:\FreeBASIC>fbc -s gui examples\gui\win32\dialogres\dialog.rc dialog.bas
dialog.bas() error 23: File not found, dialog.bas


Do I make a mistake?
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: Dialog Example does not run

Post by Vortex »

Hello,

Trying the command prompt :

Code: Select all

C:\Users\Vortex>cd C:\FreeBASIC\examples\GUI\win32\dialogres

C:\FreeBASIC\examples\GUI\win32\dialogres>\FreeBASIC\fbc.exe -s gui dialog.bas dialog.rc

C:\FreeBASIC\examples\GUI\win32\dialogres>dir /od
 Volume in drive C is OS
 Volume Serial Number is

 Directory of C:\FreeBASIC\examples\GUI\win32\dialogres

25.12.2020  09:23             1.258 dialog.bas
25.12.2020  09:23               187 dialog.bi
25.12.2020  09:23               228 dialog.rc
15.03.2021  19:43            17.408 dialog.exe
Lothar Schirm
Posts: 430
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Dialog Example does not run

Post by Lothar Schirm »

Strange!

Here is my copy from command prompt:

Code: Select all

C:\FreeBASIC>fbc.exe -s gui  examples\gui\win32\dialogres\dialog.bas dialog.rc
examples\gui\win32\dialogres\dialog.bas(22) warning 3(1): Passing different poin
ter types, at parameter 4 of DIALOGBOXPARAM()

Error!
Could not open source file (dialog.RC)
OBJ file not made
For me it is not a good solution to compile from the command line only because the dialog box uses an external resource file. Is there a possibilty to integrate all three files (dialogs.bas, dialog.bi and dialog.rc) into one file or to write the informations of dialog.rc into the main file (dialog.bas)? Or is it possible to place a pointer to dialog.rc or something similiar into the main file (dialog.bas) so that dialog.bas can be compiled easily from the IDE?
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: Dialog Example does not run

Post by Vortex »

Why not to use a batch file ?

Code: Select all

SET path=%path%;C:\FreeBASIC

pushd %CD%

cd C:\FreeBASIC\examples\GUI\win32\dialogres

fbc.exe -s gui dialog.bas dialog.rc

popd
SARG
Posts: 1753
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Dialog Example does not run

Post by SARG »

@lothar
You should also set the path for the rc file.....
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: Dialog Example does not run

Post by Vortex »

Hi Lothar,

SARG is right. Also, you can copy the content of the include file dialog.bi to dialog.bas and dialog.rc After, you will need to remove the include directives pointing the file include.bi Of course, you could lose the flexebility of the .bi file :

Code: Select all

C:\FreeBASIC>fbc.exe -s gui C:\FreeBASIC\examples\GUI\win32\dialogres\dialog.bas C:\FreeBASIC\examples\GUI\win32\dialogres\dialog.rc
Lothar Schirm
Posts: 430
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Dialog Example does not run

Post by Lothar Schirm »

Thank you, Vortex and Sarg. For the moment, regarding dialog boxes, I do not need much more than the standard Windows messagebox and an inputbox which I built as a normal windows GUI and which can be modified and extended easily. Additionally, I found two simple file dialog boxes by MichaelW viewtopic.php?f=3&t=20302&p=178095&hilit=GUI#p178095.
So I can compile directly from the IDE which is the most comfortable way for me.
Vortex
Posts: 118
Joined: Sep 19, 2005 9:50

Re: Dialog Example does not run

Post by Vortex »

Hi Lothar,

You can use binary resource data to create a compact source file :

Code: Select all

#include once "windows.bi"

Dim As LPDLGTEMPLATE DialogTemplate

Declare Function DlgProc (hwnd As HWND, umsg As UINT, wparam As WPARAM, lparam As LPARAM) As Integer

Asm
    
    .data
    
    RsrcData:
    
    .byte 1,0,255,255,128,0,26,1,0,0,0,0,64,8,202,16
    .byte 0,0,20,0,10,0,200,0,120,0,0,0,0,0,68,0
    .byte 105,0,97,0,108,0,111,0,103,0,32,0,98,0,111,0
    .byte 120,0,0,0,12,0,188,2,0,1,83,0,121,0,115,0
    .byte 116,0,101,0,109,0,0,0
    
    .text
    
    push OFFSET RsrcData
    pop  [DialogTemplate]
    
End Asm

DialogBoxIndirectParam(GetModuleHandle(NULL),DialogTemplate,NULL,@DlgProc,NULL) 

Function DlgProc (hwnd As HWND, umsg As UINT,wparam As WPARAM,lparam As LPARAM) As Integer
    
    Select Case uMsg
    
    Case WM_CLOSE
        EndDialog(hwnd,0) 
        
    Case Else
        Return FALSE
        
    End Select
    
    Return TRUE
    
End Function
Resource script to extract the binary data with Resorce Hacker :

Code: Select all

LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL

10000 DIALOGEX 20, 10, 200, 120, 18481280
STYLE DS_SETFONT | DS_CENTER | WS_MINIMIZEBOX | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_GROUP
CAPTION "Dialog box"
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
FONT 12, "System", FW_BOLD, FALSE, 1
{
}
Lothar Schirm
Posts: 430
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Dialog Example does not run

Post by Lothar Schirm »

Hi Vortex,
thank you again. I think FBEdit would be the best solution to create and use resource files. For the moment, however, I am not interested to use resource files. I use my own template which can be modified or extended easily to create a Windows GUI:

Code: Select all

'===============================================================================
' WinAPI_GUI.bas
' Windows API GUI 
' Vorlage mit Menue, Textbox, Editor, Buttons, Listbox
' Erstellt am 20.02.2021
' Letzte Bearbeitung am 22.02.2021
'===============================================================================

#include once "windows.bi"

Dim Shared As HMENU hMenu, hDatei, hHilfe
Dim Shared As HWND Edit1, Edit2, List1, Button1, Button2


Function WndProc(ByVal hWnd As HWND, ByVal Msg As UINT, ByVal wParam As WPARAM, _
                 ByVal lParam As LPARAM ) As LRESULT
      
  Dim As HFONT Font
  Dim As Integer i
  Dim As ZString*1024 text
  
	Function = 0
	
	Select Case Msg
	
		Case WM_CREATE            
			
			'Menü:
			hMenu = CreateMenu()
			hDatei = CreateMenu()
			hHilfe = CreateMenu()
			InsertMenu(hMenu, 0, MF_POPUP, CInt(hDatei), "Datei")
			InsertMenu(hMenu, 0, MF_POPUP, CInt(hHilfe), "Hilfe")
			AppendMenu(hDatei, 0, 1, "Neu" )
			AppendMenu(hDatei, 0, 2, "Oeffnen" )
			AppendMenu(hDatei, 0, 3, "Speichern" )
			AppendMenu(hDatei, 0, 4, "Beenden" )
			AppendMenu(hHilfe, 0, 5, "?")
			SetMenu(hwnd, hMenu)
			
			'Controls:
  		Var hStatic1 = CreateWindowEx(0, "STATIC", "Geben Sie hier einen Text ein:", _
				WS_VISIBLE Or WS_CHILD, _
				20, 20, 200, 20, hWnd, 0, 0, 0) 
			Edit1 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Text ...", WS_BORDER Or WS_VISIBLE Or WS_CHILD Or ES_AUTOHSCROLL, _
				20, 50, 200, 20, hWnd, 0, 0, 0 )
			Button1 = CreateWindowEx(0, "BUTTON", "Kopieren", WS_VISIBLE Or WS_CHILD, _
				60, 80, 100, 20, hWnd, 0, 0, 0 )
			Edit2 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Bitte Text eingeben!", _
				WS_BORDER Or WS_VISIBLE Or WS_CHILD Or WS_HSCROLL Or WS_VSCROLL Or ES_MULTILINE Or ES_WANTRETURN, _
				20, 120, 300, 200, hWnd, 0, 0, 0 )
			Button2 = CreateWindowEx(0, "BUTTON", "Kopieren", WS_VISIBLE Or WS_CHILD, _
				340, 200, 100, 20, hWnd, 0, 0, 0 )
			List1 = CreateWindowEx(WS_EX_CLIENTEDGE, "LISTBOX", "", _
				WS_BORDER Or WS_VISIBLE Or WS_CHILD Or WS_VSCROLL Or LBS_NOTIFY, _
				20, 350, 200, 200, hWnd, 0, 0, 0 )
	
			'Schriftart fuer den Editor:
			Font = CreateFont(0, 0, 0, 0, 0, 0, 0, 0, ANSI_CHARSET, FALSE, FALSE, _
        DEFAULT_QUALITY, DEFAULT_PITCH or FF_ROMAN, "Courier New")
			SendMessage(Edit2, WM_SETFONT, Cast(WPARAM, Font), True)
			SetWindowText(Edit2, "Bitte hier einen Text schreiben!") 
			
			'Listbox befuellen:
			For i = 0 To 20
				text = "Eintrag Nr. " + Str(i)
				SendMessage(List1, LB_ADDSTRING, 0, Cast(LPARAM, @text))
			Next
		
		Case WM_COMMAND
		
			Select Case LoWord(wParam)
				'Menü:
				Case 1
					MessageBox(0, "Neue Datei ...", "Datei", 0)
				Case 2
					MessageBox(0, "Oeffnen ...", "Datei", 0)
				Case 3
					MessageBox(0, "Speichern ...", "Datei", 0)
				Case 4
					SendMessage(hWnd, WM_CLOSE, 0, 0)
				Case 5
					MessageBox(0, "Ich kann Ihnen leider nicht helfen!", "Hilfe", 0)
			End Select
				
			Select Case HiWord(wParam)
			
				Case BN_CLICKED
					Select Case lParam
						Case Button1
							'Text aus Edit1 auf die Konsole kopieren:
							GetWindowText(Edit1, text, SizeOf(text))
							text = RTrim(text)
							Print text
						Case Button2
							'Text aus Edit2 auf die Konsole kopieren:
							GetWindowText(Edit2, text, SizeOf(text))
							text = RTrim(text)
							Print text
					End Select
					
				Case LBN_SELCHANGE 
					If lParam = List1 Then
						'Gewählten Index mit Text auf Konsole ausgeben
						i = SendMessage(List1, LB_GETCURSEL, 0, 0)
						SendMessage(List1, LB_GETTEXT, i, Cast(LPARAM, @text))
						text = RTrim(text)
						Print i; Space(1); text
					End If
						
			End Select
				
		Case WM_PAINT
		
		Case WM_SIZE            
			
		Case WM_KEYDOWN
			'Beenden mit ESC-Taste: Funktioniert nicht zuverlässig
			If(LoByte(wParam) = 27) Then PostMessage(hWnd, WM_CLOSE, 0, 0)
					
		Case WM_DESTROY
			PostQuitMessage(0)
			Exit Function
			
	End Select
	
	Return DefWindowProc(hWnd, Msg, wParam, lParam)    
    
End Function


Function WinMain(ByVal hInstance As HINSTANCE, ByVal hPrevInstance As HINSTANCE, _
                  ByVal szCmdLine As zstring ptr, ByVal iCmdShow As integer) As integer    
     
	Dim Msg As MSG
	Dim wcls As WNDCLASS     
	Dim hWnd As HWND
	 
	Function = 0
	 
	with wcls
		.style         = CS_HREDRAW or CS_VREDRAW
		.lpfnWndProc   = @WndProc
		.cbClsExtra    = 0
		.cbWndExtra    = 0
		.hInstance     = hInstance
		.hIcon         = LoadIcon(0, IDI_APPLICATION )
		.hCursor       = LoadCursor(0, IDC_ARROW )
		.hbrBackground = Cast(HBRUSH,COLOR_WINDOW)
		.lpszMenuName  = 0
		.lpszClassName = @"MainWindow"
	End with
				
	If(RegisterClass( @wcls) = FALSE ) Then
		 MessageBox(0, "Failed to register wcls", "Error", MB_ICONERROR )
		 Exit Function
	End If
	
	'Fenster:
	hWnd = CreateWindowEx(0, @"MainWindow", "Windows GUI", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, _
		CW_USEDEFAULT, CW_USEDEFAULT, 500, 650, 0, 0, hInstance, 0 )
	
	ShowWindow(hWnd, iCmdShow)
	UpdateWindow(hWnd)
	 
	While( GetMessage(@Msg, 0, 0, 0 ) <> FALSE )    
		TranslateMessage(@Msg )
		DispatchMessage(@Msg )
	Wend
	
	Return Msg.wParam

End Function

WinMain(GetModuleHandle(0), 0, Command(), SW_NORMAL)

End
In order to build a GUI, I copy the code into a new file, delete what I do not need and add what I need - easy to do!
dodicat
Posts: 7967
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Dialog Example does not run

Post by dodicat »

fbide is really easy to use exernal files, .o, .rc . . .
In view/settings/freebasic
at compiler command
"<$fbc>" "<$file>" = the default
So
"<$fbc>" myresource.rc "<$file>"
would load a resource file in the same folder as a source code file, which you would get in the usual way e.g.
file
open
Nice template Lothar Schirm.
RNBW
Posts: 264
Joined: Apr 11, 2015 11:06
Location: UK

Re: Dialog Example does not run

Post by RNBW »

Lothar Schirm wrote:Hi Vortex,
thank you again. I think FBEdit would be the best solution to create and use resource files. For the moment, however, I am not interested to use resource files. I use my own template which can be modified or extended easily to create a Windows GUI:
...
...
In order to build a GUI, I copy the code into a new file, delete what I do not need and add what I need - easy to do!
Lothar's Template (very good idea) but with English translation:

Code: Select all

'===============================================================================
' WinAPI_GUI.bas
' Windows API GUI
'  by Lothar Schirm
' Template with menu, text box, editor, buttons, list box 
' Created on 02/20/2021 
' Last update on 02/22/2021 
'===============================================================================

#include once "windows.bi"

Dim Shared As HMENU hMenu, hDatei, hHilfe
Dim Shared As HWND Edit1, Edit2, List1, Button1, Button2


Function WndProc(ByVal hWnd As HWND, ByVal Msg As UINT, ByVal wParam As WPARAM, _
                 ByVal lParam As LPARAM ) As LRESULT
     
  Dim As HFONT Font
  Dim As Integer i
  Dim As ZString*1024 text
 
   Function = 0
   
   Select Case Msg
   
      Case WM_CREATE           
         
         'Menu: 
         hMenu = CreateMenu()
         hDatei = CreateMenu()
         hHilfe = CreateMenu()
         InsertMenu(hMenu, 0, MF_POPUP, CInt(hDatei), "File")
         InsertMenu(hMenu, 0, MF_POPUP, CInt(hHilfe), "Help")
         AppendMenu(hDatei, 0, 1, "New" )
         AppendMenu(hDatei, 0, 2, "Open" )
         AppendMenu(hDatei, 0, 3, "Save" )
         AppendMenu(hDatei, 0, 4, "Quit" )
         AppendMenu(hHilfe, 0, 5, "?")
         SetMenu(hwnd, hMenu)
         
         'Controls:
        Var hStatic1 = CreateWindowEx(0, "STATIC", "Enter a text here :", _
            WS_VISIBLE Or WS_CHILD, _
            20, 20, 200, 20, hWnd, 0, 0, 0)
         Edit1 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Text ...", WS_BORDER Or WS_VISIBLE Or WS_CHILD Or ES_AUTOHSCROLL, _
            20, 50, 200, 20, hWnd, 0, 0, 0 )
         Button1 = CreateWindowEx(0, "BUTTON", "Copy", WS_VISIBLE Or WS_CHILD, _
            60, 80, 100, 20, hWnd, 0, 0, 0 )
         Edit2 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Please enter text! ", _
            WS_BORDER Or WS_VISIBLE Or WS_CHILD Or WS_HSCROLL Or WS_VSCROLL Or ES_MULTILINE Or ES_WANTRETURN, _
            20, 120, 300, 200, hWnd, 0, 0, 0 )
         Button2 = CreateWindowEx(0, "BUTTON", "Copy", WS_VISIBLE Or WS_CHILD, _
            340, 200, 100, 20, hWnd, 0, 0, 0 )
         List1 = CreateWindowEx(WS_EX_CLIENTEDGE, "LISTBOX", "", _
            WS_BORDER Or WS_VISIBLE Or WS_CHILD Or WS_VSCROLL Or LBS_NOTIFY, _
            20, 350, 200, 200, hWnd, 0, 0, 0 )
   
         'Editor Font
         Font = CreateFont(0, 0, 0, 0, 0, 0, 0, 0, ANSI_CHARSET, FALSE, FALSE, _
        DEFAULT_QUALITY, DEFAULT_PITCH or FF_ROMAN, "Courier New")
         SendMessage(Edit2, WM_SETFONT, Cast(WPARAM, Font), True)
         SetWindowText(Edit2, "Please write a text here! ")
         
         'Fill Listbox
         For i = 0 To 20
            text = "Item Nr. " + Str(i)
            SendMessage(List1, LB_ADDSTRING, 0, Cast(LPARAM, @text))
         Next
      
      Case WM_COMMAND
      
         Select Case LoWord(wParam)
            'Menü:
            Case 1
               MessageBox(0, "New File ...", "File", 0)
            Case 2
               MessageBox(0, "Open ...", "File", 0)
            Case 3
               MessageBox(0, "Save ...", "File", 0)
            Case 4
               SendMessage(hWnd, WM_CLOSE, 0, 0)
            Case 5
               MessageBox(0, "Unfortunately I cannot help you! ", "help", 0)
         End Select
            
         Select Case HiWord(wParam)
         
            Case BN_CLICKED
               Select Case lParam
                  Case Button1
                     'Copy text from Edit1 to the console: 
                     GetWindowText(Edit1, text, SizeOf(text))
                     text = RTrim(text)
                     Print text
                  Case Button2
                     'Copy text from Edit2 to the console: 
                     GetWindowText(Edit2, text, SizeOf(text))
                     text = RTrim(text)
                     Print text
               End Select
               
            Case LBN_SELCHANGE
               If lParam = List1 Then
                  'Output the selected index with text on the console 
                  i = SendMessage(List1, LB_GETCURSEL, 0, 0)
                  SendMessage(List1, LB_GETTEXT, i, Cast(LPARAM, @text))
                  text = RTrim(text)
                  Print i; Space(1); text
               End If
                  
         End Select
            
      Case WM_PAINT
      
      Case WM_SIZE           
         
      Case WM_KEYDOWN
         'Exit with ESC key: does not work reliably 
         If(LoByte(wParam) = 27) Then PostMessage(hWnd, WM_CLOSE, 0, 0)
               
      Case WM_DESTROY
         PostQuitMessage(0)
         Exit Function
         
   End Select
   
   Return DefWindowProc(hWnd, Msg, wParam, lParam)   
   
End Function


Function WinMain(ByVal hInstance As HINSTANCE, ByVal hPrevInstance As HINSTANCE, _
                  ByVal szCmdLine As zstring ptr, ByVal iCmdShow As integer) As integer   
     
   Dim Msg As MSG
   Dim wcls As WNDCLASS     
   Dim hWnd As HWND
   
   Function = 0
   
   with wcls
      .style         = CS_HREDRAW or CS_VREDRAW
      .lpfnWndProc   = @WndProc
      .cbClsExtra    = 0
      .cbWndExtra    = 0
      .hInstance     = hInstance
      .hIcon         = LoadIcon(0, IDI_APPLICATION )
      .hCursor       = LoadCursor(0, IDC_ARROW )
      .hbrBackground = Cast(HBRUSH,COLOR_WINDOW)
      .lpszMenuName  = 0
      .lpszClassName = @"MainWindow"
   End with
            
   If(RegisterClass( @wcls) = FALSE ) Then
       MessageBox(0, "Failed to register wcls", "Error", MB_ICONERROR )
       Exit Function
   End If
   
   'Window:
   hWnd = CreateWindowEx(0, @"MainWindow", "Windows GUI", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, _
      CW_USEDEFAULT, CW_USEDEFAULT, 500, 650, 0, 0, hInstance, 0 )
   
   ShowWindow(hWnd, iCmdShow)
   UpdateWindow(hWnd)
   
   While( GetMessage(@Msg, 0, 0, 0 ) <> FALSE )   
      TranslateMessage(@Msg )
      DispatchMessage(@Msg )
   Wend
   
   Return Msg.wParam

End Function

WinMain(GetModuleHandle(0), 0, Command(), SW_NORMAL)

End
Lothar Schirm
Posts: 430
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Dialog Example does not run

Post by Lothar Schirm »

dodicat, thank you! But anyway for own applications you will have to write your own resource file, I think that is most easily done with FBEdit. FBEdit does all that work for you with its GUI designer: resource file, .bi file and .bas file. I think other IDEs are not so well usable for working with resource files.

RNBW, thank you for the translation. I should have taken the english version you sent to me recently, sorry.
RNBW
Posts: 264
Joined: Apr 11, 2015 11:06
Location: UK

Re: Dialog Example does not run

Post by RNBW »

Hi Lothar

Hope you are keeping well.

I've just been looking at your WinAPI template and one thing puzzles me. You don't appear to actually register the window class. If you insert "RegisterClass @wcls", then your error checking code brings up the Messagebox "Failed to register wcls". Is the window class automatically registered, or should the statement "RegisterClass @wcls" be included?

I've looked online and I've noticed that a few WinAPI code submissions do include RegisterClass wcls (or whatever thy call the window class), but don't include any error checking that it has been successfully registered. When the error check is included, the Messagebox with the error pops up.

Code: Select all

'===============================================================================
' WinAPI_GUI.bas
' Windows API GUI
' Vorlage mit Menue, Textbox, Editor, Buttons, Listbox
' Erstellt am 20.02.2021
' Letzte Bearbeitung am 22.02.2021
'===============================================================================

#include once "windows.bi"

Dim Shared As HMENU hMenu, hDatei, hHilfe
Dim Shared As HWND Edit1, Edit2, List1, Button1, Button2


Function WndProc(ByVal hWnd As HWND, ByVal Msg As UINT, ByVal wParam As WPARAM, _
                 ByVal lParam As LPARAM ) As LRESULT
     
  Dim As HFONT Font
  Dim As Integer i
  Dim As ZString*1024 text
 
   Function = 0
   
   Select Case Msg
   
      Case WM_CREATE           
         
         'Menü:
         hMenu = CreateMenu()
         hDatei = CreateMenu()
         hHilfe = CreateMenu()
         InsertMenu(hMenu, 0, MF_POPUP, CInt(hDatei), "Datei")
         InsertMenu(hMenu, 0, MF_POPUP, CInt(hHilfe), "Hilfe")
         AppendMenu(hDatei, 0, 1, "Neu" )
         AppendMenu(hDatei, 0, 2, "Oeffnen" )
         AppendMenu(hDatei, 0, 3, "Speichern" )
         AppendMenu(hDatei, 0, 4, "Beenden" )
         AppendMenu(hHilfe, 0, 5, "?")
         SetMenu(hwnd, hMenu)
         
         'Controls:
        Var hStatic1 = CreateWindowEx(0, "STATIC", "Geben Sie hier einen Text ein:", _
            WS_VISIBLE Or WS_CHILD, _
            20, 20, 200, 20, hWnd, 0, 0, 0)
         Edit1 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Text ...", WS_BORDER Or WS_VISIBLE Or WS_CHILD Or ES_AUTOHSCROLL, _
            20, 50, 200, 20, hWnd, 0, 0, 0 )
         Button1 = CreateWindowEx(0, "BUTTON", "Kopieren", WS_VISIBLE Or WS_CHILD, _
            60, 80, 100, 20, hWnd, 0, 0, 0 )
         Edit2 = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Bitte Text eingeben!", _
            WS_BORDER Or WS_VISIBLE Or WS_CHILD Or WS_HSCROLL Or WS_VSCROLL Or ES_MULTILINE Or ES_WANTRETURN, _
            20, 120, 300, 200, hWnd, 0, 0, 0 )
         Button2 = CreateWindowEx(0, "BUTTON", "Kopieren", WS_VISIBLE Or WS_CHILD, _
            340, 200, 100, 20, hWnd, 0, 0, 0 )
         List1 = CreateWindowEx(WS_EX_CLIENTEDGE, "LISTBOX", "", _
            WS_BORDER Or WS_VISIBLE Or WS_CHILD Or WS_VSCROLL Or LBS_NOTIFY, _
            20, 350, 200, 200, hWnd, 0, 0, 0 )
   
         'Schriftart fuer den Editor:
         Font = CreateFont(0, 0, 0, 0, 0, 0, 0, 0, ANSI_CHARSET, FALSE, FALSE, _
        DEFAULT_QUALITY, DEFAULT_PITCH or FF_ROMAN, "Courier New")
         SendMessage(Edit2, WM_SETFONT, Cast(WPARAM, Font), True)
         SetWindowText(Edit2, "Bitte hier einen Text schreiben!")
         
         'Listbox befuellen:
         For i = 0 To 20
            text = "Eintrag Nr. " + Str(i)
            SendMessage(List1, LB_ADDSTRING, 0, Cast(LPARAM, @text))
         Next
      
      Case WM_COMMAND
      
         Select Case LoWord(wParam)
            'Menü:
            Case 1
               MessageBox(0, "Neue Datei ...", "Datei", 0)
            Case 2
               MessageBox(0, "Oeffnen ...", "Datei", 0)
            Case 3
               MessageBox(0, "Speichern ...", "Datei", 0)
            Case 4
               SendMessage(hWnd, WM_CLOSE, 0, 0)
            Case 5
               MessageBox(0, "Ich kann Ihnen leider nicht helfen!", "Hilfe", 0)
         End Select
            
         Select Case HiWord(wParam)
         
            Case BN_CLICKED
               Select Case lParam
                  Case Button1
                     'Text aus Edit1 auf die Konsole kopieren:
                     GetWindowText(Edit1, text, SizeOf(text))
                     text = RTrim(text)
                     Print text
                  Case Button2
                     'Text aus Edit2 auf die Konsole kopieren:
                     GetWindowText(Edit2, text, SizeOf(text))
                     text = RTrim(text)
                     Print text
               End Select
               
            Case LBN_SELCHANGE
               If lParam = List1 Then
                  'Gewählten Index mit Text auf Konsole ausgeben
                  i = SendMessage(List1, LB_GETCURSEL, 0, 0)
                  SendMessage(List1, LB_GETTEXT, i, Cast(LPARAM, @text))
                  text = RTrim(text)
                  Print i; Space(1); text
               End If
                  
         End Select
            
      Case WM_PAINT
      
      Case WM_SIZE           
         
      Case WM_KEYDOWN
         'Beenden mit ESC-Taste: Funktioniert nicht zuverlässig
         If(LoByte(wParam) = 27) Then PostMessage(hWnd, WM_CLOSE, 0, 0)
               
      Case WM_DESTROY
         PostQuitMessage(0)
         Exit Function
         
   End Select
   
   Return DefWindowProc(hWnd, Msg, wParam, lParam)   
   
End Function


Function WinMain(ByVal hInstance As HINSTANCE, ByVal hPrevInstance As HINSTANCE, _
                  ByVal szCmdLine As zstring ptr, ByVal iCmdShow As integer) As integer   
     
   Dim Msg As MSG
   Dim wcls As WNDCLASS     
   Dim hWnd As HWND
   
   Function = 0
   
   with wcls
      .style         = CS_HREDRAW or CS_VREDRAW
      .lpfnWndProc   = @WndProc
      .cbClsExtra    = 0
      .cbWndExtra    = 0
      .hInstance     = hInstance
      .hIcon         = LoadIcon(0, IDI_APPLICATION )
      .hCursor       = LoadCursor(0, IDC_ARROW )
      .hbrBackground = Cast(HBRUSH,COLOR_WINDOW)
      .lpszMenuName  = 0
      .lpszClassName = @"MainWindow"
   End With
   
   RegisterClass @wcls  
            
   If(RegisterClass( @wcls) = FALSE ) Then
       MessageBox(0, "Failed to register wcls", "Error", MB_ICONERROR )
       Exit Function
   End If
   
   'Fenster:
   hWnd = CreateWindowEx(0, @"MainWindow", "Windows GUI", WS_OVERLAPPEDWINDOW Or WS_VISIBLE, _
      CW_USEDEFAULT, CW_USEDEFAULT, 500, 650, 0, 0, hInstance, 0 )
   
   ShowWindow(hWnd, iCmdShow)
   UpdateWindow(hWnd)
   
   While( GetMessage(@Msg, 0, 0, 0 ) <> FALSE )   
      TranslateMessage(@Msg )
      DispatchMessage(@Msg )
   Wend
   
   Return Msg.wParam

End Function

WinMain(GetModuleHandle(0), 0, Command(), SW_NORMAL)

End
Run the code and you'll see what I mean.
Lothar Schirm
Posts: 430
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Dialog Example does not run

Post by Lothar Schirm »

Hi RNBW,

I hope you are well also. I found the mistake. "RegisterClass" is used twice:

Code: Select all

   
   RegisterClass @wcls	'<--- this should be deleted 
           
   If(RegisterClass( @wcls) = FALSE ) Then
       MessageBox(0, "Failed to register wcls", "Error", MB_ICONERROR )
       Exit Function
   End If
In my own version I use only "RegisterClass" with error checking. So delete one of them. See also the codes from 17 March above.
RNBW
Posts: 264
Joined: Apr 11, 2015 11:06
Location: UK

Re: Dialog Example does not run

Post by RNBW »

Hi Lothar

Thanks for replying. I was looking at the clause in your code was just error checking, whereas it is registering the windows class.

Ray
Post Reply