PDA

View Full Version : tb_graphx



Lionheart008
18-11-2009, 19:05
hello.

perhaps somebody can check the "powerbasic" code for my "graphx" example. it's just a test how to understand building a thinbasic module coming from powerbasic. I have some ideas for sdk windows or other graphic input things, I am not quite sure, but want to try it to realize...

I have done some mistakes in pb code, I am sure, but wanted to do this experiment.

demo: powerbasic code


#IF 0
=============================================================================
Program NAME: UserDefinedLib.bas
Author : Eros Olmi
Version :
Description : DLL TO test thinBasic loading library
=============================================================================
'COPYRIGHT AND PERMISSION NOTICE
'============================================================================
Copyright (c) 2003 - 2009, Eros Olmi, <eros.olmi@thinbasic.com>

ALL rights reserved.

Permission TO use this software IS granted ONLY FOR the purpose TO develop
thinBasic language modules both FOR commercial OR non commercial purpose.
IN ANY CASE the above copyright notice AND this permission notice must appear
IN ALL copies.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
=============================================================================
#ENDIF

#COMPILE DLL
#REGISTER NONE
#DIM ALL

'---Resource file. Change UserDefinedLib.RC resource file as needed and remember to compile
' into UserDefinedLib.PBR file using resource compiler
#RESOURCE "UserDefinedLib.PBR"

#INCLUDE "WIN32API.INC"

'---Every used defined thinBasic module must include this file
#INCLUDE "thinCore.inc"

%ButtonClose = 1001

'----------------------------------------------------------------------------
FUNCTION Exec_HEX() AS STRING
' Return a string that is the hexadecimal (base 16) representation of its argument.
' Usage: s$ = HEX$(numeric_expression [, digits])
'----------------------------------------------------------------------------
LOCAL lNumber AS EXT
LOCAL lDigits AS EXT

'---Here we have to parse the syntax we have decided this new keyword will have
' Syntax is: an open pares
' followed by a numeric expression
' followed by an optional comma with an optional numeric expression
' a close parens
'---Here how to do

'---Parse open pares
IF thinBasic_CheckOpenParens() THEN

'---If ok, parse a numeric expression and return it's value.
' Remember to always use EXT numeric type even if you need a different one
thinBasic_ParseNumber lNumber

'---Here we see if there is a comma.
' If not, we have instruct not to produce a runtime error and to put back
' the found token into the parsing stack
IF thinBasic_CheckComma(%TRUE, %TRUE) THEN

'---If optional comma was specified we parse another numeric expression
thinBasic_ParseNumber lDigits

END IF

'---At the end check if close parens was specified
IF thinBasic_CheckCloseParens() THEN

'---At the end here we are. If optional parameter was specified, call the function
' with the optional parameter
IF lDigits > 0 THEN
FUNCTION = HEX$(lNumber, lDigits)
ELSE

'---otherwise call function without optional parameter
FUNCTION = HEX$(lNumber)
END IF

END IF

END IF

END FUNCTION

'######################################################################################################

FUNCTION Exec_windowMe (BYVAL hdlg AS LONG, BYVAL windox AS LONG, BYVAL cbDialox AS LONG) AS Ext 'LONG

DIM a AS LONG
DIM b AS LONG

DIALOG NEW 0, "Frankos_Test_Dialogue",-1,-1, 330, 203, _
%WS_POPUP OR _
%WS_VISIBLE OR _
%WS_CLIPCHILDREN OR _
%WS_CAPTION OR _
%WS_SYSMENU OR _
%WS_MINIMIZEBOX, _
0 TO hDlg

CONTROL ADD BUTTON, hDlg, %ButtonClose, "Click to kill me softly", 90, 50, 150, 100
DIALOG SHOW MODAL hDlg CALL cbDialog

IF thinBasic_CheckOpenParens() THEN
thinBasic_ParseLong hDlg
IF thinBasic_CheckOpenParens() THEN
thinBasic_ParseLong windox
IF thinBasic_CheckCloseParens() THEN
windox = %TRUE
IF thinBasic_CheckCloseParens() THEN
hdlg = %TRUE

IF hDlg THEN windox = %buttonclose

FUNCTION = windox

END IF
END IF
END IF
END IF
END FUNCTION

'######################################################################################################

'------------------------------------------------
' Callback function used to handle dialog events
'------------------------------------------------

CALLBACK FUNCTION cbDialog() AS LONG

SELECT CASE CBMSG
CASE %WM_COMMAND
IF CBWPARAM = %ButtonClose THEN DIALOG END CBHNDL

CASE %WM_DESTROY
MSGBOX "Window is to be destroyed.", %MB_OK, "tbGraphx_yes!"
END SELECT

END FUNCTION


'----------------------------------------------------------------------------
FUNCTION Exec_Console_FColor() AS EXT
' Compose a new console foreground color.
' Usage: n = Console_fColor ( R AS LONG, G AS LONG, B AS LONG, Intense AS LONG)
'----------------------------------------------------------------------------
LOCAL R AS EXT, lR AS LONG
LOCAL G AS EXT, lG AS LONG
LOCAL B AS EXT, lB AS LONG
LOCAL Intense AS EXT, lIntense AS LONG
LOCAL ForeColor AS LONG

IF thinBasic_CheckOpenParens() THEN
thinBasic_ParseNumber R
IF thinBasic_CheckComma() THEN
thinBasic_ParseNumber G
IF thinBasic_CheckComma() THEN
thinBasic_ParseNumber B
IF thinBasic_CheckComma() THEN
thinBasic_ParseNumber Intense
IF thinBasic_CheckCloseParens() THEN
lR = R
lG = G
lB = B
lIntense = Intense

IF lR THEN ForeColor = %FOREGROUND_RED
IF lG THEN ForeColor = ForeColor OR %FOREGROUND_GREEN
IF lB THEN ForeColor = ForeColor OR %FOREGROUND_BLUE
IF lIntense THEN ForeColor = ForeColor OR %FOREGROUND_INTENSITY

FUNCTION = ForeColor
END IF
END IF
END IF
END IF
END IF

END FUNCTION


SUB Exec_tbgraphx_Test

LOCAL VPTR AS LONG
LOCAL VAbsPos AS LONG
LOCAL myValue AS EXT
VPTR = 123456789

IF thinBasic_CheckOpenParens( ) THEN
thinBasic_VariableParse(VPtr, VAbsPos)
IF thinBasic_CheckCloseParens( ) THEN
MSGBOX STR$ (VPtr) & "," & STR$ (VAbsPos) & ", [" & "]", , "tbGraphx"
MyValue = 2009
thinBasic_ChangeVariableNumberDirect (VPtr, VAbsPos, MyValue)
END IF
END IF
END SUB


'----------------------------------------------------------------------------
FUNCTION LoadLocalSymbols ALIAS "LoadLocalSymbols" (OPTIONAL BYVAL sPath AS STRING) EXPORT AS LONG
' This function is automatically called by thinCore whenever this DLL is loaded.
' This function MUST be present in every external DLL you want to use
' with thinBasic
' Use this function to initialize every variable you need and for loading the
' new symbol (read Keyword) you have created.
'----------------------------------------------------------------------------

'---
'Add here Initialization code if needed
'---


'---
'Here define/add new thinBasic keywords
'---
'---Here an example. Defined a new keyword named HEX$ which will return a string.
' Connect this keyword with Exec_HEX internal function passing its pointer with CODEPTR.
' Last parameter tells thinBasic that even if HEX$ keyword has already been defined, this new
' definition will have priority and will overwite previous keyword configuration
thinBasic_LoadSymbol "MyHEX$", %thinBasic_ReturnString, CODEPTR(Exec_HEX), %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "Console_fColor", %thinBasic_ReturnNumber, CODEPTR(Exec_Console_FColor), %thinBasic_ForceOverWrite

thinBasic_LoadSymbol "tbGraphx_Test", %thinBasic_ReturnNone, CODEPTR(Exec_tbGraphx_Test ), %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "windowMe", %thinBasic_ReturnCodeLong, CODEPTR(Exec_windowMe ), %thinBasic_ForceOverWrite


'---
'Here define/add new thinBasic equates
'---
thinBasic_AddEquate "%MyNumericEquate", "" , 1
thinBasic_AddEquate "$MyStringEquate", "ABC" , 0



END FUNCTION

'----------------------------------------------------------------------------
FUNCTION UnLoadLocalSymbols ALIAS "UnLoadLocalSymbols" () EXPORT AS LONG
' This function is automatically called by thinCore whenever this DLL is unloaded.
' This function CAN be present but it is not necessary. If present, this function
' will be executed by thinBasic core when module will be released.
' Use this function to perform uninitialize process, if needed.
'----------------------------------------------------------------------------

'---
'Add here DeInitialization code if needed
'---

FUNCTION = 0&
END FUNCTION


FUNCTION LIBMAIN ALIAS "LibMain" (BYVAL hInstance AS LONG, _
BYVAL fwdReason AS LONG, _
BYVAL lpvReserved AS LONG) EXPORT AS LONG
SELECT CASE fwdReason
CASE %DLL_PROCESS_ATTACH

FUNCTION = 1
EXIT FUNCTION
CASE %DLL_PROCESS_DETACH

FUNCTION = 1
EXIT FUNCTION
CASE %DLL_THREAD_ATTACH

FUNCTION = 1
EXIT FUNCTION
CASE %DLL_THREAD_DETACH

FUNCTION = 1
EXIT FUNCTION
END SELECT

END FUNCTION




here the thinbasic test code:


MODULE "graphx" '
uses "console"

dim res as long
dim u as ext
dim x,y,z as long
dim s as string

tbGraphx_Test(u)
res = Msgbox(0, "Ute was born in:" & str$(u))

x = msgbox 0, "color_value: " + Console_fColor(1,2,3,4)
printl "color_value: " + Console_fColor(1,2,3,4)

z = msgbox 0, "hex_value: " + MyHex$(2,4)
printl "hex_value: " + MyHex$(2,4)

y = msgbox 0, "window: " + windowMe(1,2,3), %MB_OK, "graphx_test value: "




would be nice if somebody can tell what's the main reason I have got the gpf :)
to check the code it isn't so important to do this one fast. I can wait.

best regards, Frank

Petr Schreiber
18-11-2009, 19:19
Frank,



IF thinBasic_CheckOpenParens() THEN
thinBasic_ParseLong hDlg
IF thinBasic_CheckOpenParens() THEN
thinBasic_ParseLong windox
IF thinBasic_CheckCloseParens() THEN
windox = %TRUE
IF thinBasic_CheckCloseParens() THEN
hdlg = %TRUE

IF hDlg THEN windox = %buttonclose

FUNCTION = windox

END IF
END IF


OpenParens = (
CloseParens = )

So your code follows this pattern:
(hDlg(windox))

So correct line would be (and works):


y = msgbox (0, "window: " + windowMe(1(2)), %MB_OK, "graphx_test value: ")


Pretty weird syntax, isn't it :) If you want to have it separated by comma, just use thinbasic_CheckCommaMandatory.

Also one detail - run time error is not GPF. When GPF (general protection fault) occurs, you see message from Windows, not from thinBASIC.

I hope it helped.

Lionheart008
18-11-2009, 19:37
hi petr. many thanks. it was last evening very late, I have built this little pb code (incl. thinbasic test code) and I can agree, this is weired syntax and was not my intention ;)


y = msgbox (0, "window: " + windowMe(1(2)), %MB_OK, "graphx_test value: ")

good to know, that I have done the main part with correct powerbasic code, so I am proud of it. one good start for me :)

I will check more possibilities to make graphic inputs by powerbasic I will show my examples, if there is more time for this module.

I will have a closer look for your advice, I have used an old powerbasic example from thinbasic sdk. thank you for help.


Also one detail - run time error is not GPF. When GPF (general protection fault) occurs, you see message from Windows, not from thinBASIC.

I have meant "run time error" in my first example !

best wishes, Frank
oh yes, I know, I was too fast with writing down, sorry.

Lionheart008
18-11-2009, 22:03
hello.

simple improvements, simple modifications, but it wasn't so simple to make it.
- if you like this little experiment, test this tb_graphx example. I have included two new buttons with open ("click to openGraphx") new dialog and ColorBox
- cleared the tb syntax too. no more weird code !
- treeview feature I will build next time, have already started with this example.
- colorBox :)


MODULE "graphx"
uses "console"

dim res as long
dim u as ext
dim x,y,z as long
dim s as string

tbGraphx_Test(u)
res = Msgbox(0, "Ute was born in:" & str$(u))

x = msgbox 0, "color_value: " + Console_fColor(1,2,3,4)
printl "color_value: " + Console_fColor(1,2,3,4)

z = msgbox 0, "hex_value: " + MyHex$(2,4)
printl "hex_value: " + MyHex$(2,4)

y = msgbox (0, "window: " + windowMe(1,2), %MB_OK, "graphx_test value: ")
printl
printl "windowMe: " + windowMe(1,2)
printl
printl "return any key to exit"
waitkey



any feedback is welcome. I will try to start a sdk like window with gradient style.
info: graphx.dll + thinbasic example as attachement

best regards, Frank

Lionheart008
04-03-2010, 17:06
(post: part one:)

hello.

some month ago I've tried to use "gdiplus" feature for this experimental "graphx" (just a word play) module. that last attempt couldn't load pictures, because my background wasn't fit for that one ;)
For learning effects and how to understand the sdk with loading any image. this is the first successfully way for me to load an image (fixed image I add with this zip file). I am not sure, if it's 100 per cent best way, but it does what it's for. making some calculations by functions for simple testing and loading by a dialog (window) a new popup window to show this picture :)

you can test it if you like.

thinbasic test code:

Module "graphxA"
Uses "console", "ui"

dim res as long
dim u as ext
Dim x,y,z,t As Long
dim s as string

tbGraphx_Test(u)
res = MsgBox(0, "Genivieve was born in:" & Str$(u))

x = MsgBox 0, "my ColorValue: " + Console_fColor(100,250,255,1)
PrintL "my ColorValue: " + Console_fColor(100,250,255,1)

z = MsgBox 0, "String HexValue: " + MyHex$(1,2)
PrintL "String HexValue: " + MyHex$(1,2)

y = MsgBox (0, "Popup window: " + graphMe(1,2,""), %MB_OK, "tb graphx feedback Test: ")

PrintL
PrintL "windowMe: " + graphMe(1,2,"")
printl
printl "return any key to exit"
waitkey



I have some more question, but I like it and it's perhaps possible to use this module for next thinbasic issues.

would be nice to get any feedback.

more infos about powerbasic and very good "cWindow.in" class from josé roca (thanks a lot!) you can find here according to this exercise follow with next post :)

http://www.jose.it-berater.org/smfforum/index.php?topic=3497.0

best regards, frank

Lionheart008
04-03-2010, 17:14
(post part two:)

...

powerbasic code:

'----------------------------------------------------------------------------------------
'-- tb_graphx module by frank brübach, germany, 14-18.nov.2009 / and: 03.03-04.03.2010 /

#COMPILE DLL
#REGISTER NONE
#DIM ALL

'---Resource file. Change UserDefinedLib.RC resource file as needed and remember to compile
' into UserDefinedLib.PBR file using resource compiler
' #RESOURCE "UserDefinedLib.PBR"

#INCLUDE "WIN32API.INC"
#INCLUDE "commctrl.inc"
#INCLUDE "objbase.inc"
#INCLUDE "gdiplus.inc"
GLOBAL ghInstance AS DWORD

'---Every used defined thinBasic module must include this file
#INCLUDE "thinCore.inc"

%ButtonClose = 1001
%ButtonOpen = 1002
%TreeView = 1003
%IDCombobox = 1004
%IDTEXT = 1005
%ButtonColBox = 1006
%ButtonImage = 1007
%ButtonColor = 10024
%IDC_IMAGENAME = 1008
%MyImage = 1009
%lImage = 1010
%lText = 1011
'----------------------------------------------------------------------------
FUNCTION Exec_HEX() AS STRING
'----------------------------------------------------------------------------
LOCAL lNumber AS EXT
LOCAL lDigits AS EXT

'---Parse open pares
IF thinBasic_CheckOpenParens() THEN

thinBasic_ParseNumber lNumber

IF thinBasic_CheckComma(%TRUE, %TRUE) THEN
thinBasic_ParseNumber lDigits
END IF

'---At the end check if close parens was specified
IF thinBasic_CheckCloseParens() THEN
IF lDigits > 0 THEN
FUNCTION = HEX$(lNumber, lDigits)
ELSE
'---otherwise call function without optional parameter
FUNCTION = HEX$(lNumber)
END IF
END IF
END IF
END FUNCTION

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

FUNCTION Exec_graphMe (BYVAL hdlg AS LONG, BYVAL windox AS LONG, BYVAL cbDialox AS LONG) AS EXT

DIM a AS LONG
DIM b AS LONG
LOCAL lvbi AS LVBKIMAGE
LOCAL szFile AS STRING 'ASCIIZ*%MAX_PATH
LOCAL lvi AS LVITEM
LOCAL sFilter AS STRING

InitCommonControls

DIALOG NEW 0, "TB Graphx_Test_Dialogue 1",-1,-1, 340, 290, _
%WS_POPUP OR _
%WS_VISIBLE OR _
%WS_CLIPCHILDREN OR _
%WS_CAPTION OR _
%WS_SYSMENU OR _
%WS_MINIMIZEBOX, _
0 TO hDlg

' Set the color with an RGB value
DIALOG SET COLOR hDlg, -1, RGB(0,100,255)
CONTROL ADD BUTTON, hDlg, %ButtonImage, "load new fixed Image", 90, 20, 140, 140, CALL PopupDlgProc
'CONTROL ADD BUTTON, hDlg, %ButtonOpen, "Click to open Dialog", 210, 200, 80, 20 CALL PopupDlgProc
CONTROL ADD BUTTON, hDlg, %ButtonColBox, "open ColBox", 220, 200, 80, 20, CALL PopupDlgProc
CONTROL ADD BUTTON, hDlg, %ButtonClose, "Click to kill me softly", 60, 180, 100, 80, CALL PopupDlgProc
'CONTROL SET COLOR hDlg, %ButtonColor, RGB(255,255,255), RGB(0,0,255)

