哪个 API 可用于在移动 OS X“Carbon”时捕获鼠标?视窗?

发布于 2024-08-03 22:07:27 字数 1618 浏览 12 评论 0原文

根据要求,我实现了对通过使用窗口内容部分内的区域拖动 OS X 窗口来移动 OS X 窗口的支持,即复制标题栏的拖动和移动功能,但在另一个区域中。

我尚未解决的问题是,如果用户快速拖动鼠标,它可以离开窗口区域,然后不再接收到鼠标移动事件。

在 Windows 上,此类问题只需调用 win32 方法 SetCapture() 即可解决,相应的 OSX 方法是什么?

该应用程序是一个跨平台 C++ 应用程序,使用 Carbon 作为 OS X 特定部分。 (是的,我知道所有关于 Cocoa 的好处,但这是一个较旧的代码库,目前没有时间也没有金钱用于 Cocoa 端口。)

我已经找到了 Carbon API 方法,例如 TrackMouseLocation() 但不能'我真的不知道如何将它们用于此应用程序。在清单 2-7 中 http: //developer.apple.com/legacy/mac/library/documentation/Carbon/Conceptual/Carbon_Event_Manager/Tasks/CarbonEventsTasks.html 鼠标被捕获,但问题是 TrackMouseLocation() 阻塞等待输入。该应用程序无法执行阻塞操作,因为它还托管一个必须每秒调用多次的 Flash 播放器。

我在尝试解决这个问题时组装的原型基本上如下所示:

