使标题图标成为浏览器窗口的拖动源

时间:2010-07-13 03:37:00

标签: c++ windows drag-and-drop

我创建了一个用纯WinAPI编写的编辑器。有些用户希望标题图标成为在编辑器中打开的文件的拖动源,就像浏览器窗口所做的那样。我不知道实现这样的功能。有人可以给我一个例子吗?

1 个答案:

答案 0 :(得分:1)

以下是一个示例,说明如何使用系统菜单(“标题图标”)来检测何时启动拖放操作:

  class SysMenuDragSample
    : public CWindowImpl< SysMenuDragSample, CWindow, CFrameWinTraits >
  {
  private:
    static const UINT WM_SHOWSYSTEMMENU = 0x313;
    bool mouse_down_in_sys_menu_;

  public:
    BEGIN_MSG_MAP_EX( SysMenuDragSample )
      MSG_WM_NCLBUTTONDOWN( OnNcLButtonDown )
      MSG_WM_NCLBUTTONUP( OnNcLButtonUp )
      MSG_WM_MOUSEMOVE( OnMouseMove )
      MSG_WM_LBUTTONUP( OnLButtonUp )
    END_MSG_MAP()

    SysMenuDragSample()
      : mouse_down_in_sys_menu_( false )
    {
    }

    void BeginDragDropOperation()
    {
      // TODO: Implement
    }

    void OnNcLButtonDown( UINT hit_test, CPoint cursor_pos )
    {
      if( hit_test == HTSYSMENU )
      {
        mouse_down_in_sys_menu_ = true;
        SetCapture();

        // NOTE: Future messages will come through WM_MOUSEMOVE, WM_LBUTTONUP, etc.
      }
      else
        SetMsgHandled( FALSE );
    }

    void OnNcLButtonUp( UINT hit_test, CPoint cursor_pos )
    {
      if( hit_test == HTSYSMENU )
      {
        // This message and hit_test combination should never be received because
        // SetCapture was called in OnNcLButtonDown.
        assert( false );
      }
      else
        SetMsgHandled( FALSE );
    }

    void OnMouseMove( UINT modifiers, CPoint cursor_pos )
    {
      if( mouse_down_in_sys_menu_ )
      {
        ClientToScreen( &cursor_pos );

        UINT hit_test = SendMessage( WM_NCHITTEST, 0, MAKELPARAM( cursor_pos.x, cursor_pos.y ) );
        if( hit_test != HTSYSMENU )
        {
          // The cursor has moved outside of the system menu icon so begin the drag-
          // drop operation.
          mouse_down_in_sys_menu_ = false;
          ReleaseCapture();

          BeginDragDropOperation();
        }
      }
      else
        SetMsgHandled( FALSE );
    }

    void OnLButtonUp( UINT modifiers, CPoint cursor_pos )
    {
      if( mouse_down_in_sys_menu_ )
      {
        // The button was released inside the system menu so simulate a normal click.
        mouse_down_in_sys_menu_ = false;
        ReleaseCapture();

        ClientToScreen( &cursor_pos );
        SendMessage( WM_SHOWSYSTEMMENU, 0, MAKELPARAM( cursor_pos.x, cursor_pos.y ) );
      }
      else
        SetMsgHandled( FALSE );
    }
  };