lvbi.ulFlags = %LVBKIF_STYLE_NORMAL OR %LVBKIF_SOURCE_URL

lvbi.pszImage = VARPTR(szFile)
lvbi.cchImageMax = LEN(szFile)

CONTROL SEND hDlg, 500, %LVM_SETBKIMAGE, 0, VARPTR(lvbi)
DIALOG SHOW MODAL hDlg CALL cbDialog

IF thinBasic_CheckOpenParens() THEN
thinBasic_ParseLong hDlg
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong windox
windox = %TRUE
IF thinBasic_CheckComma() THEN
thinBasic_ParseString szFile

IF thinBasic_CheckCloseParens() THEN
hdlg = %TRUE
IF hDlg THEN windox = %buttonclose
IF windox THEN szFile = ""
FUNCTION = hDlg
FUNCTION = VARPTR(szFile) + windox
END IF
END IF
END IF
END IF
END FUNCTION

'------------------------------------------------
' Callback function used to handle dialog events
'------------------------------------------------

CALLBACK FUNCTION cbDialog() AS LONG
SELECT CASE CBMSG
CASE %WM_COMMAND
IF CBWPARAM = %ButtonClose THEN DIALOG END CBHNDL
CASE %WM_DESTROY
MSGBOX "Graphx is to be destroyed.", %MB_OK, "tbGraphx_yes!"
END SELECT

END FUNCTION

'----------------------------------------------------------------------------
FUNCTION Exec_Console_FColor() AS EXT
' Compose a new console foreground color.
' Usage: n = Console_fColor ( R AS LONG, G AS LONG, B AS LONG, Intense AS LONG)
'----------------------------------------------------------------------------
LOCAL R AS EXT, lR AS LONG
LOCAL G AS EXT, lG AS LONG
LOCAL B AS EXT, lB AS LONG
LOCAL Intense AS EXT, lIntense AS LONG
LOCAL ForeColor AS LONG

IF thinBasic_CheckOpenParens() THEN
thinBasic_ParseNumber R
IF thinBasic_CheckComma() THEN
thinBasic_ParseNumber G
IF thinBasic_CheckComma() THEN
thinBasic_ParseNumber B
IF thinBasic_CheckComma() THEN
thinBasic_ParseNumber Intense
IF thinBasic_CheckCloseParens() THEN
lR = R
lG = G
lB = B
lIntense = Intense
IF lR THEN ForeColor = %FOREGROUND_RED
IF lG THEN ForeColor = ForeColor OR %FOREGROUND_GREEN
IF lB THEN ForeColor = ForeColor OR %FOREGROUND_BLUE
IF lIntense THEN ForeColor = ForeColor OR %FOREGROUND_INTENSITY
FUNCTION = ForeColor
END IF
END IF
END IF
END IF
END IF

END FUNCTION

'---------------------------------------------------------------------
SUB Exec_tbgraphx_Test
'---------------------------------------------------------------------
LOCAL VPTR AS LONG
LOCAL VAbsPos AS LONG
LOCAL myValue AS EXT
VPTR = 123456789

IF thinBasic_CheckOpenParens( ) THEN
thinBasic_VariableParse(VPtr, VAbsPos)
IF thinBasic_CheckCloseParens( ) THEN
MSGBOX STR$ (VPtr) & "," & STR$ (VAbsPos) & ", [" & "]", , "tbGraphx: " + DATE$
MyValue = 2010
thinBasic_ChangeVariableNumberDirect (VPtr, VAbsPos, MyValue)
END IF
END IF
END SUB

'------------------------------------------------------------------------------------
FUNCTION LoadLocalSymbols ALIAS "LoadLocalSymbols" (OPTIONAL BYVAL sPath AS STRING) EXPORT AS LONG
'------------------------------------------------------------------------------------
thinBasic_LoadSymbol "MyHEX$", %thinBasic_ReturnString, CODEPTR(Exec_HEX), %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "Console_fColor", %thinBasic_ReturnNumber, CODEPTR(Exec_Console_FColor), %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "tbGraphx_Test", %thinBasic_ReturnNone, CODEPTR(Exec_tbGraphx_Test ), %thinBasic_ForceOverWrite
thinBasic_LoadSymbol "graphMe", %thinBasic_ReturnCodeLong, CODEPTR(Exec_graphMe ), %thinBasic_ForceOverWrite
'---
'Here define/add new thinBasic equates
'---
thinBasic_AddEquate "%MyNumericEquate", "" , 1
thinBasic_AddEquate "$MyStringEquate", "ABC" , 0

END FUNCTION

'----------------------------------------------------------------------------
FUNCTION UnLoadLocalSymbols ALIAS "UnLoadLocalSymbols" () EXPORT AS LONG

FUNCTION = 0&
END FUNCTION


FUNCTION LIBMAIN ALIAS "LibMain" (BYVAL hInstance AS LONG, _
BYVAL fwdReason AS LONG, _
BYVAL lpvReserved AS LONG) EXPORT AS LONG
SELECT CASE fwdReason
CASE %DLL_PROCESS_ATTACH

FUNCTION = 1
EXIT FUNCTION
CASE %DLL_PROCESS_DETACH

FUNCTION = 1
EXIT FUNCTION
CASE %DLL_THREAD_ATTACH

FUNCTION = 1
EXIT FUNCTION
CASE %DLL_THREAD_DETACH

FUNCTION = 1
EXIT FUNCTION
END SELECT

END FUNCTION

' ======================
' Popup dialog procedure
' ==================================================================
FUNCTION PopupDlgProc (BYVAL hWnd AS DWORD, BYVAL wMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG
LOCAL szFile AS STRING
LOCAL hParent AS LONG

SELECT CASE wMsg
CASE %WM_CREATE
EnableWindow GetWindow(hWnd, %GW_OWNER), %FALSE ' To make the popup dialog modal

CASE %WM_COMMAND
SELECT CASE LO(WORD, wParam)
CASE %IDCANCEL
IF HI(WORD, wParam) = %BN_CLICKED THEN
SendMessage hWnd, %WM_CLOSE, 0, 0
EXIT FUNCTION
END IF

CASE %ButtonOpen
IF HI(WORD, wParam) = %BN_CLICKED THEN
'ShowPopupDialog hwnd
EXIT FUNCTION
END IF

CASE %ButtonImage
IF HI(WORD, wParam) = %BN_CLICKED THEN
MSGBOX "test loading for new dialog with picture ok?", %MB_OK, "thinbasic Image Tester"
ShowImageDialog hwnd
'---------------------------------------------------------------------
' SendMessage hWnd, EXE.PATH$ & "leopardenX.jpg"
'IF wParam THEN SendMessage hWnd,1,1, %LVM_SETBKIMAGE '%WM_COMMAND
EXIT FUNCTION
END IF

CASE %ButtonColBox
MSGBOX "Coloured Box first part :) ", %MB_ICONINFORMATION, "tb_graphx Test"
LOCAL hWinx AS DWORD
LOCAL z AS LONG
LOCAL vt AS LONG
LOCAL xt AS LONG
GRAPHIC WINDOW "BoxColEffect", 300, 300, 240, 240 TO hWinx
GRAPHIC ATTACH hWinx, 0
FOR z& = 0 TO 255
GRAPHIC LINE (0, z&) - (255, z&), RGB(55, 210, z&+50)*128
NEXT
FOR vt& = 0 TO 255
GRAPHIC LINE (0, vt&) - (255, vt&), RGB(255, 210, vt&+150)*255
NEXT
SLEEP 1500

END SELECT

CASE %WM_CLOSE
EnableWindow GetWindow(hWnd, %GW_OWNER), %TRUE ' Maintains parent's zorder

CASE %WM_DESTROY
PostQuitMessage 0 ' This function closes the main window
EXIT FUNCTION

END SELECT

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

END FUNCTION

==================================================================
FUNCTION ShowPopupDialog (BYVAL hParent AS LONG) AS LONG 'hParent

LOCAL hWndPopup AS LONG
LOCAL hCtl AS LONG
LOCAL hFont AS LONG
LOCAL rc AS RECT
LOCAL wcex AS WNDCLASSEX
LOCAL szClassName AS ASCIIZ * 80
LOCAL szCaption AS ASCIIZ * 255
LOCAL hInstance AS DWORD

hFont = GetStockObject(%ANSI_VAR_FONT)

szClassName = "MyPopupClassName"
wcex.cbSize = SIZEOF(wcex)
wcex.style = %CS_HREDRAW OR %CS_VREDRAW
wcex.lpfnWndProc = CODEPTR(PopupDlgProc)
wcex.cbClsExtra = 0
wcex.cbWndExtra = 0
wcex.hInstance = GetModuleHandle("")
wcex.hCursor = LoadCursor(%NULL, BYVAL %IDC_ARROW)
wcex.hbrBackground = %COLOR_3DFACE + 1
wcex.lpszMenuName = %NULL
wcex.lpszClassName = VARPTR(szClassName)
wcex.hIcon = 0
wcex.hIconSm = 0
RegisterClassEx wcex

GetWindowRect hParent, rc ' For centering child in parent
rc.nRight = rc.nRight - rc.nLeft ' Parent's width
rc.nBottom = rc.nBottom - rc.nTop ' Parent's height

szCaption = "Graphx_Popup dialog"
hWndPopup = CreateWindowEx(%WS_EX_DLGMODALFRAME OR %WS_EX_CONTROLPARENT, _
szClassName, szCaption, %WS_CAPTION OR %WS_POPUPWINDOW OR %WS_VISIBLE OR %WS_OVERLAPPEDWINDOW OR %WS_CLIPCHILDREN, _
rc.nLeft + (rc.nRight - 290) / 2, _
rc.nTop + (rc.nBottom - 180) / 2, _
360, 220, hParent, 0, GetModuleHandle(""), BYVAL %NULL)

hCtl = CreateWindowEx(0, "BUTTON", "&Close", %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_FLAT, _
200, 118, 75, 23, hWndPopup, %IDCANCEL, hInstance, BYVAL %NULL)
hCtl = CreateWindowEx(0, "BUTTON", "&Batman", %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_FLAT, _
60, 118, 75, 23, hWndPopup, %IDOK, hInstance, BYVAL %NULL)
IF hFont THEN SendMessage hCtl, %WM_SETFONT, hFont, 0

ShowWindow hWndPopup, %SW_SHOW
UpdateWindow hWndPopup
' Message handler loop
LOCAL uMsg AS tagMsg
WHILE GetMessage(uMsg, %NULL, 0, 0)
IF ISFALSE IsDialogMessage(hWndPopup, uMsg) THEN
TranslateMessage uMsg
DispatchMessage uMsg
END IF
WEND

FUNCTION = uMsg.wParam

END FUNCTION

'--- last part of code follows

for any ideas for optimizing this code I would be appreciated :)

best regards, frank

Lionheart008
04-03-2010, 17:16
I had to split this post because I couldn't send more characters in this postbox editor ;)
more than 20000 characters aren't possible I have got this error message.

=>last part of powerbasic code:


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

FUNCTION WindowProc4 (BYVAL hwnd AS DWORD, BYVAL uMsg AS DWORD, BYVAL wParam AS DWORD, BYVAL lParam AS LONG) AS LONG

LOCAL hDC AS DWORD
LOCAL pPaint AS PAINTSTRUCT
LOCAL rc AS RECT

SELECT CASE uMsg

CASE %WM_COMMAND
SELECT CASE LO(WORD, wParam)
CASE %IDCANCEL
IF HI(WORD, wParam) = %BN_CLICKED THEN
SendMessage hwnd, %WM_CLOSE, 0, 0
EXIT FUNCTION
END IF

CASE %IDOK
IF HI(WORD, wParam) = %BN_CLICKED THEN
'
EXIT FUNCTION
END IF

END SELECT

CASE %WM_PAINT
hDC = BeginPaint(hwnd, pPaint)
GetClientRect hwnd, rc
SetBkMode hDC, %TRANSPARENT
SetTextColor hDC, %GREEN
DrawText hDC, "Hello, thinbasic Image Test:)", -1, rc, %DT_SINGLELINE OR %DT_CENTER OR %DT_VCENTER
EndPaint hwnd, pPaint
FUNCTION = %TRUE
EXIT FUNCTION

CASE %WM_ERASEBKGND
hDC = wParam
DrawGradient hwnd, hDC
GDIP_SetImageAttributesWrapMode hDC
FUNCTION = %TRUE
EXIT FUNCTION

CASE %WM_SIZE
IF wParam <> %SIZE_MINIMIZED THEN
GetClientRect hwnd, rc
MoveWindow GetDlgItem(hwnd, %IDOK), (rc.nRight - rc.nLeft) - 185, (rc.nBottom - rc.nTop) - 35, 75, 23, %TRUE
MoveWindow GetDlgItem(hwnd, %IDCANCEL), (rc.nRight - rc.nLeft) - 95, (rc.nBottom - rc.nTop) - 35, 75, 23, %TRUE
END IF

CASE %WM_DESTROY
PostQuitMessage 0
EXIT FUNCTION

END SELECT

FUNCTION = DefWindowProc(hwnd, uMsg, wParam, lParam)

END FUNCTION

' ==================================================================
SUB DrawGradient (BYVAL hwnd AS DWORD, BYVAL hdc AS DWORD)

LOCAL rc AS RECT
DIM vertex(1) AS TRIVERTEX

GetClientRect hwnd, rc

vertex(0).x = 0
vertex(0).y = 0
vertex(0).Red = &HFF00
vertex(0).Green = &HFF00
vertex(0).Blue = &H0000 'HF lila ;)
vertex(0).Alpha = &H0000

vertex(1).x = rc.nRight - rc.nLeft
vertex(1).y = rc.nBottom - rc.nTop
vertex(1).Red = &H8000
vertex(1).Green = &H0000
vertex(1).Blue = &HF000 '&H0000
vertex(1).Alpha = &H0000

LOCAL gRect AS GRADIENT_RECT

gRect.UpperLeft = 0
gRect.LowerRight = 1

GradientFill hDc, vertex(0), 2, gRect, 1, %GRADIENT_FILL_RECT_H

END SUB

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

SUB GDIP_SetImageAttributesWrapMode (BYVAL hdc AS DWORD)

LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pImage AS DWORD
LOCAL pImgAttr AS DWORD
LOCAL strFileName AS STRING
LOCAL nWidth AS LONG
LOCAL nHeight AS LONG

hStatus = GdipCreateFromHDC(hdc, pGraphics)

strFileName = UCODE$("sonnenuntergang.png")
hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
hStatus = GdipCreateImageAttributes(pImgAttr)

GdipSetImageAttributesWrapMode(pImgAttr, %WrapModeClamp, GDIP_ARGB(255, 255, 0, 0), %FALSE)
hStatus = GdipGetImageWidth(pImage, nWidth)
hStatus = GdipGetImageHeight(pImage, nHeight)
hStatus = GdipDrawImageRectRectI(pGraphics, pImage, _
10, 10, nWidth, nHeight, _ ' dest rect
0, 0, 2*nWidth, 2*nHeight, _ ' source dest
%UnitPixel, pImgAttr, %NULL, %NULL)
GdipSetImageAttributesWrapMode(pImgAttr, %WrapModeTile, GDIP_ARGB(255, 255, 0, 0), %FALSE)
hStatus = GdipDrawImageRectRectI(pGraphics, pImage, _
200, 10, nWidth, nHeight, _ ' dest rect
0, 0, 2*nWidth, 2*nHeight, _ ' source dest
%UnitPixel, pImgAttr, %NULL, %NULL)

' // Cleanup
IF pImgAttr THEN GdipDisposeImageAttributes(pImgAttr)
IF pImage THEN GdipDisposeImage(pImage)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB
' ==================================================================

FUNCTION ShowImageDialog (BYVAL hParent AS LONG) AS LONG

LOCAL hWndPopup AS LONG
LOCAL hCtl AS LONG
LOCAL hFont AS LONG
LOCAL hwnd AS DWORD
LOCAL rc AS RECT
LOCAL wcex AS WNDCLASSEX
LOCAL szClassName AS ASCIIZ * 80
LOCAL szCaption AS ASCIIZ * 255
LOCAL hInstance AS DWORD
LOCAL hr AS LONG
LOCAL token AS DWORD
LOCAL StartupInput AS GdiplusStartupInput

' // Initialize the control

' Initialize GDI+
StartupInput.GdiplusVersion = 1
hr = GdiplusStartup(token, StartupInput, BYVAL %NULL)
IF hr THEN
MSGBOX "Error initializing GDI+"
EXIT FUNCTION
END IF

hFont = GetStockObject(%ANSI_VAR_FONT)

szClassName = "MyPopupClassName"
wcex.cbSize = SIZEOF(wcex)
wcex.style = %CS_HREDRAW OR %CS_VREDRAW
wcex.lpfnWndProc = CODEPTR(WindowProc4)
wcex.cbClsExtra = 0
wcex.cbWndExtra = 0
wcex.hInstance = GetModuleHandle("")
wcex.hCursor = LoadCursor(%NULL, BYVAL %IDC_ARROW)
wcex.hbrBackground = %COLOR_3DFACE + 1
wcex.lpszMenuName = %NULL
wcex.lpszClassName = VARPTR(szClassName)
wcex.hIcon = 0
wcex.hIconSm = 0
RegisterClassEx wcex

GetWindowRect hParent, rc ' For centering child in parent
rc.nRight = rc.nRight - rc.nLeft ' Parent's width
rc.nBottom = rc.nBottom - rc.nTop ' Parent's height

szCaption = "Graphx_Popup 2 dialog with IMAGE :) "
hWndPopup = CreateWindowEx(%WS_EX_DLGMODALFRAME OR %WS_EX_CONTROLPARENT, _
szClassName, szCaption, %WS_CAPTION OR %WS_POPUPWINDOW OR %WS_VISIBLE OR %WS_OVERLAPPEDWINDOW OR %WS_CLIPCHILDREN, _
rc.nLeft + (rc.nRight - 520) / 2, _
rc.nTop + (rc.nBottom - 300) / 2, _
560, 380, hParent, 0, GetModuleHandle(""), BYVAL %NULL)

hCtl = CreateWindowEx(0, "BUTTON", "&Close", %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_FLAT, _
390, 310, 75, 23, hWndPopup, %IDCANCEL, hInstance, BYVAL %NULL)
hCtl = CreateWindowEx(0, "BUTTON", "&TIGER", %WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP OR %BS_FLAT, _
60, 310, 75, 23, hWndPopup, %IDOK, hInstance, BYVAL %NULL)

IF hFont THEN SendMessage hCtl, %WM_SETFONT, hFont, 0

ShowWindow hWndPopup, %SW_SHOW
UpdateWindow hWndPopup
' Message handler loop
LOCAL uMsg AS tagMsg
WHILE GetMessage(uMsg, %NULL, 0, 0)
IF ISFALSE IsDialogMessage(hWndPopup, uMsg) THEN
TranslateMessage uMsg
DispatchMessage uMsg
END IF
WEND

' // Force the resizing of the buttons by sending a WM_SIZE message
SendMessage hwnd, %WM_SIZE, 0, 0
GdiplusShutdown token

FUNCTION = uMsg.wParam

END FUNCTION

cut this last part and copy at the end of my last post, if somebody has fun to test the powerbasic code :)

If I understand how to handle with gdiplus features I can translate some ideas from powerbasic to thinbasic too. but it's more a learning tree for me to understand more about using "sdk" grasping. next step could be to load a different picture format (jpg, png, bmp, ico) and save it.


servus, frank

Lionheart008
07-03-2010, 13:38
good morning all.

hi eros. thanks first of all for this new topic column for "tb_graphx" !

1) one important question how to compile a function like "GetImageThumbnail(dc)" for thinbasic sdk:

my mind says this could be right way, but what I need to use for function?



'---------frankos how to do question with powerbasic :)

FUNCTION Exec_GetImageThumbnail (BYVAL hdc AS DWORD) AS Ext

LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL strFileName AS STRING
LOCAL pImage AS DWORD
LOCAL nWidth AS DWORD
LOCAL nHeight AS DWORD
LOCAL pThumbnail AS DWORD
LOCAL nThumbnailWidth AS DWORD
LOCAL nThumbnailHeight AS DWORD

'---Parse open pares
IF thinBasic_CheckOpenParens() THEN
thinBasic_ParseLong pImage
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong nWidth
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong nHeigth
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong pThumbnail

'---At the end check if close parens was specified
IF thinBasic_CheckCloseParens() THEN

FUNCTION = GdipGetImageThumbnail(pImage, 50, 50, pThumbnail, 0, 0)

END IF
END IF
END IF
END IF
END IF

'--------this part isn't important, isn't it ? I can delete this one ---------???
hStatus = GdipCreateFromHDC(hdc, pGraphics)

' // Create an image and a thumbnail of the image.
strFileName = UCODE$("leoparden1.png")
hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
hStatus = GdipGetImageThumbnail(pImage, 50, 50, pThumbnail, %NULL, %NULL)

' // Draw the original and the thumbnail images.
hStatus = GdipGetImageWidth(pImage, nWidth)
hStatus = GdipGetImageHeight(pImage, nHeight)
hStatus = GdipDrawImageRect(pGraphics, pImage, 70, 70, nWidth, nHeight)

hStatus = GdipGetImageWidth(pThumbnail, nThumbnailWidth)
hStatus = GdipGetImageHeight(pThumbnail, nThumbnailHeight)
hStatus = GdipDrawImageRect(pGraphics, pThumbnail, 200, 20, nThumbnailWidth, nThumbnailHeight)

' // Cleanup
IF pThumbnail THEN GdipDisposeImage(pThumbnail)
IF pImage THEN GdipDisposeImage(pImage)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)


END FUNCTION


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

2) there are two ways for creating and including such "tb_graph" module:
a) with thinbasic ui features ("Dialog new pixels" etcpp) : here user have many
chances and flexibilities to get new commands and influence to Image size/formats

b) complete new own UI for loading Images using GDIplus features with own sdk
window frame code in powerbasic (this must be faster so I have heard about).

3) well, better and easier could be to use "Dialog new pixels" and make some new
commands for special Image Properties (thumbnail, setPixel, noise factor, stretch,
rgba colour switching and much more). I prefered last evening this idea ;)

4) interesting part would however be to create complete new dialogue window forms with
loading/saving/renaming/ changing Image formats and their properties.
(see my next post to point "2b" )

I think loud, because I am not sure what's the best way to do that.

any suggestions would be nice to hear, many thanks, nice sunday, Frank

Petr Schreiber
07-03-2010, 16:55
Ciao Frank,

I must say I consider it not very practical to have function to show dialog with hardwired buttons inside.

You can see UI module in ThinBASIC has Canvas control, which accepts data in BGRA format. So function returning image in this form would be slightly more "general purpose".


Petr

Lionheart008
08-03-2010, 15:36
thanks petr,


I must say I consider it not very practical to have function to show dialog with hardwired buttons inside.

1) Yes, I agree! -> I've decided to go the "classical" way to include new functions for UI module. I will try it. If you (or anybody else) have time and fun to test this little function ? I don't know how to define %NULL ("0)" for thinbasic ?

I can compile this with powerbasic. if I run this example for thinbasic side to test with
for example:


printl GetImageThumbnail(pImage,50,50,1,0,0)

then I've got general protection fault, although my function definition should be correct. I hope so.

powerbasic side:



FUNCTION Exec_GetImageThumbnail (BYVAL hdc AS DWORD) AS EXT

LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL strFileName AS STRING
LOCAL pImage AS DWORD
LOCAL nWidth AS DWORD
LOCAL nHeight AS DWORD
LOCAL pThumbnail AS DWORD
LOCAL nThumbnailWidth AS DWORD
LOCAL nThumbnailHeight AS DWORD
LOCAL pZero AS LONG ' %NULL ?
LOCAL pZero2 AS Long ' %NULL ?

'---Parse open pares
IF thinBasic_CheckOpenParens() THEN
thinBasic_ParseLong pImage
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong nWidth
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong nHeight
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong pThumbnail
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong pZero '?
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong pZero2 '?

'---At the end check if close parens was specified
IF thinBasic_CheckCloseParens() THEN

' Function = GdipGetImageThumbnail(pImage, 50, 50, pThumbnail, %NULL, %NULL)
FUNCTION = GdipGetImageThumbnail(pImage, nWidth, nHeight, pThumbnail, 0, 0)
END IF
END IF
END IF
END IF
END IF
END IF
END IF
hStatus = GdipCreateFromHDC(hdc, pGraphics)

End Function



2) I've tested this gdiplus example, but adding my module the example doesn't work.
I'm thinking this is caused by my powerbasic function, there must be missing little thing.

thinbasic side:

'------------------------------------------------------------------------------
' Load needed modules
'------------------------------------------------------------------------------
Uses "UI"
Module "graphx"

'------------------------------------------------------------------------------
' Equates
'------------------------------------------------------------------------------
Begin Const
%lImage = 1000
%lText
End Const

Function TBMain()
'---Test if module was loaded. If not, abort!
If Uses("GDIP") < 0 Then
MsgBox 0, "GDI+ lib not found"
Stop
End If

'------------------------------------------------------------------------------
' Create dialog
'------------------------------------------------------------------------------
Dim hDlg As DWord

Dialog NEW PIXELS, 0, "thinBasic using GDI+", -1, -1, 400, 326, _
%WS_DLGFRAME Or _
%DS_CENTER Or _
%WS_CAPTION Or _
%WS_SYSMENU Or _
%WS_CLIPCHILDREN Or _
%WS_OVERLAPPEDWINDOW, _
0 To hDlg

'------------------------------------------------------------------------------
' Show dialog
'------------------------------------------------------------------------------
Dialog SHOW MODAL hDlg, Call dlgCallback

End Function
'------------------------------------------------------------------------------
' Main loop
'------------------------------------------------------------------------------
CallBack Function dlgCallback() As Long
Dim JPGFileName As String = APP_SourcePath + "Images\ferrari-enzo.png"
Dim JPGFileName2 As String = APP_SourcePath + "Images\leoparden1.png"
Dim lWidth As Long
Dim lHeight As Long
Dim hBitmap, hGpBitmap As Long
Dim x,y As Long
'Dim pThumbnail as long = 120

