View Full Version : draw paint Demo (canvas) :)
Lionheart008
23-11-2009, 18:18
hello all.
I have a simple question what would be the best way to draw a "line", "circle" or "box" by user like illustrator application or corel draw app. with thinbasic UI module? no joke. I have used canvas for my example, but I am not satisfied with this one. may be the MDI features are made better for this draw modus ? I have no experience with this case, perhaps anybody can make some suggestions. thanks in advance.
best regards, Frank
Petr Schreiber
23-11-2009, 20:22
Hi Frank,
what does it mean "not satisfied". It doesn't work?
Here is bare bones drawing example:
Uses "UI"
' -- ID numbers of controls
Begin Const
%bClose = 1000
%cCanvas
End Const
' -- Create dialog here
Function TBMAIN()
Local hDlg As DWord
Dialog NEW 0, "Click to paint",-1,-1, 320, 220, _
%WS_POPUP Or %WS_VISIBLE Or %WS_CAPTION Or %WS_SYSMENU Or %WS_MINIMIZEBOX To hDlg
' -- Place controls here
Control ADD CANVAS, hDlg, %cCanvas, "", 5, 5, 310, 190
Control ADD BUTTON, hDlg, %bClose, "Click to close", 255, 200, 60, 14, Call bCloseProc
Dialog SHOW MODAL hDlg, Call dlgProc
End Function
' -- Callback for dialog
CallBack Function dlgProc()
Dim mousePosition As POINTAPI
Static lastMousePosition As POINTAPI
Dim locX, locY As Long
Dim lastPos As POINTAPI
' -- Test for messages
Select Case CBMSG
Case %WM_INITDIALOG
' -- Put code to be executed after dialog creation here
CANVAS_Attach(CBHNDL, %cCanvas, %FALSE)
CANVAS_Clear(%BLACK)
CANVAS_Scale PIXELS
Case %WM_MOUSEMOVE
If CBWPARAM = %MK_LBUTTON Then
' -- Get mouse position, convert it to canvas local coordinates
Control GET LOC CBHNDL, %cCanvas To locX, locY
WIN_GetCursorPos(mousePosition)
WIN_ScreenToClient(CBHNDL, mousePosition)
mousePosition.x -= locX
mousePosition.y -= locY
' -- Paint a line
If lastMousePosition.x > -1 Then
CANVAS_Line((lastMousePosition.x, lastMousePosition.y), (mousePosition.x, mousePosition.y), %WHITE)
CANVAS_Redraw
End If
lastMousePosition.x = mousePosition.x
lastMousePosition.y = mousePosition.y
End If
Case %WM_LBUTTONDOWN
' -- Get mouse position, convert it to canvas local coordinates
Control GET LOC CBHNDL, %cCanvas To locX, locY
WIN_GetCursorPos(lastMousePosition)
WIN_ScreenToClient(CBHNDL, lastMousePosition)
lastMousePosition.x -= locX
lastMousePosition.y -= locY
Case %WM_LBUTTONUP
lastMousePosition.x = -1
lastMousePosition.y = -1
Case %WM_CLOSE
' -- Put code to be executed before dialog end here
End Select
End Function
' -- Callback for close button
CallBack Function bCloseProc()
If CBMSG = %WM_COMMAND Then
If CBCTLMSG = %BN_CLICKED Then
' -- Closes the dialog
Dialog End CBHNDL
End If
End If
End Function
Lionheart008
23-11-2009, 23:46
hello petr, all.
thanks for your example! I have used another example with different ui, but I have done a very long and complicate way (tried to include some kind of "graphic*.inc") to success for painting ;)
better to make a clear, more simpler issue: I am showing one paint example you can see what you are able do with this application in nearly future. in a short time (some days) there will more to come (one step after another) with "tb_paintMe".
I add only one Demo.exe you can draw some line for text or graphic shapes. try and risk it for fun! running exe file I add. this idea of "tb_paint_demo" is exactly one year old.
best regards, frank
TomLebowski
24-11-2009, 15:25
hello frank. thank you for this ineresting draw example! I was looking for such nice things. I am curious how you continue. if I canhelp, say to me. good luck. tom
Lionheart008
25-11-2009, 20:52
good evening.
I have a serious question about how I can get the mouse position / handling for
a) canvas_MouseGetRButton (I wish to have this command for ui/canvas module)
b) canvas_MouseGetPosX (I wish to have this command for ui/canvas module)
for tbgl this commands already exists.
added: how I can handle this behaviour for canvas ui ???
Case %WM_MOUSEMOVE
gMousePt.X = LO(WORD,lParam)
gMousePt.Y = HI(WORD,lParam) ' Current Mouse X and Y Position
IF glbdown THEN
'msgbox 0, "StartDrag"
startpt = gmousept
WHILE dragdetect (hWnd, startpt)
SLEEP 2
getcursorpos ( dragpt) ' screen coordinates
screentoclient(hWnd, dragpt) ' window coordinates
'msgbox 0, "DrawDrag", dragpt.x, dragpt.y
WEND
'msgbox 0, "EndDrag"
gmousept = dragpt
END IF
any help would be appreciated. thanks. frank
Petr Schreiber
25-11-2009, 21:51
Hi Frank,
everything you need is in the example I posted, really!
All you need to do is to make simple wrapper.
I will not torture you, here you have it, but please, when I give you code, try to analyze it, you will see everything is there.
So here you have the functions (did not tested, I let the tweaking on you):
Function Frank_CanvasMouseGetPosX(hDlg As DWord, id As Long) As Long
Local mousePosition As POINTAPI
Local locX, locY As Long
WIN_GetCursorPos(mousePosition)
WIN_ScreenToClient(hDlg, mousePosition)
Control GET LOC hDlg, id To locX, locY
mousePosition.x -= locX
mousePosition.y -= locY
Return mousePosition.x
End Function
Function Frank_CanvasMouseGetPosY(hDlg As DWord, id As Long) As Long
Local mousePosition As POINTAPI
Local locX, locY As Long
WIN_GetCursorPos(mousePosition)
WIN_ScreenToClient(hDlg, mousePosition)
Control GET LOC hDlg, id To locX, locY
mousePosition.x -= locX
mousePosition.y -= locY
Return mousePosition.y
End Function
Function Frank_CanvasMouseGetLButton() As Long
GetAsyncKeyState(%VK_LBUTTON)
Return GetAsyncKeyState(%VK_LBUTTON)
End Function
Function Frank_CanvasMouseGetRButton() As Long
GetAsyncKeyState(%VK_RBUTTON)
Return GetAsyncKeyState(%VK_RBUTTON)
End Function
Lionheart008
27-11-2009, 00:08
hello petr.
thanks first of all for your example. I have mixed my two draw examples. one I have built three days before you have sent your bare-bone-example and then I have updated all things with my first not-very-perfect example I have written as you can see in my first post. better for me to ask more for details in such cases and check all with calm. I am truly and didn't want to have one kind of solution for it. I am learning more to jump over hurdles after doing experiments and find the best way for my task. I have checked your last code example and see it's not necessary to have new canvas_mousgetpos or canvas_MouseGetRButton features. :blush: I think you know what I am meaning ;) thanks.
nice evening. frank