在画布上获取鼠标位置

时间:2017-08-06 17:35:46

标签: c# wpf canvas position mouse

我想获得相对于我的画布的鼠标位置。 我知道这通常通过以下方式完成: Mouse.GetPosition(my_canvas); 我在透明画布上听到了这样做的问题。我的画布不透明,但看起来如下......

<s:Editor x:Name="canvas_editor"
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Stretch" AllowDrop="True" >


                    <!-- Background Grid -->
                    <s:Editor.Background>
                        <DrawingBrush TileMode="Tile" Viewport="0,0,10,10" 
                                     ViewportUnits="Absolute" >
                            <DrawingBrush.Drawing>
                                <GeometryDrawing>
                                    <GeometryDrawing.Geometry>
                                        <RectangleGeometry Rect="0,0,25,25"/>
                                    </GeometryDrawing.Geometry>
                                    <GeometryDrawing.Pen>
                                        <Pen Brush="LightGray" Thickness="0.5"/>
                                    </GeometryDrawing.Pen>
                                </GeometryDrawing>
                            </DrawingBrush.Drawing>
                        </DrawingBrush>
                    </s:Editor.Background>


                    <Button Content="Add" s:Editor.Left="10" s:Editor.Top="10" Width="75" Click="Button_Click"/>
                    <Button Content="IO" s:Editor.Left="10" s:Editor.Top="30" Width="75" Click="Button_Click_IO"/>

                </s:Editor>

此编辑器是从画布派生的自定义控件。 我的画布位于屏幕的右下角。 现在,当我试图获得上述位置时,我得到以下位置:-300 -140 ...... 对应于主窗口上的0,0。 为什么会这样?任何人都可以帮助我或已经处理过类似的问题吗?

一切顺利 MQ

0 个答案:

没有答案