2016-08-07 2 views
-1

내 C# 응용 프로그램에서 flashwindowex winapi를 사용하여 아이콘 트레이에서 IE 창을 플래싱합니다. 코드에 중단 점을 추가하고 디버그에서 실행하면 코드가 잘 실행됩니다. 그러나 중단 점이 제거 될 때 같은 코드가 작동하지 않습니다.창 깜박임

그것은 콘솔 응용 프로그램이며 이름으로 만든 IE 프로세스의 핸들을 찾기 위해 몇 가지 Windows API를 사용하고 있습니다. 프로세스의 핸들은 프로세스를 플래싱하기 위해 FlashWindowEX WINAPI로 전달됩니다.

public static System.IntPtr hnd = IntPtr.Zero; 
public delegate bool CallBackPtr(IntPtr hwnd, int lParam); 
public static CallBackPtr callBackPtr; 
public const UInt32 FLASH_T = 3; 
public const UInt32 FLASH_S = 12; 

static void Main(string[] args) 
{ 
    try 
    { 
     OrigCode(); 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.Message); 
    } 
    Console.ReadLine(); 
} 

public static void OrigCode() 
{ 
    string strPzszPath = string.Empty; 
    string strCommandLine = string.Empty; 
    string strpath1 = "http://localhost/Till/Default.aspx"; 

    strPzszPath = string.Concat(strPzszPath, strpath1); 
    strPzszPath = string.Concat(strPzszPath, "?TSCashierID=JILL&TSBalPeriod=2&TSBalDate=2015-06-02"); 
    strCommandLine = strPzszPath; 
    Process procRequested = new Process(); 

    ////Create the process in minimised mode by default 
    procRequested.StartInfo.WindowStyle = ProcessWindowStyle.Minimized; 
    procRequested.StartInfo.UseShellExecute = false; 

    ////Get the file name in which process is required to be started 
    procRequested.StartInfo.FileName = @"C:\Program Files\Internet Explorer\IExplore.exe"; 

    procRequested.StartInfo.Arguments = strCommandLine; 

    ////Start the process, process should be created in minimised mode 
    procRequested.Start(); 

    callBackPtr = new CallBackPtr(SetFocusEnum); 
    int intResult = EnumWindows(callBackPtr, 0); 

    FLASHWINFO fi = new FLASHWINFO(); 
    fi.cbSize = Convert.ToUInt32(Marshal.SizeOf(fi)); 
    fi.hwnd = hnd; 
    fi.dwFlags = FLASH_T | FLASH_S; 
    fi.ucount = UInt32.MaxValue; 
    fi.dwTimeout = 0; 
    FlashWindowEx(ref fi); 
} 

