Hi,
I came across some phenomena of GRAPHIC WINDOW which I would like to discuss.
(1) You can get the mouse coordinates easily without subclassing the GW: GetCursorPos(lpPoint)
(2) When you work in a GW it is difficult to get the exact location of the GW client area.
The GRAPHIC GET LOC statement only gives you the top left corner of the GW INCLUDING the
window edge and the caption bar. It becomes obvious when the caption is used
(in my case: 3 pixel edge and 29 pixel caption bar).
Still, without the caption you are offset by one pixel!
The width and height of GRAPHIC GET CLIENT are correct.
The goal is to get the exact location of the client area of a GW, i.e. the area in which you can draw.
The code below shows that x0, y0 are not good enough for that purpose.
Using the Api call GetWindowRect() and a bit of calculation gives us the correct client coordinates x1 and y1.
This also reveals that a GW without caption has a 1 pixels frame that can be corrected using x1 and y1.
I had used this method already in the "Graphic Buttons for PB/CC 5.0" for getting the hover effect right.
(3) On my two computers (PC and laptop, both Win XP) moving the scroll wheel crashes the program (without any leftovers, I hope).
In my discussion with PB support they claimed that it could possibly be caused by my mouse driver(s).
Does any one else also see that effect?
With the code below you can test the situation with/without caption and checking if the mouse is within x0/y0 or x1/y1.
I used the spaces in the print statement because I am too lazy to erased the old text exactly ...
Regards,
Gert Voland.
I came across some phenomena of GRAPHIC WINDOW which I would like to discuss.
(1) You can get the mouse coordinates easily without subclassing the GW: GetCursorPos(lpPoint)
(2) When you work in a GW it is difficult to get the exact location of the GW client area.
The GRAPHIC GET LOC statement only gives you the top left corner of the GW INCLUDING the
window edge and the caption bar. It becomes obvious when the caption is used
(in my case: 3 pixel edge and 29 pixel caption bar).
Still, without the caption you are offset by one pixel!
The width and height of GRAPHIC GET CLIENT are correct.
The goal is to get the exact location of the client area of a GW, i.e. the area in which you can draw.
The code below shows that x0, y0 are not good enough for that purpose.
Using the Api call GetWindowRect() and a bit of calculation gives us the correct client coordinates x1 and y1.
This also reveals that a GW without caption has a 1 pixels frame that can be corrected using x1 and y1.
I had used this method already in the "Graphic Buttons for PB/CC 5.0" for getting the hover effect right.
(3) On my two computers (PC and laptop, both Win XP) moving the scroll wheel crashes the program (without any leftovers, I hope).
In my discussion with PB support they claimed that it could possibly be caused by my mouse driver(s).
Does any one else also see that effect?
With the code below you can test the situation with/without caption and checking if the mouse is within x0/y0 or x1/y1.
I used the spaces in the print statement because I am too lazy to erased the old text exactly ...
Regards,
Gert Voland.
Code:
'test_coordinates.bas, Gert Voland, 11. Nov. 2008 #COMPILE EXE #DIM ALL #BREAK ON #CONSOLE OFF #INCLUDE "Win32Api.inc" '---------------------------------------------------------------------------------------------- FUNCTION PBMAIN LOCAL z$ LOCAL hGW AS LONG LOCAL x0, y0, x1, y1 AS LONG LOCAL deltax, deltay AS SINGLE LOCAL x, y, w, h AS SINGLE LOCAL xold, yold AS SINGLE LOCAL lpPoint AS POINTAPI ' Pointer type defined in Win32Api LOCAL GWRect AS RECT ' Data structure defined in Win32Api LOCAL grstat AS LONG x0 = 350 y0 = 450 w = 400 h = 200 '' GRAPHIC WINDOW "", x0, y0, w, h TO hGW ' Without caption GRAPHIC WINDOW "test_coordinates", x0, y0, w, h TO hGW GRAPHIC ATTACH hGW, 0, REDRAW GRAPHIC SET POS (10, 10) : GRAPHIC PRINT "Test the coordinates. Press ESC to quit" GRAPHIC SET POS (10, 30) : GRAPHIC PRINT "Move scroll wheel -> Crash?" DO ' Update the coordinates when GW is moved GRAPHIC GET LOC TO x0, y0 ' Absolute pixels of the left top corner of the GW bounding rectangle GetWindowRect(hGW, GWRect) ' API call: absolute coordinates of the GW bounding rectangle deltax = (GWRect.nRight - GWRect.nLeft - w)/2 ' Half of: Width of the total GW - desired width (= edge of GW) deltay = GWRect.nBottom - GWRect.nTop - h - deltax ' Height of the total GW - desired height - edge (= width of caption bar) x1 = GWRect.nLeft + deltax ' x of the GW client area, absolute pixels y1 = GWRect.nTop + deltay ' y of the GW client area, absolute pixels GetCursorPos(lpPoint) ' API call: absolute x/y coordinates x = lpPoint.x y = lpPoint.y IF x <> xold OR y <> yold THEN ' Update GW whenever the mouse is moved GRAPHIC SET POS (10, 60) : GRAPHIC PRINT "Position of GW: "; x0; y0; SPACE$(10); "Width and Height: ";w; h GRAPHIC SET POS (10, 80) : GRAPHIC PRINT "Absolute mouse coordinates: "; x ;y; SPACE$(10) ''' IF x >= x0 AND x <= x0 + w AND y >= y0 AND y <= y0 + h THEN ' Using the GET LOC data: we are not in the client area of GW IF x >= x1 AND x <= x1 + w AND y >= y1 AND y <= y1 + h THEN ' Using the API call: now inside the client area, independent of caption GRAPHIC SET POS ( 10, 120) : GRAPHIC PRINT "Relative coordinates - GRAPHIC GET LOC : "; GRAPHIC SET POS (220, 120) : GRAPHIC PRINT x - x0; y - y0; SPACE$(10) GRAPHIC SET POS ( 10, 140) : GRAPHIC PRINT "Relative coordinates - Corrected: "; GRAPHIC SET POS (220, 140) : GRAPHIC PRINT x - x1; y - y1; SPACE$(10) ELSE GRAPHIC SET POS (10, 120) : GRAPHIC PRINT SPACE$(100) GRAPHIC SET POS (10, 140) : GRAPHIC PRINT SPACE$(100) END IF xold = x yold = y END IF GRAPHIC INSTAT TO grstat IF ISTRUE grstat THEN GRAPHIC INKEY$ TO z$ GRAPHIC SET POS (10, 180) : GRAPHIC PRINT "Pressed key: "; z$ ; SPACE$(10) END IF GRAPHIC REDRAW LOOP UNTIL z$ = $ESC GRAPHIC WINDOW END END FUNCTION '---end of file ------------------------------------------------------------------------------------
Comment