Click here to Skip to main content
11,496,146 members (514 online)
Click here to Skip to main content

Window Location in WPF

, 12 Dec 2008 CPOL 35K 2.3K 20
Shows you how we can change window location in WPF
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.


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

Comments and Discussions

QuestionInteresting Pin
kmf11-Aug-14 11:58
memberkmf11-Aug-14 11:58 
GeneralJust to properties Pin
R. M. Masudur23-Jan-10 20:18
memberR. M. Masudur23-Jan-10 20:18 
GeneralMy vote of 2 Pin
domnisoaradiane3-Apr-09 13:38
memberdomnisoaradiane3-Apr-09 13:38 
Very basic example - lacks depth. No explanation for beginners. Did not contain the information I was looking for.
GeneralCool Robot Pin
Jose M. Menendez Poó13-Dec-08 5:22
memberJose M. Menendez Poó13-Dec-08 5:22 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 13 Dec 2008
Article Copyright 2008 by Mohammad Dayyan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid