• Welcome to Theos PowerBasic Museum 2017.

winapi window + rectangles

Started by Frank Brübach, October 02, 2009, 05:23:38 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach

hello.

I have downloaded some examples with creating winapi's and have done this little example.

question: how to load an icon into window title bar ?
I have managed to use the icon for exe application :)

it's also possible to change the a) font size and b) get a second text line some lines below my "Hello, my Lion FX Warp Windows"?

and c) where can I get more informations about 3d primitives (circle, pyramid, torus and so on) and the include files like #INCLUDE "GLU.INC", #INCLUDE "GDIPLUS.INC", #INCLUDE "GDIPUTILS.INC", #INCLUDE "OPENGLUT.INC" and open gl features? sometimes there are no these include files in zip file attached I have seen with a short first look ! I want to check some nehe lessons ;)


#COMPILER PBWIN 9
#COMPILE EXE
#DEBUG ERROR ON
#DIM ALL

%USEMACROS = 1
#INCLUDE "Win32API.inc"
#RESOURCE "HelloWin1.pbr"


'==============================================================================
FUNCTION WINMAIN (BYVAL hInstance     AS DWORD, _
                 BYVAL hPrevInstance AS DWORD, _
                 BYVAL lpCmdLine     AS ASCIIZ PTR, _
                 BYVAL iCmdShow      AS LONG) AS LONG
'------------------------------------------------------------------------------
   ' Program entry point
   '--------------------------------------------------------------------------

   LOCAL Msg       AS tagMsg
   LOCAL wce       AS WndClassEx
   LOCAL szAppName AS ASCIIZ * 80
   LOCAL szUntitled AS ASCIIZ * 60
   LOCAL hWnd      AS DWORD
   LOCAL hDlg      AS DWORD

   DIALOG NEW 0, "My stupid painting rectangles",,, 280, 185, _
            %WS_CAPTION OR %WS_SYSMENU OR %WS_THICKFRAME, 0 TO hDlg

   DIALOG SHOW MODAL hDlg CALL DlgProc

   szAppName         = "HelloLionWin"
   szUntitled        = "(untitled)"
   wce.cbSize        = SIZEOF(wce)
   wce.STYLE         = %CS_HREDRAW OR %CS_VREDRAW
   wce.lpfnWndProc   = CODEPTR(WndProc)
   wce.cbClsExtra    = 0
   wce.cbWndExtra    = 0
   wce.hInstance     = hInstance
   wce.hIcon         = LoadIcon(hInstance, "CJ.ICON")
   wce.hCursor       = LoadCursor(%NULL, BYVAL %IDC_ARROW)
   wce.hbrBackground = %NULL ' No class background, we do it outselves
   wce.lpszMenuName  = %NULL
   wce.lpszClassName = VARPTR(szAppName)
   wce.hIconSm       = LoadIcon(hInstance, BYVAL %IDI_APPLICATION)
   'wce.hIConSm       = LoadIcon(hInstance, szAppName)
   RegisterClassEx wce

   ' Create a window using the registered class
   hWnd = CreateWindow(szAppName, _               ' window class name
                       "The boring but important Hello Program", _     ' window caption
                       %WS_OVERLAPPEDWINDOW, _    ' window style
                       %CW_USEDEFAULT, _          ' initial x position
                       %CW_USEDEFAULT, _          ' initial y position
                       %CW_USEDEFAULT, _          ' initial x size
                       %CW_USEDEFAULT, _          ' initial y size
                       %NULL, _                   ' parent window handle
                       %NULL, _                   ' window menu handle
                       hInstance, _               ' program instance handle
                       BYVAL %NULL)               ' creation parameters

   IF hWnd = 0 THEN  ' exit on failure
       MSGBOX "Unable to create window"
       EXIT FUNCTION
   END IF

   ' Display the window on the screen
   ShowWindow hWnd, iCmdShow
   UpdateWindow hWnd

   ' Main message loop:
   ' Messages sent to HELLOWIN while it has the focus are received by
   ' GetMessage().  This loop translates each message and dispatches it
   ' to the appropriate handler.  When PostQuitMessage() is called, the
   ' loop terminates which ends the application.
   DO WHILE GetMessage(Msg, %NULL, 0, 0)
       TranslateMessage Msg
       DispatchMessage Msg
   LOOP

   FUNCTION = msg.wParam

