Easy Windows API Tutorial

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
JaDogg
Posts: 345
Joined: Apr 13, 2008 12:11
Location: Sri Lanka - Negombo
Contact:

Post by JaDogg »

this is good
square1
Posts: 97
Joined: Nov 12, 2007 2:27

Post by square1 »

Great post.

I am wondering whether there is a way of forcing the text within the edit box to wrap? Tried to look at MSDN, but just got more confused.

edit:

...just found it on MSDN [slapforehead]
If you do not specify ES_AUTOHSCROLL, the control automatically wraps words to the beginning of the next line when necessary.
Cman
Posts: 30
Joined: May 10, 2011 22:01

Post by Cman »

Thank you sir, this has really helped my win32 programming.
Cman
Posts: 30
Joined: May 10, 2011 22:01

Post by Cman »

Ok I also might add this, that you should really explain the code... I had to go through several MSDN searches to find out what the parameters to the functions mean.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Easy Windows API Tutorial

Post by leopardpm »

ok, given the window handle ("hWnd"), is there a way to use FBGX commands on it (line, circle, cls, color, PUT, etc)?

for instance, is there a way to translate the handle into a screen pointer?
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Easy Windows API Tutorial

Post by St_W »

leopardpm wrote:ok, given the window handle ("hWnd"), is there a way to use FBGX commands on it (line, circle, cls, color, PUT, etc)?

for instance, is there a way to translate the handle into a screen pointer?
In general, no, I'd say. Mixing fbgfx (gfxlib2) and win32 graphics is dangerous and should be avoided IMHO, because you bypass all the gfxlib abstractions with win32 which may lead to unexpected/unwanted behaviour.

If you need an interface between fbgfx and win32 you probably should realize that using your own offscreen-buffers (in-memory Bitmaps). IIRC there are examples here on the forums how to convert a FBIMAGE to a BITMAP or the other way round. Of course that procedure is only suitable for non-time-critical tasks.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Easy Windows API Tutorial

Post by caseih »

EDIT. I misread the original question.

Hmm I used to be able to delete posts, but can't do that anymore.
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: Easy Windows API Tutorial

Post by leopardpm »

St_W wrote:
leopardpm wrote:ok, given the window handle ("hWnd"), is there a way to use FBGX commands on it (line, circle, cls, color, PUT, etc)?

for instance, is there a way to translate the handle into a screen pointer?
In general, no, I'd say. Mixing fbgfx (gfxlib2) and win32 graphics is dangerous and should be avoided IMHO, because you bypass all the gfxlib abstractions with win32 which may lead to unexpected/unwanted behaviour.

If you need an interface between fbgfx and win32 you probably should realize that using your own offscreen-buffers (in-memory Bitmaps). IIRC there are examples here on the forums how to convert a FBIMAGE to a BITMAP or the other way round. Of course that procedure is only suitable for non-time-critical tasks.
ok, so how do I get the BITMAP image into the GUI window? This isn't time critical by any means, just would like to add some flavor to my GUI window is all, not animating or a game window.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Easy Windows API Tutorial

Post by caseih »

Which GUI window are you talking about? If it's a Windows native window (not controlled by FB gfxlib) you can use win32 calls to do this. If you mean the FB window that results from using Screen or ScreenRes, you can use bload and put statements. See the example code here: http://www.freebasic.net/wiki/wikka.php ... KeyPgBload
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Easy Windows API Tutorial

Post by Tourist Trap »

Nice tutorial!
leopardpm wrote: ok, so how do I get the BITMAP image into the GUI window? This isn't time critical by any means, just would like to add some flavor to my GUI window is all, not animating or a game window.
As caseih said, changing the background of a window gui box is doable via the API. There is indeed many ways for that. The gdi stuff and so on generally will handle that the most classical way.
Lothar Schirm
Posts: 437
Joined: Sep 28, 2013 15:08
Location: Germany

Re: Easy Windows API Tutorial

Post by Lothar Schirm »

Here is a simple code: a window with a menu, a static text, a single line editbox, a multiline editbox, a listbox and buttons which might be used as a template for simple applications (of course also keeping in mind the other examples of this topic).

First, however, some remarks regarding the menu example on the first page:
1. When comparing this code with the other examples, I was confused at the first time that you use message number 161 to exit the program in this example while you use message number 273 in the other examples. This seemed to by an inconsistence for me which was not explained. I was looking for a way to use the same message to close the window and exit the program in all cases.
2. If you use item ID number 2 in "AppendMenu", wParam = 2 of message number 273 will also open the message box "This is error message!" if you want to close the window in the window system menu. So I think using message number 161 wParam 20 to close the window is not the optimum solution. In the code below I use therefore menu items 101 to 105, see also "menu.bas" in the FreeBASIC examples folder (GUI\win32). Message number 273 is WM_COMMAND. So I suggest the following code:

Code: Select all

#Include "windows.bi"

Dim As MSG msg
Dim As HWND Window_Main, Edit1, Edit2, Button1, Button2, List1
Dim As HFONT Font
Dim As HMENU hMenu, hDatei, hHilfe
Dim As Integer i
Dim As ZString*1024 text 

'Window with all controls:
Window_Main = CreateWindow("#32770", "Windows GUI", WS_OVERLAPPEDWindow Or WS_VISIBLE, _
	100, 100, 500, 650, 0, 0, 0, 0 )
Var Label = CreateWindow("STATIC", "Enter a text:", WS_VISIBLE Or WS_CHILD, _
	20, 20, 200, 20, Window_Main, 0, 0, 0) 
Edit1 = CreateWindow("EDIT", "", WS_BORDER Or WS_VISIBLE Or WS_CHILD Or ES_AUTOHSCROLL, _
	20, 50, 200, 20, Window_Main, 0, 0, 0 )
Button1 = CreateWindow("BUTTON", "Copy", WS_VISIBLE Or WS_CHILD, _
	60, 80, 100, 20, Window_Main, 0, 0, 0 )
Edit2 = CreateWindow("EDIT", "", _
	WS_BORDER Or WS_VISIBLE Or WS_CHILD Or WS_HSCROLL Or WS_VSCROLL Or ES_MULTILINE Or ES_WANTRETURN, _
	20, 120, 300, 200, Window_Main, 0, 0, 0 )
Button2 = CreateWindow("BUTTON", "Copy", WS_VISIBLE Or WS_CHILD, _
	340, 200, 100, 20, Window_Main, 0, 0, 0 )
List1 = CreateWindow("LISTBOX", "", _
	WS_BORDER Or WS_VISIBLE Or WS_CHILD Or WS_HSCROLL Or WS_VSCROLL, _
	20, 350, 200, 200, Window_Main, 0, 0, 0 )
	
'Font to be used for the multiline editbox:
Font = CreateFont(16, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Courier New")
SendMessage(Edit2, WM_SETFONT, Cast(WPARAM, Font), True)
SetWindowText(Edit2, "Please write text here!") 
	
'Add items to listbox:
For i = 0 To 20
	text = "Item number " + Str(i)
	SendMessage(List1, LB_ADDSTRING, 0, Cast(LPARAM, @text))
Next

'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, 101, "New" )
AppendMenu(hDatei, 0, 102, "Open" )
AppendMenu(hDatei, 0, 103, "Save" )
AppendMenu(hDatei, 0, 104, "Exit" )
AppendMenu(hHilfe, 0, 105, "?")
SetMenu(Window_Main, hMenu )


'Event Loop:

Do

	GetMessage(@msg, 0, 0, 0)
  TranslateMessage(@msg)
  DispatchMessage(@msg)
  
  Select Case msg.hwnd
  
    Case Window_Main
			Select Case msg.message
				Case WM_COMMAND 	'Menu messages, including window menu
          Select Case msg.wParam
						Case 2
							If MessageBox(0, "Do you really want to exit?", "Exit", _
								MB_YESNO Or MB_ICONQUESTION) = IDYES Then Exit Do
						Case 101
              MessageBox(0, "New file ...", "File", 0)
            Case 102
              MessageBox(0, "Open file ...", "File", 0)
            Case 103
              MessageBox(0, "Save file ...", "File", 0)
            Case 104
              If MessageBox(0, "Do you really want to exit?", "File", _
							  MB_YESNO Or MB_ICONQUESTION) = IDYES Then Exit Do
            Case 105
							MessageBox(0, "Sorry, I cannot help you!", "Help", 0)
					End Select
      End Select
      
    Case Button1
			If msg.message = WM_LBUTTONDOWN Then
				'Copy text from Edit1 to console:
				GetWindowText(Edit1, text, SizeOf(text))
				Print text
			End If
			
		Case Button2
			If msg.message = WM_LBUTTONDOWN Then
				'Copy text from Edit2 to console:
				GetWindowText(Edit2, text, SizeOf(text))
				text = RTrim(text)
				Print text
			End If
			
		Case List1
			If msg.message = WM_LBUTTONDOWN Then
				'Show selected index and item from listbox:
				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
  
Loop

DeleteObject(Font)

End
I thank the author very much for this nice tutorial, it has helped me very much to use the Windows API.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Easy Windows API Tutorial

Post by jj2007 »

This is not really necessary - fonts are not an OS-wide shared resource:

Code: Select all

DeleteObject(Font)
End
@Lothar: do you realise you woke up a really old thread? There would be a lot to comment on the OP's code, e.g. the use of "161" instead of WM_NCLBUTTONDOWN and "273" instead of WM_COMMAND; the weird omission of a WndProc callback (how can it work? a mystery...), but I would suggest to abandon this thread altogether. It is simply very idiosyncratic and confusing.

Your code is OK - maybe you should open a new thread with a similar title.
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: Easy Windows API Tutorial

Post by aurelVZAB »

Your code is OK - maybe you should open a new thread with a similar title.
maybe...
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: Easy Windows API Tutorial

Post by aurelVZAB »

maybe this :

Code: Select all

#Include "windows.bi"

Dim As MSG msg
Dim As HWND Window_Main, Edit1, Edit2, Button1, Button2, List1
Dim As HFONT Font ,cFont
Dim As HMENU hMenu, hDatei, hHilfe
Dim As Integer i,icon1
Dim As ZString*1024 text
const ICONBUTTON = 1409351744

'Window with all controls:
Window_Main = CreateWindow("#32770", "Windows GUI", WS_OVERLAPPEDWindow Or WS_VISIBLE, _
   100, 100, 500, 650, 0, 0, 0, 0 )
Var Label = CreateWindow("STATIC", "Enter a text:", WS_VISIBLE Or WS_CHILD, _
   20, 20, 200, 20, Window_Main, 0, 0, 0)
Edit1 = CreateWindow("EDIT", "", WS_BORDER Or WS_VISIBLE Or WS_CHILD Or ES_AUTOHSCROLL, _
   20, 50, 200, 20, Window_Main, 0, 0, 0 )
Button1 = CreateWindow("BUTTON", "Copy", WS_VISIBLE Or WS_CHILD, _
   60, 80, 100, 32, Window_Main, 0, 0, 0 )
Edit2 = CreateWindow("EDIT", "", _
   WS_BORDER Or WS_VISIBLE Or WS_CHILD Or WS_HSCROLL Or WS_VSCROLL Or ES_MULTILINE Or ES_WANTRETURN, _
   20, 120, 300, 200, Window_Main, 0, 0, 0 )
   
Button2 = CreateWindow("BUTTON", "Copy", ICONBUTTON, 340, 200, 100, 32, Window_Main, 0, 0, 0 )
icon1 = LoadImage(0, "fbButtonIcon.ico", IMAGE_ICON, 98, 28, 24)   'Icon via resource name
SendMessage(Button2, 247, 1, icon1)

List1 = CreateWindow("LISTBOX", "", _
   WS_BORDER Or WS_VISIBLE Or WS_CHILD Or WS_HSCROLL Or WS_VSCROLL, _
   20, 350, 200, 200, Window_Main, 0, 0, 0 )
   
'Font to be used for the multiline editbox:
Font = CreateFont(16, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Courier New")
cFont = CreateFont(16, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Consolas")
SendMessage(Button1, WM_SETFONT, Cast(WPARAM, cFont), True)
SendMessage(List1, WM_SETFONT, Cast(WPARAM, cFont), True)
SendMessage(Edit2, WM_SETFONT, Cast(WPARAM, Font), True)
SetWindowText(Edit2, "Please write text here!")
   
'Add items to listbox:
For i = 0 To 20
   text = "Item number " + Str(i)
   SendMessage(List1, LB_ADDSTRING, 0, Cast(LPARAM, @text))
Next

'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, 101, "New" )
AppendMenu(hDatei, 0, 102, "Open" )
AppendMenu(hDatei, 0, 103, "Save" )
AppendMenu(hDatei, 0, 104, "Exit" )
AppendMenu(hHilfe, 0, 105, "?")
SetMenu(Window_Main, hMenu )


'Event Loop:

Do

   GetMessage(@msg, 0, 0, 0)
  TranslateMessage(@msg)
  DispatchMessage(@msg)
 
  Select Case msg.hwnd
 
    Case Window_Main
         Select Case msg.message
            Case WM_COMMAND    'Menu messages, including window menu
          Select Case msg.wParam
                  Case 2
                     If MessageBox(0, "Do you really want to exit?", "Exit", _
                        MB_YESNO Or MB_ICONQUESTION) = IDYES Then Exit Do
                  Case 101
              MessageBox(0, "New file ...", "File", 0)
            Case 102
              MessageBox(0, "Open file ...", "File", 0)
            Case 103
              MessageBox(0, "Save file ...", "File", 0)
            Case 104
              If MessageBox(0, "Do you really want to exit?", "File", _
                       MB_YESNO Or MB_ICONQUESTION) = IDYES Then Exit Do
            Case 105
                     MessageBox(0, "Sorry, I cannot help you!", "Help", 0)
               End Select
      End Select
     
    Case Button1
         If msg.message = WM_LBUTTONDOWN Then
            'Copy text from Edit1 to console:
            GetWindowText(Edit1, text, SizeOf(text))
            Print text
         End If
         
      Case Button2
         If msg.message = WM_LBUTTONDOWN Then
            'Copy text from Edit2 to console:
            GetWindowText(Edit2, text, SizeOf(text))
            text = RTrim(text)
            Print text
         End If
         
      Case List1
         If msg.message = WM_LBUTTONDOWN Then
            'Show selected index and item from listbox:
            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
 
Loop

DeleteObject(Font)

End
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Easy Windows API Tutorial

Post by jj2007 »

What you are posting is really non-standard, Aurel, and I am surprised that it works. I will have to study it more carefully...
Post Reply