如何防止用户拖动窗口/框架

发布于 2025-01-11 08:02:05 字数 733 浏览 1 评论 0原文

我需要创建一个简单的应用程序,不能调整大小、最小化、最大化和移动。我已经能够满足所有这些要求,但要感谢这个答案,但我找不到如何防止窗口拖动。

我尝试改编这个C++答案:当触发移动事件时,我只是将窗口移回到原来的位置,但它不是很干净:它使窗口摇晃,有时会最小化所有其他正在运行的应用程序。另外,“移动”选项是系统菜单中可用的样式,我想禁用它。

那么如何在单击标题栏时禁用拖动窗口并禁用系统菜单中的“移动”选项呢?

我正在运行 Windows 10Python 3.10wxpython 4.1.1

def __init__(self):
    # stuffs ...
    self.Center()
    self.Show()
    x, y = self.GetPosition()
    self.x = x
    self.y = y
    self.Bind(wx.EVT_MOVE, self.on_move)

def on_move(self, ev):
    ev.Skip()
    self.Move(self.x, self.y)

I need to create a simple app that cannot be resized, minimised, maximised and moved. I've been able to fulfill all this requirements but one thanks to this answer but I cant find how to prevent window dragging.

I've tried to adapt this C++ answer : when move event is trigerred, I just move back the window to its original position, but it's not very clean : it makes the windows shaky and sometimes minimize all other running apps. Also, the "Move" options is style available from system menu and I'd like to disable it.

So how can I disable dragging the window when clicking on title bar and disable Move option from system menu ?

I'm running Windows 10, Python 3.10 and wxpython 4.1.1.

def __init__(self):
    # stuffs ...
    self.Center()
    self.Show()
    x, y = self.GetPosition()
    self.x = x
    self.y = y
    self.Bind(wx.EVT_MOVE, self.on_move)

def on_move(self, ev):
    ev.Skip()
    self.Move(self.x, self.y)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹过旳痕迹 2025-01-18 08:02:05

我永远不会建议有一个不可移动的窗口,但如果这是一个游戏规则改变者,在您的具体情况下,那么您可以定义一个没有框架因此不可移动的窗口。
缺点是您必须自己在窗口中添加任何标题和关闭功能。
我还建议将其设为 Always_on_top
正如您所看到的,您获得了限制,但失去了与桌面上其他窗口的兼容性。

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, ("Moveable or Stuck"), size=(420, 210), \
                          style = wx.FRAME_NO_TASKBAR \
                          & ~(wx.MINIMIZE_BOX|wx.RESIZE_BORDER|wx.MAXIMIZE_BOX))
        panel = wx.Panel(self)
        caption = wx.StaticText(panel, -1, "Moveable or Stuck", pos=(10,5))
        caption.SetBackgroundColour('lightgrey')
        self.CloseButton = wx.BitmapButton(panel, bitmap=wx.ArtProvider.GetBitmap(wx.ART_CLOSE), \
                                           pos=(380,5), size=(32,32))
        self.Bind(wx.EVT_BUTTON, self.OnExit)

        self.SetWindowStyle(wx.STAY_ON_TOP | wx.BORDER_NONE | wx.FRAME_NO_TASKBAR )
                
    def OnExit(self, event):
        self.Destroy() 

app = wx.App()
frame = MyFrame(None)
frame.Show()

app.MainLoop()

输入图片此处描述

I would never recommend having a window that is not moveable but if that is a game changer, in your specific situation, then you could define a window that has no frame and thus is not moveable.
The downside is that you would have to include any caption and a close facilty within the window yourself.
I'd also recommend making it Always_on_top.
As you can see, you gain the restrictions but lose compatability with other windows on the desktop.

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, ("Moveable or Stuck"), size=(420, 210), \
                          style = wx.FRAME_NO_TASKBAR \
                          & ~(wx.MINIMIZE_BOX|wx.RESIZE_BORDER|wx.MAXIMIZE_BOX))
        panel = wx.Panel(self)
        caption = wx.StaticText(panel, -1, "Moveable or Stuck", pos=(10,5))
        caption.SetBackgroundColour('lightgrey')
        self.CloseButton = wx.BitmapButton(panel, bitmap=wx.ArtProvider.GetBitmap(wx.ART_CLOSE), \
                                           pos=(380,5), size=(32,32))
        self.Bind(wx.EVT_BUTTON, self.OnExit)

        self.SetWindowStyle(wx.STAY_ON_TOP | wx.BORDER_NONE | wx.FRAME_NO_TASKBAR )
                
    def OnExit(self, event):
        self.Destroy() 

app = wx.App()
frame = MyFrame(None)
frame.Show()

app.MainLoop()

enter image description here

残疾 2025-01-18 08:02:05

我不运行 Windows 操作系统,因此无法测试 wx.EVT_MOVE_END 事件,这应该会给您带来更好的结果。
样式只为您提供一个关闭框,并防止窗口包含在任务栏中。
我相信,至少在应用程序级别上,覆盖操作系统提供的 Move 功能是不可能的。

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, ("Moveable or Stuck"), size=(420, 210), \
                          style = wx.SYSTEM_MENU|wx.CLOSE_BOX|wx.FRAME_NO_TASKBAR \
                          & ~(wx.MINIMIZE_BOX|wx.RESIZE_BORDER|wx.MAXIMIZE_BOX))
        self.Pos = self.GetPosition()
        # other than Windows OS
        self.Bind(wx.EVT_MOVE, self.OnMove)
        # Windows Only
        #self.Bind(wx.EVT_MOVE_END, self.OnMove)
                
    def OnMove(self, event):
        self.SetPosition(self.Pos) 
        
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()

I don't run windows OS so I can't test the wx.EVT_MOVE_END event, which should give you a better result.
The style gives you just a Close box and prevents the window being included in the taskbar.
Overriding the Move ability provided by the OS, is I believe, not possible, at an App level at least.

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, ("Moveable or Stuck"), size=(420, 210), \
                          style = wx.SYSTEM_MENU|wx.CLOSE_BOX|wx.FRAME_NO_TASKBAR \
                          & ~(wx.MINIMIZE_BOX|wx.RESIZE_BORDER|wx.MAXIMIZE_BOX))
        self.Pos = self.GetPosition()
        # other than Windows OS
        self.Bind(wx.EVT_MOVE, self.OnMove)
        # Windows Only
        #self.Bind(wx.EVT_MOVE_END, self.OnMove)
                
    def OnMove(self, event):
        self.SetPosition(self.Pos) 
        
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文