2013-06-19 1 views
2

저는 Windows 개발자가 아닙니다 (AS3 작업). Visual C# 2010에서이 C# 콘솔 응용 프로그램을 테스트하여 무언가를 테스트했습니다. 앱이 열린 창을 열고 크기를 조정하고 위치를 변경해야합니다.user32 MoveWindow는 C#, Windows 7, 콘솔 응용 프로그램에서 작동하지 않습니다.

빈 크롬 창 (제목 없음)을 열었지만 창을 제어하는 ​​기능이 작동하지 않습니다 (디버거가 멈춘다해도 앱이 올바른 창을 찾았 음).

이유가 무엇입니까?

using System; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 


     [DllImport("user32.dll", SetLastError = true)] 
     internal static extern bool MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); 

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


     static void Main(string[] args) 
     { 
      Process[] processlist = Process.GetProcesses(); 

      foreach (Process proc in processlist) 
      { 
       if (!String.IsNullOrEmpty(proc.MainWindowTitle) && proc.MainWindowTitle == "Untitled") 
       { 
        ShowWindow(proc.Handle, 3); 
        MoveWindow(proc.Handle, 0, 0, 100, 100, true); 
       } 
      } 


     } 
    } 
} 

답변

6
MoveWindow(proc.Handle, ...); 

proc.Handle 당신이 생각하는 것이 아니다. 관심이있는 Process.MainWindowHandle이 아니라 프로세스 핸들입니다.

오류를 확인하지 않기 때문에이 질문을하고 있습니다. 그래서 왜 작동하지 않는지 당신은 모른다. 이 함수는 bool을 반환합니다. 틀린 것은 잘못된 것입니다. Win32Exception을 던져서 완전히 진단 할 수 없도록하십시오.

if (!MoveWindow(proc.MainWindowHandle, ...)) { 
    throw new Win32Exception(); 
} 
+0

thanx a many Hans! – Saariko