Drag and Drop Item Outside WPF Application

In WFP applications drag and drop functionality is provided by subscribing several events. First of all you need LeftMouseButtonClick and MouseMove events in order to start Drag and Drop.

This is accomplished by the following code:

private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    this._startPoint = e.GetPosition(null);

private void Window_MouseMove(object sender, MouseEventArgs e)
    //drag is heppen
    //Prepare for Drag and Drop
    Point mpos = e.GetPosition(null);
    Vector diff = this._startPoint - mpos;

    if (e.LeftButton == MouseButtonState.Pressed &&
        (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
        Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))

        //hooking on Mouse Up
        InterceptMouse.m_hookID = InterceptMouse.SetHook(InterceptMouse.m_proc);

        //ataching the event for hadling drop
        this.QueryContinueDrag += queryhandler;
        //begin drag and drop
        DataObject dataObj = new DataObject(this.text1);
        DragDrop.DoDragDrop(this.text1, dataObj, DragDropEffects.Move);


As we can see from the code above, first with LeftMouseButtonClick event, we captured the starting point, then by MouseMove event, calculated distance between starting point and the current mouse position. If the distance is big enough we start DragDrop.

When the Drag starts it is necessary to prepare Drag functionality by providing the object to be dragged. In our case (code sample above) we are going to drag TextBox. In order to drag TextBox first we create DragData object by specifying TextBox object in the Constructor, and call DragDrop static method by passing objects we mentioned.
Beside preparing data to be dragged, we need to subscribe to QueryContinueDrag event in order to track dragging status.

This is all we need to prepare Drag and Drop in our application. If we want to drag object out of WPF aplication, we have no enough information to accomplish drop. As soon as the mouse is outside the app, mousemove event is not firing any more. One of the solution of the problem could be capturing the mouse move position outside the WFP application, and when the left mouse button is up, start with dropping item functionality.

In order to track mouse position outside the WPF application we need to hook and subscribe to the messages of whole Windows OS and filter only we are interesting in. Great blog post about how to capture  mouse messages regardless of the  mouse position can be found here.

Implementation of Low Level Mouse Hook in C# blog post can be modified quickly in order to adopt to our case.

First we need a property to indicate when the mouse is outside the application.

public static bool IsMouseOutsideApp 

Then we need to modify HookCallback method so that when the Left mouse button is up, and set the property (IsMouseOutsideApp ) to true if the mouse outside the application.

internal static IntPtr HookCallback( int nCode, IntPtr wParam, IntPtr lParam)
    if (nCode >= 0 && MouseMessages.WM_LBUTTONUP == (MouseMessages)wParam)
        MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));

        //check if POint in main window
        Point pt = new Point(hookStruct.pt.x, hookStruct.pt.y);
        var ptw = App.Current.MainWindow.PointFromScreen(pt);
        var w = App.Current.MainWindow.Width;
        var h = App.Current.MainWindow.Height;
        //if point is outside MainWindow
        if (ptw.X < 0 || ptw.Y < 0 || ptw.X > w || ptw.Y > h)
            IsMouseOutsideApp = true;
            IsMouseOutsideApp = false;
    return CallNextHookEx(m_hookID, nCode, wParam, lParam);

The Last thing we need to implement is DragSourceQueryContinueDrag event. The implementation is straightforward:
1. check the keystate value None – this is the case when the mouse is released.
2. unhook from the mouse intercepting messages
3. show the message text based on the result of drag and dropping.

/// <summary>
/// Continuosly tracking Dragging mouse position
/// </summary>
private void DragSourceQueryContinueDrag(object sender, QueryContinueDragEventArgs e)
    //when keystate is non, draop is heppen
    if (e.KeyStates == DragDropKeyStates.None)
        //unsubscribe event
        this.QueryContinueDrag -= queryhandler;
        e.Handled = true;
        //Unhooking on Mouse Up

        //notifiy user about drop result
            () =>;
                //Drop hepend outside Instantly app
                if (InterceptMouse.IsMouseOutsideApp)
                    MessageBox.Show("Dragged outside app");
                    MessageBox.Show("Dragged inside app");

Complete demo with source code can be found here:

Announcing Oral Defense of my Doctoral Dissertation



I am very happy I can announce the Event which is very important to me, and long awaited in my life.

At Saturday this week,  Oral Defense of my Doctoral Dissertation will be held. More precisely at 26th of April at 11:00  AM the Main Ceremony will begin in Amphitheater 1 of Technical Faculty in Bihać.

