ページ

2008年8月20日水曜日

マウスカーソルのキャプチャ (6)

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク

マウスカーソルの画像や位置情報などをまとめるクラスを追加する。名前は MouseCursor(そのまんま)。

MouseCursor.h

@interface MouseCursor : NSObject {

NSImage* _image;
NSPoint _location;
NSPoint _hot_spot;
}
+ (MouseCursor*)mouseCursor;
- (NSImage*)image;
- (NSSize)size;
- (NSPoint)location;
- (NSPoint)hotSpot;
- (NSPoint)pointForDrawing;


MouseCursor.m
@implementation MouseCursor

- (id)init
{
self = [super init];
if (self) {
NSCursor* cursor = [NSCursor arrowCursor];
_image = [[cursor image] retain];
_location = [NSEvent mouseLocation];
_hot_spot = [cursor hotSpot];
}
return self;
}

- (void) dealloc
{
[_image release];
[super dealloc];
}


+ (MouseCursor*)mouseCursor
{
return [[[MouseCursor alloc] init] autorelease];
}

- (NSImage*)image
{
return _image;
}
- (NSSize)size
{
return [_image size];
}
- (NSPoint)location
{
return _location;
}

- (NSPoint)hotSpot
{
return _hot_spot;
}

- (NSPoint)pointForDrawing
{
NSPoint p = _location;
p.y -= [self size].height;
p.x -= _hot_spot.x;
p.y += _hot_spot.y;

return p;
}


前回のソースコードへ組み込み動作を確認する。


ちゃんと動作している。

- - - -
処理内容は前回までに紹介したコードとほぼ同じで、特に大したことはおこなっていない。重要なのはクラスとして切り出されていること。後でマウスカーソル画像の取得処理を差し替えることができる。次回はプライベート関数 CGSGetGlobalCursorData( ) に置き換えてみる。