Disable resizing for python console application in Windows - python

Here is a code to disable resizing using c++
How to change console window style at runtime?
HWND consoleWindow = GetConsoleWindow();
SetWindowLong(consoleWindow, GWL_STYLE, GetWindowLong(consoleWindow, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX);
However, I want to use this code in python. It's my effort so far:
def fix_borders():
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
hWnd = kernel32.GetConsoleWindow()
kernel32.SetWindowLong(hWnd, GWL_STYLE,
kernel32.GetWindowLong(consoleWindow, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX)
However, SetWindowLong isn't recognized and I don't know how to import other constants...

This is how I solved it still utilizing ctypes and using win32api (definitely not the most elegant solution though):
def lock_resize():
while True:
STDOUT = -11
hdl = windll.kernel32.GetStdHandle(STDOUT)
rect = ct.wintypes.SMALL_RECT(0, 50, 50, 80) # (left, top, right, bottom)
windll.kernel32.SetConsoleWindowInfo(hdl, True, byref(rect))
_thread.start_new_thread(lock_resize,()) #using the _thread module to keep everything else running

Related

Simulating mouse click using python and win32api

I am trying to simulate a mouse left click on a window (Samsung Flow) but unfortunately it is not working. When I try on a google chrome window it works, but I have tried on Paint and on Samsung Flow but it does not work. Here is my code:
import win32api
import win32con
import time
import random
def enumHandler(hwnd, lParam):
if win32gui.IsWindowVisible(hwnd):
# if 'Stack Overflow' in win32gui.GetWindowText(hwnd):
if 'Samsung Flow' in win32gui.GetWindowText(hwnd):
print(win32gui.GetWindowText(hwnd))
for _ in range(50):
l_param = win32api.MAKELONG(random.randint(10, 500), random.randint(10, 500))
win32gui.PostMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, l_param)
time.sleep(0.1)
win32gui.PostMessage(hwnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, l_param)
time.sleep(0.1)
win32gui.EnumWindows(enumHandler, None)
I know that it detects the window as I am printing the detected text, but I do not know why it only work on the Chrome window.
Update
I have tried the following code, to search two windows notepad and browser:
import win32gui
import win32api
import win32con
import time
def enumHandler(hwnd, lParam):
if win32gui.IsWindowVisible(hwnd):
# if 'Notepad' in win32gui.GetWindowText(hwnd):
if 'Stack Overflow' in win32gui.GetWindowText(hwnd):
print(win32gui.GetWindowText(hwnd))
win32gui.SetForegroundWindow(hwnd)
win32api.SendMessage(hwnd, win32con.WM_CHAR, ord("c"), 0)
time.sleep(0.1)
win32gui.EnumWindows(enumHandler, None)
And the result was:
when using on the browser (searching for Stack Overflow window), the window came to foreground and printed the letter c
when using the notepad the window came to foreground but the letter was not printed! and I have no idea why.
Well, I found the problem.
With the example trying to send the letter C to notepad and the chrome browser I assumed that the first hwnd is the right one BUT in some cases, you have to interact with a child window. A window may have more than one child window, so, I will post a code here where you can find the window to interact with.
import win32gui
import win32con
import win32api
import time
def send_char(hwnd, lparam):
s = win32gui.GetWindowText(hwnd)
print("child_hwnd: %d txt: %s" % (hwnd, s))
win32api.PostMessage(hwnd, win32con.WM_CHAR, ord('c'), 0)
time.sleep(5)
return 1
def main():
main_app = 'Untitled - Notepad'
hwnd = win32gui.FindWindow(None, main_app)
if hwnd:
win32gui.EnumChildWindows(hwnd, send_char, None)
main()
With that you can find the child window that you should send the messages to (the code print the window id and name, send the character and wait 5 seconds, so when you notice the character on your window just get the last printed window id and use it instead of the parent window).
I hope it help someone facing the same problem.

In Python, how do I make a specific window stay on top?

I have seen a few methods, but none of the work.
import win32gui, win32process, win32con
import os
windowList = []
win32gui.EnumWindows(lambda hwnd, windowList: windowList.append((win32gui.GetWindowText(hwnd),hwnd)), windowList)
cmdWindow = [i for i in windowList if "c:\python26\python.exe" in i[0].lower()]
win32gui.SetWindowPos(cmdWindow[0][1],win32con.HWND_TOPMOST,0,0,100,100,0) #100,100 is the size of the window
'''
This one doens't work because the range exceeds the list.
'''
import win32gui
import win32con
hwnd = win32gui.GetForegroundWindow()
win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,100,100,200,200,0)
This one works as in it stays on top, but not the window I want.
Is there a line I could add so it knows which window I want? Or is there another way to approach this?
Use win32gui.FindWindow
hwnd = win32gui.FindWindow(None, 'Untitled - Notepad')
win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,100,100,200,200,0)

