Click here to Skip to main content
13,594,672 members
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 12 Dec 2008
Licenced CPOL

Window Location in WPF

, 12 Dec 2008
Rate this:
Please Sign up or sign in to vote.
Shows you how we can change window location in WPF


This is a simple application that shows how we can change window location in WPF.
This robot follows your cursor forever.

Using the Code

In a few steps, we can create this application.
First I created a window in WPF - I've done it with Expression Blend 2 SP1.







  Width="85" Height="135" 

  AllowsTransparency="True" WindowStyle="None" 




  mc:Ignorable="d" Topmost="True">
      <Grid x:Name="LayoutRoot">
          <Image Margin="-23,0,-20,7" Source="Rbo rx2.png" 

          	Stretch="Fill" RenderTransformOrigin="0.5,0.5">
                      <ScaleTransform x:Name="mamadScaleTransform" 

                      	ScaleX="1" ScaleY="1"/>
                      <SkewTransform AngleX="0" AngleY="0"/>
                      <RotateTransform Angle="0"/>
                      <TranslateTransform X="0" Y="0"/>

Then we can access the window location with these properties:

  • Left
  • Top

I've added some methods to use cursor position for changing the window location:

public Window1()
    // Insert code required on object creation below this point.
    Timer timer = new Timer();
    timer.Interval = 10;
    timer.Tick += new EventHandler(timer_Tick);
    timer.Enabled = true; 

static extern bool GetCursorPos(ref System.Drawing.Point lpPoint);
float coefficient = 0.02f;

void timer_Tick(object sender, EventArgs e)
    // New point that will be updated by the function with the current coordinates
    System.Drawing.Point mouseposition = new System.Drawing.Point();
    // Call the function and pass the Point, defPnt
    GetCursorPos(ref mouseposition);
    if (mouseposition.X < this.Left)
       mamadScaleTransform.ScaleX = 1;
       mamadScaleTransform.ScaleX = -1;
    this.Left += (mouseposition.X - this.Left) * coefficient;
    this.Top += (mouseposition.Y - this.Top) * coefficient;

That's all.


  • 13th December, 2008: First post


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


About the Author

Mohammad Dayyan
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionInteresting Pin
kmf11-Aug-14 10:58
memberkmf11-Aug-14 10:58 
GeneralJust to properties Pin
R. M. Masudur23-Jan-10 19:18
memberR. M. Masudur23-Jan-10 19:18 
GeneralMy vote of 2 Pin
domnisoaradiane3-Apr-09 12:38
memberdomnisoaradiane3-Apr-09 12:38 
GeneralCool Robot Pin
Jose M. Menendez Poó13-Dec-08 4:22
memberJose M. Menendez Poó13-Dec-08 4:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180621.3 | Last Updated 13 Dec 2008
Article Copyright 2008 by Mohammad Dayyan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid