2012-12-27 2 views
5

내가 뭘 하려는지는 (같은 프로세스에서,하지만 내가 제어 할 수없는) 컨트롤을 그 자체로 다시 그려야하고, 그리고 그 때까지는 코드를 차단해야한다. 다시 그릴 완료.동기식 (블로킹 방법 포함)

UpdateWindow을 사용해 보았지만 다시 그리기가 완료 될 때까지 기다리지 않는 것 같습니다.

내가 다시 그릴 때까지 기다릴 필요가있는 이유는 나중에 화면을 잡고 싶다는 것입니다.

컨트롤은 dotNet 컨트롤이 아니며 일반 Windows 컨트롤입니다.

  • 핸들이 올바른 :

    나는 것을 확인했습니다.

  • UpdateWindow이 true를 반환합니다.
  • UpdateWindow을 호출하기 바로 전에 InvalidateRect(hWnd, IntPtr.Zero, true)을 보내려고하면 창을 무효화해야합니다.
  • 컨트롤의 부모 창에서 같은 작업을 시도했습니다. 사용

코드 :

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
private static extern bool InvalidateRect(IntPtr hWnd, IntPtr rect, bool bErase); 

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
private static extern bool UpdateWindow(IntPtr hWnd); 

public bool PaintWindow(IntPtr hWnd) 
{ 
    InvalidateRect(hWnd, IntPtr.Zero, true); 
    return UpdateWindow(hWnd); 
} 
//returns true 
+0

은 아마 당신은 WM_PAINT의 경우 렌더링을 해제 할 수 있습니다 그리고는 다시 활성화 한 후 후. –

+0

@ S.A.Parkhid 제발 좀 더 자세히 설명해 주시겠습니까? – Rotem

+0

흠 .. 왜 _UpdateWindow가 다시 그리기가 끝나기를 기다리지 않는 것 같습니까? –

답변

1

당신은 강제로 응용 프로그램은 모든 대기열에 메시지를 처리하는 Application.DoEvents를 사용합니다 (WM_PAINT를 포함하여!). 이런 식으로 뭔가 :

public bool PaintWindow(IntPtr hWnd) 
{ 
    InvalidateRect(hWnd, IntPtr.Zero, true); 
    if (UpdateWindow(hWnd)) 
    { 
     Application.DoEvents(); 
     return true; 
    } 

    return false; 
} 

하지만 당신은 더 나은 WM_PRINT 메시지를 보내 일석이조하지 않는 것, 어쨌든 화면을 잡아 거라면?

다음 코드를 수행 할 수 있습니다

internal static class NativeWinAPI 
{ 
    [Flags] 
    internal enum DrawingOptions 
    { 
     PRF_CHECKVISIBLE = 0x01, 
     PRF_NONCLIENT = 0x02, 
     PRF_CLIENT = 0x04, 
     PRF_ERASEBKGND = 0x08, 
     PRF_CHILDREN = 0x10, 
     PRF_OWNED = 0x20 
    } 

    internal const int WM_PRINT = 0x0317; 

    [DllImport("user32.dll", CharSet = CharSet.Auto)] 
    internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, 
     IntPtr wParam, IntPtr lParam); 
} 

public static void TakeScreenshot(IntPtr hwnd, Graphics g) 
{ 
    IntPtr hdc = IntPtr.Zero; 
    try 
    { 
     hdc = g.GetHdc(); 

     NativeWinAPI.SendMessage(hwnd, NativeWinAPI.WM_PRINT, hdc, 
      new IntPtr((int)(
       NativeWinAPI.DrawingOptions.PRF_CHILDREN | 
       NativeWinAPI.DrawingOptions.PRF_CLIENT | 
       NativeWinAPI.DrawingOptions.PRF_NONCLIENT | 
       NativeWinAPI.DrawingOptions.PRF_OWNED 
       )) 
      ); 
    } 
    finally 
    { 
     if (hdc != IntPtr.Zero) 
      g.ReleaseHdc(hdc); 
    } 
} 
+0

고마워, 나는 이것을 시도하고 알려주지. – Rotem

+0

@Rotem, 그래서, 당신은 당신의 문제를 해결 했습니까? –

+0

내일 만 테스트 할 수 있습니다. 최대한 빨리 업데이트하겠습니다. – Rotem

관련 문제