【问题标题】:Drag window with NSImage as handle?以 NSImage 为句柄拖动窗口?
【发布时间】:2012-01-22 06:23:20
【问题描述】:

我正在编写一个mac应用程序并使用此代码隐藏窗口句柄,

[self.window setStyleMask:NSBorderlessWindowMask];

这很好,但我仍然需要一个句柄来在窗口周围移动,所以有没有办法从另一个对象(如 NSImage)移动窗口,就像窗口句柄一样?

我的图片的代码在 .h 中,

@interface WOWAppDelegate : NSObject <NSApplicationDelegate> {
        NSWindow *window;
    IBOutlet NSImageView* winView;

}

在 .m 文件中,

- (void)awakeFromNib {

    NSString *inFilePathwin = [[[NSBundle mainBundle] resourcePath]  stringByAppendingPathComponent:@"win.png"];
    NSImage *winImage = [[[NSImage alloc] initWithContentsOfFile:inFilePathwin] autorelease];
    [winView setImage:winImage];

} 

所以图像正在工作并显示,那么我如何链接该功能?

【问题讨论】:

    标签: xcode macos window handle


    【解决方案1】:

    在 NSView 子视图上挂钩 mouseDragged 事件。

    -(void)mouseDragged:(NSEvent *)theEvent
    {       
    CGFloat deltax = [theEvent deltaX];
    CGFloat deltay = [theEvent deltaY];
    
    NSRect frame = [[self window] frame];
    frame.origin.x += deltax;
    frame.origin.y -= deltay;
    
    [[self window] setFrameOrigin:frame.origin];    
    }
    

    所以

    1. 将 NSView 子类化为 XView
    2. 将句柄图像放在此视图容器的实例中。
    3. 将容器放置在 windows 主视图中所需的位置。
    4. 在 XView 中使用这个 sn-p 来拖动窗口。
    5. 使用 mouseDown 和 MouseUp 事件调整行为。

    【讨论】:

    • 我是应用程序编程的新手,如何链接所有内容?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 2013-11-11
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多