Drag Image co - ordinates

Hi All,

Well I’ve been up all night trying to sort this out. I’m still experimenting with the DragImage demo. Could someone tell me am I going about this the wrong way?

I am displaying a background ‘paper’ on which to apply text to. The ‘paper’ is just an A4 sized draggable bmp.

    bmp = wx.Bitmap("Paper.BMP", wx.BITMAP_TYPE_ANY)
    shape1 = DragShape(bmp)
    shape1.pos = (125, 55)
    shape1.fullscreen = False       
    self.shapes.append(shape1)

To this I have appended some text and everything seems to work fine - paper drags with text attached. The problem is of course that the text is attached to the default upper left hand corner of the bitmap.

     words = ['a','b','c','d','a','b','c','d','a','b','c','d']
    text = " "+"".join(words)
    bg_colour = wx.Colour(57, 115, 57)  # matches the bg image
    font = wx.Font(36, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Arial")
    textExtent = self.GetFullTextExtent(text, font)
   
    # create a bitmap the same size as our text
    bmp1 = wx.EmptyBitmap(textExtent[0], textExtent[1])

    # 'draw' the text onto the bitmap
    dc = wx.MemoryDC()
    dc.SelectObject(bmp)
   
    #dc.SetBackground(wx.Brush(bg_colour, wx.SOLID))       
    #dc.Clear()
   
    dc.SetTextForeground(wx.BLACK)
    dc.SetFont(font)
  
    dc.DrawText(text, 0, 0)
  
    dc.SelectObject(wx.NullBitmap)
    mask = wx.Mask(bmp, bg_colour)
    bmp1.SetMask(mask)
   
    shape = DragShape(bmp1)
    shape.pos = (145, 100)
   
    self.shape1.append(shape)

Could someone show / tell me how I go about controlling the co-ordinates for the text so that they are no longer global but refer to the paper?

Thanks,

Malcolm

Malcolm Clift wrote:

Hi All,
Well I've been up all night trying to sort this out. I'm still experimenting with the DragImage demo. Could someone tell me am I going about this the wrong way?
I am displaying a background 'paper' on which to apply text to. The 'paper' is just an A4 sized draggable bmp.

[...]

To this I have appended some text and everything seems to work fine - paper drags with text attached. The problem is of course that the text is attached to the default upper left hand corner of the bitmap.

[...]

Could someone show / tell me how I go about controlling the co-ordinates for the text so that they are no longer global but refer to the paper?

I'm not sure I understand what your problem is. When you draw the text on a bitmap via a wx.MemoryDC then coordinantes are relative to the upper-left corner of the DC. When you position the bitmap on the main window then the coordinants are relative to the upper-left of the Window (or its DC), so you just need to shift using the relative positions of each and it should do what you want. Am I missing something?

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Hi Robin,

Thanks for the reply. I have appended the text to the paper by experimenting
with the DragImage code .

        bmp = wx.Bitmap("Paper.BMP", wx.BITMAP_TYPE_ANY)
        shape1 = DragShape(bmp)
        shape1.pos = (125, 55)
        shape1.fullscreen = False
        self.shapes.append(shape1)

        l1 = ['a','b','c','d']
        text = " "+"".join(l1)
        bg_colour = wx.Colour(57, 115, 57)
        font = wx.Font(36, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Arial")
        textExtent = self.GetFullTextExtent(text, font)

        bmp1 = wx.EmptyBitmap(textExtent[0], textExtent[1])

        dc = wx.MemoryDC()
        dc.SelectObject(bmp)

        #dc.SetBackground(wx.Brush(bg_colour, wx.SOLID))
        #dc.Clear()
        dc.SetTextForeground(wx.BLACK)
        dc.SetFont(font)

        dc.DrawText(text, 0, 0)#Use to set mask area

        dc.SelectObject(wx.NullBitmap)
        mask = wx.Mask(bmp, bg_colour)
        bmp1.SetMask(mask)

        shape = DragShape(bmp1)
        shape.pos = (145, 100)#Position

        self.shape1.append(shape)

I think that I'm going about this the wrong way. I understand what you're
saying, but how do I alter the code?

Thanks,

Malcolm

Hi All,

I have been trying to do this myself. I am learning, but haven't quite got
what I wanted. Any help would be gratefully appreciated.

        self.bg_bmp = wx.Bitmap("Paper, white fine laid.bmp",
wx.BITMAP_TYPE_ANY)
        self.Paper = wx.Bitmap("Paper.bmp", wx.BITMAP_TYPE_ANY)

def DoDrawing(self, dc, printing=False):
        dc.BeginDrawing()

        l1 = ['a','b','c','d']
        text = " "+"".join(l1)
        bg_colour = wx.Colour(57, 115, 57)
        dc.SetFont(wx.Font(36, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Arial"))
        dc.SetTextForeground(wx.BLACK)
        te = dc.GetTextExtent(text)

        dc.DrawBitmap(self.Paper, 200, 20, True)
        dc.DrawText(text2, 60, 65)

Could someone show me how to draw the text on to the paper dc and not the
main canvas? It looks fine at the moment with the text overlaid, but I wish
to impliment draggable paper once I can get the text on to it.

Thanks,

Malcolm

Malcolm Clift wrote:

        self.bg_bmp = wx.Bitmap("Paper, white fine laid.bmp",
wx.BITMAP_TYPE_ANY)
        self.Paper = wx.Bitmap("Paper.bmp", wx.BITMAP_TYPE_ANY)

def DoDrawing(self, dc, printing=False):
        dc.BeginDrawing()

        l1 = ['a','b','c','d']
        text = " "+"".join(l1)
        bg_colour = wx.Colour(57, 115, 57)
        dc.SetFont(wx.Font(36, wx.MODERN, wx.NORMAL, wx.NORMAL, 0, "Arial"))
        dc.SetTextForeground(wx.BLACK)
        te = dc.GetTextExtent(text)

        dc.DrawBitmap(self.Paper, 200, 20, True)
        dc.DrawText(text2, 60, 65)

Could someone show me how to draw the text on to the paper dc and not the
main canvas? It looks fine at the moment with the text overlaid, but I wish
to impliment draggable paper once I can get the text on to it.

you need something like:

dc = wxMemoryDC()
dc.SelectObject(self.Paper)
self.DoDrawing(dc)

and remove: dc.DrawBitmap(self.Paper, 200, 20, True) from DoDrawing.

then the text will get drawn on the paper bitmap.

By the way, if this doesn't seem to work, send a complete, minimal, app, so we can actually try out he code and debug it easily.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov