Easy Windows API Tutorial
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]
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.
Re: Easy Windows API Tutorial
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?
for instance, is there a way to translate the handle into a screen pointer?
Re: Easy Windows API Tutorial
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.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?
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.
Re: Easy Windows API Tutorial
EDIT. I misread the original question.
Hmm I used to be able to delete posts, but can't do that anymore.
Hmm I used to be able to delete posts, but can't do that anymore.
Re: Easy Windows API Tutorial
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.St_W wrote: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.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?
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.
Re: Easy Windows API Tutorial
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
-
- Posts: 2958
- Joined: Jun 02, 2015 16:24
Re: Easy Windows API Tutorial
Nice tutorial!
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.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.
-
- Posts: 437
- Joined: Sep 28, 2013 15:08
- Location: Germany
Re: Easy Windows API Tutorial
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:
I thank the author very much for this nice tutorial, it has helped me very much to use the Windows API.
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
Re: Easy Windows API Tutorial
This is not really necessary - fonts are not an OS-wide shared resource:
@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.
Code: Select all
DeleteObject(Font)
End
Your code is OK - maybe you should open a new thread with a similar title.
Re: Easy Windows API Tutorial
maybe...Your code is OK - maybe you should open a new thread with a similar title.
Re: Easy Windows API Tutorial
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
Re: Easy Windows API Tutorial
What you are posting is really non-standard, Aurel, and I am surprised that it works. I will have to study it more carefully...