Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm coding a fixed-size map on a canvas that is built from polygon user controls that then has another user control for game counters added on top. I'm building towards moving the counters using mouse drag and drop; the first thing I wanted to do was get the Mouse position when the left button is clicked. When the mouse is clicked, regardless of where on the canvas the mouse is I get the same x and y values returned. Also, the canvas is inside a scroll viewer as the Canvas/map is larger than the window, when I resize the window, the returned value changes compared to a different sized window, but is the same regardless of position. I'm obviously missing something, probably because the canvas is inside a scroll viewer. XAML and C# code below, any advice would be appreciated.

What I have tried:

XAML:
<DockPanel>
        <ToolBarTray DockPanel.Dock="Top">
            <ToolBar >
                <Menu>
                    <MenuItem Header="Turn Options">
                        <MenuItem Header="Advance Phase"/>
                        <MenuItem Header="End Turn" />
                        <Separator/>
                    </MenuItem>
                        <MenuItem Header="Map Options">
                        <MenuItem Header="Load Map"/>
                    </MenuItem>
                    <Separator />
                    <MenuItem Header="Designer Mode"/>
                    <Separator/>
                    <MenuItem Header="Economics">
                        <MenuItem Header="Open Economics"/>
                    </MenuItem>
                    <Separator/>
                    <MenuItem Header="Game Stats"/>
                </Menu>
            </ToolBar>
        </ToolBarTray>
        <ScrollViewer DockPanel.Dock ="Top" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" >
        <Canvas x:Name="MainMap" Background="Black" Height="1740" Width="4740" IsHitTestVisible="true" MouseDown="MainMap_MouseClick" >
            
        </Canvas>
        </ScrollViewer>
    </DockPanel>


C#
private void MainMap_MouseClick(object sender, MouseButtonEventArgs e)
       {
          try
           {
               Control src = e.Source as Control;

               if (e.Source is GameCounter)
               {
                   if (src != null)
                   {
                       switch (e.ChangedButton)
                       {
                           case MouseButton.Left:
                               GameCounter clickedGameCounter = (GameCounter)e.Source;

                               Point p = Mouse.GetPosition(MainMap );

                               MessageBox.Show("X = " + p.X + " Y = " + p.Y);
                               break;
                       }
                   }
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
Posted
Updated 4-Jun-21 23:07pm

1 solution

 
Share this answer
 
Comments
Welchbloke 5-Jun-21 17:11pm    
That will be helpful for my full implementation thanks; however, I still don't understand why GetPostion returns the same X and Y coordinates for every click regards of where on the canvas I click (no dragging involved). Any thoughts or suggestions on why that might be?
Richard MacCutchan 6-Jun-21 3:01am    
Sorry, no idea. I have used similar method calls in Win32 (C/C++), and Windows Forms applications, but never had a problem. Maybe it is something unique to WPF.

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