Question

I'm trying to grab a window by the process name and focus it, then take a screenshot of it. It works perfectly unless I have Teamviewer open (not even while using teamviewer to screenshare, just when teamviewer is running)

When teamviewer is running the window isn't focused or brought to the foreground, and the rect it screenshots is very small (33x21) where normally it would be 1600x900.

Here is the code in question:

        proc = Process.GetProcessesByName(procName)[0];
        SetForegroundWindow(proc.MainWindowHandle);
        ShowWindow(proc.MainWindowHandle, SW_RESTORE);

        Rect rect = new Rect();
        GetWindowRect(proc.MainWindowHandle, ref rect);

        int width = rect.right - rect.left;
        int height = rect.bottom - rect.top;

        Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
        Graphics.FromImage(bmp).CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);

Here is where I'm getting those functions:

    [DllImport("user32.dll")]
    private static extern IntPtr ShowWindow(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);

    [DllImport("user32.dll")]
    private static extern int SetForegroundWindow(IntPtr hWnd);
Était-ce utile?

La solution

I too have found that teamViewer break UI Automation. Disabling the "Present this Application" function re-enables UI Automation to work.

Autres conseils

I have encountered a similar problem. On two Windows 7 Pro computers I noticed that with the TeamViewer client running the following code stops working.

var wordProcess = Process.GetProcessesByName("winword")
    .FirstOrDefault(process => process.MainWindowTitle.Contains(documentName));

Setting a breakpoint and inspecting single running WINWORD process reveals that Process.MainWindowTitle property is blank most of the times. While WinWord windows taskbar icon clearly shows the title.

Exiting TeamViewer restores the things back to normal: Process.MainWindowTitle every time becomes properly populated.

I have reported the issue to the TeamViewer team.


Tested with: TeamViewer 9 ver. 9.0.27339, set for unattended access; MS Word 2007

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top