END FUNCTION


'==============================================================================
SUB DrawGradient (BYVAL hDC AS DWORD)
'------------------------------------------------------------------------------
   ' Custom draw procedure for gradiend fill
   '--------------------------------------------------------------------------

   LOCAL rectFill AS RECT
   LOCAL rectClient AS RECT
   LOCAL fStep AS SINGLE
   LOCAL hBrush AS DWORD
   LOCAL lOnBand AS LONG

   GetClientRect WindowFromDC(hDC), rectClient
   fStep = rectClient.nbottom / 200

   FOR lOnBand = 0 TO 199
       SetRect rectFill, 0, lOnBand * fStep, rectClient.nright + 1, (lOnBand + 1) * fStep
       'hBrush = CreateSolidBrush(RGB(0, 0, 255 - lOnBand))
       hBrush = CreateSolidBrush(RGB(RND(120,140), RND(60), RND(240,255)*SIN(128) - lOnBand))
       Fillrect hDC, rectFill, hBrush
       DeleteObject hBrush
   NEXT

END SUB


'==============================================================================
FUNCTION WndProc (BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, _
                 BYVAL wParam AS DWORD, BYVAL lParam AS LONG) EXPORT AS LONG
'------------------------------------------------------------------------------

   LOCAL hDC    AS DWORD
   LOCAL pPaint AS PAINTSTRUCT
   LOCAL tRect  AS RECT

   SELECT CASE wMsg

   CASE %WM_CREATE

   CASE %WM_PAINT
       hDC = BeginPaint(hWnd, pPaint)
       GetClientRect hWnd, tRect
       SetBkMode hDC, %TRANSPARENT
       SetTextColor hDC, %WHITE
       DrawText hDC, "Hello, my Lion FX Warp Windows!", -1, tRect, %DT_SINGLELINE OR %DT_CENTER OR %DT_VCENTER
       EndPaint hWnd, pPaint
       FUNCTION = 1
       EXIT FUNCTION

   CASE %WM_ERASEBKGND
       hDC = wParam
       DrawGradient hDC
       FUNCTION = 1
       EXIT FUNCTION

   CASE %WM_DESTROY
       PostQuitMessage 0
       EXIT FUNCTION

   END SELECT

   FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)

END FUNCTION

'-------------------------------------------------
SUB PaintRect( BYVAL hdc AS DWORD, rc AS Rect, BYVAL colour AS DWORD) EXPORT
  LOCAL oldcr AS DWORD
  oldcr = SetBkColor( hdc, colour )
  ExtTextOut hdc, 0, 0, %ETO_OPAQUE, rc, "", 0, 0
  SetBkColor hdc, oldcr
END SUB

'---------------------------------

CALLBACK FUNCTION DlgProc() AS LONG

 SELECT CASE AS LONG CBMSG

    CASE %WM_ERASEBKGND
       LOCAL hDC AS DWORD
       LOCAL rc  AS Rect
       LOCAL rc1  AS Rect
       LOCAL rc2  AS Rect
       LOCAL rc3  AS Rect

       hDC = CBWPARAM
       GetClientRect CBHNDL, rc
       GetClientRect CBHNDL, rc1
       GetClientRect CBHNDL, rc2
       GetClientRect CBHNDL, rc3

       PaintRect hDC, rc, %BLUE
       PaintRect hDC, rc1, %GREEN
       PaintRect hDC, rc2, %YELLOW
       PaintRect hDC, rc3, %MAGENTA

       SetRect rc, 5, 15, 150, 180
       'randomize 4.5!
       PaintRect hDC, rc, RGB( RND(1,60), RND(1,120), RND(1,250) )

       SetRect rc1, 24, 64, 280, 190
       PaintRect hDC, rc1, RGB( 160, 120, 60 )

       SetRect rc2, 86, 166, 210, 220
       PaintRect hDC, rc2, RGB( 60, 20, 160 )

       SetRect rc3, 226, 126, 220, 280
       PaintRect hDC, rc3, RGB( 255, 255, 255 )

       SetRect rc3, 298, 126, 220, 280
       PaintRect hDC, rc3, RGB( 55, 255, 255 )

       FUNCTION = %TRUE

 END SELECT