how to find the color of the button on wpf application using pywinauto tool?

How to find the color of the button on wpf application using pywinauto tool?
Currently I am using pywinauto 0.6.3 for automating WPF application.
Pywinauto does not support that, but you can get screen coordinates of element using rectangle() function and middle point using mid_point() (But in some cases in middle can be text). How to get color of some particular pixel you can check there.
For example:
from pywinauto import Desktop
def get_pixel_colour(point):
import win32gui
i_desktop_window_id = win32gui.GetDesktopWindow()
i_desktop_window_dc = win32gui.GetWindowDC(i_desktop_window_id)
long_colour = win32gui.GetPixel(i_desktop_window_dc, point.x, point.y)
i_colour = int(long_colour)
return (i_colour & 0xff), ((i_colour >> 8) & 0xff), ((i_colour >> 16) & 0xff)
dlg = Desktop(backend="win32").window(title_re='YourAppTitle')
bt = dlg.Button
bt.set_focus()
print(get_pixel_colour(bt.rectangle().mid_point()))

wxPython MacOS X Lion full screen mode

I am making a wxPython application that needs to work in full screen. I want to use the new full screen mode that came in OS X Lion. How can I make the full screen icon appear on the top right corner?
Until bug #14357 is fixed, there's no direct way to do this using only wxPython functions that I know of.
However, you can bypass wxWidgets and access the Cocoa APIs directly to do what you need. Note that you must be using the wxMac/Cocoa bindings (wxPython 2.9 or above).
This is the code necessary to make a frame full-screen capable:
# from http://stackoverflow.com/questions/12328143/getting-pyobjc-object-from-integer-id
import ctypes, objc
_objc = ctypes.PyDLL(objc._objc.__file__)
# PyObject *PyObjCObject_New(id objc_object, int flags, int retain)
_objc.PyObjCObject_New.restype = ctypes.py_object
_objc.PyObjCObject_New.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_int]
def objc_object(id):
return _objc.PyObjCObject_New(id, 0, 1)
def SetFullScreenCapable(frame):
frameobj = objc_object(frame.GetHandle())
NSWindowCollectionBehaviorFullScreenPrimary = 1<<7
window = frameobj.window()
newBehavior = window.collectionBehavior() | NSWindowCollectionBehaviorFullScreenPrimary
window.setCollectionBehavior_(newBehavior)
And here's a short test app that demonstrates it:
import wxversion
wxversion.select('2-osx_cocoa') # require Cocoa version of wxWidgets
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.Bind(wx.EVT_CLOSE, self.OnClose)
wx.Button(self, label="Hello!") # test button to demonstrate full-screen resizing
SetFullScreenCapable(self)
def OnClose(self, event):
exit()
app = wx.App()
frame = Frame()
frame.Show()
app.MainLoop()

Get text from popup window

I'm trying to read the text from a popup window.
The title is always the same. I've managed to identify the hwnd and get the title with the code below, but I can't figure out how to read the contents.
import time
import win32gui, win32con
windows = []
def _MyCallback( hwnd, extra ):
extra.append(hwnd)
win32gui.EnumWindows(_MyCallback, windows)
while True:
window = win32gui.GetForegroundWindow()
title = win32gui.GetWindowText(window)
if title == 'Errors occurred': print 'error window'
time.sleep(1)
Here's the working version:
import time
import win32gui
while True:
window = win32gui.GetForegroundWindow()
title = win32gui.GetWindowText(window)
if title == 'Errors occurred':
control = win32gui.FindWindowEx(window, 0, "static", None)
print 'text: ', win32gui.GetWindowText(control)
time.sleep(1)
You will only be able to read this text programmatically if it is contained in a windowed control. You can easily check this with Spy++. Many GUI frameworks don't use windowed controls for their child controls, or only use windowed controls for some children.
If it is a windowed control then you can identify it by calling GetWindow() and walking the child structure (obviously you need to use the win32gui equivalent).
I don't have access to the framework or the error dialog you are using, so I can only say in general what you want.
You need the FindWindowEx function, and use it to find a control whose class name is 'static' (or whatever the class name of the control is). I imagine this would be the line:
control = win32gui.FindWindowEx(window, 0, "Static", 0)
That returns the handle to the control, and you can then use GetWindowText on that to get the text.

Categories

Resources