Пример #1
0
#coding=utf-8
import S
gsTitle = '- Yandex Browser'
giClickY = 34
from qgb import py, Win, U, T, N, F
# U=py.importU()
from pywinauto import Application
app = Application()
pids = [i[0] for i in Win.getAllWindows() if i[1].endswith(gsTitle)]
ws = []
for i in pids:
    i = app.Connect(handle=i)
    i = i.windows()
    for w in i:
        if w.WindowText().endswith(gsTitle):
            if w not in ws: ws.append(w)
# F.dill_dump(obj=ws,file='browserGo_ws')
U.pprint(U.il(ws))
if U.ct(ws) == 0: Win.setCurPos(giClickY, giClickY)


def istop():
    x, y = Win.getCurPos()
    if y > giClickY * 6 or y > ih - 9:
        U.pln(iw, ih, i, 'Cur:', x, y)
        U.exit()


def click(w, x, y=giClickY):
    istop()
    try:
Пример #2
0
def SetForegroundWindow(title=None,
                        handle=None,
                        pid=None,
                        process_name='',
                        raise_error=0,
                        retry=99,
                        **ka):
    U, T, N, F = py.importUTNF()
    if py.isint(title) and not handle:
        handle, title = title, ''
    if not title: title = U.get_duplicated_kargs(
            ka,
            't',
    )
    if not handle: handle = U.get_duplicated_kargs(ka, 'hwnd', 'h')
    if not process_name:
        process_name = U.get_duplicated_kargs(ka, 'name', 'pn', 'process')
    no_raise = U.get_duplicated_kargs(ka,
                                      'no_raise',
                                      'noRaise',
                                      'no_raise_err',
                                      default=not raise_error)
    raise_error = (not no_raise) or U.get_duplicated_kargs(
        ka,
        'err',
        'error',
        'exp',
        'exception',
        'Exception',
        'raise_err',
        'raise_error',
        'raiseError',
        'raiseErr',
        'raise_EnvironmentError',
        'EnvironmentError',
        'raiseEnvironmentError',
        default=raise_error)

    if not handle and not title and not pid and not process_name:
        handle = get_current_cmd_windows()
    if not handle:
        from qgb import Win
        for h, t, p in Win.getAllWindows():
            if t == title or p == pid:
                handle = h
                break
            if process_name and process_name == U.get_process_name_by_pid(p):
                handle = h
                break
        else:
            if (not title) and (not process_name):
                raise py.ArgumentError(py.locals())
            for h, t, p in Win.getAllWindows():
                if title and title in t:
                    handle = h
                    break
                if process_name and process_name in U.get_process_name_by_pid(
                        p):
                    handle = h
                    break
            else:
                if raise_error:
                    raise py.EnvironmentError(
                        'cannot find ForegroundWindow title : ' + a)
        # if py.isint(title):
        # handle=title
    # else:
    # raise py.ArgumentError('foreground,a',row)
    import win32gui
    # if not win32gui.IsWindowVisible(handle): #先不考虑

    try:
        # win32gui.SetForegroundWindow(handle)
        import win32gui, win32com.client, win32con, pythoncom
        pythoncom.CoInitialize(
        )  #加上这句解决 #pywintypes.com_error: (-2147221008, '尚未调用 CoInitialize。', None, None)
        shell = win32com.client.Dispatch("WScript.Shell")
        shell.SendKeys('%')  ##For ALT   prefix with %
        win32gui.ShowWindow(handle, win32con.SW_SHOW)
        win32gui.SetForegroundWindow(handle)
    except Exception as e:
        #BUG 窗口在后台,通过 http_rpc 调用此函数,第一次总会出错:,第二次才成功?
        # error(0, 'SetForegroundWindow', 'No error message is available')
        if 'No error message is available' in repr(e):
            for i in py.range(1, retry):
                try:
                    if i % 9 == 1: U.sleep(0.01)  # sleep一下有奇效,为什么?
                    win32gui.SetForegroundWindow(handle)
                    return U.IntCustomRepr(
                        handle,
                        repr='Win.set_foreground(%r) #retry:%s' % (handle, i))
                except:
                    pass
            # return py.No(e,'')
        if raise_error: raise
        return py.No(e)

    return U.IntCustomRepr(handle, repr='Win.set_foreground(%r)' % handle)