2011-03-04 65 views

回答

6

您的自定義視圖必須實現-acceptsFirstMouse:方法並返回YES

2

[NSWindow windowNumberAtPoint:mouseDownCoordinates belowWindowWithWindowNumber:0];

mouseDownCoordinates中,您將通過Quartz Event Services捕捉。它會返回鼠標懸停在上面的窗口號。抓住那個窗口,做你的動作/調整大小。

樣品實施(主要來自here拍攝):

#import <ApplicationServices/ApplicationServices.h> 

// Required globals/ivars: 
// 1) CGEventTap eventTap is an ivar or other global 
// 2) NSInteger (or int) myWindowNumber is the window 
// number of your borderless window 

void createEventTap(void) 
{ 
CFRunLoopSourceRef runLoopSource; 

CGEventMask eventMask = NSLeftMouseDownMask; // mouseDown event 

//create the event tap 
eventTap = CGEventTapCreate(kCGSessionEventTap, 
      kCGHeadInsertEventTap, // triggers before other event taps do 
      kCGEventTapOptionDefault, 
      eventMask, 
      myCGEventCallback, //the callback we receive when the event fires 
      nil); 

// Create a run loop source. 
runLoopSource = 
    CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0); 

// Add to the current run loop. 
CFRunLoopAddSource(CFRunLoopGetCurrent(), 
        runLoopSource, 
        kCFRunLoopCommonModes); 

// Enable the event tap. 
CGEventTapEnable(eventTap, true); 
} 


//the CGEvent callback that does the heavy lifting 
CGEventRef myCGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef theEvent, void *refcon) 
{ 
// handle the event here 
if([NSWindow windowNumberAtPoint:CGEventGetLocation(theEvent) 
    belowWindowWithWindowNumber:0] == myWindowNumber) 
{ 
    // now we know our window is the one under the cursor 
} 

// If you do the move/resize at this point, 
// then return NULL to prevent anything else 
// from responding to the event, 
// otherwise return theEvent. 

return theEvent; 
} 
+0

這不是您自己的應用程序中的窗口所必需的。你只需要實現'-acceptsFirstMouse:'就像我的答案一樣。 – 2011-03-04 04:03:48

+1

織補。哦,希望它能幫助別人! – darvids0n 2011-03-05 13:37:18