class IForm(Form): def __init__(self): self.Text = 'RTOS Demo LED0 Display Form' self.ClientSize = Size(440, 100) self.FormBorderStyle = FormBorderStyle.FixedSingle self.MaximizeBox = False font = Font('MS UI Gothic', 32, FontStyle.Bold) ta = ContentAlignment.MiddleCenter self.LED0 = Label(Top=20, Left=20, Height=60, Width=120, Font=font, TextAlign=ta) self.LED0.Text = 'LED0' self.LED0.ForeColor = Color.White self.Controls.Add(self.LED0) self.timer = Timer() self.timer.Interval = 200 self.timer.Tick += self.OnTickTimer def OnLoad(self, event): print 'Now Running...' self.ThrowExceptSave = common.ThrowExcept self.ViewOutputSave = common.ViewOutput common.ThrowExcept = False common.ViewOutput = False self.timer.Start() self.BringToFront() def OnClosed(self, event): self.timer.Stop() common.ThrowExcept = self.ThrowExceptSave common.ViewOutput = self.ViewOutputSave print 'Quit' def OnTickTimer(self, sender, event): # The debugger (or build) object does not exist if no project is opened. # Or the debugger (or build) object will disappear after project is closed. try: if 0 == debugger.Watch.GetValue("PORT4.PODR.B0"): self.LED0.BackColor = Color.Blue else: self.LED0.BackColor = Color.DarkGray except: pass
class App(Form): def __init__(self): self.Title="Timer" self.timer1=Timer() self.timer1.Interval=1000 self.timer1.Tick+=self.timer1_tick label1=Label() label1.AutoSize=True label1.Location=Point(41,22) label1.Text="00:00:00" label1.Font=Font("MS UI Gothic",24.0,FontStyle.Regular) self.label1=label1 self.Controls.Add(self.label1) clientwidth=255 b1=Button() b1.Location=Point((clientwidth-b1.Width*2)/3,68) b1.Text="Click" b1.Click+=self.start_Click self.Controls.Add(b1) b2=Button() b2.Location=Point((clientwidth-b1.Width*2)*2/3+b1.Width,68) b2.Text="Stop" b2.Click+=self.stop_Click self.Controls.Add(b2) self.ClientSize=Size(clientwidth,103) self.Text="Stop Watch" self.StartPosition=FormStartPosition.CenterScreen def timer1_tick(self,sender,e): self.starttime=self.starttime+TimeSpan(0,0,1) self.label1.Text=self.starttime.ToString() def start_Click(self,sender,e): self.starttime=TimeSpan(0,0,0) self.label1.Text=self.starttime.ToString() self.timer1.Start() def stop_Click(self,sender,e): self.timer1.Stop()
class PickSnipTool(Form): mouse_down = None mouse_down_button = None mouse_down_seconds = 0 mouse_down_timer = None mouse_up = None snip_enabled = False snip_rectangle = None click_timeout = 1 # set this to eg. 4 to enable click + pick @staticmethod def take_screenshot(): bounds = Screen.PrimaryScreen.Bounds screenshot = Bitmap( bounds.Width, bounds.Height, PixelFormat.Format32bppPArgb, ) graphics = Graphics.FromImage(screenshot) graphics.CopyFromScreen(0, 0, 0, 0, screenshot.Size) return screenshot @staticmethod def pick(snip=False): snipper = PickSnipTool(PickSnipTool.take_screenshot(), snip=snip) while True: result = snipper.ShowDialog() if result in [DialogResult.OK, DialogResult.Cancel]: break if snipper.mouse_down_seconds == 1: Mouse.Instance.Click(snipper.mouse_down) time.sleep(0.5) snipper.BackgroundImage = PickSnipTool.take_screenshot() if result == DialogResult.OK: if (snipper.mouse_down_seconds and snipper.mouse_down_seconds <= snipper.click_timeout): Mouse.Instance.Click(snipper.mouse_down) time.sleep(0.5) el = AutomationElement.FromPoint(snipper.mouse_up) result = { prop.ProgrammaticName.split('.', 1)[-1]: el.GetCurrentPropertyValue(prop) for prop in el.GetSupportedProperties() } result.update({ 'NameProperty': el.GetCurrentPropertyValue(el.NameProperty), 'ControlTypeProperty': el.GetCurrentPropertyValue( el.ControlTypeProperty ).ProgrammaticName.split('.', 1)[-1], 'AutomationIdProperty': el.GetCurrentPropertyValue( el.AutomationIdProperty ), }) return result else: return {} def __init__(self, screenshot=None, snip=False): super(PickSnipTool, self).__init__() self.snip_enabled = snip self.Cursor = Cursors.Cross self.BackgroundImage = screenshot self.ShowInTaskbar = False self.FormBorderStyle = getattr(FormBorderStyle, 'None') self.WindowState = FormWindowState.Maximized self.DoubleBuffered = True self.TopMost = True self.TopLevel = True self.MouseDown += self.OnMouseDown self.MouseMove += self.OnMouseMove self.MouseUp += self.OnMouseUp self.KeyUp += self.OnKeyUp self.KeyDown += self.OnKeyDown self.Paint += self.OnPaint def OnTimerTick(self, sender, e): self.mouse_down_seconds += 1 if self.mouse_down_seconds == 1: self.DialogResult = DialogResult.Retry if self.mouse_down_seconds > self.click_timeout: self.DialogResult = DialogResult.Retry def OnMouseDown(self, sender, e): self.mouse_down = Point(e.Location.X, e.Location.Y) self.mouse_down_button = e.Button self.mouse_down_seconds = 0 if not self.snip_enabled: self.mouse_down_timer = Timer() self.mouse_down_timer.Interval = 1000 self.mouse_down_timer.Tick += self.OnTimerTick self.mouse_down_timer.Start() self.snip_rectangle = Rectangle(e.Location, Size(0, 0)) def OnMouseMove(self, sender, e): if self.mouse_down and self.snip_enabled: x1 = Math.Min(e.X, self.mouse_down.X) y1 = Math.Min(e.Y, self.mouse_down.Y) x2 = Math.Max(e.X, self.mouse_down.X) y2 = Math.Max(e.Y, self.mouse_down.Y) self.snip_rectangle = Rectangle(x1, y1, x2 - x1, y2 - y1) self.Invalidate() def OnMouseUp(self, sender, e): self.mouse_up = Point(e.Location.X, e.Location.Y) if self.mouse_down_timer is not None: self.mouse_down_timer.Stop() self.DialogResult = DialogResult.OK def OnPaint(self, sender, e): if self.snip_enabled and self.snip_rectangle is not None: br = SolidBrush(Color.FromArgb(120, Color.White)) rc = self.snip_rectangle pen = Pen(Color.Red, 3) x1 = rc.X x2 = rc.X + rc.Width y1 = rc.Y y2 = rc.Y + rc.Height e.Graphics.FillRectangle(br, Rectangle(0, 0, x1, self.Height)) e.Graphics.FillRectangle( br, Rectangle(x2, 0, self.Width - x2, self.Height) ) e.Graphics.FillRectangle(br, Rectangle(x1, 0, x2 - x1, y1)) e.Graphics.FillRectangle( br, Rectangle(x1, y2, x2 - x1, self.Height - y2) ) e.Graphics.DrawRectangle(pen, rc) def OnKeyUp(self, sender, e): if e.KeyCode == Keys.Escape: self.DialogResult = DialogResult.Cancel