Windows GUI

New to FreeBASIC? Post your questions here.
Post Reply
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Windows GUI

Post by Josep Roca »

Provoni wrote:
Josep Roca wrote:
Provoni wrote:Thanks Josep, can the program get a handle on the icon that was attached to it? The .rc file.
DIM hIcon AS HICON = LoadImage(GetModuleHandle(NULL), "OOP_16x16.ico", IMAGE_ICON, 16, 16, LR_SHARED)
Josep,

I tried the following code in my program but an icon does not show.

Code: Select all

dim hIcon AS HICON = LoadImage(GetModuleHandle(NULL), "OOP_16x16.ico", IMAGE_ICON, 16, 16, LR_SHARED)
SendMessage(window_main, WM_SETICON, CAST(WPARAM, ICON_BIG), CAST(LPARAM, hIcon))
SendMessage(window_main, WM_SETICON, CAST(WPARAM, ICON_SMALL), CAST(LPARAM, hIcon))
Sorry, it should be:

Code: Select all

dim hIcon AS HICON = LoadImage(GetModuleHandle(NULL), "FB_PROGRAM_ICON", IMAGE_ICON, 16, 16, LR_SHARED)
SendMessage(window_main, WM_SETICON, CAST(WPARAM, ICON_BIG), CAST(LPARAM, hIcon))
SendMessage(window_main, WM_SETICON, CAST(WPARAM, ICON_SMALL), CAST(LPARAM, hIcon))
The syntax depends on what you use in your .rc file.
Provoni
Posts: 513
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Windows GUI

Post by Provoni »

Thanks Josep,

It works now and by the changing the dimensions to 256, 256 it comes out much better than it does at 16, 16.
Provoni
Posts: 513
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Windows GUI

Post by Provoni »

Lothar,

Is it possible to set a custom background for my program instead of the Windows gray?
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Windows GUI

Post by Josep Roca »

This sets the background color of the main window to blue.

Code: Select all

SetClassLongPtr hwnd, GCLP_HBRBACKGROUND, CAST(LONG_PTR, CreateSolidBrush(BGR(0, 0, 255)))
Where hwnd is the handle to the window.
Provoni
Posts: 513
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Windows GUI

Post by Provoni »

Josep Roca wrote:This sets the background color of the main window to blue.

Code: Select all

SetClassLongPtr hwnd, GCLP_HBRBACKGROUND, CAST(LONG_PTR, CreateSolidBrush(BGR(0, 0, 255)))
Where hwnd is the handle to the window.
Okay, thanks Josep. How to set an image as background of a window?
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Windows GUI

Post by MichaelW »

CreatePatternBrush is a possibility, as are some of the related brushes.
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Windows GUI

Post by Josep Roca »

> Okay, thanks Josep. How to set an image as background of a window?

You can't simply draw an image and that's all. You can't make it persistent, therefore you need to redraw it each time that WIndows redraws the background. In an application that uses a callback function, this is done processing the WM_ERASEBKGND message.

Code: Select all

CASE WM_ERASEBKGND
   DIM hDC AS HDC = CAST(HDC, wParam)
   ' // TODO: Draw the image
   ' // Put here the code that you want to use to draw the image
   FUNCTION = CTRUE
   EXIT FUNCTION
The code to load and draw the image depends on the kind of image and what you intend to do. You can use GDI, GDI+, etc.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Windows GUI

Post by MichaelW »

I was assuming "image" meant a static image.

Code: Select all

''=============================================================================
#include "windows.bi"
''=============================================================================
''---------------------------------------------------------------------
'' This procedure sizes the specified window so the client area is the
'' specified width and height and optionally centers the window on the
'' the screen. Unlike AdjustWindowRect and AdjustWindowRectEx, this
'' procedure can handle a window with the WS_OVERLAPPED style.
''---------------------------------------------------------------------

sub SetClientSize( hwnd as HWND, pixelWidth as integer, _
                   pixelHeight as integer, bCenter as BOOL )

    dim as integer x, y, w, h
    dim as RECT rcc, rcw

    GetClientRect( hwnd, @rcc )
    GetWindowRect( hwnd, @rcw )

    w = (rcw.right - rcw.left) - (rcc.right - pixelWidth) - 1
    h = (rcw.bottom - rcw.top) - (rcc.bottom - pixelHeight) - 1

    if bCenter then
        x = (GetSystemMetrics( SM_CXSCREEN ) / 2) - w / 2
        y = (GetSystemMetrics( SM_CYSCREEN ) / 2) - h / 2
    else
        x = rcw.left
        y = rcw.top
    end if

    MoveWindow( hwnd, x, y, w, h, TRUE )

end sub

''=============================================================================

function WindowProc( hwnd as HWND, uMsg as UINT, _
                     wParam as WPARAM, lParam as LPARAM) as LRESULT

    static as HBRUSH hBrushBmp
                     
    select case uMsg

        case WM_CREATE
        
            ''-----------------------------------------
            '' Fblogo.bmp is in ...\FreeBASIC\examples
            '' Size derived from the file properties.
            ''-----------------------------------------
        
            SetClientSize( hwnd, 320, 240, TRUE )
        
            dim as HANDLE hBmp = LoadImage( NULL, _
                                            "fblogo.bmp", _
                                            IMAGE_BITMAP, _
                                            0, _
                                            0, _
                                            LR_LOADFROMFILE )            
       
            hBrushBmp = CreatePatternBrush( hBmp )
            
            SetClassLongPtr( hwnd, _
                             GCLP_HBRBACKGROUND, _
                             CAST(LONG_PTR, hBrushBmp) )

        case WM_COMMAND

            if wParam = IDCANCEL then            
                DestroyWindow(hwnd)
            end if  
    
        case WM_CLOSE

            DestroyWindow( hwnd )            

        case WM_DESTROY

            PostQuitMessage( 0 )

        case else

            return DefWindowProc( hwnd, uMsg, wParam, lParam )

    end select

end function

''=============================================================================

dim as HWND hwnd
dim as MSG msg
dim as WNDCLASSEX wcx

wcx.cbSize = sizeof(WNDCLASSEX)
wcx.style = 0
wcx.lpfnWndProc = @WindowProc
wcx.cbClsExtra = 0
wcx.cbWndExtra = 0
wcx.hInstance = GetModuleHandle( NULL )
wcx.hIcon = LoadIcon( NULL, IDI_APPLICATION )
wcx.hCursor = LoadCursor( NULL, IDC_ARROW )
wcx.hbrBackground = GetStockObject( WHITE_BRUSH )
wcx.lpszMenuName = NULL
wcx.lpszClassName = strptr("test")
wcx.hIconSm = 0

RegisterClassEx( @wcx )

hwnd = CreateWindowEx( 0, "test", "Test", WS_OVERLAPPED or WS_SYSMENU, _
                       0, 0, 0, 0, NULL, NULL, NULL, NULL )

ShowWindow( hwnd, SW_SHOWNORMAL )
UpdateWindow( hwnd )

do while GetMessage( @msg, NULL, 0, 0 ) > 0
    if IsDialogMessage(hwnd, @msg ) = 0 then
        TranslateMessage( @msg )
        DispatchMessage( @msg )
    end if
loop
Provoni
Posts: 513
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Windows GUI

Post by Provoni »

Thanks Josep and MichaelW.

My program needs to draw a graph (with FreeBASIC draw functions) and then save it to a .bmp taking in consideration WinGUI. Placing the following code somewhere in the program will do the trick but then the user is left with an extra 800 by 600 that cannot be closed.

Code: Select all

screenres(800,600,32)
line(1,1)-(500,500),rgb(0,0,255) 'imagine graph
bsave "graph.bmp",0 'save to disk
Is it possible to close a window created with screenres without closing the program entirely?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Windows GUI

Post by MrSwiss »

Provoni wrote:1) Placing the following code somewhere in the program will do the trick but then the user is left with an extra 800 by 600 that cannot be closed.
Provoni wrote:2) Is it possible to close a window created with screenres without closing the program entirely?
Could you please ask in an understandable way? The two above statements are conflicting.

1) don't understand, what you mean to say ...
2) generally NO (closing "main program window" usually terminates a "windowed" program)
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Windows GUI

Post by fxm »

'Screen 0' closes the graphics window opened by "Screenres' and open a console window that can be closed immediately if necessary by 'FreeConsole()'.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Windows GUI

Post by MichaelW »

Provoni,

Perhaps what you need is a Windows API window that uses the GFX_NULL driver, example here.
Provoni
Posts: 513
Joined: Jan 05, 2014 12:33
Location: Belgium

Re: Windows GUI

Post by Provoni »

MichaelW wrote:Provoni,

Perhaps what you need is a Windows API window that uses the GFX_NULL driver, example here.
Yes, tyvm. Here's an example:

Code: Select all

#include "fbgfx.bi"
using fb
screenres(100,100,32,1,GFX_NULL)
print "hello forum!"
bsave "hello.bmp",0
One step further would be to save it as a png instead of a bmp.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Windows GUI

Post by MrSwiss »

Provoni wrote:One step further would be to save it as a png instead of a bmp.
Can only be done using a external Library, since FB doesn't support .png directly.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: Windows GUI

Post by MichaelW »

GDI+ can do the conversion, Microsoft example here.
Sorry, no time to code an example.
Post Reply