将你 嵌入NSSlider
.NSView
子类 NSView 并捕获鼠标移动事件
@interface PBView : NSView {
id delegate;
}
@property (assign)id delegate;
@end
@implementation PBView
@synthesize delegate;
-(void) mouseMoved: (NSEvent *) thisEvent
{
NSPoint cursorPoint = [ thisEvent locationInWindow ];
[delegate sliderValueChanged];
}
- (void)createTrackingArea
{
NSTrackingAreaOptions focusTrackingAreaOptions = NSTrackingMouseMoved;
focusTrackingAreaOptions |= NSTrackingActiveInActiveApp;
focusTrackingAreaOptions |= NSTrackingMouseEnteredAndExited;
focusTrackingAreaOptions |= NSTrackingAssumeInside;
focusTrackingAreaOptions |= NSTrackingInVisibleRect;
NSTrackingArea *focusTrackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
options:focusTrackingAreaOptions owner:self userInfo:nil];
[self addTrackingArea:focusTrackingArea];
}
- (void)awakeFromNib
{
[self createTrackingArea];
}
@end
现在实施
-(void)sliderValueChanged
{
NSPoint mouseLoc;
mouseLoc = [NSEvent mouseLocation]; // mouse location
NSRect r= [window frame];// window location
NSLog(@"%@", NSStringFromPoint(r.origin));
NSLog(@"%@", NSStringFromPoint(mouseLoc));
[silder setIntValue:(mouseLoc.x -r.origin.x)];//silder is object of NSSlider
}