private static bool SetFocusEnum(IntPtr hWnd, int intLParam) 
{ 
    int intSize = GetWindowTextLength(hWnd); 
    try 
    { 

     if (intLParam == 0) //Process created so check by name 
     { 
      if (intSize++ > 0) 
      { 

       //Capture the running window name 
       StringBuilder sbWindowName = new StringBuilder(intSize); 
       GetWindowText(hWnd, sbWindowName, intSize); 
       //Capture the running process with the window name 
       if (sbWindowName.ToString().Contains("My Web Application Title")) 
       { 
       //Capture the handle which will be used to set the focus 
       hnd = hWnd; 
       } 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
    } 
    finally 
    { 
    } 
    return true; 
} 

아래는 내가 사용하는 WINAPI 기능은 다음과 같습니다

[DllImport("user32.dll", CharSet = CharSet.Unicode)] 
internal static extern int EnumWindows(CallBackPtr callPtr, int intProc); 

[DllImport("user32.dll", CharSet = CharSet.Unicode)] 
internal static extern int GetWindowTextLength(IntPtr intptrHwnd); 

[DllImport("user32.dll", CharSet = CharSet.Unicode)] 
internal static extern int GetWindowText(IntPtr intptrHwnd, StringBuilder strText, int intMaxCount); 

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
private static extern bool FlashWindowEx(ref FLASHWINFO pwfi); 

[StructLayout(LayoutKind.Sequential)] 
private struct FLASHWINFO 
{ 
    public UInt32 cbSize; 
    public IntPtr hwnd; 
    public UInt32 dwFlags; 
    public UInt32 ucount; 
    public UInt32 dwTimeout; 
} 
+0

우리가 계속 아무 상관이없는 경우 우리가 당신을 도울 수, 코드를 제시해주십시오. 무엇이 무엇인지, 어떤 일이 일어나지 않았는지에 대한 설명은 코드 없이는별로 도움이되지 않습니다. –

+0

안녕하세요, 내 휴대폰과 코드를 통해 내일 공유 할 수있는 쿼리를 게시 오전하지만 만약 내가 접근 방식에 대해 이야기한다면, 나는이 링크에 표시된 것과 같은 방식으로 flashwindoex를 사용하고 있습니다. http://stackoverflow.com/questions/4889181/get-user-attention-ste-stealing-focus. 해당 창 핸들 값을 얻으려면 enumwindows winapi 및 해당 값을 사용하여 오전 나는 flashwindowex 메서드에 전달하고있다. –

+0

저는 현재 내 전화에도 있습니다. 따라서 특정 코드가 없으면 많은 것을 할 수 없습니다 (모든 것을 올바르게 사용했는지 확인하기를 원했을 것입니다). 컴퓨터에 접속할 때 시험을 보겠습니다. –

답변

0

내 의견에 명시된 것처럼, 여기에 문제는 윈도우의 텍스트에 의해 변경 될 페이지를로드하는 데 시간이 너무 오래 걸려서이다 EnumWindows() 함수를 실행하는 시간. 사용하는 코드는 거의 즉시 실행되며, 프로세스를 열고 페이지를로드하는 데는 몇 백 밀리 초에서 몇 초가 걸립니다. 지연을 추가하는 것이 문제를 해결할 수있는 가장 간단한 해결 방법입니다.

procRequested.Start(); 
procRequested.WaitForInputIdle(); 

System.Threading.Thread.Sleep(750); //Wait for approximately 750 ms. 

그것은 일반적으로 Thread.Sleep()를 사용에 대해 adviced하지만, 당신이 콘솔 응용 프로그램에있어 이후로는 약간의 시간이 그 사용자의 입력을 차단 이외의 많은 차이를하지 않습니다 여기에

내가 무슨 짓을했는지 실행됩니다.

Process.WaitForInputIdle()에 대한 호출을 추가했음을 알았을 수도 있습니다. 이는 응용 프로그램이 프로세스 시작 절차 (예 : 리소스로드 또는 응용 프로그램이 실제로 시작되기 전에 프로세스로 어떤 작업을하는지 등)가 완료 될 때까지 기다릴 수있게하는 것입니다.) 우리가 실제로 창을 기대할 수있는시기를 알 수 있습니다.

750ms 지연이 때때로 충분하지 않다고 느끼거나 경험하는 경우 1250이나 그와 비슷한 것을 시도해보십시오.


편집 :

(당신이 말한대로) 집중 창이 깜박하지 않습니다 당신이 이전에 초점을 맞추고 창에 창 핸들을 저장할 수 있으며, 다시 오른쪽 창에 초점을 맞출 것을 사용하기 때문에 FlashWindowEx 코드를 실행하기 전에 이를 위해

당신은 WinAPI를 호출이 필요합니다

[DllImport("user32.dll")] 
private static extern IntPtr GetForegroundWindow(); 

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool SetForegroundWindow(IntPtr hWnd); 
  • GetForegroundWindow() 현재 창을 집중에 창 핸들을 가져옵니다.

  • 은 창 핸들을 지정하여 현재 포커스가 설정된 창을 설정합니다.

은 그럼 그냥 여기 두 가지 기능을 사용

IntPtr focusedhWnd = GetForegroundWindow(); //Get the window handle to the currently focused window. 

procRequested.Start(); 
procRequested.WaitForInputIdle(); 

System.Threading.Thread.Sleep(750); //Wait for approximately 750 ms. 

callBackPtr = new CallBackPtr(SetFocusEnum); 
int intResult = EnumWindows(callBackPtr, 0); 

SetForegroundWindow(focusedhWnd); //Focus the previously focused window again. 

FLASHWINFO fi = new FLASHWINFO(); 
fi.cbSize = Convert.ToUInt32(Marshal.SizeOf(fi)); 
fi.hwnd = hnd; 
fi.dwFlags = FLASH_T | FLASH_S; 
fi.ucount = UInt32.MaxValue; 
fi.dwTimeout = 0; 
FlashWindowEx(ref fi); 
+0

고맙습니다. 문제는 당신이 말한 것과 같았습니다. 그러나 그 과정이 단지 시간이 걸리는 경우는 거의 없습니다. 그러나 이전에 코드 실행과 관련하여 언급 한 요점은 중단 점 삽입입니다. 디버깅 모드에서는 Visual Studio에서 포커스가 유지되므로 응용 프로그램이 플래시 윈도우 ex를 호출 할 때 실행 된 응용 프로그램이 깜박이기 때문입니다. 그러나 exe를 통해 곧바로 실행되는 동안 포커스가 최소화 된 트레이로 이동하므로 깜박이지 않습니다. 이제 새로운 문제는 창이 트레이에 포커스가없는 프로세스를 만들어야 만이 플래시 창이 나타나면 –

+0

@bhupeshjoshi : 그걸 도울 수 있습니다. 잠시만 기다려주십시오 ... –

+0

고맙습니다 @ 비주얼 빈센트 저는 또한 노력하고 있습니다. –