hide window from MS windows taskbar

2019-02-15 05:20发布

问题:

Using pyGtk I created a window without decoration. The Window is hidden from task bar and top of all windows. On linux it works fine, but on MS Windows window sometimes it hides under some other window and always has "python.exe" the taskbar in windows.

Image representing my problem:

How can I hide this "python.exe" window from taskbar?

My code:

class Infowindow(gtk.Window):
'''
Klasa okienka informacyjnego
'''
def __init__(self, json, index, destroy_cb, device):
    gtk.Window.__init__(self)
    self.size_x = 260+48
    self.size_y = 85
    self.separator_size = 10
    self.set_type_hint(gtk.gdk.WINDOW_TYPE_HINT_SPLASHSCREEN)
    self.set_decorated(False)
    self.set_property('skip-taskbar-hint', True)
    self.set_opacity(1)
    self.set_keep_above(True)
    self.add_events(gtk.gdk.BUTTON_PRESS_MASK)
    self.connect("enter-notify-event", self.__on_hover)
    self.connect("leave-notify-event", self.__on_leave)
    self.connect("button_press_event", self.__on_click)
    self.set_size_request(self.size_x, self.size_y)
    color = gtk.gdk.color_parse('#f3f3f3')
    self.modify_bg(gtk.STATE_NORMAL, color)

    self.expanded = False
    self.index = index
    self.destroy_cb = destroy_cb
    self.json = json['data']
    self.system_info = False if 'system' not in self.json or not self.json['system'] else True
    self.device = device
    f = gtk.Frame()
    self.move_window(index) #move window to specified place
    self.box_area = gtk.VBox()
    self.box_area.set_spacing(10)
    f.add(self.box_area)
    self.add(f)
    self.show_all()

回答1:

You have two options to remove a window from the taskbar:

  1. Add the WS_EX_TOOLWINDOW extended window style. But this has other consequences and I cannot say whether or not they are serious.
  2. Give the window an owner that is not visible. This allows you freedom to use whatever window styles and extended styles you wish, but does involve more work.

It's natural that your window will go beneath other windows. That's how windows works. If you want to make your window appear on top, show it with HWND_TOPMOST.

I've no idea how any of this is (or is not) implemented under PyGtk. I've just given you the Win32 answer!



回答2:

Again thanks David Heffernan. Works perfect!

For people who want a full solution in python.

  • Name your windowin a characteristic way for example: 'alamakota'
  • Use find_window('alamakota'),
  • With given handler use hide_from_taskbar(handler)
  • Last use set_topmost(handler)

Window is hidden from taskbar and it's alwoays on top.

I know it's not a beatyfull code, but works fine on windows XP and above.

import ctypes
import win32gui
import win32api
from win32con import SWP_NOMOVE 
from win32con import SWP_NOSIZE 
from win32con import SW_HIDE
from win32con import SW_SHOW
from win32con import HWND_TOPMOST
from win32con import GWL_EXSTYLE 
from win32con import WS_EX_TOOLWINDOW

@staticmethod
def find_window(name):
    try:
        return win32gui.FindWindow(None, name)
    except win32gui.error:
        print("Error while finding the window")
        return None

@staticmethod   
def hide_from_taskbar(hw):
    try:
        win32gui.ShowWindow(hw, SW_HIDE)
        win32gui.SetWindowLong(hw, GWL_EXSTYLE,win32gui.GetWindowLong(hw, GWL_EXSTYLE)| WS_EX_TOOLWINDOW);
        win32gui.ShowWindow(hw, SW_SHOW);
    except win32gui.error:
        print("Error while hiding the window")
        return None

@staticmethod
def set_topmost(hw):
    try:
          win32gui.SetWindowPos(hw, HWND_TOPMOST, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE)
    except win32gui.error:
        print("Error while move window on top")