如何转换wx.GetMousePosition坐标以适合DC [英] How to convert wx.GetMousePosition co-ordinates to fit in a DC

查看:247
本文介绍了如何转换wx.GetMousePosition坐标以适合DC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个照片查看器,用户可以使用它来标记图像(例如在facebook中).单击图像以进行标记后,我使用wx.GetMousePosition查找在图像上单击鼠标的位置,然后创建用户已标记的图像的wx.MemoryDC,以便我可以绘制标记在图像上使用dc.DrawRectangle.

I'm creating a photo viewer which the user can use to tag images like in facebook. Once the image is clicked on to be tagged, I use wx.GetMousePosition to find the position where the mouse was clicked on the image.I then create a wx.MemoryDC of the image which the user has tagged so that I can draw the tags on the image using dc.DrawRectangle.

以下是用于绘制图像的代码: 我正在使用wxPython

Below is the code for drawing onto the image: I'm using wxPython by the way

        W=495 #Position of mouse (y)
        H=205 #Position of mouse (x)
        dc = wx.MemoryDC()
        dc.SelectObject(bitImg)#name of bitmap image
        dc.SetPen(wx.Pen('#FF0000'))

        #dc.SetBrush(wx.Brush('#aaffff'))
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
        dc.DrawRectangle(W, H, 20, 20)# Drawing the rectangle using the W and H 
                                      # for the position
        dc.SelectObject(wx.NullBitmap)

前两行是我从wx.GetMousePosition接收到的X和Y坐标.这是标签在图像之一上的位置.但是,当我绘制矩形时,这些点W和H不会显示在图像上,因为它们太大而不会显示在屏幕上.如果我同时将W和H都设置为90,则会在图像上绘制一个小方块.

The first two lines are the X and Y co-ordinates which I receive from wx.GetMousePosition. This is the position of a tag on one of the images. These points W and H however do not show on the image when I draw the rectangle as they are too big and don't show on the screen. If I set W and H both to 90 it will draw a small square on the image.

那么我该如何转换这些wx.GetMousePosition坐标,使其适合"dc"?或有关获取鼠标坐标的任何其他建议?

So how can I convert these wx.GetMousePosition co-ordinates so that they fit in the 'dc'? or any other suggestions on getting the mouse co-ordinates?

推荐答案

wx.GetMousePosition将返回鼠标在屏幕上的坐标,该坐标可能位于图像的错误位置,或者可能位于屏幕边缘您所看到的图像.

wx.GetMousePosition will return co-ordinates for the mouse on the screen which will either be in the wrong place in the image, or likely off the edge of the image as you're seeing.

转换这些坐标的一种方法是将它们传递到图像所在的面板上,panel.ScreenToClient(wx.GetMousePosition())会将它们转换为相对于面板左上角的坐标.

One way you can convert these co-ordinates is to pass them to the panel the image is on, panel.ScreenToClient(wx.GetMousePosition()) which will convert them into co-ordinates relative to the top left of the panel.

这篇关于如何转换wx.GetMousePosition坐标以适合DC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