beginner (?) windows event handling
Posted: Tue Nov 11, 2003 11:04 pm
not sure if it belongs here or in the beginners area...
here's a small sample using windows / messaging etc. including TrakMouseEvent_()
(it's now on the survival guide pages as well in the snippets file)
here's a small sample using windows / messaging etc. including TrakMouseEvent_()
(it's now on the survival guide pages as well in the snippets file)
Code: Select all
; purebasic survival guide
; messages_1 - 10.11.2003 ejn (blueznl)
; http://www.xs4all.nl/~bluez/datatalk/pure1.htm
;
; - opening a window, and processing messages using WaitWindowEvent()
; - how to use TrackMouseEvent_()
; - retrieve cursor (mouse) coordinates from a windows WM_MOUSEMOVE event
;
;
;
IncludeFile "x_lib.pb"
;
x_init()
;
screen_w.l = GetSystemMetrics_(#SM_CXSCREEN)
screen_h.l = GetSystemMetrics_(#SM_CYSCREEN)
;
main_nr.l = x_nr()
main_whnd.l = OpenWindow(main_nr,300,350,400,200,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget,"Test")
;
; wanna' have a message to notify the mouse leaving the window? use the trackmouseevent_() function
; here's the structure and the constants we might need later (uncomment when needed)
;
; Structure tagTRACKMOUSEEVENT
; cbSize.l
; dwFlags.l
; hwndTrack.l
; dwHoverTime.l
; EndStructure
; #TME_LEAVE = 2
;
#WM_MOUSELEAVE = $2A3
;
UseWindow(main_nr)
;
mouselmb.l = #False
done.l = #False
Repeat
;
; processing windows messages using waitevent() instead of callback
;
event.l = WaitWindowEvent() ; wait for an event
window.l = EventWindowID() ; on which window?
;
Select event
Case #WM_KEYDOWN
Debug "256 #WM_KEYDOWN key pressed"
event_parameter.l = EventwParam() ; undocumented / deprecated / windows only
Select event_parameter ; eventwparam() gets additional info on event
Case #VK_ESCAPE
done=#True
EndSelect
Case #PB_Event_Menu
Debug "13101 #PB_Event_Menu menu"
Case #PB_Event_CloseWindow
; Debug "16 #PB_Event_CloseWindow close window"
done = #True
Case 5
; Debug "5 ? resize"
Case #PB_Event_Repaint
; Debug "15 #PB_Event_Repaint repaint #WM_SYSCOLORCHANGE"
Case 160
; Debug "160 mouse over dragbar"
Case 161
; Debug "161 window move"
Case 275
; Debug "275 ? focus / activation"
Case #WM_MOUSEMOVE
; Debug "512 $200 #WM_MOUSEMOVE mouse moved over window"
;
; EventwParam() contains info on variuos virtual keys
; EventlParam() contains the cursor coords
;
; the implementation of mousecoordinates is somewhat fuzzy in purebasic, they are
; reported in relation to the upper left corner of the window, not of the client area
; using the EventlParam() field an accurate position can be retrieved
;
mousex.l = EventlParam() % 65536
mousey.l = EventlParam() / 65536
;
; if you want to use the build in commands, you have to compensate for the size
; of window borders etc.
;
; mousex.l = WindowMouseX()-GetSystemMetrics_(#SM_CYSIZEFRAME)
; mousey.l = WindowMouseY()-GetSystemMetrics_(#SM_CYCAPTION)-GetSystemMetrics_(#SM_CYSIZEFRAME)
;
; if you want To detect when the mouse leaves the window, you could set a TrackMouseEvent_()
;
; mouseleave.tagTRACKMOUSEEVENT
; mouseleave\cbSize = SizeOf(tagTRACKMOUSEEVENT)
; mouseleave\dwFlags = #TME_LEAVE
; mouseleave\hwndTrack = main_whnd
; TrackMouseEvent_(@mouseleave)
;
; when using SetCapture_() windows 'prefilters' messages, that is you won't receive
; messages unless another condition is met (virtual keys, mousebuttons, etc.)
; so you can't use SetCapture() to see if the cursor left the area with no button
; pressed
;
;
Case #WM_LBUTTONDOWN
;
; Debug "513 $201 #WM_LBUTTONDOWN lmb down"
;
mouselmb = #True
;
; if a mousebutton is pressed, and the cursor is moved outside the window client area
; no more messages will be received, including the WM_LBUTTONUP message
; to make sure such a message is received grab all messages until we got what we
; want
;
SetCapture_(main_whnd)
;
Case #WM_LBUTTONUP
;
; Debug "514 $202 #WM_LBUTTONUP lmb up"
;
mouselmb = #False
;
ReleaseCapture_()
;
Case 516
; rmb down
Case 517
; rmb up
Case 519
; mmb down
Case 520
; mmb up
Case 522
event_parameter.l = EventwParam()
If event_parameter>0
Debug "522 $20A eventwparam>0 scrollwheel up"
Else
Debug "522 $20A eventwparam<0 scrollwheel down"
EndIf
Case 674
Debug "674 ?"
Case #WM_MOUSELEAVE
;
Debug "675 $2A3WM_MOUSELEAVE mouse left window (after a trackmouseevent was used)"
;
; only generated after calling TrackMouseEvent_(), see #WM_MOUSEMOVE above
;
Default
Debug event
EndSelect
;
If mouselmb = #True
StartDrawing(WindowOutput())
Line(mousex,mousey,1,1,RGB(0,0,0))
StopDrawing()
EndIf
;
Until done = #True
;
x_end()
x_hidedebugger()