There is going to be very exciting event for me, even more because lot of my friends, student mates, teachers from high school and faculty will be there too, beside my family mother and dad, brother and sisters, as well as my daughters and my wife.

The Doctoral Dissertation main field is Mechanical Engineering. Main subject of the thesis is about  determination of Fractal Mechanics parameters by using experimental, numerical and evolution methods in welded joints of pressure vessels. It presents synergy of applying experimental research with  Finite Element Method and Genetic Programming.


Sa radosnom viješću objavljujem događaj koji je vrlo važan i dugo isčekivan u mom životu.

U subotu ove sedmice održat će se javna odbrana moje doktorske disertacije. Tačnije 26. aprila u 11 sati započet će ceremonija odbrane u Amfiteatru 1 Tehničkog fakulteta u Bihaću.

Ovo je zaista jedan od najvažnijih događaja u životu, a posebno kada očekujem prisustvo mojih dragih prijatelja, bivših studentskih kolega, mojih profesora iz srednje škole i fakulteta, pored moje uže familije majke, oca, brata i sestara, mojih kćerkih i supruge.

Doktorka disertacija koju branim je iz područja mašinstva. Glavna tema rada je određivanje parametara mehanike loma pomoću eksperimentalnih, numeričkih i evolucijskih metoda. Rad na svojstven način predstavlja sinergiju eksperimentalnog istraživanja sa metodom konačnih elemenata i metodom genetskog programinja.

How to Setup Counter Strike Dedicated Server on Windows Azure Virtual Machine

To setup Counter Strike Dedicated Server on Virtual Machine running on Windows Azure you have to perform 10 steps. This blog post is going to explain all them in detail.

1. First thing you should grab is Windows Azure Account. If you dont have one, you can create trial. More information about creating Windows Azure Account you can find on Windows Azure Portal.


2. Create Virtual Machine on Windows Azure. Choose New From Bottom Bar and Choose: Compute->Virtual Machine-From Gallery (see pic below).


3. From List of Virtual Machine Select Windows Server 2012.


4. Setup Name of VM, default username and password, and follow instructions. After you finish with VM, by clicking Finish button, Windows Azure will create your VM in few minutes. If you need more information how to setup VM on Windows AZure please follow instruction on this link.

5. Download RDP Connection from Windows Azure Portal and connect to you Machine (See picture below).


6. Open your Windows Server VM and Install Counter Strike. After installation go to Counter Strike Folder and Run hlds.exe to cretae Dedicated Server.  Fill the fields with the required information and choose Start Server.


Now your dedicated server is running but no one can access it yet. We need to open default UDP port 27015 on Windows Firewall, as well as create endpoint on Windows Azure Portal.

7. Open Windows Firewall and Create Incoming and Outcoming Rule for UDP Port= 27015.


8. Go to Windows Azure Portal and create Endpoint for this UDP Port.


9. This is all you need to create. Go to Client PC choose Add Server and put public IP adress:27015  of you Windows Azure Machine or VMName.cloudapp.net:27015.

10. Invite your friends to play the Game by providing them with the  IP and the UDP Port.


Using TaskCompletionSource in wraping event handler

Using TaskCompletionSource you can wrap any operation in to task, so that you can do anything you can with the task object. The TaskCompletionSource class is very important and today’s post will be explain how to wrap button click event in to TaskCompletionSource. With this wrap we will see how complicated operation behind click button handler can be simplified.

The for this blog post is simple Windows Store app shown on the picture below.


When the Play Slides is clicked, Image slide is started which start animation of images. XAML code behind this app is listed here:

    <Storyboard x:Name="animImageSlideIn">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)"
            <EasingDoubleKeyFrame KeyTime="0" Value="900"/>
            <EasingDoubleKeyFrame KeyTime="0:0:2" Value="0" />
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Width="200" Height="250" >
        <Button x:Name="playbtn" Height="50" Margin="0,5,0,5" Content="Play Slides" HorizontalAlignment="Center" Click="playbtn_Click"></Button>

        <Image x:Name="img" HorizontalAlignment="Center">
                <CompositeTransform TranslateY="900" />

First, it will be presented  the implementation without TaskCompletionSource. The Play Slides Click event is the following code:

private async void  playbtn_Click(object sender, RoutedEventArgs e)
    int i = 1;

    EventHandler<object> handler = null;

    handler = delegate
        if (i <= 3)
            playbtn.Content = string.Format("Slide Image {0}",i);



            playbtn.Content = "Play Slides";
            animImageSlideIn.Completed -= handler;


    animImageSlideIn.Completed += handler;

As we can see from the listing above the code is pretty much long and little bit confused because we subscribe to the handler and call it as much as we reach the magic number of slides. Whe the number of slides is reached we unsubscribed from the handler and exit  the method.

Now implement the same functionality with the TaskCompletionSource class. The following listing shows the implementation:

private async void playbtn_Click(object sender, RoutedEventArgs e)

for(int i=1; i<=3; i++)
playbtn.Content = string.Format("Slide Image {0}",i);
await animImageSlideIn.RunAsync();

playbtn.Content = "Play Slides";

As we can see the implementation is very simple and concise. in for loop we call LoadSourceImage then asynchrony run animation.

The source code of the demo can be found by clicking the image below:

This blog post is inspired by Stephen Toub //build/ session.

Pausing and cancelling async method in C#

Responsiveness of your app is not just fee UI thread by implementing async. It is more that that. When a long operation is under process in your app, user sometimes wants to cancel it  or  pause the operation. Imagine your app processing hundreds of files or images. Such a operation can take more that few seconds and user must have option to cancel it. Canceling and pausing are very important feature for every app that implements long operations.

This blog post will present the way of using CancelationToken built in cancel feature in .NET, as well as a PauseToken custom implementation which is very similar to CancelationToken.

Original implementation of PauseToken is from the pfxteam blog which you can find here.

We will implement simple Windows Store app with cancel and pausing the async operation. The picture below shows the sample app:



As you can see when the Start Process button is clicked it begins process of processing image files. There is also ProgressRing control which shows the progress and percentage of completeness. From the right side you can see two buttons. The Cancel button cancels the operation, and pause button pauses operation until the Pause button is clicked again.

The implementation behind Process button is the folowing:

private async  void btnProcess_Click(object sender, RoutedEventArgs e)
    //creating cancel and pause token sources
    m_pauseTokeSource = new PauseTokenSource();
    m_cancelationTokenSource = new CancellationTokenSource();

    //get al picture from picture library
    var picturesFolder = KnownFolders.PicturesLibrary;
    var fileList = await picturesFolder.GetFilesAsync();

    //set ProgressRing to active
    ring2.IsActive = true;

        //asynchrony process files, by passing pasue and calcel tokens

        await ProcessImages(fileList, m_pauseTokeSource.Token, m_cancelationTokenSource.Token);
    catch (Exception)
        //do nothing when somthing went wrong not when taks is canceled
        //make inactive ProgressRing
        ring2.IsActive = false;

Click event implementation of the Start Process button 

First we create Cancel and Pause Source tokens. Gent the picture content in form of list of files. Then we call asynchonious ProcessImages method by passing list of images files, cancel and pause tokens. Process images is called within try catch finally blocks, because every cancel task throws exception.

Implementation ProcessImages async method

ProcesImage method is async method which accept cancelation and pasue token nad returns Task object.

public async Task ProcessImages(IEnumerable<StorageFile> images, PauseToken pauseToken, CancellationToken cancelToken)
    double count=images.Count();
    double current=0;
    foreach (var file in images)
        //if the paise is active the code will wait here but not block UI thread
        await pauseToken.WaitWhilePausedAsync();

        ring2Text.Text = string.Format("{0}%",(int)(100*current / count));

        await ProcessAsync(file, cancelToken);
    ring2Text.Text = string.Format("100%");

In foreach loop first we await pauseToken.WaitWhilePausedAsync(); which wait if IsPause property of the Token class is true, otherwize there is no awaiting here. The next await is out Delay which takes cancelation token as parameters. When the Pause button is clicked, pauseToken is awaiting until the pause button is clicked again. In case of cancelation when the Cancel button is clicked the Cancel() method of the cancelationTokenSource is called and exception is thorwn. Then processImages method is interupted and finally blick progressring is disabled.
Pause and Cancel Click implementation are shown in the following listing:

private void btnPause_Click(object sender, RoutedEventArgs e)
 m_pauseTokeSource.IsPaused = !m_pauseTokeSource.IsPaused;

private void btnCancel_Click(object sender, RoutedEventArgs e)

Complete source code can be downloaded from link below.