2010-07-13 125 views
1

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

回答

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); 
    } 
    };