Select Case CBMSG

Case %WM_INITDIALOG
'------------------------------------------------------------------------------
' Create constrols and Load images
'------------------------------------------------------------------------------
Control ADD LABEL, CBHNDL, %lImage,"", 20, 20, 380, 300, %SS_BITMAP

'---Create an image handle
hGpBitmap = GDIp_CreateBitMapFromFile(JPGFileName2)
'---Convert to a BMP handle
hBitmap = GDIP_HBitMapFromBitmap(hGpBitmap)

Control SEND CBHNDL, %lImage, %STM_SETIMAGE, %IMAGE_BITMAP, hBitmap
Control REDRAW CBHNDL, %lImage

'---Get image W and H
lWidth = GDIP_GetImageWidth(hGpBitmap)
lHeight = GDIP_GetImageHeight(hGpBitmap)

'-------------------------------------------- frankos new attempt
lWidth = GetImageThumbnail(hGpBitmap,50, 50, 120, 0, 0)
lHeight = GetImageThumbnail(hGpBitmap,50, 50, 120, 0, 0)
'-------- (pImage, 50, 50, pThumbnail, %NULL, %NULL)
'-------------------------------------------- frankos new attempt

Control ADD LABEL, CBHNDL, %lText, "thinBasic GDI+ module wrapper example (" & lWidth & " x " & lHeight & ")", 0, 311, 400, 15, %SS_CENTER
Control SET COLOR CBHNDL, %lText, RGB(0,0,0), RGB(255,128,0)
Control REDRAW CBHNDL, %lText

Control SET RESIZE CBHNDL, %lImage, 1, 1, 1, 1
Control SET RESIZE CBHNDL, %lText , 1, 1, 0, 1

DoEvents

Case %WM_SIZE
Control REDRAW CBHNDL, %lImage

Case %WM_CLOSE
' -- Erase resources
'---Release temp hBitmap
Object_Delete(hBitmap)
'---Release GDI image
GDIP_DisposeImage(hGpBitmap)

End Select

End Function


best regards, thanks, frank

Petr Schreiber
08-03-2010, 16:03
Hi Frank,

on the first place, your module function must not have any parameters:


FUNCTION Exec_GetImageThumbnail (BYVAL hdc AS DWORD) AS EXT

=>


FUNCTION Exec_GetImageThumbnail () AS EXT

Everything you need should be parsed in body. Maybe this caused some problems.


Must goto school, will check it here at night,
Petr

ErosOlmi
08-03-2010, 16:42
Hi Frank,

I said you the same in my personal message.

Module function MUST MUST always be defined without any parameters
Parameters are parsed inside the function using interface functions.
Also all module functions retuning a number MUST return an EXT number. All conversions will be made by thinBasic.

Ciao
Eros

Lionheart008
08-03-2010, 19:01
hello petr, eros, thanks for help and hints, I was too fast this morning, overseen this one, my mistake.

I send my "getImageThumbnail" function as zip file and thinbasic gdiplus example.

1) I am not sure, if this version is quite correct, but the gpf doesn't appear in gdiplus example anymore with loading background (here: leopards) picture.

2) for testing purpose I have made a message box for function feedback, the result is "0". but no more gpf's. for console (printl) test it works in my last example too.

3) would be nice to get feedback by some users, if this added gdiplus example works fine. see my zip file.

4) if the example doesn't work: the mistake does belongs to this thumbnail function, I am sure now.

(my thoughts were: but something's still missing. sorry. my idea is: perhaps I have forgotten to add two more functions for "pGraphic" and "pImage" because in powerbasic function these three functions were deleted at the end of script. )



'---> this function works for powerbasic
'------------------------------------------------------------------


FUNCTION Exec_GetImageThumbnail () AS EXT
LOCAL hdc AS DWORD
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL strFileName AS STRING
LOCAL pImage AS DWORD
LOCAL nWidth AS DWORD
LOCAL nHeight AS DWORD
LOCAL pThumbnail AS DWORD
LOCAL nThumbnailWidth AS DWORD
LOCAL nThumbnailHeight AS DWORD
LOCAL pZero AS LONG ' %NULL ?
LOCAL pZero2 AS LONG ' %NULL ?

'---Parse open pares
IF thinBasic_CheckOpenParens() THEN
thinBasic_ParseLong pImage
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong nWidth
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong nHeight
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong pThumbnail
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong pZero '?
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong pZero2 '?

'---At the end check if close parens was specified
IF thinBasic_CheckCloseParens() THEN

' Function = GdipGetImageThumbnail(pImage, 50, 50, pThumbnail, %NULL, %NULL)

FUNCTION = GdipGetImageThumbnail(pImage, nWidth, nHeight, pThumbnail, 0, 0)

END IF
END IF
END IF
END IF
END IF
END IF
END IF
hStatus = GdipCreateFromHDC(hdc, pGraphics)

' // Create an image and a thumbnail of the image.
strFileName = UCODE$("leoparden1.png")
hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
hStatus = GdipGetImageThumbnail(pImage, 50, 50, pThumbnail, %NULL, %NULL)

' // Draw the original and the thumbnail images.
hStatus = GdipGetImageWidth(pImage, nWidth)
hStatus = GdipGetImageHeight(pImage, nHeight)
hStatus = GdipDrawImageRect(pGraphics, pImage, 70, 70, nWidth, nHeight)

hStatus = GdipGetImageWidth(pThumbnail, nThumbnailWidth)
hStatus = GdipGetImageHeight(pThumbnail, nThumbnailHeight)
hStatus = GdipDrawImageRect(pGraphics, pThumbnail, 200, 20, nThumbnailWidth, nThumbnailHeight)

' // Cleanup
IF pThumbnail THEN GdipDisposeImage(pThumbnail)
IF pImage THEN GdipDisposeImage(pImage)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END FUNCTION



best regards, servus, thanks for looking at the tb script example, Frank

christianssen
09-03-2010, 09:46
hello mr. lionheart :) good morning,


3) would be nice to get feedback by some users, if this added gdiplus example works fine. see my zip file.

I can do that: well, have made a copy. yes, your example works well with windows xp2, amd dual processor with no errors, no protection fault.

your new thumbnail functions will come soon ? I understand you will see what's going on with new module. For testing purpose it's important, I can imagine, that new scripts work at different machines, isn't it?

there will be a new module called "tb_graphx" like "UI" ? for me it sounds good to have new image manipulations / possibilities (properties) for users. I don't know canvas module. but I will check it too next weeks. - it's easy to create new functions? I think it's very hard stuff, spend a lot of time for it to make new module, I see like michael, petr and eros have done this over weeks, month, years. respectable work I can only say! I am not up to date with all posts here. it's nearly impossible if you are off the road for some days ;)

bye, have still much to learn with thinbasic (tbgl, ui), I am on a good way. denis

Lionheart008
09-03-2010, 21:18
hello denis!

1) thanks for your testing help. don't believe it's not necessary, it's the contrary act! it's very useful. I need this help with feedback, without there is no way to improve new features or functions. It takes only a lot of time to create new things and adept a module to thinbasic. there are limited freedoms for that, you have to oblige to hold serious standards for compiling, I had to learn some month too ("liongfx", created from freebasic to thinbasic), but this was a very, very good exercise and knowledge for me to understand how to parse new functions for thinbasic language interpreter. you can follow this board and can learn a lot, I am sure ;)

2) It is important, that I've got feedback for my first function, so I can build next issue to fetch an image by my little "tb_graphx" module and can manipulate (here: size) images for new width, height, making thumbnails and some more. it's a good sign, I get an "0" (means: it works ok!) for thumbnail feedback function and not any more error messages or gpf. I can promise you, be patient, there is more to come !


your new thumbnail functions will come soon ? I understand you will see what's going on with new module. For testing purpose it's important, I can imagine, that new scripts work at different machines, isn't it?

3) I can say this only for myself, but it's a hard and detailed, continuing working at a new module. I will follow this work with enthusiasm and like to have constructive critics for new suggestions and ideas, I am wide open to hear new things! I have to learn for this module a lot, because there's a lot about gdiplus and other image libraries to learn. without learning some basically aspects of powerbasic programming (e.g. freebasic or any other basic language) it wouldn't be possible to create such a module I think. for me it's an increasing learning process for both sides. I have started programming at all with thinbasic (no joke!) and checked java some month before, but I liked the "easy" and "clear" language of thinbasic and of course the engaged team here with a lot of help.

4) In general: don't hesitate to ask something isn't clear for you. if you are able to learn something new e.g. at school, you can use it for tbgl or other module show your results here at the forum and ask if you haven't something understand completely. same play belongs to me.

If there is a new feature or a first good result for a picture manipulation with tb_graphx I will show you here at the new board. wait some days. best and good progress for you with thinbasic learning.

5) I am little proud that this example caused no more errors ;)



...
'-------------------------------------------------------------------
x = GetImageThumbnail(hGpBitmap,150,150,1,0,0) '--------> frankos test !
y = GetImageThumbnail(hGpBitmap,150,150,1,0,0)
'-------------------------------------------------------------------

MsgBox 0, "All ok here with Thumbnail?: " + GetImageThumbnail(hGpBitmap,150,150,1,0,0),%MB_ICONINFORMATION, "test tb_graphx"
PrintL GetImageThumbnail(hGpBitmap,150,150,1,0,0)
...


nice evening, best regards, thanks, frank

Lionheart008
10-03-2010, 16:30
hi all.

I have this little problem how to parse this function correct:


FUNCTION Exec_DrawImageRect () AS EXT
LOCAL hdc AS DWORD
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL strFileName AS STRING
LOCAL pImage AS DWORD
LOCAL nWidth AS DWORD
LOCAL nWidth1 AS DWORD
LOCAL nHeight AS DWORD
LOCAL nHeight1 AS DWORD
LOCAL pThumbnail AS DWORD
LOCAL nThumbnailWidth AS DWORD
LOCAL nThumbnailHeight AS DWORD
LOCAL pZero AS LONG ' %NULL ?
LOCAL pZero2 AS LONG ' %NULL ?

'---Parse open pares
IF thinBasic_CheckOpenParens() THEN
thinBasic_ParseLong pImage
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong nWidth
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong nHeight
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong pThumbnail
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong nWidth1
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong nHeight1

'---At the end check if close parens was specified
IF thinBasic_CheckCloseParens() THEN

FUNCTION = GdipDrawImageRect(pGraphics, pImage, nWidth1, nHeight1, nWidth, nHeight)
'hStatus = GdipDrawImageRect(pGraphics, pImage, 70, 70, nWidth, nHeight)
END IF
END IF
END IF
END IF
END IF
END IF
END IF

End Function

this is right input for users (if everything is ok):

"GdipDrawImageRect(pGraphics, pImage, 70, 70, nWidth, nHeight)"

my problem: what is "70,70" ? this should be a) numbers or b) long ?

If I compile with powerbasic my GDIplus Example for thinbasic side gives feedback for "0" (all ok), but if I am changing the values for example from thinbasic side

"pGraphics, pImage, 0, 0, nWidth, nHeight"

to
"GdipDrawImageRect(pGraphics, pImage, 20, 20, nWidth, nHeight)"

then the thinbasic example breaks, if I change values from 0 to 20 or bigger. I think it's not exactly defined what are these numbers.

2) my thinbasic example is nearly perfect:


'------------------------------------------------------------------------------
' Load needed modules
'------------------------------------------------------------------------------
Uses "UI", "console"
Module "graphx_1xy"

'------------------------------------------------------------------------------
' Equates
'------------------------------------------------------------------------------
Begin Const
%lImage = 1000
%lImage2
%lText
End Const

Function TBMain()
'---Test if module was loaded. If not, abort!
If Uses("GDIP") < 0 Then
MsgBox 0, "GDI+ lib not found"
Stop
End If

'------------------------------------------------------------------------------
' Create dialog
'------------------------------------------------------------------------------
Dim hDlg As DWord

Dialog NEW PIXELS, 0, "thinBasic using GDI+", -1, -1, 400, 326, _
%WS_DLGFRAME Or _
%DS_CENTER Or _
%WS_CAPTION Or _
%WS_SYSMENU Or _
%WS_CLIPCHILDREN Or _
%WS_OVERLAPPEDWINDOW, _
0 To hDlg

'------------------------------------------------------------------------------
' Show dialog
'------------------------------------------------------------------------------
Dialog SHOW MODAL hDlg, Call dlgCallback

End Function
'------------------------------------------------------------------------------
' Main loop
'------------------------------------------------------------------------------
CallBack Function dlgCallback() As Long
Dim JPGFileName As String = APP_SourcePath + "Images\ferrari-enzo.png"
Dim JPGFileName2 As String = APP_SourcePath + "Images\leoparden1.png"
Dim lWidth As Long
Dim lWidth1 As Long

Dim lHeight As Long
Dim lHeight1 As Long
Dim hBitmap,hBitmap1, hGpBitmap,hGpBitmap1, pImage, pGraphics,pThumbnail As Long
Dim x,y,z,v,v1 As Long
Dim nThumbnailWidth, nThumbnailHeight As Long
nThumbnailWidth = 0
nThumbnailHeight = 0
Select Case CBMSG

Case %WM_INITDIALOG
'------------------------------------------------------------------------------
' Create constrols and Load images
'------------------------------------------------------------------------------
Control Add Label, CBHNDL, %lImage,"", 20, 20, 420, 310, %SS_BITMAP

'---Create an image handle
hGpBitmap = GDIp_CreateBitMapFromFile(JPGFileName2)

'---Convert to a BMP handle
hBitmap = GDIP_HBitMapFromBitmap(hGpBitmap)

Control SEND CBHNDL, %lImage, %STM_SETIMAGE, %IMAGE_BITMAP, hBitmap
Control REDRAW CBHNDL, %lImage

'---Get image W and H
'lWidth = GDIP_GetImageWidth(hGpBitmap) '--- old
'lHeight = GDIP_GetImageHeight(hGpBitmap) '--- old

'lWidth = ImageWidth(hGpBitmap,100)
'PrintL ImageWidth(hGpBitmap,100)
'lHeight = ImageHeight(hGpBitmap,100)

z = DrawImageRect(hGpBitmap,pImage, 0,0,200,200)

'lWidth1 = GDIP_GetImageWidth(hGpBitmap1) '--- old
'lHeight1 = GDIP_GetImageHeight(hGpBitmap1) '--- old

'-> new commands ------------------------------------------------------------------
lWidth1 = ImageWidth(hGpBitmap1, 100)'nThumbnailWidth
lHeight1 = ImageHeight(hGpBitmap1,120 )'nThumbnailHeight
v1 = DrawImageRect(hGpBitmap1, pImage, 0, 0, 400, 400)

x = GetImageThumbnail(hGpBitmap,150,150,1,0,0) '--------> frankos test !
y = GetImageThumbnail(hGpBitmap,150,150,1,0,0)

MsgBox 0, "All ok here with Thumbnail ?: " + GetImageThumbnail(hGpBitmap,150,150,1,0,0),%MB_ICONINFORMATION, "test tb_graphx"
MsgBox 0, "All ok here with DrawImage ?: " + DrawImageRect(hGpBitmap,pImage, 0,0,100,100),%MB_ICONINFORMATION, "test tb_graphx"
MsgBox 0, "all ok with ImageRect+pThumbnail ?: " + DrawImageRect(hGpBitmap, pThumbnail, 0, 0, nThumbnailWidth, nThumbnailHeight),%MB_ICONINFORMATION, "test tb_graphx"

'-> new commands ------------------------------------------------------------------

PrintL GetImageThumbnail(hGpBitmap,150,150,1,0,0)
PrintL DrawImageRect(hGpBitmap,pImage,0,0,100,100)
'PrintL DrawImageRect(hGpBitmap, pThumbnail, 0, 0, nThumbnailWidth, nThumbnailHeight)
PrintL DrawImageRect(hGpBitmap, pThumbnail, 0, 0, 40, 40)

Control ADD LABEL, CBHNDL, %lText, "thinBasic GDI+ module wrapper example (" & lWidth & " x " & lHeight & ")", 0, 311, 400, 15, %SS_CENTER
Control SET COLOR CBHNDL, %lText, RGB(0,0,0), RGB(255,128,0)
Control REDRAW CBHNDL, %lText

Control Set Resize CBHNDL, %lImage, 1, 1, 1, 1
Control SET RESIZE CBHNDL, %lText , 1, 1, 0, 1

DoEvents

Case %WM_SIZE
Control Redraw CBHNDL, %lImage

Case %WM_CLOSE
' -- Erase resources
'---Release temp hBitmap
Object_Delete(hBitmap)
'---Release GDI image
GDIP_DisposeImage(hGpBitmap)

End Select

End Function

I add the file plus DLL for testing.

3) four new commands get positive feedback :)


MsgBox 0, "All ok here with Thumbnail ?: " + GetImageThumbnail(hGpBitmap,150,150,1,0,0),%MB_ICONINFORMATION, "test tb_graphx"
MsgBox 0, "All ok here with DrawImage ?: " + DrawImageRect(hGpBitmap,pImage, 0,0,100,100),%MB_ICONINFORMATION, "test tb_graphx"
MsgBox 0, "all ok with ImageRect+pThumbnail ?: " + DrawImageRect(hGpBitmap, pThumbnail, 0, 0, nThumbnailWidth, nThumbnailHeight),%MB_ICONINFORMATION, "test tb_graphx"
lWidth1 = ImageWidth(hGpBitmap1, 100)'nThumbnailWidth
lHeight1 = ImageHeight(hGpBitmap1,120 )'nThumbnailHeight



perhaps anybody has a good tip for me.

best regards, frank

Michael Hartlef
10-03-2010, 18:55
Hi Franck,

check the order you parse your parameters. They don't match your command syntax.

You are parsing right now

GdipDrawImageRect(pImage, nWidth, nHeight, pThumbnail, nWidth1, nHeight1)

Lionheart008
10-03-2010, 21:43
thanks michael, I should take a big picture at my wall: "don't work after midnight, if you are tired!" ;) :violent: I see, I have copied this function from notebook to desktop pc but haven't cleared the code this early, early morning. my mistake.

ok, example works. I can change values like I wish.

now I try to loadImageFromFile and GetImageWidth / GetImageHeight so I am finished ;)

(hGpBitmap = LoadImageFromFile(StrPtr(strFileName), pImage )

if I have checked all parameters by parsing I can use my module for first working examples.

nice evening, thanks for taking attention to my post, Frank

Michael Hartlef
10-03-2010, 22:12
thanks michael, I should take a big picture at my wall: "don't work after midnight, if you are tired!" ;)


Been there, did that too. I think it happens to all of us.

Lionheart008
17-03-2010, 18:39
hello.

sorry, but I don't know how to parse this well compiled example from powerbasic side to thinbasic side or how to use this command for thinbasic side:

powerbasic:

FUNCTION Exec_LoadImageFromFile() AS EXT
LOCAL hdc AS DWORD
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL strFileName AS STRING
LOCAL strUFileName AS STRING
LOCAL pImage AS DWORD


'---Parse open pares
IF thinBasic_CheckOpenParens() THEN
IF thinBasic_CheckOpenParens() THEN
thinBasic_ParseString strFileName
IF thinBasic_CheckCloseParens() THEN
IF thinBasic_CheckComma() THEN
thinBasic_ParseLong pImage
IF thinBasic_CheckCloseParens() THEN

'hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
FUNCTION = GdipLoadImageFromFile(STRPTR(strFileName), pImage) '--> correct!

'strUFileName = ucode$(strFileName)
'GdipLoadImageFromFile(strUFileName, pImage) '--> doesn't work

END IF
END IF
END IF
END IF
END IF
END FUNCTION


my problem is how to program "STRPTR(strFileName), pImage" correct for thinbasic side using this command:

thinbasic side (for example):


hGpBitmap = LoadImageFromFile(strFileName, pImage) '--> not ok

or

hGpBitmap = LoadImageFromFile(strptr(strFileName), pImage) '--> not ok


perhaps anybody has experience with this case, would be great. best regards, frank

Michael Hartlef
17-03-2010, 19:36
Most likely any windows api needs a pointer to a null terminated string. So if you want to parse a string,

1) define strFileName as an ASCII string withz maybe 256 characters of length
2) Your StrPtr approach should work then.

Look into the source of my TBDI module. Look where I load the effect files. Should work similar.

Petr Schreiber
17-03-2010, 20:23
Also do not forget the string has to be unicode, so UCODE$ should serve well.


Petr

Lionheart008
19-03-2010, 17:57
thanks michael, petr :)

do for me a little favour? somebody can test this tb example ? would be nice to hear what kind of results you're getting.

I am thinking it's just missing one special line or command at powerbasic side, so this part is ready and I can load images (png, jpg, bmp).

simple test example only for feedback:

'------------------------------------------------------------------------------
' Load needed modules
'------------------------------------------------------------------------------
Uses "UI", "console"
Module "graphx_1xy"

'------------------------------------------------------------------------------
' Equates
'------------------------------------------------------------------------------
Begin Const
%lImage = 1000
%lImage2
%lText
End Const

Function TBMain()
'---Test if module was loaded. If not, abort!
If Uses("GDIP") < 0 Then
MsgBox 0, "GDI+ lib not found"
Stop
End If

'------------------------------------------------------------------------------
' Create dialog
'------------------------------------------------------------------------------
Dim hDlg As DWord

Dialog New Pixels, 0, "frankos thinBasic using tb_graphx/GDI+", -1, -1, 400, 326, _
%WS_DLGFRAME Or _
%DS_CENTER Or _
%WS_CAPTION Or _
%WS_SYSMENU Or _
%WS_CLIPCHILDREN Or _
%WS_OVERLAPPEDWINDOW, _
0 To hDlg

'------------------------------------------------------------------------------
' Show dialog
'------------------------------------------------------------------------------
Dialog SHOW MODAL hDlg, Call dlgCallback

End Function
'------------------------------------------------------------------------------
' Main loop
'------------------------------------------------------------------------------
CallBack Function dlgCallback() As Long
Dim JPGFileName As Asciiz*256 = APP_SourcePath + "Images\ferrari-enzo.png"
Dim JPGFileName2 As Asciiz*256 = APP_SourcePath + "Images\leoparden1.png"
Dim strFileName As Asciiz*256 = APP_SourcePath + "Images\leoparden1.png"
Dim strUFileName As Asciiz*256 = APP_SourcePath + "Images\leoparden1.png" 'String
Dim lWidth As Long
Dim lWidth1 As Long
Dim lHeight As Long
Dim lHeight1 As Long
Dim hBitmap,hBitmap1, hGpBitmap,hGpBitmap1, pImage, pGraphics,pThumbnail As Long
Dim x,y,z,v,v1 As Long
Dim nThumbnailWidth, nThumbnailHeight As Long

Select Case CBMSG

Case %WM_INITDIALOG
'------------------------------------------------------------------------------
' Create constrols and Load images
'------------------------------------------------------------------------------
Control Add Label, CBHNDL, %lImage,"", 40, 40, 420, 310, %SS_BITMAP

v = DrawImageRect(hGpBitmap1, hBitmap, 10, 10, 400, 400)

MsgBox 0, "getImageThumbnail feedback: " + GetImageThumbnail(hGpBitmap,40,40,10,10,10)
MsgBox 0, "yes DrawImageRect!: " + DrawImageRect(hGpBitmap,hBitmap, 10,10,200,200)

lWidth = ImageWidth(hGpBitmap,10)
MsgBox 0, "width ok? " + lWidth
lHeight = ImageHeight(hGpBitmap,20)
MsgBox 0, "height ok? " + lHeight

MsgBox 0, "load image ok: " + LoadImageFromFile(JPGFileName2, hBitmap)

MsgBox 0, "pic: " + StrPtr(jpgfilename2) '--> only for test purpose


Control Send CBHNDL, %lImage, %STM_SETIMAGE, %IMAGE_BITMAP, hBitmap
Control Redraw CBHNDL, %lImage

Control Add Label, CBHNDL, %lText, "thinBasic tb_graphx/GDI+ module wrapper example (" & lWidth & " x " & lHeight & ")", 0, 311, 400, 15, %SS_CENTER
Control SET COLOR CBHNDL, %lText, RGB(0,0,0), RGB(255,128,0)
Control REDRAW CBHNDL, %lText

Control Set Resize CBHNDL, %lImage, 1, 1, 1, 1
Control SET RESIZE CBHNDL, %lText , 1, 1, 0, 1

DoEvents

Case %WM_SIZE
Control Redraw CBHNDL, %lImage

Case %WM_CLOSE
' -- Erase resources
'---Release temp hBitmap
Object_Delete(hBitmap)
'---Release GDI image
'GDIP_DisposeImage(hGpBitmap)

End Select
'PrintL GetImageThumbnail(hGpBitmap,40,40,10,10,10)
End Function

all you need I send as zip file. / I will break through this wall and understand gdiplus ;)

best thanks, frank

Lionheart008
25-03-2010, 20:18
hello all.

sorry, no new success but ten per cent smarter, some explanations and notices:


Load image can handle bitmaps, cursors and icons, not .jpg, .gif or .png files.


That's the main reason why I don't get any picture to show at my display. I will find a new way. I have done everything right with parsing and compiling. It's just the cored disadvantage why gdiplus don't load straight the way png, jpgs or gifs. Now I know why you can only use BMP Files for loading and then converting in jpgs, png's and much more. my idea was to load png, jpgs via directly input.



...Before discussing the code itself, there is a caveat with GDI+ that must be addressed. With JPG, some TIFF and other formats, the original image information must be available at all times. In other words, if you open a bitmap using Bitmap::FromFile, you cannot delete or otherwise change that file while the image is open. This same restriction applies to CGdiPlusBitmapResource. (My testing found that PNG and BMP files don't seem to follow this generalization, though I don't know if this is standard behavior or just a fluke with my file set.)...

It takes only three days to find this error behaviour. Good to know and learn much more about gdiplus. If there is any new success in this direction I will show my results.

At the moment I have few time because there are some professional things I am involved, but I will have a closer look at this "tb_graphx" chapter, because I will get the gold medaille to win this little eager cup to calm my own curiosity ;)


After a bit of testing and several errors, due mostly to the horrible GDI+ documentation, I came up with working code. After a night of rest, I decided to encapsulate the code in a simple class to ensure memory got freed. The result were two classes: "CGdiPlusBitmap" and "CGdiPlusBitmapResource".


If I load one of my first example with steer powerbasic created dialog (included gdiplus feature) I can load this PNG, JPG, GIF image I wished to have but not for thinbasic side. This lacked hole I will find to solve.

thanks for taking attention. best regards, frank lionhead