我很感谢@Willeke - Willeke为我指明了正确的方向。
正如我的问题中提到的,我正在查看GitHub 上的Easy Move+Resize App的代码。此代码/应用程序的问题在于它不适用于当前最大化的 Windows,即它试图移动这些 Windows,但它不能,因为它们是固定的。(注意:这仅在多显示器设置中有用且相关。)此应用程序适用于未最大化的 Windows。
在这里,我尝试添加将取消最大化窗口以移动它的代码,然后在移动它后再次最大化它。显然,下面的代码是在这个应用程序的上下文中,但我确信在其他上下文中对用户有用。
我首先在EMRMoveResize.hwasMaximized
中添加了一个属性
//EMRMoveResize.h
@property bool wasMaximized;
接下来,我转到了实际事件回调代码所在的EMRAppDelegate.m 。需要注意的是,我们只关心移动,即只关心鼠标左键。(这个应用程序使用鼠标右键来调整大小,当窗口最大化时,这并不相关。)所以,我们只关心kCGEventLeftMouseDown
,kCGEventLeftMouseDragged
最后关心kCGEventLeftMouseUp
。在伪代码中,我做了类似的事情:
If (LeftMouseDown) {
Find out if Window is Maximized
If (Window is Maximized) {
set the wasMaximized property
Click FullScreen Button to UnMaximize the Window in Order to Move it
}
Window is now UnMaximized现在将像LeftMouseDragged事件中的其他窗口一样移动,我没有对其进行任何更改。最后,
If(LeftMouseUp) {
If(wasMaximized value was set) {
Click FullScreen Button again to Maximize the Window (Since it started out as Maximized)
Reset the wasMaximized property
}
}
现在查看EMRAppDelegate.m的代码更改片段
if (type == kCGEventLeftMouseDown
|| type == kCGEventRightMouseDown) {
//..
//Skipped Unchanged Code
//..
//Find out if Window is Maximized
CFTypeRef TypeRef = nil;
if (AXUIElementCopyAttributeValue((AXUIElementRef)_clickedWindow, CFSTR("AXFullScreen"), &TypeRef)) {
if(Debug) NSLog(@"Could not get wasMaximized Value");
} else {
[moveResize setWasMaximized: CFBooleanGetValue(TypeRef)];
if(Debug) NSLog(CFBooleanGetValue(TypeRef) ? @"Updated Maximized to True" : @"Updated Maximized to False");
}
//Click FullScreen Button to UnMaximize the Window in Order to Move it
if([moveResize wasMaximized]) {
AXUIElementRef buttonRef = nil;
AXUIElementCopyAttributeValue(_clickedWindow, kAXFullScreenButtonAttribute, (CFTypeRef*)&buttonRef);
if(Debug) NSLog(@"buttonRef: %p", buttonRef);
AXUIElementPerformAction(buttonRef, kAXPressAction);
CFRelease(buttonRef);
}
//..
//Skipped Unchanged Code
//..
}
if (type == kCGEventLeftMouseUp
|| type == kCGEventRightMouseUp) {
//..
//Skipped Unchanged Code
//..
//Click FullScreen Button again to Maximize the Window (Since it started out as Maximized)
AXUIElementRef _clickedWindow = [moveResize window];
if([moveResize wasMaximized]) {
AXUIElementRef buttonRef = nil;
AXUIElementCopyAttributeValue(_clickedWindow, kAXFullScreenButtonAttribute, (CFTypeRef*)&buttonRef);
if(Debug) NSLog(@"buttonRef: %p", buttonRef);
AXUIElementPerformAction(buttonRef, kAXPressAction);
CFRelease(buttonRef);
[moveResize setWasMaximized: false];
}
//..
//Skipped Unchanged Code
//..
}
这对我有用。但我不是Objective C或MacOS方面的专家,所以如果你觉得有什么可以改进的,请随时编辑这篇文章。