switch(GetEventKind(inEvent))
{
  case kEventMouseDown:
    // A silly test to make parts of the window border "draggable"
    dragging = local_loc.v < 25 || local_loc.h < 25; 
    last_screen_loc = screen_loc;
    break;
  case kEventMouseUp:
    dragging = false;
    break;
  case kEventMouseMoved:
    break;
  case kEventMouseDragged:
    if (dragging) {
      Rect rect;
      GetWindowBounds (windowRef, kWindowContentRgn, &rect);
      int dx = screen_loc.h - last_screen_loc.h;
      int dy = screen_loc.v - last_screen_loc.v;
      rect.left += dx;
      rect.right += dx;
      rect.top += dy;
      rect.bottom += dy;
      SetWindowBounds (windowRef, kWindowContentRgn, &rect);
    }
    last_screen_loc = screen_loc;
    break;

有什么想法值得赞赏吗?

On request I have implemented support for moving an OS X window by dragging it using an area within the content part of the window, i.e replicating the drag and move functionality of the title bar but in another area.

The problem I have yet to resolve is the fact that if the user drags the mouse quickly it can leave the window area and then no more mouse move events are received.

On windows this type of problem can simply be fixed by calling the win32 method SetCapture(), what's the corresponding OSX method?

This application is a cross platform C++ application using Carbon for the OS X specific parts. (And yes, I know all about the Cocoa benefits but this is an older code base and there no time nor money for a Cocoa port at this point in time.)

I have found Carbon API methods like for example TrackMouseLocation() but can't really see how I could use them for this application. In listing 2-7 here http://developer.apple.com/legacy/mac/library/documentation/Carbon/Conceptual/Carbon_Event_Manager/Tasks/CarbonEventsTasks.html
the mouse is captured but the problem is that TrackMouseLocation() blocks waiting for input. Blocking is something this application can not do since it also host a flash player that must be called many times per second.

The protototype I have assembled when trying to figure this out basically looks like this:

switch(GetEventKind(inEvent))
{
  case kEventMouseDown:
    // A silly test to make parts of the window border "draggable"
    dragging = local_loc.v < 25 || local_loc.h < 25; 
    last_screen_loc = screen_loc;
    break;
  case kEventMouseUp:
    dragging = false;
    break;
  case kEventMouseMoved:
    break;
  case kEventMouseDragged:
    if (dragging) {
      Rect rect;
      GetWindowBounds (windowRef, kWindowContentRgn, &rect);
      int dx = screen_loc.h - last_screen_loc.h;
      int dy = screen_loc.v - last_screen_loc.v;
      rect.left += dx;
      rect.right += dx;
      rect.top += dy;
      rect.bottom += dy;
      SetWindowBounds (windowRef, kWindowContentRgn, &rect);
    }
    last_screen_loc = screen_loc;
    break;

Any ideas appreciated?

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

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

发布评论

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

评论(3

dawn曙光 2024-08-10 22:07:27

我觉得你应该在窗口内和窗口外跟踪鼠标。以下代码应该可以解决您的问题,

EventHandlerRef     m_ApplicationMouseDragEventHandlerRef;          
EventHandlerRef     m_MonitorMouseDragEventHandlerRef;

{
    OSStatus ErrStatus;

    static const EventTypeSpec kMouseDragEvents[] =
      {
        { kEventClassMouse, kEventMouseDragged }
      };

    ErrStatus = InstallEventHandler(GetEventMonitorTarget(), NewEventHandlerUPP(MouseHasDragged), GetEventTypeCount(kMouseDragEvents), kMouseDragEvents, this, &m_MonitorMouseDragEventHandlerRef);

    ErrStatus = InstallApplicationEventHandler(NewEventHandlerUPP(MouseHasDragged), GetEventTypeCount(kMouseDragEvents), kMouseDragEvents, this, &m_ApplicationMouseDragEventHandlerRef);

    return true;
}

//implement these functions
OSStatus MouseHasDragged(EventHandlerCallRef inCaller, EventRef inEvent, void *pUserData){}

希望有帮助!

I feel you should track mouse in Window as well as out of window. Following code should solve your problem,

EventHandlerRef     m_ApplicationMouseDragEventHandlerRef;          
EventHandlerRef     m_MonitorMouseDragEventHandlerRef;

{
    OSStatus ErrStatus;

    static const EventTypeSpec kMouseDragEvents[] =
      {
        { kEventClassMouse, kEventMouseDragged }
      };

    ErrStatus = InstallEventHandler(GetEventMonitorTarget(), NewEventHandlerUPP(MouseHasDragged), GetEventTypeCount(kMouseDragEvents), kMouseDragEvents, this, &m_MonitorMouseDragEventHandlerRef);

    ErrStatus = InstallApplicationEventHandler(NewEventHandlerUPP(MouseHasDragged), GetEventTypeCount(kMouseDragEvents), kMouseDragEvents, this, &m_ApplicationMouseDragEventHandlerRef);

    return true;
}

//implement these functions
OSStatus MouseHasDragged(EventHandlerCallRef inCaller, EventRef inEvent, void *pUserData){}

Hope it helps!!

丢了幸福的猪 2024-08-10 22:07:27

我希望它也对你有帮助:

   // Get Mouse Position --> WAY 1      
printf("Get Mouse Position Way 1\n");   

HICoordinateSpace space = 2;    

HIGetMousePosition(space, NULL, &point);    
printf("Mouse Position: %.2f %.2f \n", point.x, point.y);


// Get Mouse Position --> WAY 2 
printf("Get Mouse Position Way 2\n");

CGEventRef ourEvent = CGEventCreate(NULL);
point = CGEventGetLocation(ourEvent);
printf("Mouse Position: %.2f, y = %.2f \n", (float)point.x, (float)point.y);

我正在寻找在某个位置(在所有应用程序的所有窗口上)获取 WindowPart 引用的方法

Carbon 中的某些方法不起作用,总是返回 0 作为 windowRef..有什么想法吗?

I Hope It Help´s you too:

   // Get Mouse Position --> WAY 1      
printf("Get Mouse Position Way 1\n");   

HICoordinateSpace space = 2;    

HIGetMousePosition(space, NULL, &point);    
printf("Mouse Position: %.2f %.2f \n", point.x, point.y);


// Get Mouse Position --> WAY 2 
printf("Get Mouse Position Way 2\n");

CGEventRef ourEvent = CGEventCreate(NULL);
point = CGEventGetLocation(ourEvent);
printf("Mouse Position: %.2f, y = %.2f \n", (float)point.x, (float)point.y);

I´m looking for the way to get a WindowPart Reference at a certain location (over all windows of all aplications)

Certain methods in Carbon doesn´t work, always return 0 as a windowRef... Any ideas?

得不到的就毁灭 2024-08-10 22:07:27

您还可以尝试仅调用 DragWindow 来响应窗口内容区域中的点击。我认为您不需要自己实现拖动。

You could also try just calling DragWindow in response to a click in your window's content area. I don't think you need to implement the dragging yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文