拖动地图般的图像

时间:2014-11-22 22:12:44

标签: c# wpf image click drag

我正在制作类似于谷歌地图的程序,但规模要小得多,仅由我的家乡组成。它基本上是一个大分辨率的图像,比窗口大,我想在点击事件上拖动它。

问题在于,当我点击地图时,无论我在哪里点击它,它总是会捕捉到它的中心。我怎么能这样做,所以图像不像这样,所以无论我点击它,它都保持在相同的坐标上?

编辑:

这是我的程序的代码。我必须说我是C#的新手,我之前只在学校做过几年C ++,所以对我不要太苛刻!

CS:

    public MainWindow()
    {
        InitializeComponent();
        System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0,0,0,0,1);
        dispatcherTimer.Start();

        System.Windows.Threading.DispatcherTimer dispatcherTimer2 = new System.Windows.Threading.DispatcherTimer();
        dispatcherTimer2.Tick += new EventHandler(dispatcherTimer2_Tick);
        dispatcherTimer2.Interval = new TimeSpan(0, 0, 0, 0, 10);
        dispatcherTimer2.Start();


    }

    bool ok=false, ok2=false;
    int count;
    float x = 0, x2=250;
    float y=5;   
    bool click = false;


    void move_map(Image img, double left, double top, double right, double bot )
    {
        img.Margin = new Thickness(img.Margin.Left + left, img.Margin.Top + top, img.Margin.Right + right, img.Margin.Bottom + bot);
    }



    private void dispatcherTimer2_Tick(object sender, EventArgs e)
    {

       //HERE I WANT TO ADD THE CODE WHICH DRAGS THE MAP
    }




    //HERE I DO THE ZOOM IN AND ZOOM OUT ( WHICH IS NOT REALLY THE BEST BUT IT WORKS! )
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {

        text.Text = click.ToString() + " " + map.Margin.Left + " " + map.Margin.Top;

        if (ok == true)
        {
            count++;
            ok2 = false;




                if (x < 100)
                {
                    map.Height += y;
                    x += y;
                    map.Width += 2 * y;

                    y *= 1.1f;


                }

                else
                {

                    if (x < 250)
                    {
                        map.Height += y;
                        x += y;
                        map.Width += 2 * y;

                        y *= 0.9f;

                    }

                    else
                    {

                        ok = false;
                        x = 0;
                        y = 5;
                    }

                }




            }




        if (ok2 == true && count > -1)
        {
            if (count > 0) count--;


                if (x2 > 150)
                {
                    map.Height -= y;
                    x2 -= y;
                    map.Width -= 2*y;

                    y *= 1.1f;


                }
                else
                {

                    if (x2 > 0)
                    {
                        map.Height -= y;
                        x2 -=  y;
                        map.Width -= 2 * y;

                        y *= 0.9f;

                    }
                    else
                    {

                        ok2 = false;
                        x2 = 250;
                        y = 5;
                    }
                }






            }
        }




    private void z_in_click(object sender, MouseButtonEventArgs e)
    {
        if(count<4*37)
        ok = true;

    }


    private void z_out_click(object sender, MouseButtonEventArgs e)
    {
        if(count!=0)
        ok2 = true;

    }


    private void map_click(object sender, MouseButtonEventArgs e)
    {
        click = true;
    }

    private void map_noclick(object sender, MouseButtonEventArgs e)
    {
        click = false;
    }

}

}

XAML:

<Grid x:Name="grid">


        <Image x:Name="map" Height="904" Width="1372" Source="satu mare 3.png" Stretch="Fill" HorizontalAlignment="Left" VerticalAlignment="Top" Canvas.Left="-200" Canvas.Top="-197" Margin="-178,-238,-200,-195">
            <Image.Effect>
                <DropShadowEffect Opacity="0.5" Color="#FF171717"/>
            </Image.Effect>
        </Image>

    <TextBox x:Name="text" HorizontalAlignment="Left" Height="43" Margin="384,393,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="235"/>
    <Image x:Name="z_in1" HorizontalAlignment="Left" Height="69" Margin="870,392,0,0" VerticalAlignment="Top" Width="89" Source="plus.png" Stretch="Fill" MouseLeftButtonUp="z_in_click"/>
    <Image x:Name="z_out1" HorizontalAlignment="Left" Height="69" Margin="761,393,0,0" VerticalAlignment="Top" Width="89" Source="minus.png" Stretch="Fill" MouseLeftButtonUp="z_out_click"/>


</Grid>

0 个答案:

没有答案