ReneMiner
19-10-2013, 23:48
I have some problem concerning getting correct mouseposition inside canvas.
Canvas supposed to have Pixels, Mouse probably also has Pixels... in the end I'm only interested to have in Mouse.X and Mouse.Y the correct coordinates in relation to canvas.
Problem is: I can not use Dialog New Pixels on create because then the controls and the dialog are different sized than in Resed designed. But I need the exact mouse-position inside canvas in Pixels...
Here is some script I created using my rc-translator. (I shortened a little) - keep eyes on the callback-function
Problem is it does not give me correct X and Y - my head is smoking already - what can I do?
I tried lot of different ways already and now I'm at some point where fun begins to end... :suicide:
'[] Top of script
Uses "UI"
' code created from .rc-file
' C:\Users\René\Downloads\RCIMPORT\someTest.rc
' 2013-10-19 21:26:10
'[] ControlIDs
Begin ControlID
%ID_CANVAS
%IDC_EDT1
End ControlID
Type t_Mouse
X As Long
Y As Long
LButton As Long
MButton As Long
RButton As Long
Wheel As Long
' isOver As DWord ' only needed for multiple dialog apps
End Type
Dim Mouse As t_Mouse
Boolean keepRunning = TRUE
DWord IDD_DLG1
' - - - - - - - - - - - - - - - - - - - -
Function IDD_DLG1_StartUp(ByVal hParent As DWord) As DWord
DWord hDlg, hFont
hDlg = Dialog New hParent,
"Try to resize dialog...",
10,10,330,260,
%WS_VISIBLE | %WS_OVERLAPPEDWINDOW
hFont = Font_Create("MS Sans Serif", 12)
Dialog Send hDlg, %WM_SETFONT, hFont, 0
Control Add Canvas hDlg, %ID_CANVAS, "", 60, 70, 198, 180
Control Set Resize hDlg, %ID_CANVAS, 1, 1, 1, 1
Control Add "Edit", hDlg, %IDC_EDT1, "", 18, 9, 273, 48, %WS_CHILD | %WS_VISIBLE | %WS_TABSTOP | %ES_MULTILINE, %WS_EX_CLIENTEDGE
Function = hDlg
End Function
'[] Callbacks
' - - - - - - - - - - - - - - - - - - - -
CallBack Function IDD_DLG1_Callback()
' ##############################
Static canvasX, canvasY As Long
Static canvasW, canvasH As Long
' ##############################
Select Case CBMSG
Case %WM_INITDIALOG
' -- Put code to be executed after dialog creation here
Canvas_Attach(CBHNDL, %ID_Canvas, %FALSE)
Canvas_Clear(Rgb(0,0,0))
Canvas_Scale Pixels
Canvas_Redraw
' initial memorize canvas x,y,w & h
Control Get Loc CBHNDL, %ID_Canvas To canvasX, canvasY
Control Get Size CBHNDL, %ID_Canvas To canvasW, canvasH
Dialog Units CBHNDL, canvasW, canvasH To Pixels canvasW, canvasH
Case %WM_SIZE, %WM_SIZING
' -- react on resize here
' if form resizes need to check canvas if used Control Set Resize on startup
Control Get Loc CBHNDL, %ID_Canvas To canvasX, canvasY
Control Get Size CBHNDL, %ID_Canvas To canvasW, canvasH
Dialog Units CBHNDL, canvasW, canvasH To Pixels canvasW, canvasH
Case %WM_MOUSEWHEEL
Mouse.Wheel = Sgn(HI(Integer, CBWPARAM))
Case %WM_LBUTTONDOWN
Mouse.LButton = 1
Case %WM_LBUTTONUP
Mouse.LButton = -1
Case %WM_MBUTTONDOWN
Mouse.MButton = 1
Case %WM_MBUTTONUP
Mouse.MButton = -1
Case %WM_RBUTTONDOWN
Mouse.RButton = 1
Case %WM_RBUTTONUP
Mouse.RButton = -1
Case %WM_MOUSEMOVE
Mouse.X = LO(Integer, CBLPARAM)
Mouse.Y = HI(Integer, CBLPARAM)
Dialog Pixels CBHNDL, Mouse.X, Mouse.Y To Units Mouse.X, Mouse.Y
Mouse.X -= canvasX
Mouse.Y -= canvasY
Dialog Units CBHNDL, Mouse.X, Mouse.Y To Pixels Mouse.X, Mouse.Y
Mouse.LButton = IIf((CBWPARAM And %MK_LBUTTON), 2, 0)
Mouse.MButton = IIf((CBWPARAM And %MK_MBUTTON), 2, 0)
Mouse.RButton = IIf((CBWPARAM And %MK_RBUTTON), 2, 0)
String sOut = "Mouse.X = " + Mouse.X + $CRLF
sOut += "Mouse.Y = " + Mouse.Y + $CRLF
If All (_
Between( Mouse.X, 0, CanvasW-1), _
Between( Mouse.Y, 0, CanvasH-1) _
) Then
sOut += "INSIDE CANVAS"
Else
sOut += "OUTSIDE CANVAS"
EndIf
Control Set Text CBHNDL, %IDC_EDT1, sOut
Control Redraw CBHNDL, %IDC_EDT1
Case %WM_CLOSE
' -- Put code to be executed before dialog end here
keepRunning = FALSE
End Select
End Function
' --------------------------------------------------
Function TBKeyboard() As Long
Static vk(255) As Byte
Static i As Long
For i = 1 To 255
If GetAsyncKeyState(i) Then
Select Case i
Case %VK_SHIFT, %VK_LSHIFT, %VK_RSHIFT, %VK_MENU, %VK_LMENU, %VK_RMENU, %VK_CONTROL, %VK_LCONTROL, %VK_RCONTROL
Nop
Case Else
If vk(i) = 0 Then Function = i
End Select
vk(i) = 1
Else
vk(i) = 0
EndIf
Next
End Function
' --------------------------------------------------
Function TBMain()
Long numberOfDialogsAlive
IDD_DLG1 = IDD_DLG1_StartUp(%HWND_DESKTOP)
Dialog Show Modeless IDD_DLG1 Call IDD_DLG1_Callback
While keepRunning
Dialog DoEvents To NumberOfDialogsAlive
Select Case TBKeyboard
Case %VK_ESCAPE
keepRunning = FALSE
End Select
Wend
End Function
'[] End of script
Please make me smile :unguee:
Canvas supposed to have Pixels, Mouse probably also has Pixels... in the end I'm only interested to have in Mouse.X and Mouse.Y the correct coordinates in relation to canvas.
Problem is: I can not use Dialog New Pixels on create because then the controls and the dialog are different sized than in Resed designed. But I need the exact mouse-position inside canvas in Pixels...
Here is some script I created using my rc-translator. (I shortened a little) - keep eyes on the callback-function
Problem is it does not give me correct X and Y - my head is smoking already - what can I do?
I tried lot of different ways already and now I'm at some point where fun begins to end... :suicide:
'[] Top of script
Uses "UI"
' code created from .rc-file
' C:\Users\René\Downloads\RCIMPORT\someTest.rc
' 2013-10-19 21:26:10
'[] ControlIDs
Begin ControlID
%ID_CANVAS
%IDC_EDT1
End ControlID
Type t_Mouse
X As Long
Y As Long
LButton As Long
MButton As Long
RButton As Long
Wheel As Long
' isOver As DWord ' only needed for multiple dialog apps
End Type
Dim Mouse As t_Mouse
Boolean keepRunning = TRUE
DWord IDD_DLG1
' - - - - - - - - - - - - - - - - - - - -
Function IDD_DLG1_StartUp(ByVal hParent As DWord) As DWord
DWord hDlg, hFont
hDlg = Dialog New hParent,
"Try to resize dialog...",
10,10,330,260,
%WS_VISIBLE | %WS_OVERLAPPEDWINDOW
hFont = Font_Create("MS Sans Serif", 12)
Dialog Send hDlg, %WM_SETFONT, hFont, 0
Control Add Canvas hDlg, %ID_CANVAS, "", 60, 70, 198, 180
Control Set Resize hDlg, %ID_CANVAS, 1, 1, 1, 1
Control Add "Edit", hDlg, %IDC_EDT1, "", 18, 9, 273, 48, %WS_CHILD | %WS_VISIBLE | %WS_TABSTOP | %ES_MULTILINE, %WS_EX_CLIENTEDGE
Function = hDlg
End Function
'[] Callbacks
' - - - - - - - - - - - - - - - - - - - -
CallBack Function IDD_DLG1_Callback()
' ##############################
Static canvasX, canvasY As Long
Static canvasW, canvasH As Long
' ##############################
Select Case CBMSG
Case %WM_INITDIALOG
' -- Put code to be executed after dialog creation here
Canvas_Attach(CBHNDL, %ID_Canvas, %FALSE)
Canvas_Clear(Rgb(0,0,0))
Canvas_Scale Pixels
Canvas_Redraw
' initial memorize canvas x,y,w & h
Control Get Loc CBHNDL, %ID_Canvas To canvasX, canvasY
Control Get Size CBHNDL, %ID_Canvas To canvasW, canvasH
Dialog Units CBHNDL, canvasW, canvasH To Pixels canvasW, canvasH
Case %WM_SIZE, %WM_SIZING
' -- react on resize here
' if form resizes need to check canvas if used Control Set Resize on startup
Control Get Loc CBHNDL, %ID_Canvas To canvasX, canvasY
Control Get Size CBHNDL, %ID_Canvas To canvasW, canvasH
Dialog Units CBHNDL, canvasW, canvasH To Pixels canvasW, canvasH
Case %WM_MOUSEWHEEL
Mouse.Wheel = Sgn(HI(Integer, CBWPARAM))
Case %WM_LBUTTONDOWN
Mouse.LButton = 1
Case %WM_LBUTTONUP
Mouse.LButton = -1
Case %WM_MBUTTONDOWN
Mouse.MButton = 1
Case %WM_MBUTTONUP
Mouse.MButton = -1
Case %WM_RBUTTONDOWN
Mouse.RButton = 1
Case %WM_RBUTTONUP
Mouse.RButton = -1
Case %WM_MOUSEMOVE
Mouse.X = LO(Integer, CBLPARAM)
Mouse.Y = HI(Integer, CBLPARAM)
Dialog Pixels CBHNDL, Mouse.X, Mouse.Y To Units Mouse.X, Mouse.Y
Mouse.X -= canvasX
Mouse.Y -= canvasY
Dialog Units CBHNDL, Mouse.X, Mouse.Y To Pixels Mouse.X, Mouse.Y
Mouse.LButton = IIf((CBWPARAM And %MK_LBUTTON), 2, 0)
Mouse.MButton = IIf((CBWPARAM And %MK_MBUTTON), 2, 0)
Mouse.RButton = IIf((CBWPARAM And %MK_RBUTTON), 2, 0)
String sOut = "Mouse.X = " + Mouse.X + $CRLF
sOut += "Mouse.Y = " + Mouse.Y + $CRLF
If All (_
Between( Mouse.X, 0, CanvasW-1), _
Between( Mouse.Y, 0, CanvasH-1) _
) Then
sOut += "INSIDE CANVAS"
Else
sOut += "OUTSIDE CANVAS"
EndIf
Control Set Text CBHNDL, %IDC_EDT1, sOut
Control Redraw CBHNDL, %IDC_EDT1
Case %WM_CLOSE
' -- Put code to be executed before dialog end here
keepRunning = FALSE
End Select
End Function
' --------------------------------------------------
Function TBKeyboard() As Long
Static vk(255) As Byte
Static i As Long
For i = 1 To 255
If GetAsyncKeyState(i) Then
Select Case i
Case %VK_SHIFT, %VK_LSHIFT, %VK_RSHIFT, %VK_MENU, %VK_LMENU, %VK_RMENU, %VK_CONTROL, %VK_LCONTROL, %VK_RCONTROL
Nop
Case Else
If vk(i) = 0 Then Function = i
End Select
vk(i) = 1
Else
vk(i) = 0
EndIf
Next
End Function
' --------------------------------------------------
Function TBMain()
Long numberOfDialogsAlive
IDD_DLG1 = IDD_DLG1_StartUp(%HWND_DESKTOP)
Dialog Show Modeless IDD_DLG1 Call IDD_DLG1_Callback
While keepRunning
Dialog DoEvents To NumberOfDialogsAlive
Select Case TBKeyboard
Case %VK_ESCAPE
keepRunning = FALSE
End Select
Wend
End Function
'[] End of script
Please make me smile :unguee: