Make WPF window draggable, no matter what element is clicked

前端 未结 9 1040
灰色年华
灰色年华 2020-12-04 07:47

My question is 2 fold, and I am hoping there are easier solutions to both provided by WPF rather than the standard solutions from WinForms (which Christophe

9条回答
  •  余生分开走
    2020-12-04 08:20

    It is possible to drag & drop a form by clicking anywhere on the form, not just the title bar. This is handy if you have a borderless form.

    This article on CodeProject demonstrates one possible solution to implement this:

    http://www.codeproject.com/KB/cs/DraggableForm.aspx

    Basically a descendant of the Form type is created in which the mouse down, up and move events are handled.

    • Mouse down: remember position
    • Mouse move: store new location
    • Mouse up: position form to new location

    And here's a similar solution explained in a video tutorial:

    http://www.youtube.com/watch?v=tJlY9aX73Vs

    I would not allow dragging the form when a user clicks upon a control in said form. Users epexct different results when they click on different controls. When my form suddenly starts moving because I clicked a listbox, button, label...etc. that would be confusing.

提交回复
热议问题