You do not need that function. Just use Remap().
Here is version 1.2 fixing pixel issue.
/// @link https://forum.amibroker.com/t/getcursoryposition-on-guibutton/17138/14
/// version 1.2
/// by fxshrat@gmail.com
/// Usage:
/// 1. click button "Show cursor line" to enable it
/// 2. Move mouse up/down to choose a price level
/// 3. Click CTRL+LMB to fix price level
/// 4. To release price level line and button:
/// 1. -> move mouse away from button
/// 2. -> next, hold ALT key of keyboard
/// 3. -> move mouse back to button and wait for release and speech
/// 5. Choose new price level
/// 6. see point 3. etc.
/// Notes:
/// - To disable set price button click CTRL+LMB (left mouse button) outside of price button area
/// - To re-enable set price button click left mouse button if being on GUI price button with your mouse
/// - To disable speech go to line 37 and modify it to FALSE
Version(6.30);
gcmb = GetCursorMouseButtons();
LMB = gcmb == 9;
MMB = gcmb == 12;
CTRL = GetAsyncKeyState(17) < 0;
ALT = GetAsyncKeyState(18) < 0;
gcx_px = GetCursorXPosition(1);
gcy_px = GetCursorYPosition(1);
gcy = GetCursorYPosition(0);
pxcw = Status("pxchartwidth");
x1 = 0; x2 = pxcw+5;
xbutt = pxcw-55;
static_y = StaticVarGet( "button_y" );
miny = Status( "axisminy" );
maxy = Status( "axismaxy" );
pxct = Status( "pxcharttop" );
pxcb = Status( "pxchartbottom" );
static_pxy = Remap(static_y, miny, maxy, pxcb, pxct);
speech_ON = True;
is_in_button = gcx_px >= xbutt AND gcx_px <= x2 AND
gcy_px >= static_pxy-10 and gcy_px <= static_pxy+10;
id1 = 1;
button = GuiButton(""+static_y, id1, xbutt, static_pxy-10, 60, 20, notifyClicked | notifyMouseLeave | notifyMouseEnter );
toggle = GuiToggle( "Show Cursor Line", id2 = id1+1, 0, 15, 110, 25, notifyClicked);
if ( button == guiNew ) {
GuiSetColors( id1, id1, 1, -1, colorRed, -1, -1, colorGreen, -1, -1, colorYellow, -1, -1, -1, -1);
GuiSetFont("ARIAL", 9);
}
id = GuiGetEvent(0, 0);
event = GuiGetEvent( 0, 1 );
clickevent = event == notifyClicked;
button_is_visible = NOT IsNull(static_y);
button_clicked = id == id1 AND clickevent;
printf( "notifyClicked %g\n", notifyClicked);
printf( "notifyMouseLeave %g\n", notifyMouseLeave);
printf( "notifyMouseEnter %g\n", notifyMouseEnter);
printf( "GUI event %g, CTRL: %g, ALT: %g, LMB: %g", event, CTRL, ALT, LMB );
// Remove static price line and button on ALT + entering button
if ( ALT AND id == id1 AND event == notifyMouseEnter ) {
GuiSetVisible(id1, 0);
StaticVarRemove("button_*" );
if ( speech_ON ) Say( "Gui button has been entered and removed.", 0);
}
// Set price fix level and price button via CTRL + left mouse button click
if ( CTRL AND LMB AND NOT button_is_visible ) {
GuiSetVisible(id1, 1);
StaticVarSet( "button_y", gcy);
if ( speech_ON ) Say( "Gui button has been set.", 0);
}
//if ( id == id1 AND event == notifyMouseLeave ) {
// if ( speech_ON ) Say( "Button has been left.", 0);
//}
// GUI trigger line if static GetCursorYPosition exists
if ( button_is_visible ) {
// Disable button if clicking outside of button area
if ( CTRL AND LMB ) { // CTRL + Left mouse button
if ( NOT is_in_button ) {
GuiEnable(id1, 0);
if ( speech_ON ) Say("Price Button disabled! Re-click that button to re-enable it.");
}
}
// Enable button if clicking in area of button
if ( LMB AND is_in_button ) { // left mouse button in button area
GuiEnable(id1, 1);
if ( speech_ON ) Say("Price button enabled! Click that button one more time to trigger!", 1);
}
// Do something if button is enabled (not white) and if clicked
if ( button_clicked ) {
Say(txt = "Hello world!", 0);
// Draw some message on chart
GfxSetBkMode( 1 );
GfxSetTextColor( colorRed );
GfxSelectFont("ARIAL", 40);
GfxDrawText(txt, 0, 0, pxcw+5, Status("pxchartheight"), 1+4+32);
//etc....
}
}
// ######################################## Plot #############################################
SetChartOptions( 1, chartShowDates | chartShowArrows | chartWrapTitle | chartDisableTooltips );
Plot( C, "Price", colorDefault, styleBar );
GfxSetCoordsMode(0);
GfxSetBkMode(1);
// cursor line
if ( ! button_is_visible AND GuiGetCheck(id2) ) {
GfxSelectPen( colorYellow, 1, 1 );
GfxMoveTo( 0, gcy_px );
GfxLineTo( pxcw, gcy_px );
GfxFillSolidRect( xbutt, gcy_px-10, pxcw+5, gcy_px+10, colorYellow);
GfxSelectFont("ARIAL", 9, 500, 0, 0, 0);
GfxSetTextColor( colorBlack );
GfxDrawText(""+gcy, xbutt, gcy_px-8, pxcw+10, gcy_px+8, 1+4+32);
}
// static line
if ( button_is_visible ) {
GfxSelectPen( colorRed, 1, 1 );
GfxMoveTo( x1, static_pxy );
GfxLineTo( x2, static_pxy );
}
RequestMouseMoveRefresh();