Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
With WPF touch desktop application, how to drag window with no title bar using finger not mouse?
Posted

This is not one question, but two separate questions.

First of all, pointing device software is designed the way that an application does not make any difference between a touchpad and mouse. Touchpads works the same way, by the software usually have a special feature: you can simulate holding a left mouse button by double click, when on the second click the user should keep a finger in contact, not lift it. In this case, the driver does not dispatch it to the windows system as the second click and simulated the button help down instead. The same functionality is developed for Windows and Linux, and probably other systems. (I don't touch additional multi-touch or drawing tablet features, which is a separate topic.) From the standpoint of a Windows sub-system and applications, there is only button press and the button is help down.

So, from this point, we can forget the difference between a mouse and a touchpad.

Now, dragging of a WPF window without the non-client are is a completely different question unrelated to the above. The short answer is: you should handle your usual client-area mouse events. This is a really easy problem, so, I think you should try to solve it and ask further questions only if you face problems and get stuck.

[EDIT]

On second though, I decided to give you the dragging code: I interested to see if I can write it from the first attempt all from my head using one a little bit unusual feature, the effect of closure:
C#
static void MakeDragging(Window window) {
    bool isDown = false;
    Point position = default(Point);
    window.MouseDown += (sender, eventArgs) => {
        if (eventArgs.LeftButton != MouseButtonState.Pressed) return;
        isDown = true;
        position = window.PointToScreen(eventArgs.GetPosition(window));
    };
    window.MouseUp += (sender, eventArgs) => {
        if (eventArgs.LeftButton != MouseButtonState.Released) return;
        isDown = false;
    };
    window.MouseMove += (sender, eventArgs) => {
        if (!isDown) return;
        Point newPosition = window.PointToScreen(eventArgs.GetPosition(window));
        window.Left += newPosition.X - position.X;
        window.Top += newPosition.Y - position.Y;
        position = newPosition;
    };
} //MakeDragging


Than I tested it, it worked. The closure effect works on the variables isDown and position. In this approach, local variables behave like static, because they are used in anonymous methods (event handlers). It completely isolate this code from the any properties of any types. Note that it has nothing to do with static modifier on the method itself, which is done just because no instance is used, for convenience of showing the code sample. Please see:
http://en.wikipedia.org/wiki/Closure_%28computer_science%29[^].

Note that I use screen coordinates. Not using them would create one difficulty: shifting the window position would itself invoke the MouseMove event causing unwanted recursion.

Good luck,
—SA
 
Share this answer
 
v2
Comments
_Maxxx_ 21-Oct-13 1:28am    
Sorry, but you aren't helping at all. I'm talking touch with WPF specifically - you're talking touchpads, which is "a separate topic"

WPF DOES make a difference between a touchpad and a mouse. e.g. the System.Window.DragMove(); method provided specifically drags the window while the left mouse button is down - and is not affected by touch at all (indeed, if you look at the code for it, it specifically tests for the left mouse button being down)

And the dragging of a window without the client area IS related specifically because of differences between mouse handling and touch.

As you say, it is an easy problem, would you be kind enough to supply some code to show me - as I have not found it possible to achieve despite many attempts.
Sergey Alexandrovich Kryukov 21-Oct-13 1:43am    
I don't think you are right when it comes to dragging. WPF won't see the difference is pressed left button in emulated. And if you think that WPF handles mouse and touchpad events in different ways, I would be much grateful if you send me any evidence or a link to such evidence. And as to the code, I would prefer to see your attempt first, as you are a more interested person. I've done such dragging many times, it just takes some time and careful attention, no more than that.
—SA
_Maxxx_ 21-Oct-13 7:46am    
Sorry - I didn't see you addition when I posted my last comment.
That's awesome! Thanks.

One of my attempts was similar - but I was getting into an infinite loop of windows moving events!

Thanks for your help!
Sergey Alexandrovich Kryukov 21-Oct-13 10:20am    
That was my guess, about infinite loop, please see my clause about "unwanted recursion". When you moved the window, it moves relative to the mouse position, and it triggers MouseMove event again, and then it goes into "infinite recursion".

You are welcome.
Good luck, call again.

—SA
When dragging the window with a title bar, you are right, Windows treats both the same.

Without a title bar the programmer must do some work.

The 'standard' way of handling this with the mouse is to use DragMove(); which I have tried. THis does not work with touch. As I said, the Windows code specifically checks for the mouse button being down - which it is not if you are not using a mouse but using touch alone.

Of course WPF treats mouse and touch in different ways - that's not what I think it is a fact - and it is obvious - a mouse has only a single point whereas touch supports many - if it treated them identically then a click and sideways swipe would work with teh mouse as it does with touch - which it plainly doesn't (try using Windows 8 for example).

The code I have is trivial and doesn't work - using DragMove() in a touch event handler for example.

I also tried setting the position of the window manually in the touch down event handler - buit that led to recursive issues and a wildly-vibrating window!

I even tried 'faking' a mousebutton down and then using DragMove() but i frankly was shooting in teh dark here and had no luck.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Oct-13 10:47am    
Excuse me, you should not post such content as "solution", it is not, so it's considered as abuse. Please use comments, replies to existing comments or (best in this case) — Improve question.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900