Help with creating moveable windows for my GUI

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
JaniH
Newbie
Posts: 4
Joined: Mon Apr 24, 2006 2:33 pm
Location: Finland

Help with creating moveable windows for my GUI

Post by JaniH »

Hi!

I'm creating GUI with qBasic 4.5, using screen mode 12 and I need help with creating windows which can be dragged all around desktop with mouse. I created couple programs, but they only mess up the screen. I only get it work when i move windows with keyboard..:lol: Could anyone help me with this, before i give up?..:) If someone could provide a source code explaining how to drag windows with mouse, I would be very grateful! (Hope you understand what i mean, I'm not good in english..:))
RyanKelly
Coder
Posts: 48
Joined: Sun Jan 22, 2006 6:40 pm
Contact:

Post by RyanKelly »

Please describe how the screen is being "messes up".
You might want to try hiding the mouse cursor before drawing to the screen.
JaniH
Newbie
Posts: 4
Joined: Mon Apr 24, 2006 2:33 pm
Location: Finland

Post by JaniH »

By "messed up" i mean that when i move window, background doesn't get redrawn. Is there any way to do page flipping or similar technique for screen mode 12 without using libraries? I've been examining others source code for trying to learning how to drag windows, but with no luck.
Anonymous

some code

Post by Anonymous »

Heres some messy FB code I put together tonight. I think it does what you wants. If you want a compiled version and don't have FB than email me at cant.get.nixon@gmail.com or just get someone to do it for you. Most of it should work in QB you just need to replace the mouse stuff with what you use in QB. It should all be pretty straight forward. I sorta used a few useless subs for drawing but oh well, you'll get the idea. Enjoy.

Code: Select all

Declare Sub Draw_window()
Declare Sub Hide_Window()
Declare Sub Get_Window_BG()
Declare Function Is_Mouse_In_Box(XPos, YPos)

'Define Global Variables
Dim shared TempBGImage(1000), GWindow(1000), WindowLength, HoldPartHieght
Dim shared WindowX, WindowY, True, False

'These come in very useful
False = 0
True = Not False

'set screen mode
screen 13

'make window
line (1,1)-(50,10),15,b
line (1,1)-(50,50),15,b
paint(2,2),9,15
paint(2,15),1,15

'store it in memory
get(1,1)-(50,50),GWindow

'make crap background
for i = 1 to 500
    line(rnd*320,rnd*200)-(rnd*320,rnd*200),int(rnd * 255)
next

' set variables for window positioning and clicking
WindowLength = 50
HoldPartHieght = 10
WindowX = 50
WindowY = 50

' prepare for use by getting bg and drawing the screen
Get_Window_BG
Draw_Window

' Main Program Loop

do  
    'Tells me where stuff is, pretty useless
    locate 1,1:print _MOUSEX, CurrentMouseY
    locate 2,1:print WindowX, WindowY    
    'Keep Checking mouse
    GETMOUSE _MOUSEX, CurrentMouseY, ,CurrentMouseButton
    'checks to see if left mouse button is pressed and if mouse is in area needed for the drag
    if CurrentMouseButton = 1 and Is_Mouse_In_Box(_MOUSEX, CurrentMouseY) = True then
        'Remember where the mouse was in the dragable area
        WinOffSetX = _MOUSEX - WindowX
        WinOffSetY = CurrentMouseY - WindowY        
        'loop until the button is realesed
        do 
            GETMOUSE _MOUSEX, CurrentMouseY, ,CurrentMouseButton
            'check if mouse has moved to get rid of flicker
            if OldX <> _MOUSEX or OldY <> CurrentMouseY then 
                'display window in new position
                Hide_Window()   
                'Find New position
                WindowX = _MOUSEX - WinOffSetX
                WindowY = CurrentMouseY - WinOffSetY           
                'Used to check if mouse has moved
                oldX = _MOUSEX
                oldY = CurrentMouseY
                'Get new background
                Get_Window_BG
                'Put window down in new spot
                Draw_Window
            end if
        loop until CurrentMouseButton = 0
    end if
    'loop ends when right button is pressed
loop until CurrentMouseButton = 2

'                           ////// SUBS ///////

'Checks each axis and returns weather or not it is in
Function Is_Mouse_In_Box(XPos, YPos)
    'Define variables
    InTheBox = False
    XAxis = False
    YAxis = False
    'Checks X and then Y axis
    If WindowX < Xpos and XPos < WindowX + WindowLength then XAxis = True
    If WindowY < YPos and YPos < WindowY + HoldPartHieght then YAxis = True
    'Checks to see if a True Result is given
    if XAxis = True and YAxis = True then InTheBox = True
    'Send back weather or not the mouse is in the area with a true false indicator
    Return InTheBox
End Function


'Stores the background so that it can be put back down when window is moved
Sub Get_Window_BG()
    get(WindowX,WindowY)-(WindowX+50,WindowY+50),TempBGImage
End Sub

'Puts the background down when moving so that the backgound isn't messed
Sub Hide_Window()
    put (WindowX,WindowY),TempBGImage,pset
End Sub

'Draws the window
Sub Draw_window()
    put (WindowX,WindowY),GWindow,pset
End Sub
May I add I don't usually work with gfx so don't blame me for doing a crappy job.

* Edit : Try the new code, it works better and doesn't use a couple of useless variables
JaniH
Newbie
Posts: 4
Joined: Mon Apr 24, 2006 2:33 pm
Location: Finland

Post by JaniH »

Fantastic! Thank's to Nixon, I finally know how this thing works!..:) Now i can get back to programming my GUI..:D Thank you Nixon!..:)
Post Reply