END FUNCTION


and one more important question:

I must use the predefined ms window behaviour and resources e.g. of win xp (my machine!) to build this kind of win api, isn't it? I have no chance to create new window with other properties and power ? otherwise I have to include my personal win api with #include "windows-of-my-own-wishes.inc" ? :)

best regards, servus, frank
ps: I am not sure if this is the right place for this post.

José Roca

 
I will leave the graphics questions to the OpenGL experts of this forum, but regarding the include files, here we use my translation of the latest C headers of the Windows Platform SDK (more than 900 files), available here: http://www.jose.it-berater.org/smfforum/index.php?topic=3189.0

Please, read the entire thread and, specially, this remark:

Quote
Unzip the attached file to a folder of your choice and replace the PB Include path in the PB Ide or the editor that you are using to that folder instead of C:\PBWin90\WinApi.

That is, you must no mix them with the ones provided with the compiler.

José Roca

 
Also, the examples from the book of Charles Petzold for Windows 98 (the ones available in the PB forum are from the book for Windows 95), are available here:

http://www.jose.it-berater.org/smfforum/index.php?board=248.0

Contrarily to most users of the PB forum, here we use the Windows SDK extensively instead of PB's DDT. Therefore, we need updated full headers and not the small subset provided with the compilers.

I also use low-level COM extensively, and my headers provide interface definitions for all the COM subsystem.

Frank Brübach

thank you josé for the links! I needed some of the examples :)

To include an own Icon in windows title bar I have tried to use "HelloWin" sdk example from petzold.

then I have got an error message about

resource file ("lion.ico") is not in 3.00 format. I have saved my 32x32 picture from photoshop and saved it with 24 bpp depth. See my attached file, all included, the *.rc file too.

perhaps somebody can tell me what's going wrong to load my "lion.ico" in titlebar. The application starts with the lion.ico, that's very fine :)

'==============================================================================
'
'   HELLOWIN.BAS for PowerBASIC for Windows
'   Copyright (c) 1997-2008 PowerBASIC, Inc.
'   All Rights Reserved.
'
'   Translation of HELLOWIN.C from Charles Petzold's book:
'
'     "Programming Windows 95" published by Microsoft Press.
'     ISBN 1-55615-676-6
'
'   Note: The original code does not contain the "gradient" effect.
'
'==============================================================================

#COMPILER PBWIN 9
#COMPILE EXE
#DIM ALL

%USEMACROS = 1
#INCLUDE "Win32API.inc"

#RESOURCE "HelloWin2.pbr"     ' this gives the program the "hellowin" icon


'==============================================================================
FUNCTION WINMAIN (BYVAL hInstance     AS DWORD, _
                 BYVAL hPrevInstance AS DWORD, _
                 BYVAL lpCmdLine     AS ASCIIZ PTR, _
                 BYVAL iCmdShow      AS LONG) AS LONG
