使用 NSImage 作为句柄拖动窗口?

发布于 2024-12-28 07:46:37 字数 709 浏览 3 评论 0原文

我正在编写一个 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];

} 

因此图像正在工作并显示,那么我如何链接该函数?

I am writing a mac app and have used this code to hide the window handle,

[self.window setStyleMask:NSBorderlessWindowMask];

Well this is great, but i still need a handle to move around the window, so is there a way to move around the window from another object like an NSImage, acting like the window handle?

The code for my image is in the .h,

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

}

and in the .m file,

- (void)awakeFromNib {

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

} 

So the image is working and showing so how do i link that function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倦话 2025-01-04 07:46:37

将 mouseDragged 事件挂接到 NSView 子视图上。

-(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 中使用此代码片段来拖动窗口。
  5. 使用 mouseDown 和 MouseUp 事件调整行为。

Hook the mouseDragged event on a NSView subview.

-(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];    
}

So

  1. subclass NSView as XView
  2. place your handle image in an instance of this view container.
  3. Place the container where you want in the windows main view.
  4. Use this snippet in XView to drag the window around.
  5. Adjust the behaviour with the mouseDown and MouseUp events.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文