Clear the internal paint flag in the server before returning a
WM_PAINT message to avoid an extra server round-trip.

diff --git a/dlls/user/message.c b/dlls/user/message.c
index 417cbd3..e6bac74 100644
--- a/dlls/user/message.c
+++ b/dlls/user/message.c
@@ -2692,11 +2692,6 @@
         return FALSE;
     }
 
-    WIN_RestoreWndsLock( locks );
-
-    if (msg.message == WM_PAINT)  /* clear internal paint flag */
-        RedrawWindow( msg.hwnd, NULL, 0, RDW_NOINTERNALPAINT | RDW_NOCHILDREN );
-
     if ((queue = QUEUE_Current()))
     {
         queue->GetMessageTimeVal = msg.time;
@@ -2706,6 +2701,8 @@
 
     HOOK_CallHooks( WH_GETMESSAGE, HC_ACTION, flags & PM_REMOVE, (LPARAM)&msg, TRUE );
 
+    WIN_RestoreWndsLock( locks );
+
     /* copy back our internal safe copy of message data to msg_out.
      * msg_out is a variable from the *program*, so it can't be used
      * internally as it can get "corrupted" by our use of SendMessage()
diff --git a/server/window.c b/server/window.c
index 5143808..6b7222d 100644
--- a/server/window.c
+++ b/server/window.c
@@ -589,20 +589,22 @@
 }
 
 
-/* find a window that needs to receive a WM_PAINT */
+/* find a window that needs to receive a WM_PAINT; also clear its internal paint flag */
 user_handle_t find_window_to_repaint( user_handle_t parent, struct thread *thread )
 {
     struct window *ptr, *win = find_child_to_repaint( top_window, thread );
 
-    if (win)
+    if (win && parent)
     {
-        if (!parent) return win->handle;
         /* check that it is a child of the specified parent */
         for (ptr = win; ptr; ptr = ptr->parent)
-            if (ptr->handle == parent) return win->handle;
+            if (ptr->handle == parent) break;
         /* otherwise don't return any window, we don't repaint a child before its parent */
+        if (!ptr) win = NULL;
     }
-    return 0;
+    if (!win) return 0;
+    win->paint_flags &= ~PAINT_INTERNAL;
+    return win->handle;
 }