スマートフォン・ジン | Smartphone-Zine

引っ越し先→ https://smartphone-zine.com/

OSXでマウスのドラッグでウィンドウを移動する

OSXツールバーを非表示にしているNSWindowでドラッグで移動したい。 色々探したがなかなか情報が少なかったのでメモしておく。 NSWindowを継承したクラスを作成。 mouseDownで画面のどの場所をクリックしたのか記録。 mouseDraggedでウィンドウの位置をマウスの位置へ移動。マウスの現在位置は[NSEvent mouseLocation]にて取得可能。 それにmouseDown時の位置を加味すればOK. ソースコードは以下のとおり。大変シンプルです。   [cpp]

import <cocoa/Cocoa.h>

@interface SGWindow : NSWindow @end [/cpp] [cpp]

import "SGWindow.h"

@implementation SGWindow { NSPoint beforePoint; } -(void)mouseDown:(NSEvent )theEvent { beforePoint = [theEvent locationInWindow]; } - (void)mouseDragged:(NSEvent )theEvent { NSPoint moLo = [NSEvent mouseLocation]; [self setFrame:NSMakeRect(moLo.x - beforePoint.x, moLo.y - beforePoint.y, self.frame.size.width, self.frame.size.height) display:YES animate:NO]; } @end [/cpp]