Mouse scrolling, How?

Just starting out? Need help? Post your questions and find answers here.
eucodepb2025
New User
New User
Posts: 1
Joined: Mon Sep 22, 2025 12:58 pm

Mouse scrolling, How?

Post by eucodepb2025 »

I have a custom gadget like person name with icon and logo list item,
where i need to make it work when mouse scroll event happen.

But the problem is while using PureBasic 6.20 it only working when you use OpenScreen https://www.purebasic.com/documentation ... se.pb.html)

But i need to make the scroll work while using OpenWindow method,

Can anyone check the code please?
How to apply mouse wheel scrolling, on that white area here with my existing lines for example:
Image

Code:
====

Code: Select all

Global MainWindow
Global scrollOffset1 = 0, scrollOffset2 = 0   ; Scroll offsets
Global contentHeight1 = 180, contentHeight2 = 140
Global activeScrollArea = 0
Global mouseX, mouseY, mouseDown = #False

Enumeration FormFont
  #Font_Person_Big
EndEnumeration

; Win32 constants
#WS_EX_LAYERED = $00080000
#GWL_EXSTYLE   = -20
#AC_SRC_OVER   = 0
#AC_SRC_ALPHA  = 1

Declare HandleMouseWheel(scrollArea, delta)

; --- Draw layered window ---
Procedure OpenMainWindow(x = 0, y = 0, width = 330, height = 340)
  ExamineDesktops()
  x = DesktopWidth(0)  - width - 200
  y = DesktopHeight(0) - height - 200

  LoadFont(#Font_Person_Big, "Tahoma", 12, #PB_Font_Bold)

  If IsWindow(MainWindow) = 0
    MainWindow = OpenWindow(#PB_Any, x, y, width, height, "", #PB_Window_BorderLess)
    Protected hWnd = WindowID(MainWindow)
    Protected style = GetWindowLong_(hWnd, #GWL_EXSTYLE)
    SetWindowLong_(hWnd, #GWL_EXSTYLE, style | #WS_EX_LAYERED)
  EndIf

  Protected img = CreateImage(#PB_Any, width, height, 32, #PB_Image_Transparent)
  If StartDrawing(ImageOutput(img))
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    Box(0, 0, width, height, RGBA(110,110,110,128)) ; gray background
    Protected margin = 10
    Box(margin, margin, width - margin*2, height - margin*2, RGBA(200,200,200,255))

    ; --- Scroll Area 1 ---
    DrawingMode(#PB_2DDrawing_Default)
    Box(margin+5, margin+5, 300, 65, RGB(240,240,240))
    ClipOutput(margin+5, margin+5, 300, 65)
    DrawingMode(#PB_2DDrawing_Transparent)
    DrawingFont(FontID(#Font_Person_Big))
    DrawText(margin+10, margin+10 - scrollOffset1, "1. Elon Monk", RGB(0,0,0))
    DrawText(margin+10, margin+60 - scrollOffset1, "2. Bill Plate", RGB(0,0,0))
    DrawText(margin+10, margin+110 - scrollOffset1, "3. Steve Work", RGB(0,0,0))
    DrawText(margin+10, margin+160 - scrollOffset1, "4. Byran Eve", RGB(0,0,0))
    UnclipOutput()
    

    StopDrawing()
  EndIf

  Dim pt.POINT(1) : pt(0)\x = x : pt(0)\y = y
  Dim sz.SIZE(1)  : sz(0)\cx = width : sz(0)\cy = height
  Dim srcPt.POINT(1)
  Define blend.BLENDFUNCTION
  blend\BlendOp = #AC_SRC_OVER
  blend\BlendFlags = 0
  blend\SourceConstantAlpha = 255
  blend\AlphaFormat = #AC_SRC_ALPHA
  Protected screenDC = GetDC_(0)
  Protected memDC = CreateCompatibleDC_(screenDC)
  Protected oldObj = SelectObject_(memDC, ImageID(img))
  UpdateLayeredWindow_(hWnd, screenDC, @pt(0), @sz(0), memDC, @srcPt(0), 0, @blend, 2)
  SelectObject_(memDC, oldObj)
  DeleteDC_(memDC)
  ReleaseDC_(0, screenDC)
  FreeImage(img)
EndProcedure

; --- Scroll logic ---
Procedure HandleMouseWheel(scrollArea, delta)
  If scrollArea = 1
    scrollOffset1 - delta*20
    If scrollOffset1 < 0 : scrollOffset1 = 0 : EndIf
    If scrollOffset1 > contentHeight1 - 65 : scrollOffset1 = contentHeight1 - 65 : EndIf
  ElseIf scrollArea = 2
    scrollOffset2 - delta*20
    If scrollOffset2 < 0 : scrollOffset2 = 0 : EndIf
    If scrollOffset2 > contentHeight2 - 90 : scrollOffset2 = contentHeight2 - 90 : EndIf
  EndIf
  OpenMainWindow()
EndProcedure

; --- Mouse events ---
Procedure HandleMouseEvents()
  Protected Event = WindowEvent()
  If Event = #PB_Event_CloseWindow : End : EndIf

  Protected mx = WindowMouseX(MainWindow)
  Protected my = WindowMouseY(MainWindow)

  If mx>=15 And mx<=315 And my>=125 And my<=190
    activeScrollArea = 1
  ElseIf mx>=20 And mx<=310 And my>=220 And my<=310
    activeScrollArea = 2
  Else
    activeScrollArea = 0
  EndIf

  If GetAsyncKeyState_(#VK_MBUTTON) & $8000
    Static lastWheelTime
    If ElapsedMilliseconds()-lastWheelTime > 100
      If my < WindowHeight(MainWindow)/2
        HandleMouseWheel(activeScrollArea, 1)
      Else
        HandleMouseWheel(activeScrollArea, -1)
      EndIf
      lastWheelTime = ElapsedMilliseconds()
    EndIf
  EndIf

  Delay(10)
EndProcedure

OpenMainWindow()
Repeat
  HandleMouseEvents()
ForEver

Axolotl
Addict
Addict
Posts: 865
Joined: Wed Dec 31, 2008 3:36 pm

Re: Mouse scrolling, How?

Post by Axolotl »

After a quick look at your code, here are some pointers.

Code: Select all

;  better use this functions (64-bit ready) 
    Protected style = GetWindowLongPtr_(hWnd, #GWL_EXSTYLE)
    SetWindowLongPtr_(hWnd, #GWL_EXSTYLE, style | #WS_EX_LAYERED)

; you should add this (only for demo) 
Procedure GET_WHEEL_DELTA_WPARAM(wParam) 
  Protected result.w  ; typecast 

  result = ((wParam >> 16) & $FFFF)       ; 120 or -120 
  ProcedureReturn result / #WHEEL_DELTA   ; 1 or -1 
EndProcedure

; in your HandleMouseEvents() 
If Event = #WM_MOUSEWHEEL : Debug "Mouse Wheel " + GET_WHEEL_DELTA_WPARAM(EventwParam()) : EndIf 

; Maybe you should use a CanvasGadget istead of the ImageGadget -> more Events like #PB_EventType_MouseWheel 
; but you will loose the AlphaChannel stuff... 

; BTW: Your app can only be closed with Alt+F4.

; otherwise your code is to far from my coding style, so I cannot go deeper..... 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Axolotl
Addict
Addict
Posts: 865
Joined: Wed Dec 31, 2008 3:36 pm

Re: Mouse scrolling, How?

Post by Axolotl »

ok, I forgot.

the mousewheel message gives you the following button states as well

Code: Select all

Procedure GET_KEYSTATE_WPARAM(wParam) 
  ProcedureReturn wParam & $FFFF  ; LoWord() 

; #MK_CONTROL ;	0x0008 	The CTRL key is down.
; #MK_LBUTTON ;	0x0001 	The left mouse button is down.
; #MK_MBUTTON ;	0x0010 	The middle mouse button is down.
; #MK_RBUTTON ;	0x0002 	The right mouse button is down.
; #MK_SHIFT 	;	0x0004 	The SHIFT key is down.
; #MK_XBUTTON1; 	0x0020 	The first X button is down.
; #MK_XBUTTON2; 	0x0040 	The second X button is down.
EndProcedure
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
User avatar
kenmo
Addict
Addict
Posts: 2049
Joined: Tue Dec 23, 2003 3:54 am

Re: Mouse scrolling, How?

Post by kenmo »

I definitely recommend you check out the CanvasGadget...

- has mousewheel events (and most other mouse and keyboard events)
- double buffered drawing, no flicker
- 100% PB commands, no API (cross platform if you ever need that in the future)
Post Reply