From 2ba819474b87b33ba4012275c1e62c525d72a469 Mon Sep 17 00:00:00 2001 From: yutent Date: Wed, 30 Aug 2023 09:33:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- draft/test.py | 86 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 34 deletions(-) diff --git a/draft/test.py b/draft/test.py index 49a4cce..84e00cf 100755 --- a/draft/test.py +++ b/draft/test.py @@ -4,7 +4,7 @@ from Xlib import display # from pprint import pprint as print import gi, cairo gi.require_version('Gtk', '3.0') -from gi.repository import Gtk, Gdk +from gi.repository import Gtk, Gdk, GdkPixbuf # 获取默认显示器 dp = Gdk.Display.get_default() @@ -45,17 +45,19 @@ class App(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, type=Gtk.WindowType.POPUP) + + self.set_name('Xshot') + self.set_default_size(total_width, total_height) self.set_type_hint(Gdk.WindowTypeHint.DESKTOP) # 设置透明背景 screen = self.get_screen() visual = screen.get_rgba_visual() - if visual and screen.is_composited(): - self.set_visual(visual) - - self.set_app_paintable(True) + # if visual and screen.is_composited(): + # self.set_visual(visual) + # self.set_app_paintable(True) self.set_events(Gdk.EventMask.POINTER_MOTION_MASK) @@ -71,60 +73,76 @@ class App(Gtk.Window): # layout.put(btn, 0, 0) # self.add(layout) - + layout = Gtk.Fixed() self.drawing_area = Gtk.DrawingArea() + self.drawing_area.set_size_request(total_width, total_height) + self.drawing_area.add_events( Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK | Gdk.EventMask.POINTER_MOTION_MASK) self.drawing_area.connect("draw", self.on_draw) self.drawing_area.connect("button-press-event", self.on_button_press) self.drawing_area.connect("button-release-event", self.on_button_release) self.drawing_area.connect("motion-notify-event", self.on_motion_notify) - self.add(self.drawing_area) + layout.add(self.drawing_area) + + + self.add(layout) # Initialize rectangle and control point size self.start_x = None self.start_y = None self.current_x = None self.current_y = None - + self.dragging = False self.resize_dragging = False self.connect("draw", on_draw) + + def on_draw(self, widget, cr): - if self.start_x is not None and self.start_y is not None: - width = self.current_x - self.start_x - height = self.current_y - self.start_y - cr.set_dash([6.0, 4.0]) - cr.rectangle(self.start_x, self.start_y, width, height) - cr.stroke() - # draw resizing dots - for x in [self.start_x, self.start_x + width / 2, self.current_x]: - for y in [self.start_y, self.start_y + height / 2, self.current_y]: - cr.arc(x, y, 5, 0, 2 * 3.14) - cr.fill() + if self.start_x is not None and self.start_y is not None: + width = self.current_x - self.start_x + height = self.current_y - self.start_y + cr.set_dash([6.0, 4.0]) + cr.rectangle(self.start_x, self.start_y, width, height) + cr.stroke() + # draw resizing dots + for x in [self.start_x, self.start_x + width / 2, self.current_x]: + for y in [self.start_y, self.start_y + height / 2, self.current_y]: + cr.arc(x, y, 5, 0, 2 * 3.14) + cr.fill() + + def on_button_press(self, widget, event): - if event.button == 1: # left mouse button - self.start_x = event.x - self.start_y = event.y - self.current_x = event.x - self.current_y = event.y - self.dragging = True + if event.button == 1: # left mouse button + self.start_x = event.x + self.start_y = event.y + self.current_x = event.x + self.current_y = event.y + self.dragging = True + + def on_button_release(self, widget, event): - if event.button == 1: # left mouse button - self.dragging = False - self.queue_draw() + if event.button == 1: # left mouse button + self.dragging = False + self.queue_draw() + + def on_motion_notify(self, widget, event): - if self.dragging: - self.current_x = event.x - self.current_y = event.y - self.queue_draw() + if self.dragging: + self.current_x = event.x + self.current_y = event.y + self.queue_draw() def run(self): - self.show_all() - Gtk.main() + try: + self.show_all() + Gtk.main() + except: + pass