Skip to content

Commit

Permalink
Move DrawOutline into BaseWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
moden-py committed Mar 6, 2016
1 parent 196f467 commit 55f0200
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 67 deletions.
71 changes: 68 additions & 3 deletions pywinauto/base_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import unicode_literals
from __future__ import print_function

import time
import abc
import ctypes
import locale
import re
import time
import win32process
import locale
import abc

try:
from PIL import ImageGrab
Expand Down Expand Up @@ -441,6 +442,70 @@ def get_properties(self):
# Non PEP-8 alias
GetProperties = get_properties

#-----------------------------------------------------------
def draw_outline(
self,
colour='green',
thickness=2,
fill=win32defines.BS_NULL,
rect=None):
"""Draw an outline around the window.
* **colour** can be either an integer or one of 'red', 'green', 'blue'
(default 'green')
* **thickness** thickness of rectangle (default 2)
* **fill** how to fill in the rectangle (default BS_NULL)
* **rect** the coordinates of the rectangle to draw (defaults to
the rectangle of the control.
"""

# don't draw if dialog is not visible
if not self.is_visible():
return

colours = {
"green": 0x00ff00,
"blue": 0xff0000,
"red": 0x0000ff,
}

# if it's a known colour
if colour in colours:
colour = colours[colour]

if rect is None:
rect = self.rectangle()

# create the pen(outline)
pen_handle = win32functions.CreatePen(
win32defines.PS_SOLID, thickness, colour)

# create the brush (inside)
brush = win32structures.LOGBRUSH()
brush.lbStyle = fill
brush.lbHatch = win32defines.HS_DIAGCROSS
brush_handle = win32functions.CreateBrushIndirect(ctypes.byref(brush))

# get the Device Context
dc = win32functions.CreateDC("DISPLAY", None, None, None )

# push our objects into it
win32functions.SelectObject(dc, brush_handle)
win32functions.SelectObject(dc, pen_handle)

# draw the rectangle to the DC
win32functions.Rectangle(
dc, rect.left, rect.top, rect.right, rect.bottom)

# Delete the brush and pen we created
win32functions.DeleteObject(brush_handle)
win32functions.DeleteObject(pen_handle)

# delete the Display context that we created
win32functions.DeleteDC(dc)
# Non PEP-8 alias
DrawOutline = draw_outline

#-----------------------------------------------------------
def is_child(self, parent):
"""
Expand Down
64 changes: 0 additions & 64 deletions pywinauto/controls/HwndWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,70 +746,6 @@ def debug_message(self, text):
# Non PEP-8 alias
DebugMessage = debug_message

#-----------------------------------------------------------
def draw_outline(
self,
colour = 'green',
thickness = 2,
fill = win32defines.BS_NULL,
rect = None):
"""Draw an outline around the window
* **colour** can be either an integer or one of 'red', 'green', 'blue'
(default 'green')
* **thickness** thickness of rectangle (default 2)
* **fill** how to fill in the rectangle (default BS_NULL)
* **rect** the coordinates of the rectangle to draw (defaults to
the rectangle of the control.
"""

# don't draw if dialog is not visible
if not self.is_visible():
return

colours = {
"green" : 0x00ff00,
"blue" : 0xff0000,
"red" : 0x0000ff,
}

# if it's a known colour
if colour in colours:
colour = colours[colour]

if not rect:
rect = self.rectangle()

# create the pen(outline)
pen_handle = win32functions.CreatePen(
win32defines.PS_SOLID, thickness, colour)

# create the brush (inside)
brush = win32structures.LOGBRUSH()
brush.lbStyle = fill
brush.lbHatch = win32defines.HS_DIAGCROSS
brush_handle = win32functions.CreateBrushIndirect(ctypes.byref(brush))

# get the Device Context
dc = win32functions.CreateDC("DISPLAY", None, None, None )

# push our objects into it
win32functions.SelectObject(dc, brush_handle)
win32functions.SelectObject(dc, pen_handle)

# draw the rectangle to the DC
win32functions.Rectangle(
dc, rect.left, rect.top, rect.right, rect.bottom)

# Delete the brush and pen we created
win32functions.DeleteObject(brush_handle)
win32functions.DeleteObject(pen_handle)

# delete the Display context that we created
win32functions.DeleteDC(dc)
# Non PEP-8 alias
DrawOutline = draw_outline

#-----------------------------------------------------------
def set_transparency(self, alpha = 120):
"Set the window transparency from 0 to 255 by alpha attribute"
Expand Down
19 changes: 19 additions & 0 deletions pywinauto/unittests/test_HwndWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,25 @@ def testGetProperties(self):
# def testCaptureAsImage(self):
# pass

# def testDrawOutline(self):
# """Test the outline was drawn."""
# # make sure window is ready
# self.dlg.Wait('active')
# self.dlg.Show.Click()
#
# # not sure why, but this extra call makes the test stable
# self.dlg.draw_outline()
#
# # outline control
# self.dlg.Show.draw_outline()
# img1 = self.dlg.Show.capture_as_image()
# self.assertEquals(img1.getpixel((0, 0)), (0, 255, 0)) # green
#
# # outline window
# self.dlg.draw_outline(colour="red")
# img2 = self.dlg.capture_as_image()
# self.assertEquals(img2.getpixel((0, 0)), (255, 0, 0)) # red

def testEquals(self):
self.assertNotEqual(self.ctrl, self.dlg.handle)
self.assertEqual(self.ctrl, self.ctrl.handle)
Expand Down
15 changes: 15 additions & 0 deletions pywinauto/unittests/test_UIAWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ def testGetProperties(self):
props = set(self.edit.get_properties().keys())
self.assertEquals(props, uia_props)

# def testDrawOutline(self):
# """Test the outline was drawn."""
# # not sure why, but this extra call makes the test stable
# self.dlg.draw_outline()
#
# # outline control
# self.button.draw_outline()
# img1 = self.button.capture_as_image()
# self.assertEquals(img1.getpixel((0, 0)), (0, 255, 0)) # green
#
# # outline window
# self.dlg.draw_outline(colour="blue")
# img2 = self.dlg.capture_as_image()
# self.assertEquals(img2.getpixel((0, 0)), (0, 0, 255)) # blue

class UIAWrapperMouseTests(unittest.TestCase):
"Unit tests for mouse actions of the UIAWrapper class"

Expand Down

0 comments on commit 55f0200

Please sign in to comment.