'------------------------------------------------------------------------------
   ' Program entry point
   '--------------------------------------------------------------------------

   LOCAL Msg       AS tagMsg
   LOCAL wce       AS WndClassEx
   LOCAL szAppName AS ASCIIZ * 80
   LOCAL hWnd      AS DWORD

   ' Setup and register a window class for the main window
   ' CODEPTR is used to pass the address of the function that will
   ' receive all messages sent to any window created with this class
   szAppName         = "HelloWin"
   wce.cbSize        = SIZEOF(wce)
   wce.STYLE         = %CS_HREDRAW OR %CS_VREDRAW
   wce.lpfnWndProc   = CODEPTR(WndProc)
   wce.cbClsExtra    = 0
   wce.cbWndExtra    = 0
   wce.hInstance     = hInstance
   wce.hIcon         = LoadIcon(hInstance, "LION.ICO")
   wce.hCursor       = LoadCursor(%NULL, BYVAL %IDC_ARROW)
   wce.hbrBackground = %NULL ' No class background, we do it outselves
   wce.lpszMenuName  = %NULL
   wce.lpszClassName = VARPTR(szAppName)
   wce.hIconSm       = LoadIcon(hInstance, BYVAL %IDI_APPLICATION)'

   RegisterClassEx wce

   ' Create a window using the registered class
   hWnd = CreateWindow(szAppName, _               ' window class name
                       "The Hello Program without 'Lion.ICO' in Titlebar :)", _     ' window caption
                       %WS_OVERLAPPEDWINDOW, _    ' window style
                       %CW_USEDEFAULT, _          ' initial x position
                       %CW_USEDEFAULT, _          ' initial y position
                       %CW_USEDEFAULT, _          ' initial x size
                       %CW_USEDEFAULT, _          ' initial y size
                       %NULL, _                   ' parent window handle
                       %NULL, _                   ' window menu handle
                       hInstance, _               ' program instance handle
                       BYVAL %NULL)               ' creation parameters

   IF hWnd = 0 THEN  ' exit on failure
       MSGBOX "Unable to create window"
       EXIT FUNCTION
   END IF

   ' Display the window on the screen
   ShowWindow hWnd, iCmdShow
   UpdateWindow hWnd

   ' Main message loop:
   ' Messages sent to HELLOWIN while it has the focus are received by
   ' GetMessage().  This loop translates each message and dispatches it
   ' to the appropriate handler.  When PostQuitMessage() is called, the
   ' loop terminates which ends the application.
   DO WHILE GetMessage(Msg, %NULL, 0, 0)
       TranslateMessage Msg
       DispatchMessage Msg
   LOOP

   FUNCTION = msg.wParam

END FUNCTION


'==============================================================================
SUB DrawGradient (BYVAL hDC AS DWORD)
'------------------------------------------------------------------------------
   ' Custom draw procedure for gradiend fill
   '--------------------------------------------------------------------------

   LOCAL rectFill AS RECT
   LOCAL rectClient AS RECT
   LOCAL fStep AS SINGLE
   LOCAL hBrush AS DWORD
   LOCAL lOnBand AS LONG

   GetClientRect WindowFromDC(hDC), rectClient
   fStep = rectClient.nbottom / 200

   FOR lOnBand = 0 TO 199
       SetRect rectFill, 0, lOnBand * fStep, rectClient.nright + 1, (lOnBand + 1) * fStep
       hBrush = CreateSolidBrush(RGB(0, 0, 255 - lOnBand))
       Fillrect hDC, rectFill, hBrush
       DeleteObject hBrush
   NEXT

END SUB


'==============================================================================
FUNCTION WndProc (BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, _
                 BYVAL wParam AS DWORD, BYVAL lParam AS LONG) EXPORT AS LONG
'------------------------------------------------------------------------------
   ' WndProc is the message handler for all windows creating using the HelloWin
   ' class name.  A single WndProc procedure can handle multiple windows by
   ' testing the hWnd variable passed to it.
   '--------------------------------------------------------------------------

   LOCAL hDC    AS DWORD
   LOCAL pPaint AS PAINTSTRUCT
   LOCAL tRect  AS RECT

   ' The SELECT CASE is used to catch only those messages which the message
   ' handler needs to process.  All other messages are passed through the
   ' tests to the default handler.
   SELECT CASE wMsg

   CASE %WM_CREATE

   CASE %WM_PAINT
       hDC = BeginPaint(hWnd, pPaint)
       GetClientRect hWnd, tRect
       SetBkMode hDC, %TRANSPARENT
       SetTextColor hDC, %WHITE
       DrawText hDC, "Hello, my dear Lion FX Windows!", -1, tRect, %DT_SINGLELINE OR %DT_CENTER OR %DT_VCENTER
       EndPaint hWnd, pPaint
       FUNCTION = 1
       EXIT FUNCTION

   CASE %WM_ERASEBKGND
       hDC = wParam
       DrawGradient hDC              ' Pass the DC of the region to repaint
       FUNCTION = 1
       EXIT FUNCTION

   CASE %WM_DESTROY
       PostQuitMessage 0
       EXIT FUNCTION

   END SELECT

   ' Any message which is not handled in the above SELECT CASE reaches this
   ' point and is processed by the Windows default message handler.
   FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)

END FUNCTION


best regards, Frank

José Roca

 
You're using:


wce.hIcon         = LoadIcon(hInstance, "LION.ICO")


You don't have to use the name of the icon, but the name of the resource identifier:


wce.hIcon         = LoadIcon(hInstance, "HELLOWIN2frank")


Frank Brübach

#5
hi josé, all.

so simple but I haven't found this little beast to include in title bar ! thank you for this nice gimmick. five cent virtual for you too ;) I had never found this little specific resource identifier name. my window api is looking like to come more from myself, very beautiful and I am happy! I like animals and of course lions.

'==============================================================================
'
'   HELLOWIN.BAS for PowerBASIC for Windows
'   Copyright (c) 1997-2008 PowerBASIC, Inc.
'   All Rights Reserved.
'
'   Translation of HELLOWIN.C from Charles Petzold's book:
'
'     "Programming Windows 95" published by Microsoft Press.
'     ISBN 1-55615-676-6
'
'   Note: The original code does not contain the "gradient" effect.
'
'==============================================================================

#COMPILER PBWIN 9
#COMPILE EXE
#DIM ALL

%USEMACROS = 1
#INCLUDE "Win32API.inc"

#RESOURCE "HelloWin2.pbr"     ' this gives the program the "hellowin" icon


'==============================================================================
FUNCTION WINMAIN (BYVAL hInstance     AS DWORD, _
                 BYVAL hPrevInstance AS DWORD, _
                 BYVAL lpCmdLine     AS ASCIIZ PTR, _
                 BYVAL iCmdShow      AS LONG) AS LONG
'------------------------------------------------------------------------------
   ' Program entry point
   '--------------------------------------------------------------------------

   LOCAL Msg       AS tagMsg
   LOCAL wce       AS WndClassEx
   LOCAL szAppName AS ASCIIZ * 80
   LOCAL hWnd      AS DWORD

   ' Setup and register a window class for the main window
   ' CODEPTR is used to pass the address of the function that will
   ' receive all messages sent to any window created with this class
   szAppName         = "HelloWin"
   wce.cbSize        = SIZEOF(wce)
   wce.STYLE         = %CS_HREDRAW OR %CS_VREDRAW
   wce.lpfnWndProc   = CODEPTR(WndProc)
   wce.cbClsExtra    = 0
   wce.cbWndExtra    = 0
   wce.hInstance     = hInstance
   wce.hIcon         = LoadIcon(hInstance, "HelloWin2")
   wce.hCursor       = LoadCursor(%NULL, BYVAL %IDC_ARROW)
   wce.hbrBackground = %NULL ' No class background, we do it outselves
   wce.lpszMenuName  = %NULL
   wce.lpszClassName = VARPTR(szAppName)
   wce.hIconSm       = LoadIcon(hInstance, BYVAL %IDI_APPLICATION)'

   RegisterClassEx wce

   ' Create a window using the registered class
   hWnd = CreateWindow(szAppName, _               ' window class name
                       "The Hello Program with 'Lion.ICO' in Titlebar :)", _     ' window caption
                       %WS_OVERLAPPEDWINDOW, _    ' window style
                       %CW_USEDEFAULT, _          ' initial x position
                       %CW_USEDEFAULT, _          ' initial y position
                       %CW_USEDEFAULT, _          ' initial x size
                       %CW_USEDEFAULT, _          ' initial y size
                       %NULL, _                   ' parent window handle
                       %NULL, _                   ' window menu handle
                       hInstance, _               ' program instance handle
                       BYVAL %NULL)               ' creation parameters

   IF hWnd = 0 THEN  ' exit on failure
       MSGBOX "Unable to create window"
       EXIT FUNCTION
   END IF

   ' Display the window on the screen
   ShowWindow hWnd, iCmdShow
   UpdateWindow hWnd

   ' Main message loop:
   ' Messages sent to HELLOWIN while it has the focus are received by
   ' GetMessage().  This loop translates each message and dispatches it
   ' to the appropriate handler.  When PostQuitMessage() is called, the
   ' loop terminates which ends the application.
   DO WHILE GetMessage(Msg, %NULL, 0, 0)
       TranslateMessage Msg
       DispatchMessage Msg
   LOOP

   FUNCTION = msg.wParam

END FUNCTION


'==============================================================================
SUB DrawGradient (BYVAL hDC AS DWORD)
'------------------------------------------------------------------------------
   ' Custom draw procedure for gradiend fill
   '--------------------------------------------------------------------------

   LOCAL rectFill AS RECT
   LOCAL rectClient AS RECT
   LOCAL fStep AS SINGLE
   LOCAL hBrush AS DWORD
   LOCAL lOnBand AS LONG

   GetClientRect WindowFromDC(hDC), rectClient
   fStep = rectClient.nbottom / 200

   FOR lOnBand = 0 TO 199
       SetRect rectFill, 0, lOnBand * fStep, rectClient.nright + 1, (lOnBand + 1) * fStep
       hBrush = CreateSolidBrush(RGB (10, 0, 255) - lOnBand)
       Fillrect hDC, rectFill, hBrush
       DeleteObject hBrush
   NEXT

END SUB


'==============================================================================
FUNCTION WndProc (BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, _
                 BYVAL wParam AS DWORD, BYVAL lParam AS LONG) EXPORT AS LONG
'------------------------------------------------------------------------------
   ' WndProc is the message handler for all windows creating using the HelloWin
   ' class name.  A single WndProc procedure can handle multiple windows by
   ' testing the hWnd variable passed to it.
   '--------------------------------------------------------------------------

   LOCAL hDC    AS DWORD
   LOCAL pPaint AS PAINTSTRUCT
   LOCAL tRect  AS RECT

   ' The SELECT CASE is used to catch only those messages which the message
   ' handler needs to process.  All other messages are passed through the
   ' tests to the default handler.
   SELECT CASE wMsg

   CASE %WM_CREATE

   CASE %WM_PAINT
       hDC = BeginPaint(hWnd, pPaint)
       GetClientRect hWnd, tRect
       SetBkMode hDC, %TRANSPARENT
       SetTextColor hDC, %RED
       DrawText hDC, "Hello, my dear Lions Windows World!", -1, tRect, %DT_SINGLELINE OR %DT_CENTER OR %DT_VCENTER
       EndPaint hWnd, pPaint
       FUNCTION = 1
       EXIT FUNCTION

   CASE %WM_ERASEBKGND
       hDC = wParam
       DrawGradient hDC              ' Pass the DC of the region to repaint
       FUNCTION = 1
       EXIT FUNCTION

   CASE %WM_DESTROY
       PostQuitMessage 0
       EXIT FUNCTION

   END SELECT

   ' Any message which is not handled in the above SELECT CASE reaches this
   ' point and is processed by the Windows default message handler.
   FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam)

END FUNCTION


good evening, thanks, frank