Forum > Cocoa

macOS how to get window position?

(1/3) > >>

Key-Real:
How to get a window position?

NSWindow.frame.origin returns me correct X but strange Y

trev:
This works for me:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---NSLog(NSSTR(FloatToStr(window.frame.origin.x)));NSLog(NSSTR(FloatToStr(window.frame.origin.y)));

Key-Real:
not at my side

here my evidence


www.sistavip.com/pub/screen2.zip

trev:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program badger; {$mode objfpc}{$H+}{$modeswitch objectivec1} usesCocoaAll, Sysutils; type  { TMyDelegate }  TMyDelegate = objcclass(NSObject)  public    procedure BadgeButtonClick(sender :  id); message 'BadgeButtonClick:';    procedure UnBadgeButtonClick(sender :  id); message 'UnBadgeButtonClick:';  end; var   appName    : NSString;   window     : NSWindow;   myBadgeButton   : NSButton;   myUnBadgeButton : NSButton;   myDelegate : TMyDelegate; procedure TMyDelegate.BadgeButtonClick(sender :  id);begin   NSApp.dockTile.setBadgeLabel(NSStr('12'));   NSLog(NSSTR(FloatToStr(window.frame.origin.x)));   NSLog(NSSTR(FloatToStr(window.frame.origin.y)));end; procedure TMyDelegate.UnBadgeButtonClick(sender :  id);begin   NSApp.dockTile.setBadgeLabel(Nil);end; begin   // app and window creation   NSApp := NSApplication.sharedApplication;   NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular);   appName := NSStr('Badger');   window := NSWindow.alloc.initWithContentRect_styleMask_backing_defer(NSMakeRect(0, 0, 200, 200),                 NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask,                NSBackingStoreBuffered, False);   myDelegate := TMyDelegate.alloc.init;    // badge button   myBadgeButton := NSButton.alloc.initWithFrame(NSMakeRect(50, 100, 100, 50));   window.contentView.addSubview(myBadgeButton);   myBadgeButton.setTitle(NSSTR('Badge'));   myBadgeButton.setButtonType(NSMomentaryLightButton);   myBadgeButton.setBezelStyle(NSRoundedBezelStyle);    // unbadge button   myUnBadgeButton := NSButton.alloc.initWithFrame(NSMakeRect(50, 60, 100, 50));   window.contentView.addSubview(myUnBadgeButton);   myUnBadgeButton.setTitle(NSSTR('UnBadge'));   myUnBadgeButton.setButtonType(NSMomentaryLightButton);   myUnBadgeButton.setBezelStyle(NSRoundedBezelStyle);    // badge button event handler   myBadgeButton.setTarget(myDelegate);   myBadgeButton.setAction(ObjCSelector(myDelegate.BadgeButtonClick));    // unbadge button event handler   myUnBadgeButton.setTarget(myDelegate);   myUnBadgeButton.setAction(ObjCSelector(myDelegate.UnBadgeButtonClick));    // Window showing and app running   window.center;   window.setTitle(appName);   window.makeKeyAndOrderFront(Nil);   NSApp.activateIgnoringOtherApps(True);   NSApp.run;end.
Start the executable from a Terminal. Move the window around the screen and the window's x, y coordinates are correctly logged to the Terminal when pressing the badge button.

Hint: Next time post your compilable code and then someone can try to replicate what you describe. It also helps to mention which version of FPC you're using.

I'm curious: why you are avoiding the LCL which would make life easier and also make your code generally cross-platform?

Key-Real:
I lunched your prog but it tells me
x=0
y=647
but I have the window at 0,0

See attached picture

Navigation

[0] Message Index

[#] Next page

Go to full version