-sync user32_winetest with wine 1.1.32
[reactos.git] / rostests / winetests / user32 / msg.c
index 5c66f0c..bfc81dc 100755 (executable)
@@ -20,6 +20,8 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
+#define _WIN32_WINNT 0x0501 /* For WM_CHANGEUISTATE,QS_RAWINPUT */
+
 #include <assert.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -76,8 +78,8 @@ typedef struct
 
 static BOOL test_DestroyWindow_flag;
 static HWINEVENTHOOK hEvent_hook;
-
-static void dump_winpos_flags(UINT flags);
+static HHOOK hCBT_hook;
+static DWORD cbt_hook_thread_id;
 
 static const WCHAR testWindowClassW[] =
 { 'T','e','s','t','W','i','n','d','o','w','C','l','a','s','s','W',0 };
@@ -110,6 +112,19 @@ struct message {
     msg_flags_t flags;     /* message props */
     WPARAM wParam;         /* expected value of wParam */
     LPARAM lParam;         /* expected value of lParam */
+    WPARAM wp_mask;        /* mask for wParam checks */
+    LPARAM lp_mask;        /* mask for lParam checks */
+};
+
+struct recvd_message {
+    UINT message;          /* the WM_* code */
+    msg_flags_t flags;     /* message props */
+    HWND hwnd;             /* window that received the message */
+    WPARAM wParam;         /* expected value of wParam */
+    LPARAM lParam;         /* expected value of lParam */
+    int line;              /* source line where logged */
+    const char *descr;     /* description for trace output */
+    char output[512];      /* trace output */
 };
 
 /* Empty message sequence */
@@ -141,6 +156,8 @@ static const struct message WmSWP_ShowOverlappedSeq[] = {
     { WM_ERASEBKGND, sent|optional },
     { HCBT_ACTIVATE, hook },
     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
+    { WM_NOTIFYFORMAT, sent|optional },
+    { WM_QUERYUISTATE, sent|optional },
     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* Win9x: SWP_NOSENDCHANGING */
     { WM_ACTIVATEAPP, sent|wparam, 1 },
@@ -152,15 +169,23 @@ static const struct message WmSWP_ShowOverlappedSeq[] = {
     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
+    { WM_GETTEXT, sent|optional },
     { WM_NCPAINT, sent|wparam|optional, 1 },
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_ERASEBKGND, sent|optional },
     /* Win9x adds SWP_NOZORDER below */
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_GETTEXT, sent|optional },
     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
     { WM_NCPAINT, sent|wparam|optional, 1 },
     { WM_ERASEBKGND, sent|optional },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
+    { WM_SYNCPAINT, sent|optional },
+    { WM_GETTITLEBARINFOEX, sent|optional },
+    { WM_PAINT, sent|optional },
+    { WM_NCPAINT, sent|beginpaint|optional },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
     { 0 }
 };
 /* SetWindowPos(SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE)
@@ -169,6 +194,11 @@ static const struct message WmSWP_ShowOverlappedSeq[] = {
 static const struct message WmSWP_HideOverlappedSeq[] = {
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
+    { HCBT_ACTIVATE, hook|optional },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_ACTIVATEAPP, sent|wparam|optional, 1 },
+    { WM_NCACTIVATE, sent|optional },
+    { WM_ACTIVATE, sent|optional },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { 0 }
 };
@@ -179,12 +209,12 @@ static const struct message WmSWP_HideOverlappedSeq[] = {
 static const struct message WmSWP_ResizeSeq[] = {
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE },
     { WM_GETMINMAXINFO, sent|defwinproc },
-    { WM_NCCALCSIZE, sent|wparam, TRUE },
+    { WM_NCCALCSIZE, sent|wparam|optional, TRUE },
     { WM_NCPAINT, sent|optional },
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_ERASEBKGND, sent|optional },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
-    { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
+    { WM_SIZE, sent|defwinproc|optional },
     { WM_NCCALCSIZE, sent|wparam|optional, TRUE },
     { WM_NCPAINT, sent|optional },
     { WM_GETTEXT, sent|defwinproc|optional },
@@ -200,12 +230,12 @@ static const struct message WmSWP_ResizeSeq[] = {
 static const struct message WmSWP_ResizePopupSeq[] = {
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE },
     { WM_GETMINMAXINFO, sent|defwinproc|optional }, /* Win9x */
-    { WM_NCCALCSIZE, sent|wparam, TRUE },
+    { WM_NCCALCSIZE, sent|wparam|optional, TRUE },
     { WM_NCPAINT, sent|optional },
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_ERASEBKGND, sent|optional },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
-    { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOCLIENTMOVE },
+    { WM_SIZE, sent|defwinproc|wparam|optional, SIZE_RESTORED },
     { WM_NCCALCSIZE, sent|wparam|optional, TRUE },
     { WM_NCPAINT, sent|optional },
     { WM_GETTEXT, sent|defwinproc|optional },
@@ -219,6 +249,9 @@ static const struct message WmSWP_ResizePopupSeq[] = {
  */
 static const struct message WmSWP_MoveSeq[] = {
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE|SWP_NOSIZE },
+    { WM_NCPAINT, sent|optional },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ERASEBKGND, sent|optional },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOCLIENTSIZE },
     { WM_MOVE, sent|defwinproc|wparam, 0 },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
@@ -229,14 +262,16 @@ static const struct message WmSWP_MoveSeq[] = {
  * SWP_NOZORDER is stripped by the logging code
  */
 static const struct message WmSWP_ResizeNoZOrder[] = {
-    { WM_WINDOWPOSCHANGING, sent|wparam, 0/*SWP_NOZORDER*/ },
+    { WM_WINDOWPOSCHANGING, sent|wparam, /*SWP_NOZORDER|*/SWP_NOACTIVATE },
     { WM_GETMINMAXINFO, sent|defwinproc },
-    { WM_NCCALCSIZE, sent|wparam, 1 },
-    { WM_NCPAINT, sent },
+    { WM_NCCALCSIZE, sent|wparam|optional, 1 },
+    { WM_NCPAINT, sent|optional },
     { WM_GETTEXT, sent|defwinproc|optional },
-    { WM_ERASEBKGND, sent|optional }, /* FIXME: remove optional once Wine is fixed */
-    { WM_WINDOWPOSCHANGED, sent|wparam, /*SWP_NOZORDER|*/SWP_NOMOVE|SWP_NOCLIENTMOVE },
-    { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
+    { WM_ERASEBKGND, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|wparam, /*SWP_NOZORDER|*/SWP_NOACTIVATE, 0,
+      SWP_NOMOVE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOCLIENTSIZE },
+    { WM_MOVE, sent|defwinproc|optional },
+    { WM_SIZE, sent|defwinproc|optional },
     { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* Win9x doesn't send it */
     { WM_NCPAINT, sent|optional }, /* Win9x doesn't send it */
     { WM_GETTEXT, sent|defwinproc|optional }, /* Win9x doesn't send it */
@@ -314,12 +349,12 @@ static const struct message WmSwitchNotMaximizedChild[] = {
     /* Activate 2nd MDI child */
     { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE}, /* in the 2nd MDI child */
     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 }, /* in the 2nd MDI child */
-    { WM_SETVISIBLE, hook }, /* in the 1st MDI child */
+    { HCBT_SETFOCUS, hook }, /* in the 1st MDI child */
     { WM_KILLFOCUS, sent|defwinproc }, /* in the 1st MDI child */
     { WM_IME_SETCONTEXT, sent|defwinproc|optional }, /* in the 1st MDI child */
     { WM_IME_SETCONTEXT, sent|optional }, /* in the  MDI client */
     { WM_SETFOCUS, sent, 0 }, /* in the  MDI client */
-    { WM_SETVISIBLE, hook },
+    { HCBT_SETFOCUS, hook },
     { WM_KILLFOCUS, sent }, /* in the  MDI client */
     { WM_IME_SETCONTEXT, sent|optional }, /* in the  MDI client */
     { WM_IME_SETCONTEXT, sent|defwinproc|optional  }, /* in the 1st MDI child */
@@ -337,7 +372,7 @@ static const struct message WmSwitchNotMaximizedChild[] = {
 static const struct message WmSWP_FrameChanged_clip[] = {
     { WM_WINDOWPOSCHANGING, sent|wparam|parent, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED },
     { WM_NCCALCSIZE, sent|wparam|parent, 1 },
-    { WM_NCPAINT, sent|parent }, /* wparam != 1 */
+    { WM_NCPAINT, sent|parent|optional }, /* wparam != 1 */
     { WM_GETTEXT, sent|parent|defwinproc|optional },
     { WM_ERASEBKGND, sent|parent|optional }, /* FIXME: remove optional once Wine is fixed */
     { WM_NCPAINT, sent }, /* wparam != 1 */
@@ -356,12 +391,12 @@ static const struct message WmSWP_FrameChangedDeferErase[] = {
     { WM_NCCALCSIZE, sent|wparam|parent, 1 },
     { WM_WINDOWPOSCHANGED, sent|wparam|parent, SWP_NOSIZE|SWP_NOMOVE|SWP_DEFERERASE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
-    { WM_PAINT, sent|parent },
-    { WM_NCPAINT, sent|beginpaint|parent }, /* wparam != 1 */
+    { WM_PAINT, sent|parent|optional },
+    { WM_NCPAINT, sent|beginpaint|parent|optional }, /* wparam != 1 */
     { WM_GETTEXT, sent|beginpaint|parent|defwinproc|optional },
     { WM_PAINT, sent },
     { WM_NCPAINT, sent|beginpaint }, /* wparam != 1 */
-    { WM_ERASEBKGND, sent|beginpaint },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
     { 0 }
 };
 
@@ -372,14 +407,14 @@ static const struct message WmSWP_FrameChangedDeferErase[] = {
 static const struct message WmSWP_FrameChanged_noclip[] = {
     { WM_WINDOWPOSCHANGING, sent|wparam|parent, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED },
     { WM_NCCALCSIZE, sent|wparam|parent, 1 },
-    { WM_NCPAINT, sent|parent }, /* wparam != 1 */
+    { WM_NCPAINT, sent|parent|optional }, /* wparam != 1 */
     { WM_GETTEXT, sent|parent|defwinproc|optional },
-    { WM_ERASEBKGND, sent|parent|optional }, /* FIXME: remove optional once Wine is fixed */
+    { WM_ERASEBKGND, sent|parent|optional },
     { WM_WINDOWPOSCHANGED, sent|wparam|parent, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
     { WM_PAINT, sent },
     { WM_NCPAINT, sent|beginpaint }, /* wparam != 1 */
-    { WM_ERASEBKGND, sent|beginpaint },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
     { 0 }
 };
 
@@ -395,7 +430,7 @@ static const struct message WmShowOverlappedSeq[] = {
     { HCBT_ACTIVATE, hook },
     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
     { WM_NCPAINT, sent|wparam|optional, 1 },
     { WM_ACTIVATEAPP, sent|wparam, 1 },
     { WM_NCACTIVATE, sent|wparam, 1 },
@@ -406,14 +441,17 @@ static const struct message WmShowOverlappedSeq[] = {
     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
+    { WM_GETTEXT, sent|optional },
     { WM_NCPAINT, sent|wparam|optional, 1 },
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_ERASEBKGND, sent|optional },
     /* Win9x adds SWP_NOZORDER below */
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { WM_NCCALCSIZE, sent|optional },
+    { WM_GETTEXT, sent|optional },
     { WM_NCPAINT, sent|optional },
     { WM_ERASEBKGND, sent|optional },
+    { WM_SYNCPAINT, sent|optional },
 #if 0 /* CreateWindow/ShowWindow(SW_SHOW) also generates WM_SIZE/WM_MOVE
        * messages. Does that mean that CreateWindow doesn't set initial
        * window dimensions for overlapped windows?
@@ -421,6 +459,8 @@ static const struct message WmShowOverlappedSeq[] = {
     { WM_SIZE, sent },
     { WM_MOVE, sent },
 #endif
+    { WM_PAINT, sent|optional },
+    { WM_NCPAINT, sent|beginpaint|optional },
     { 0 }
 };
 /* ShowWindow(SW_SHOWMAXIMIZED) for a not visible overlapped window */
@@ -434,7 +474,7 @@ static const struct message WmShowMaxOverlappedSeq[] = {
     { HCBT_ACTIVATE, hook },
     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
     { WM_ACTIVATEAPP, sent|wparam, 1 },
     { WM_NCACTIVATE, sent|wparam, 1 },
     { WM_GETTEXT, sent|defwinproc|optional },
@@ -444,6 +484,7 @@ static const struct message WmShowMaxOverlappedSeq[] = {
     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
+    { WM_GETTEXT, sent|optional },
     { WM_NCPAINT, sent|wparam|optional, 1 },
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_ERASEBKGND, sent|optional },
@@ -451,11 +492,76 @@ static const struct message WmShowMaxOverlappedSeq[] = {
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_STATECHANGED },
     { WM_MOVE, sent|defwinproc },
     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
+    { WM_GETTEXT, sent|optional },
     { WM_NCCALCSIZE, sent|optional },
     { WM_NCPAINT, sent|optional },
     { WM_ERASEBKGND, sent|optional },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
+    { WM_SYNCPAINT, sent|optional },
+    { WM_GETTITLEBARINFOEX, sent|optional },
+    { WM_PAINT, sent|optional },
+    { WM_NCPAINT, sent|beginpaint|optional },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
+    { 0 }
+};
+/* ShowWindow(SW_RESTORE) for a not visible maximized overlapped window */
+static const struct message WmShowRestoreMaxOverlappedSeq[] = {
+    { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
+    { WM_GETTEXT, sent|optional },
+    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
+    { WM_GETMINMAXINFO, sent|defwinproc },
+    { WM_NCCALCSIZE, sent|wparam, TRUE },
+    { WM_NCPAINT, sent|optional },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ERASEBKGND, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_MOVE, sent|defwinproc|optional },
+    { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
+    { WM_NCCALCSIZE, sent|wparam|optional, TRUE },
+    { WM_NCPAINT, sent|optional },
+    { WM_ERASEBKGND, sent|optional },
+    { WM_PAINT, sent|optional },
+    { WM_GETTITLEBARINFOEX, sent|optional },
+    { WM_NCPAINT, sent|beginpaint|optional },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
+    { 0 }
+};
+/* ShowWindow(SW_RESTORE) for a not visible minimized overlapped window */
+static const struct message WmShowRestoreMinOverlappedSeq[] = {
+    { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
+    { WM_QUERYOPEN, sent|optional },
+    { WM_GETTEXT, sent|optional },
+    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED|SWP_NOCOPYBITS },
+    { WM_GETMINMAXINFO, sent|defwinproc },
+    { WM_NCCALCSIZE, sent|wparam, TRUE },
+    { HCBT_ACTIVATE, hook },
+    { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_ACTIVATEAPP, sent|wparam, 1 },
+    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ACTIVATE, sent|wparam, 1 },
+    { HCBT_SETFOCUS, hook },
+    { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
+    { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
+    { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
+    { WM_GETTEXT, sent|optional },
+    { WM_NCPAINT, sent|wparam|optional, 1 },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ERASEBKGND, sent },
+    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_STATECHANGED|SWP_FRAMECHANGED|SWP_NOCOPYBITS },
+    { WM_MOVE, sent|defwinproc },
+    { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
+    { WM_NCCALCSIZE, sent|wparam|optional, TRUE },
+    { WM_NCPAINT, sent|wparam|optional, 1 },
+    { WM_ERASEBKGND, sent|optional },
+    { WM_ACTIVATE, sent|wparam, 1 },
+    { WM_GETTEXT, sent|optional },
+    { WM_PAINT, sent|optional },
+    { WM_GETTITLEBARINFOEX, sent|optional },
+    { WM_NCPAINT, sent|beginpaint|optional },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
     { 0 }
 };
 /* ShowWindow(SW_SHOWMINIMIZED) for a not visible overlapped window */
@@ -471,11 +577,11 @@ static const struct message WmShowMinOverlappedSeq[] = {
     { WM_GETMINMAXINFO, sent|defwinproc },
     { WM_NCCALCSIZE, sent|wparam, TRUE },
     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
-    { WM_NCPAINT, sent },
+    { WM_NCPAINT, sent|optional },
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_WINDOWPOSCHANGED, sent },
     { WM_MOVE, sent|defwinproc },
-    { WM_SIZE, sent|defwinproc|wparam, SIZE_MINIMIZED },
+    { WM_SIZE, sent|defwinproc|wparam|lparam, SIZE_MINIMIZED, 0 },
     { WM_NCCALCSIZE, sent|optional },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
@@ -484,6 +590,37 @@ static const struct message WmShowMinOverlappedSeq[] = {
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_ACTIVATE, sent },
     { WM_ACTIVATEAPP, sent|wparam, 0 },
+
+    /* Vista sometimes restores the window right away... */
+    { WM_SYSCOMMAND, sent|optional|wparam, SC_RESTORE },
+    { HCBT_SYSCOMMAND, hook|optional|wparam, SC_RESTORE },
+    { HCBT_MINMAX, hook|optional|lparam, 0, SW_RESTORE },
+    { WM_QUERYOPEN, sent|optional },
+    { WM_WINDOWPOSCHANGING, sent|optional|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
+    { WM_GETMINMAXINFO, sent|optional|defwinproc },
+    { WM_NCCALCSIZE, sent|optional|wparam, TRUE },
+    { HCBT_ACTIVATE, hook|optional },
+    { WM_ACTIVATEAPP, sent|optional|wparam, 1 },
+    { WM_NCACTIVATE, sent|optional },
+    { WM_GETTEXT, sent|optional },
+    { WM_ACTIVATE, sent|optional|wparam, 1 },
+    { HCBT_SETFOCUS, hook|optional },
+    { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
+    { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
+    { WM_SETFOCUS, sent|optional },
+    { WM_NCPAINT, sent|optional },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ERASEBKGND, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|optional|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
+    { WM_MOVE, sent|defwinproc|optional },
+    { WM_SIZE, sent|defwinproc|optional|wparam, SIZE_RESTORED },
+    { WM_ACTIVATE, sent|optional|wparam, 1 },
+    { WM_SYSCOMMAND, sent|optional|wparam, SC_RESTORE },
+    { HCBT_SYSCOMMAND, hook|optional|wparam, SC_RESTORE },
+
+    { WM_PAINT, sent|optional },
+    { WM_NCPAINT, sent|beginpaint|optional },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
     { 0 }
 };
 /* ShowWindow(SW_HIDE) for a visible overlapped window */
@@ -510,10 +647,10 @@ static const struct message WmDestroyOverlappedSeq[] = {
     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
     { 0x0090, sent|optional },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
-    { WM_NCACTIVATE, sent|wparam, 0 },
-    { WM_ACTIVATE, sent|wparam, 0 },
-    { WM_ACTIVATEAPP, sent|wparam, 0 },
-    { WM_KILLFOCUS, sent|wparam, 0 },
+    { WM_NCACTIVATE, sent|optional|wparam, 0 },
+    { WM_ACTIVATE, sent|optional },
+    { WM_ACTIVATEAPP, sent|optional|wparam, 0 },
+    { WM_KILLFOCUS, sent|optional|wparam, 0 },
     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
     { WM_IME_NOTIFY, sent|wparam|optional|defwinproc, 1 },
     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
@@ -545,17 +682,23 @@ static const struct message WmCreateMaxPopupSeq[] = {
     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_NCPAINT, sent|wparam|optional, 1 },
+    { WM_ERASEBKGND, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOCLIENTMOVE|SWP_NOCLIENTSIZE|SWP_NOMOVE|SWP_NOSIZE },
     { WM_ACTIVATEAPP, sent|wparam, 1 },
-    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_NCACTIVATE, sent },
     { WM_ACTIVATE, sent|wparam, 1 },
     { HCBT_SETFOCUS, hook },
     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
+    { WM_GETTEXT, sent|optional },
     { WM_SYNCPAINT, sent|wparam|optional, 4 },
     { WM_NCPAINT, sent|wparam|optional, 1 },
     { WM_ERASEBKGND, sent|optional },
+    { WM_NCPAINT, sent|wparam|defwinproc|optional, 1 },
+    { WM_ERASEBKGND, sent|defwinproc|optional },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTMOVE|SWP_NOCLIENTSIZE|SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOSIZE },
     { 0 }
 };
@@ -589,17 +732,21 @@ static const struct message WmShowMaxPopupResizedSeq[] = {
     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_NCPAINT, sent|wparam|optional, 1 },
+    { WM_ERASEBKGND, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { WM_ACTIVATEAPP, sent|wparam, 1 },
-    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_NCACTIVATE, sent },
     { WM_ACTIVATE, sent|wparam, 1 },
     { HCBT_SETFOCUS, hook },
     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
+    { WM_GETTEXT, sent|optional },
     { WM_NCPAINT, sent|wparam|optional, 1 },
     { WM_ERASEBKGND, sent|optional },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTMOVE|SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGED, sent },
     /* WinNT4.0 sends WM_MOVE */
     { WM_MOVE, sent|defwinproc|optional },
     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
@@ -618,16 +765,19 @@ static const struct message WmShowMaxPopupSeq[] = {
     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
     { WM_ACTIVATEAPP, sent|wparam, 1 },
-    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_NCACTIVATE, sent },
     { WM_ACTIVATE, sent|wparam, 1 },
     { HCBT_SETFOCUS, hook },
     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
+    { WM_GETTEXT, sent|optional },
     { WM_SYNCPAINT, sent|wparam|optional, 4 },
     { WM_NCPAINT, sent|wparam|optional, 1 },
     { WM_ERASEBKGND, sent|optional },
+    { WM_NCPAINT, sent|wparam|defwinproc|optional, 1 },
+    { WM_ERASEBKGND, sent|defwinproc|optional },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOMOVE|SWP_NOSIZE },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
     { 0 }
@@ -650,14 +800,16 @@ static const struct message WmCreatePopupSeq[] = {
     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
     { WM_NCPAINT, sent|wparam|optional, 1 },
     { WM_ERASEBKGND, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { WM_ACTIVATEAPP, sent|wparam, 1 },
-    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_NCACTIVATE, sent },
     { WM_ACTIVATE, sent|wparam, 1 },
     { HCBT_SETFOCUS, hook },
     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
+    { WM_GETTEXT, sent|optional },
     { WM_SYNCPAINT, sent|wparam|optional, 4 },
     { WM_NCPAINT, sent|wparam|optional, 1 },
     { WM_ERASEBKGND, sent|optional },
@@ -670,6 +822,7 @@ static const struct message WmShowVisMaxPopupSeq[] = {
     { WM_GETMINMAXINFO, sent },
     { WM_GETTEXT, sent|optional },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
+    { WM_GETTEXT, sent|optional },
     { WM_NCCALCSIZE, sent|wparam, TRUE },
     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
     { WM_NCPAINT, sent|wparam|optional, 1 },
@@ -710,19 +863,19 @@ static const struct message WmCreateInvisiblePopupSeq[] = {
  * for a popup window with WS_VISIBLE style set
  */
 static const struct message WmShowVisiblePopupSeq_2[] = {
-    { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
+    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { 0 }
 };
 /* SetWindowPos(SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE)
  * for a popup window with WS_VISIBLE style set
  */
 static const struct message WmShowVisiblePopupSeq_3[] = {
-    { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
+    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
     { HCBT_ACTIVATE, hook },
     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
-    { WM_WINDOWPOSCHANGING, sent|wparam, 0 },
-    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_NCACTIVATE, sent },
     { WM_ACTIVATE, sent|wparam, 1 },
     { HCBT_SETFOCUS, hook },
     { WM_KILLFOCUS, sent|parent },
@@ -731,6 +884,8 @@ static const struct message WmShowVisiblePopupSeq_3[] = {
     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_SETFOCUS, sent|defwinproc },
+    { WM_GETTEXT, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE, 0, SWP_SHOWWINDOW },
     { 0 }
 };
 /* CreateWindow (for child window, not initially visible) */
@@ -809,7 +964,7 @@ static const struct message WmHideChildSeq2[] = {
     { WM_SHOWWINDOW, sent|wparam, 0 },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
-    { WM_ERASEBKGND, sent|parent },
+    { WM_ERASEBKGND, sent|parent|optional },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { 0 }
 };
@@ -843,12 +998,13 @@ static const struct message WmShowChildSeq_4[] = {
 /* ShowWindow(SW_MINIMIZE) for child with invisible parent */
 static const struct message WmShowChildInvisibleParentSeq_1[] = {
     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_STATECHANGED },
+    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED, 0, SWP_NOACTIVATE },
     { WM_NCCALCSIZE, sent|wparam, 1 },
     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOREDRAW|SWP_NOCOPYBITS|SWP_STATECHANGED },
+    { WM_CHILDACTIVATE, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOREDRAW|SWP_NOCOPYBITS|SWP_STATECHANGED, 0, SWP_NOACTIVATE },
     { WM_MOVE, sent|defwinproc },
-    { WM_SIZE, sent|defwinproc|wparam, SIZE_MINIMIZED },
+    { WM_SIZE, sent|defwinproc|wparam|lparam, SIZE_MINIMIZED, 0 },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
     { EVENT_SYSTEM_MINIMIZESTART, winevent_hook|wparam|lparam, 0, 0 },
     /* FIXME: Wine creates an icon/title window while Windows doesn't */
@@ -888,7 +1044,7 @@ static const struct message WmShowChildInvisibleParentSeq_3[] = {
     { WM_CHILDACTIVATE, sent },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOREDRAW|SWP_NOCOPYBITS|SWP_STATECHANGED },
     { WM_MOVE, sent|defwinproc },
-    { WM_SIZE, sent|defwinproc|wparam, SIZE_MINIMIZED },
+    { WM_SIZE, sent|defwinproc|wparam|lparam, SIZE_MINIMIZED, 0 },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
     { EVENT_SYSTEM_MINIMIZESTART, winevent_hook|wparam|lparam, 0, 0 },
     /* FIXME: Wine creates an icon/title window while Windows doesn't */
@@ -909,7 +1065,7 @@ static const struct message WmShowChildInvisibleParentSeq_4[] = {
     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOREDRAW|SWP_NOCOPYBITS|SWP_STATECHANGED },
     { WM_MOVE, sent|defwinproc },
-    { WM_SIZE, sent|defwinproc|wparam, SIZE_MINIMIZED },
+    { WM_SIZE, sent|defwinproc|wparam|lparam, SIZE_MINIMIZED, 0 },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
     { EVENT_SYSTEM_MINIMIZESTART, winevent_hook|wparam|lparam, 0, 0 },
     /* FIXME: Wine creates an icon/title window while Windows doesn't */
@@ -969,6 +1125,13 @@ static const struct message WmDestroyChildSeq[] = {
     { WM_NCDESTROY, sent },
     { 0 }
 };
+/* visible child window destroyed by thread exit */
+static const struct message WmExitThreadSeq[] = {
+    { WM_NCDESTROY, sent },  /* actually in grandchild */
+    { WM_PAINT, sent|parent },
+    { WM_ERASEBKGND, sent|parent|beginpaint },
+    { 0 }
+};
 /* DestroyWindow for a visible child window with invisible parent */
 static const struct message WmDestroyInvisibleChildSeq[] = {
     { HCBT_DESTROYWND, hook },
@@ -1081,6 +1244,12 @@ static const struct message WmCreateCustomDialogSeq[] = {
     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
     { WM_CREATE, sent },
     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
+    { WM_NOTIFYFORMAT, sent|optional },
+    { WM_QUERYUISTATE, sent|optional },
+    { WM_WINDOWPOSCHANGING, sent|optional },
+    { WM_GETMINMAXINFO, sent|optional },
+    { WM_NCCALCSIZE, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|optional },
     { WM_SHOWWINDOW, sent|wparam, 1 },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
@@ -1090,13 +1259,15 @@ static const struct message WmCreateCustomDialogSeq[] = {
 
     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
 
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
 
-    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_NCACTIVATE, sent },
+    { WM_GETTEXT, sent|optional|defwinproc },
     { WM_GETTEXT, sent|optional|defwinproc },
     { WM_GETTEXT, sent|optional|defwinproc },
     { EVENT_OBJECT_DEFACTIONCHANGE, winevent_hook|wparam|lparam|optional, OBJID_CLIENT, 0 },
     { WM_ACTIVATE, sent|wparam, 1 },
+    { WM_GETTEXT, sent|optional },
     { WM_KILLFOCUS, sent|parent },
     { WM_IME_SETCONTEXT, sent|parent|wparam|optional, 0 },
     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
@@ -1108,7 +1279,7 @@ static const struct message WmCreateCustomDialogSeq[] = {
     { WM_GETTEXT, sent|optional|defwinproc },
     { WM_GETTEXT, sent|optional|defwinproc },
     { WM_ERASEBKGND, sent },
-    { WM_CTLCOLORDLG, sent|defwinproc },
+    { WM_CTLCOLORDLG, sent|optional|defwinproc },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { WM_GETTEXT, sent|optional },
     { WM_GETTEXT, sent|optional },
@@ -1136,6 +1307,9 @@ static const struct message WmEndCustomDialogSeq[] = {
     { WM_ACTIVATE, sent|wparam, 0 },
     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOACTIVATE|SWP_NOREDRAW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_GETTEXT, sent|optional|defwinproc },
+    { WM_GETTEXT, sent|optional|defwinproc },
     { HCBT_SETFOCUS, hook },
     { WM_KILLFOCUS, sent },
     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
@@ -1155,10 +1329,11 @@ static const struct message WmShowCustomDialogSeq[] = {
 
     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
 
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
     { WM_ACTIVATEAPP, sent|wparam|optional, 1 },
-    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_NCACTIVATE, sent },
     { WM_ACTIVATE, sent|wparam, 1 },
+    { WM_GETTEXT, sent|optional },
 
     { WM_KILLFOCUS, sent|parent },
     { WM_IME_SETCONTEXT, sent|parent|wparam|optional, 0 },
@@ -1194,15 +1369,15 @@ static const struct message WmModalDialogSeq[] = {
     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
-    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_NCACTIVATE, sent },
     { WM_GETTEXT, sent|optional },
     { WM_ACTIVATE, sent|wparam, 1 },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
-    { WM_NCPAINT, sent },
+    { WM_NCPAINT, sent|optional },
     { WM_GETTEXT, sent|optional },
-    { WM_ERASEBKGND, sent },
-    { WM_CTLCOLORDLG, sent },
+    { WM_ERASEBKGND, sent|optional },
+    { WM_CTLCOLORDLG, sent|optional },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { WM_GETTEXT, sent|optional },
     { WM_NCCALCSIZE, sent|optional },
@@ -1212,7 +1387,8 @@ static const struct message WmModalDialogSeq[] = {
     { WM_CTLCOLORDLG, sent|optional },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
     { WM_PAINT, sent|optional },
-    { WM_CTLCOLORBTN, sent },
+    { WM_CTLCOLORBTN, sent|optional },
+    { WM_GETTITLEBARINFOEX, sent|optional },
     { WM_ENTERIDLE, sent|parent|optional },
     { WM_ENTERIDLE, sent|parent|optional },
     { WM_ENTERIDLE, sent|parent|optional },
@@ -1246,6 +1422,7 @@ static const struct message WmModalDialogSeq[] = {
     { WM_ACTIVATE, sent|wparam, 0 },
     { EVENT_SYSTEM_FOREGROUND, winevent_hook|wparam|lparam, 0, 0 },
     { WM_WINDOWPOSCHANGING, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|optional },
     { HCBT_SETFOCUS, hook },
     { WM_IME_SETCONTEXT, sent|parent|wparam|defwinproc|optional, 1 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
@@ -1316,7 +1493,7 @@ static const struct message WmSetMenuVisibleSizeChangeSeq[] = {
     { WM_NCCALCSIZE, sent|wparam, 1 },
     { 0x0093, sent|defwinproc|optional },
     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
-    { WM_NCPAINT, sent }, /* wparam != 1 */
+    { WM_NCPAINT, sent|optional }, /* wparam != 1 */
     { 0x0093, sent|defwinproc|optional },
     { 0x0093, sent|defwinproc|optional },
     { 0x0091, sent|defwinproc|optional },
@@ -1344,7 +1521,7 @@ static const struct message WmSetMenuVisibleSizeChangeSeq[] = {
 static const struct message WmSetMenuVisibleNoSizeChangeSeq[] = {
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { WM_NCCALCSIZE, sent|wparam, 1 },
-    { WM_NCPAINT, sent }, /* wparam != 1 */
+    { WM_NCPAINT, sent|optional }, /* wparam != 1 */
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_ERASEBKGND, sent|optional },
     { WM_ACTIVATE, sent|optional },
@@ -1358,7 +1535,7 @@ static const struct message WmDrawMenuBarSeq[] =
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { WM_NCCALCSIZE, sent|wparam, 1 },
     { 0x0093, sent|defwinproc|optional },
-    { WM_NCPAINT, sent }, /* wparam != 1 */
+    { WM_NCPAINT, sent|optional }, /* wparam != 1 */
     { 0x0093, sent|defwinproc|optional },
     { 0x0093, sent|defwinproc|optional },
     { 0x0091, sent|defwinproc|optional },
@@ -1387,6 +1564,8 @@ static const struct message WmEnableWindowSeq_1[] =
 {
     { WM_CANCELMODE, sent|wparam|lparam, 0, 0 },
     { EVENT_OBJECT_STATECHANGE, winevent_hook|wparam|lparam, 0, 0 },
+    { HCBT_SETFOCUS, hook|optional },
+    { WM_KILLFOCUS, sent|optional },
     { WM_ENABLE, sent|wparam|lparam, FALSE, 0 },
     { 0 }
 };
@@ -1457,8 +1636,8 @@ static const struct message WmSetScrollRangeHV_NC_Seq[] =
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_ERASEBKGND, sent|optional },
     { WM_CTLCOLORDLG, sent|defwinproc|optional }, /* sent to a parent of the dialog */
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE|SWP_NOCLIENTMOVE },
-    { WM_SIZE, sent|defwinproc|wparam, SIZE_RESTORED },
+    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE|SWP_NOCLIENTMOVE, 0, SWP_NOCLIENTSIZE },
+    { WM_SIZE, sent|defwinproc|optional },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
     { EVENT_OBJECT_VALUECHANGE, winevent_hook|lparam|optional, 0/*OBJID_HSCROLL or OBJID_VSCROLL*/, 0 },
     { WM_GETTEXT, sent|optional },
@@ -1493,13 +1672,23 @@ static const struct message WmSHOWNAChildInvisParVis[] = {
 static const struct message WmSHOWNATopVisible[] = {
     { WM_SHOWWINDOW, sent|wparam, 1 },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
+    { WM_NCPAINT, sent|wparam|optional, 1 },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ERASEBKGND, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|optional },
     { 0 }
 };
 static const struct message WmSHOWNATopInvisible[] = {
+    { WM_NOTIFYFORMAT, sent|optional },
+    { WM_QUERYUISTATE, sent|optional },
+    { WM_WINDOWPOSCHANGING, sent|optional },
+    { WM_GETMINMAXINFO, sent|optional },
+    { WM_NCCALCSIZE, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|optional },
     { WM_SHOWWINDOW, sent|wparam, 1 },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
-    { WM_NCPAINT, sent|wparam, 1 },
+    { WM_NCPAINT, sent|wparam|optional, 1 },
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_ERASEBKGND, sent|optional },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
@@ -1514,12 +1703,15 @@ static const struct message WmSHOWNATopInvisible[] = {
 
 static int after_end_dialog, test_def_id;
 static int sequence_cnt, sequence_size;
-static struct message* sequence;
+static struct recvd_message* sequence;
 static int log_all_parent_messages;
+static int paint_loop_done;
 
 /* user32 functions */
 static HWND (WINAPI *pGetAncestor)(HWND,UINT);
+static BOOL (WINAPI *pGetMenuInfo)(HMENU,LPCMENUINFO);
 static void (WINAPI *pNotifyWinEvent)(DWORD, HWND, LONG, LONG);
+static BOOL (WINAPI *pSetMenuInfo)(HMENU,LPCMENUINFO);
 static HWINEVENTHOOK (WINAPI *pSetWinEventHook)(DWORD, DWORD, HMODULE, WINEVENTPROC, DWORD, DWORD, DWORD);
 static BOOL (WINAPI *pTrackMouseEvent)(TRACKMOUSEEVENT*);
 static BOOL (WINAPI *pUnhookWinEvent)(HWINEVENTHOOK);
@@ -1538,7 +1730,9 @@ static void init_procs(void)
     }
 
     GET_PROC(user32, GetAncestor)
+    GET_PROC(user32, GetMenuInfo)
     GET_PROC(user32, NotifyWinEvent)
+    GET_PROC(user32, SetMenuInfo)
     GET_PROC(user32, SetWinEventHook)
     GET_PROC(user32, TrackMouseEvent)
     GET_PROC(user32, UnhookWinEvent)
@@ -1548,24 +1742,154 @@ static void init_procs(void)
 #undef GET_PROC
 }
 
-static void add_message(const struct message *msg)
+static const char *get_winpos_flags(UINT flags)
+{
+    static char buffer[300];
+
+    buffer[0] = 0;
+#define DUMP(flag) do { if (flags & flag) { strcat( buffer, "|" #flag ); flags &= ~flag; } } while(0)
+    DUMP( SWP_SHOWWINDOW );
+    DUMP( SWP_HIDEWINDOW );
+    DUMP( SWP_NOACTIVATE );
+    DUMP( SWP_FRAMECHANGED );
+    DUMP( SWP_NOCOPYBITS );
+    DUMP( SWP_NOOWNERZORDER );
+    DUMP( SWP_NOSENDCHANGING );
+    DUMP( SWP_DEFERERASE );
+    DUMP( SWP_ASYNCWINDOWPOS );
+    DUMP( SWP_NOZORDER );
+    DUMP( SWP_NOREDRAW );
+    DUMP( SWP_NOSIZE );
+    DUMP( SWP_NOMOVE );
+    DUMP( SWP_NOCLIENTSIZE );
+    DUMP( SWP_NOCLIENTMOVE );
+    if (flags) sprintf(buffer + strlen(buffer),"|0x%04x", flags);
+    return buffer + 1;
+#undef DUMP
+}
+
+static BOOL ignore_message( UINT message )
+{
+    /* these are always ignored */
+    return (message >= 0xc000 ||
+            message == WM_GETICON ||
+            message == WM_GETOBJECT ||
+            message == WM_TIMECHANGE ||
+            message == WM_DISPLAYCHANGE ||
+            message == WM_DEVICECHANGE);
+}
+
+
+#define add_message(msg) add_message_(__LINE__,msg);
+static void add_message_(int line, const struct recvd_message *msg)
 {
+    struct recvd_message *seq;
+
     if (!sequence) 
     {
        sequence_size = 10;
-       sequence = HeapAlloc( GetProcessHeap(), 0, sequence_size * sizeof (struct message) );
+       sequence = HeapAlloc( GetProcessHeap(), 0, sequence_size * sizeof(*sequence) );
     }
     if (sequence_cnt == sequence_size) 
     {
        sequence_size *= 2;
-       sequence = HeapReAlloc( GetProcessHeap(), 0, sequence, sequence_size * sizeof (struct message) );
+       sequence = HeapReAlloc( GetProcessHeap(), 0, sequence, sequence_size * sizeof(*sequence) );
     }
     assert(sequence);
 
-    sequence[sequence_cnt].message = msg->message;
-    sequence[sequence_cnt].flags = msg->flags;
-    sequence[sequence_cnt].wParam = msg->wParam;
-    sequence[sequence_cnt].lParam = msg->lParam;
+    seq = &sequence[sequence_cnt];
+    seq->hwnd = msg->hwnd;
+    seq->message = msg->message;
+    seq->flags = msg->flags;
+    seq->wParam = msg->wParam;
+    seq->lParam = msg->lParam;
+    seq->line   = line;
+    seq->descr  = msg->descr;
+    seq->output[0] = 0;
+
+    if (msg->descr)
+    {
+        if (msg->flags & hook)
+        {
+            static const char * const CBT_code_name[10] =
+            {
+                "HCBT_MOVESIZE",
+                "HCBT_MINMAX",
+                "HCBT_QS",
+                "HCBT_CREATEWND",
+                "HCBT_DESTROYWND",
+                "HCBT_ACTIVATE",
+                "HCBT_CLICKSKIPPED",
+                "HCBT_KEYSKIPPED",
+                "HCBT_SYSCOMMAND",
+                "HCBT_SETFOCUS"
+            };
+            const char *code_name = (msg->message <= HCBT_SETFOCUS) ? CBT_code_name[msg->message] : "Unknown";
+
+            sprintf( seq->output, "%s: hook %d (%s) wp %08lx lp %08lx",
+                     msg->descr, msg->message, code_name, msg->wParam, msg->lParam );
+        }
+        else if (msg->flags & winevent_hook)
+        {
+            sprintf( seq->output, "%s: winevent %p %08x %08lx %08lx",
+                     msg->descr, msg->hwnd, msg->message, msg->wParam, msg->lParam );
+        }
+        else
+        {
+            switch (msg->message)
+            {
+            case WM_WINDOWPOSCHANGING:
+            case WM_WINDOWPOSCHANGED:
+            {
+                WINDOWPOS *winpos = (WINDOWPOS *)msg->lParam;
+
+                sprintf( seq->output, "%s: %p WM_WINDOWPOS%s wp %08lx lp %08lx after %p x %d y %d cx %d cy %d flags %s",
+                          msg->descr, msg->hwnd,
+                          (msg->message == WM_WINDOWPOSCHANGING) ? "CHANGING" : "CHANGED",
+                          msg->wParam, msg->lParam, winpos->hwndInsertAfter,
+                          winpos->x, winpos->y, winpos->cx, winpos->cy,
+                          get_winpos_flags(winpos->flags) );
+
+                /* Log only documented flags, win2k uses 0x1000 and 0x2000
+                 * in the high word for internal purposes
+                 */
+                seq->wParam = winpos->flags & 0xffff;
+                /* We are not interested in the flags that don't match under XP and Win9x */
+                seq->wParam &= ~SWP_NOZORDER;
+                break;
+            }
+
+            case WM_DRAWITEM:
+            {
+                DRAW_ITEM_STRUCT di;
+                DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)msg->lParam;
+
+                sprintf( seq->output, "%s: %p WM_DRAWITEM: type %x, ctl_id %x, item_id %x, action %x, state %x",
+                         msg->descr, msg->hwnd, dis->CtlType, dis->CtlID,
+                         dis->itemID, dis->itemAction, dis->itemState);
+
+                di.u.lp = 0;
+                di.u.item.type = dis->CtlType;
+                di.u.item.ctl_id = dis->CtlID;
+                if (dis->CtlType == ODT_LISTBOX ||
+                    dis->CtlType == ODT_COMBOBOX ||
+                    dis->CtlType == ODT_MENU)
+                    di.u.item.item_id = dis->itemID;
+                di.u.item.action = dis->itemAction;
+                di.u.item.state = dis->itemState;
+
+                seq->lParam = di.u.lp;
+                break;
+            }
+            default:
+                if (msg->message >= 0xc000) return;  /* ignore registered messages */
+                sprintf( seq->output, "%s: %p %04x wp %08lx lp %08lx",
+                         msg->descr, msg->hwnd, msg->message, msg->wParam, msg->lParam );
+            }
+            if (msg->flags & (sent|posted|parent|defwinproc|beginpaint))
+                sprintf( seq->output + strlen(seq->output), " (flags %x)", msg->flags );
+        }
+    }
 
     sequence_cnt++;
 }
@@ -1575,7 +1899,7 @@ static void flush_events(void)
 {
     MSG msg;
     int diff = 200;
-    int min_timeout = 50;
+    int min_timeout = 100;
     DWORD time = GetTickCount() + diff;
 
     while (diff > 0)
@@ -1583,7 +1907,6 @@ static void flush_events(void)
         if (MsgWaitForMultipleObjects( 0, NULL, FALSE, min_timeout, QS_ALLINPUT ) == WAIT_TIMEOUT) break;
         while (PeekMessage( &msg, 0, 0, 0, PM_REMOVE )) DispatchMessage( &msg );
         diff = time - GetTickCount();
-        min_timeout = 10;
     }
 }
 
@@ -1594,115 +1917,227 @@ static void flush_sequence(void)
     sequence_cnt = sequence_size = 0;
 }
 
-#define ok_sequence( exp, contx, todo) \
-        ok_sequence_( (exp), (contx), (todo), __FILE__, __LINE__)
-
-
-static void ok_sequence_(const struct message *expected, const char *context, int todo,
-        const char *file, int line)
+static void dump_sequence(const struct message *expected, const char *context, const char *file, int line)
 {
-    static const struct message end_of_sequence = { 0, 0, 0, 0 };
-    const struct message *actual;
-    int failcount = 0;
-    
-    add_message(&end_of_sequence);
-
-    actual = sequence;
+    const struct recvd_message *actual = sequence;
+    unsigned int count = 0;
 
+    trace_(file, line)("Failed sequence %s:\n", context );
     while (expected->message && actual->message)
     {
-       trace_( file, line)("expected %04x - actual %04x\n", expected->message, actual->message);
+        if (actual->output[0])
+        {
+            if (expected->flags & hook)
+            {
+                trace_(file, line)( "  %u: expected: hook %04x - actual: %s\n",
+                                    count, expected->message, actual->output );
+            }
+            else if (expected->flags & winevent_hook)
+            {
+                trace_(file, line)( "  %u: expected: winevent %04x - actual: %s\n",
+                                    count, expected->message, actual->output );
+            }
+            else
+            {
+                trace_(file, line)( "  %u: expected: msg %04x - actual: %s\n",
+                                    count, expected->message, actual->output );
+            }
+        }
 
        if (expected->message == actual->message)
        {
-           if (expected->flags & wparam)
-           {
-               if (expected->wParam != actual->wParam && todo)
-               {
-                   todo_wine {
-                        failcount ++;
-                        ok_( file, line) (FALSE,
-                           "%s: in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
-                           context, expected->message, expected->wParam, actual->wParam);
-                   }
-               }
-               else
-               ok_( file, line) (expected->wParam == actual->wParam,
-                    "%s: in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
-                    context, expected->message, expected->wParam, actual->wParam);
-           }
-           if (expected->flags & lparam)
+           if ((expected->flags & defwinproc) != (actual->flags & defwinproc) &&
+                (expected->flags & optional))
             {
-               if (expected->lParam != actual->lParam && todo)
-               {
-                   todo_wine {
-                        failcount ++;
+                /* don't match messages if their defwinproc status differs */
+                expected++;
+            }
+            else
+            {
+                expected++;
+                actual++;
+            }
+       }
+       /* silently drop winevent messages if there is no support for them */
+       else if ((expected->flags & optional) || ((expected->flags & winevent_hook) && !hEvent_hook))
+           expected++;
+        else
+        {
+            expected++;
+            actual++;
+        }
+        count++;
+    }
+
+    /* optional trailing messages */
+    while (expected->message && ((expected->flags & optional) ||
+           ((expected->flags & winevent_hook) && !hEvent_hook)))
+    {
+        trace_(file, line)( "  %u: expected: msg %04x - actual: nothing\n", count, expected->message );
+       expected++;
+        count++;
+    }
+
+    if (expected->message)
+    {
+        trace_(file, line)( "  %u: expected: msg %04x - actual: nothing\n", count, expected->message );
+        return;
+    }
+
+    while (actual->message && actual->output[0])
+    {
+        trace_(file, line)( "  %u: expected: nothing - actual: %s\n", count, actual->output );
+        actual++;
+        count++;
+    }
+}
+
+#define ok_sequence( exp, contx, todo) \
+        ok_sequence_( (exp), (contx), (todo), __FILE__, __LINE__)
+
+
+static void ok_sequence_(const struct message *expected_list, const char *context, int todo,
+                         const char *file, int line)
+{
+    static const struct recvd_message end_of_sequence;
+    const struct message *expected = expected_list;
+    const struct recvd_message *actual;
+    int failcount = 0, dump = 0;
+    unsigned int count = 0;
+
+    add_message(&end_of_sequence);
+
+    actual = sequence;
+
+    while (expected->message && actual->message)
+    {
+       if (expected->message == actual->message)
+       {
+           if (expected->flags & wparam)
+           {
+               if (((expected->wParam ^ actual->wParam) & ~expected->wp_mask) && todo)
+               {
+                   todo_wine {
+                        failcount ++;
+                        if (strcmp(winetest_platform, "wine")) dump++;
+                        ok_( file, line) (FALSE,
+                           "%s: %u: in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
+                            context, count, expected->message, expected->wParam, actual->wParam);
+                   }
+               }
+               else
+                {
+                    ok_( file, line)( ((expected->wParam ^ actual->wParam) & ~expected->wp_mask) == 0,
+                                     "%s: %u: in msg 0x%04x expecting wParam 0x%lx got 0x%lx\n",
+                                     context, count, expected->message, expected->wParam, actual->wParam);
+                    if ((expected->wParam ^ actual->wParam) & ~expected->wp_mask) dump++;
+                }
+
+           }
+           if (expected->flags & lparam)
+            {
+               if (((expected->lParam ^ actual->lParam) & ~expected->lp_mask) && todo)
+               {
+                   todo_wine {
+                        failcount ++;
+                        if (strcmp(winetest_platform, "wine")) dump++;
                         ok_( file, line) (FALSE,
-                           "%s: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
-                           context, expected->message, expected->lParam, actual->lParam);
+                           "%s: %u: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
+                            context, count, expected->message, expected->lParam, actual->lParam);
                    }
                }
                else
-                ok_( file, line) (expected->lParam == actual->lParam,
-                    "%s: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
-                    context, expected->message, expected->lParam, actual->lParam);
+                {
+                    ok_( file, line)(((expected->lParam ^ actual->lParam) & ~expected->lp_mask) == 0,
+                                     "%s: %u: in msg 0x%04x expecting lParam 0x%lx got 0x%lx\n",
+                                     context, count, expected->message, expected->lParam, actual->lParam);
+                    if ((expected->lParam ^ actual->lParam) & ~expected->lp_mask) dump++;
+                }
+            }
+           if ((expected->flags & optional) &&
+                ((expected->flags ^ actual->flags) & (defwinproc|parent)))
+            {
+                /* don't match optional messages if their defwinproc or parent status differs */
+                expected++;
+                count++;
+                continue;
             }
            if ((expected->flags & defwinproc) != (actual->flags & defwinproc) && todo)
            {
                    todo_wine {
                         failcount ++;
+                        if (strcmp(winetest_platform, "wine")) dump++;
                         ok_( file, line) (FALSE,
-                            "%s: the msg 0x%04x should %shave been sent by DefWindowProc\n",
-                            context, expected->message, (expected->flags & defwinproc) ? "" : "NOT ");
+                            "%s: %u: the msg 0x%04x should %shave been sent by DefWindowProc\n",
+                            context, count, expected->message, (expected->flags & defwinproc) ? "" : "NOT ");
                    }
            }
            else
+            {
                ok_( file, line) ((expected->flags & defwinproc) == (actual->flags & defwinproc),
-                   "%s: the msg 0x%04x should %shave been sent by DefWindowProc\n",
-                   context, expected->message, (expected->flags & defwinproc) ? "" : "NOT ");
+                   "%s: %u: the msg 0x%04x should %shave been sent by DefWindowProc\n",
+                    context, count, expected->message, (expected->flags & defwinproc) ? "" : "NOT ");
+                if ((expected->flags & defwinproc) != (actual->flags & defwinproc)) dump++;
+            }
+
            ok_( file, line) ((expected->flags & beginpaint) == (actual->flags & beginpaint),
-               "%s: the msg 0x%04x should %shave been sent by BeginPaint\n",
-               context, expected->message, (expected->flags & beginpaint) ? "" : "NOT ");
+               "%s: %u: the msg 0x%04x should %shave been sent by BeginPaint\n",
+                context, count, expected->message, (expected->flags & beginpaint) ? "" : "NOT ");
+            if ((expected->flags & beginpaint) != (actual->flags & beginpaint)) dump++;
+
            ok_( file, line) ((expected->flags & (sent|posted)) == (actual->flags & (sent|posted)),
-               "%s: the msg 0x%04x should have been %s\n",
-               context, expected->message, (expected->flags & posted) ? "posted" : "sent");
+               "%s: %u: the msg 0x%04x should have been %s\n",
+                context, count, expected->message, (expected->flags & posted) ? "posted" : "sent");
+            if ((expected->flags & (sent|posted)) != (actual->flags & (sent|posted))) dump++;
+
            ok_( file, line) ((expected->flags & parent) == (actual->flags & parent),
-               "%s: the msg 0x%04x was expected in %s\n",
-               context, expected->message, (expected->flags & parent) ? "parent" : "child");
+               "%s: %u: the msg 0x%04x was expected in %s\n",
+                context, count, expected->message, (expected->flags & parent) ? "parent" : "child");
+            if ((expected->flags & parent) != (actual->flags & parent)) dump++;
+
            ok_( file, line) ((expected->flags & hook) == (actual->flags & hook),
-               "%s: the msg 0x%04x should have been sent by a hook\n",
-               context, expected->message);
+               "%s: %u: the msg 0x%04x should have been sent by a hook\n",
+                context, count, expected->message);
+            if ((expected->flags & hook) != (actual->flags & hook)) dump++;
+
            ok_( file, line) ((expected->flags & winevent_hook) == (actual->flags & winevent_hook),
-               "%s: the msg 0x%04x should have been sent by a winevent hook\n",
-               context, expected->message);
+               "%s: %u: the msg 0x%04x should have been sent by a winevent hook\n",
+                context, count, expected->message);
+            if ((expected->flags & winevent_hook) != (actual->flags & winevent_hook)) dump++;
+
            expected++;
            actual++;
        }
-       /* silently drop winevent messages if there is no support for them */
-       else if ((expected->flags & optional) || ((expected->flags & winevent_hook) && !hEvent_hook))
+       /* silently drop hook messages if there is no support for them */
+       else if ((expected->flags & optional) ||
+                 ((expected->flags & hook) && !hCBT_hook) ||
+                 ((expected->flags & winevent_hook) && !hEvent_hook))
            expected++;
        else if (todo)
        {
             failcount++;
             todo_wine {
-                ok_( file, line) (FALSE, "%s: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
-                    context, expected->message, actual->message);
+                if (strcmp(winetest_platform, "wine")) dump++;
+                ok_( file, line) (FALSE, "%s: %u: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
+                                  context, count, expected->message, actual->message);
             }
-            flush_sequence();
-            return;
+            goto done;
         }
         else
         {
-            ok_( file, line) (FALSE, "%s: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
-                context, expected->message, actual->message);
+            ok_( file, line) (FALSE, "%s: %u: the msg 0x%04x was expected, but got msg 0x%04x instead\n",
+                              context, count, expected->message, actual->message);
+            dump++;
             expected++;
             actual++;
         }
+        count++;
     }
 
     /* skip all optional trailing messages */
     while (expected->message && ((expected->flags & optional) ||
-           ((expected->flags & winevent_hook) && !hEvent_hook)))
+                                 ((expected->flags & hook) && !hCBT_hook) ||
+                                 ((expected->flags & winevent_hook) && !hEvent_hook)))
        expected++;
 
     if (todo)
@@ -1710,25 +2145,34 @@ static void ok_sequence_(const struct message *expected, const char *context, in
         todo_wine {
             if (expected->message || actual->message) {
                 failcount++;
-                ok_( file, line) (FALSE, "%s: the msg sequence is not complete: expected %04x - actual %04x\n",
-                    context, expected->message, actual->message);
+                if (strcmp(winetest_platform, "wine")) dump++;
+                ok_( file, line) (FALSE, "%s: %u: the msg sequence is not complete: expected %04x - actual %04x\n",
+                                  context, count, expected->message, actual->message);
             }
         }
     }
     else
     {
         if (expected->message || actual->message)
-            ok_( file, line) (FALSE, "%s: the msg sequence is not complete: expected %04x - actual %04x\n",
-                context, expected->message, actual->message);
+        {
+            dump++;
+            ok_( file, line) (FALSE, "%s: %u: the msg sequence is not complete: expected %04x - actual %04x\n",
+                              context, count, expected->message, actual->message);
+        }
     }
     if( todo && !failcount) /* succeeded yet marked todo */
         todo_wine {
+            if (!strcmp(winetest_platform, "wine")) dump++;
             ok_( file, line)( TRUE, "%s: marked \"todo_wine\" but succeeds\n", context);
         }
 
+done:
+    if (dump) dump_sequence(expected_list, context, file, line);
     flush_sequence();
 }
 
+#define expect(EXPECTED,GOT) ok((GOT)==(EXPECTED), "Expected %d, got %d\n", (EXPECTED), (GOT))
+
 /******************************** MDI test **********************************/
 
 /* CreateWindow for MDI frame window, initially visible */
@@ -1740,6 +2184,12 @@ static const struct message WmCreateMDIframeSeq[] = {
     { EVENT_OBJECT_REORDER, winevent_hook|wparam|lparam, 0, 0 },
     { WM_CREATE, sent },
     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
+    { WM_NOTIFYFORMAT, sent|optional },
+    { WM_QUERYUISTATE, sent|optional },
+    { WM_WINDOWPOSCHANGING, sent|optional },
+    { WM_GETMINMAXINFO, sent|optional },
+    { WM_NCCALCSIZE, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|optional },
     { WM_SHOWWINDOW, sent|wparam, 1 },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
@@ -1748,7 +2198,7 @@ static const struct message WmCreateMDIframeSeq[] = {
     { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* XP */
     { WM_ACTIVATEAPP, sent|wparam|optional, 1 }, /* Win9x doesn't send it */
-    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_NCACTIVATE, sent },
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_ACTIVATE, sent|wparam, 1 },
     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* Win9x */
@@ -1769,10 +2219,10 @@ static const struct message WmCreateMDIframeSeq[] = {
 static const struct message WmDestroyMDIframeSeq[] = {
     { HCBT_DESTROYWND, hook },
     { 0x0090, sent|optional },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
     { WM_NCACTIVATE, sent|wparam|optional, 0 }, /* Win9x */
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { WM_NCACTIVATE, sent|wparam|optional, 0 }, /* XP */
     { WM_ACTIVATE, sent|wparam|optional, 0 }, /* Win9x */
     { WM_ACTIVATEAPP, sent|wparam|optional, 0 }, /* Win9x */
@@ -1795,26 +2245,26 @@ static const struct message WmCreateMDIclientSeq[] = {
     { WM_MOVE, sent },
     { WM_PARENTNOTIFY, sent|wparam, WM_CREATE }, /* in MDI frame */
     { WM_SHOWWINDOW, sent|wparam, 1 },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { 0 }
 };
 /* ShowWindow(SW_SHOW) for MDI client window */
 static const struct message WmShowMDIclientSeq[] = {
     { WM_SHOWWINDOW, sent|wparam, 1 },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { 0 }
 };
 /* ShowWindow(SW_HIDE) for MDI client window */
 static const struct message WmHideMDIclientSeq[] = {
     { WM_SHOWWINDOW, sent|wparam, 0 },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam|optional, 0, 0 }, /* win2000 */
     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam|optional, 0, 0 }, /* XP */
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { 0 }
 };
 /* DestroyWindow for MDI client window, initially visible */
@@ -1823,9 +2273,9 @@ static const struct message WmDestroyMDIclientSeq[] = {
     { 0x0090, sent|optional },
     { WM_PARENTNOTIFY, sent|wparam, WM_DESTROY }, /* in MDI frame */
     { WM_SHOWWINDOW, sent|wparam, 0 },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
     { WM_DESTROY, sent },
     { WM_NCDESTROY, sent },
@@ -1918,10 +2368,10 @@ static const struct message WmDestroyMDIchildVisibleSeq[] = {
     { 0x0090, sent|optional },
     { WM_PARENTNOTIFY, sent /*|wparam, WM_DESTROY*/ }, /* in MDI client */
     { WM_SHOWWINDOW, sent|wparam, 0 },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
     { WM_ERASEBKGND, sent|parent|optional },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
 
     /* { WM_DESTROY, sent }
      * Win9x: message sequence terminates here.
@@ -2044,23 +2494,23 @@ static const struct message WmCreateMDIchildVisibleMaxSeq1[] = {
     { WM_MDIREFRESHMENU, sent/*|wparam|lparam, 0, 0*/ },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE },
     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
-    { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc|optional, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE, 0, SWP_FRAMECHANGED },
 
     /* Win9x: message sequence terminates here. */
 
-    { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
-    { HCBT_SETFOCUS, hook }, /* in MDI client */
+    { WM_NCACTIVATE, sent|wparam|defwinproc|optional, 1 },
+    { HCBT_SETFOCUS, hook|optional }, /* in MDI client */
     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
     { WM_IME_NOTIFY, sent|wparam|optional, 2 }, /* in MDI client */
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
-    { WM_SETFOCUS, sent }, /* in MDI client */
-    { HCBT_SETFOCUS, hook },
-    { WM_KILLFOCUS, sent }, /* in MDI client */
+    { WM_SETFOCUS, sent|optional }, /* in MDI client */
+    { HCBT_SETFOCUS, hook|optional },
+    { WM_KILLFOCUS, sent|optional }, /* in MDI client */
     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
-    { WM_SETFOCUS, sent|defwinproc },
-    { WM_MDIACTIVATE, sent|defwinproc },
+    { WM_SETFOCUS, sent|defwinproc|optional },
+    { WM_MDIACTIVATE, sent|defwinproc|optional },
      /* in MDI frame */
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { WM_NCCALCSIZE, sent|wparam, 1 },
@@ -2129,7 +2579,7 @@ static const struct message WmCreateMDIchildVisibleMaxSeq2[] = {
 
     { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
     { HCBT_SETFOCUS, hook },
-    { WM_KILLFOCUS, sent|defwinproc }, /* in the 1st MDI child */
+    { WM_KILLFOCUS, sent|defwinproc|optional }, /* in the 1st MDI child */
     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 0 }, /* in the 1st MDI child */
     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
@@ -2260,6 +2710,9 @@ static const struct message WmCreateMDIchildInvisibleMaxSeq4[] = {
     { WM_CREATE, sent },
     { EVENT_OBJECT_CREATE, winevent_hook|wparam|lparam, 0, 0 },
     { WM_SIZE, sent|wparam, SIZE_RESTORED },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE, 0, SWP_NOZORDER }, /* MDI frame */
+    { WM_NCCALCSIZE, sent|wparam|optional, 1 }, /* MDI frame */
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE, 0, SWP_NOZORDER }, /* MDI frame */
     { WM_MOVE, sent },
     { HCBT_MINMAX, hook|lparam, 0, SW_MAXIMIZE },
     { WM_GETMINMAXINFO, sent },
@@ -2469,12 +2922,12 @@ static const struct message WmDestroyMDIchildVisibleMaxSeq1[] = {
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* XP sends a duplicate */
 
      /* in MDI frame */
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
-    { WM_NCCALCSIZE, sent|wparam, 1 },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_NCCALCSIZE, sent|wparam|optional, 1 },
     { 0x0093, sent|defwinproc|optional },
     { 0x0093, sent|defwinproc|optional },
     { 0x0093, sent|defwinproc|optional },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI frame */
     { 0x0093, sent|optional },
 
@@ -2534,10 +2987,10 @@ static const struct message WmDestroyMDIchildVisibleMaxSeq1[] = {
     { WM_PARENTNOTIFY, sent /*|wparam, WM_DESTROY*/ }, /* in MDI client */
 
     { WM_SHOWWINDOW, sent|wparam, 0 },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_HIDE, winevent_hook|wparam|lparam, 0, 0 },
     { WM_ERASEBKGND, sent|parent|optional },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
 
     { EVENT_OBJECT_DESTROY, winevent_hook|wparam|lparam, 0, 0 },
     { WM_DESTROY, sent },
@@ -2553,19 +3006,19 @@ static const struct message WmMaximizeMDIchildInvisibleSeq[] = {
     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
 
-    { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
-    { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
-    { HCBT_SETFOCUS, hook },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_NCACTIVATE, sent|wparam|optional|defwinproc, 1 },
+    { HCBT_SETFOCUS, hook|optional },
     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
-    { WM_SETFOCUS, sent }, /* in MDI client */
-    { HCBT_SETFOCUS, hook },
-    { WM_KILLFOCUS, sent }, /* in MDI client */
+    { WM_SETFOCUS, sent|optional }, /* in MDI client */
+    { HCBT_SETFOCUS, hook|optional },
+    { WM_KILLFOCUS, sent|optional }, /* in MDI client */
     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
-    { WM_SETFOCUS, sent|defwinproc },
-    { WM_MDIACTIVATE, sent|defwinproc },
+    { WM_SETFOCUS, sent|optional|defwinproc },
+    { WM_MDIACTIVATE, sent|optional|defwinproc },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_STATECHANGED },
     { WM_SIZE, sent|defwinproc|wparam, SIZE_MAXIMIZED },
      /* in MDI frame */
@@ -2586,19 +3039,19 @@ static const struct message WmMaximizeMDIchildInvisibleSeq2[] = {
     { EVENT_OBJECT_SHOW, winevent_hook|wparam|lparam, 0, 0 },
     { WM_CHILDACTIVATE, sent|wparam|lparam, 0, 0 },
 
-    { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
-    { WM_NCACTIVATE, sent|wparam|defwinproc, 1 },
-    { HCBT_SETFOCUS, hook },
+    { WM_WINDOWPOSCHANGING, sent|wparam|defwinproc|optional, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_NCACTIVATE, sent|wparam|defwinproc|optional, 1 },
+    { HCBT_SETFOCUS, hook|optional },
     { WM_IME_SETCONTEXT, sent|wparam|optional, 1 }, /* in MDI client */
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
-    { WM_SETFOCUS, sent }, /* in MDI client */
-    { HCBT_SETFOCUS, hook },
-    { WM_KILLFOCUS, sent }, /* in MDI client */
+    { WM_SETFOCUS, sent|optional }, /* in MDI client */
+    { HCBT_SETFOCUS, hook|optional },
+    { WM_KILLFOCUS, sent|optional }, /* in MDI client */
     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 }, /* in MDI client */
     { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
-    { WM_SETFOCUS, sent|defwinproc },
-    { WM_MDIACTIVATE, sent|defwinproc },
+    { WM_SETFOCUS, sent|defwinproc|optional },
+    { WM_MDIACTIVATE, sent|defwinproc|optional },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
     { 0 }
@@ -2733,7 +3186,7 @@ static const struct message WmMinimizeMDIchildVisibleSeq[] = {
     { WM_NCCALCSIZE, sent|wparam, 1 },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOCLIENTSIZE|SWP_STATECHANGED },
     { WM_MOVE, sent|defwinproc },
-    { WM_SIZE, sent|defwinproc|wparam, SIZE_MINIMIZED },
+    { WM_SIZE, sent|defwinproc|wparam|lparam, SIZE_MINIMIZED, 0 },
     { WM_CHILDACTIVATE, sent|wparam|lparam|defwinproc, 0, 0 },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
     { EVENT_SYSTEM_MINIMIZESTART, winevent_hook|wparam|lparam, 0, 0 }, /* MDI child */
@@ -2764,7 +3217,7 @@ static WNDPROC old_mdi_client_proc;
 
 static LRESULT WINAPI mdi_client_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    struct message msg;
+    struct recvd_message msg;
 
     /* do not log painting messages */
     if (message != WM_PAINT &&
@@ -2774,38 +3227,14 @@ static LRESULT WINAPI mdi_client_hook_proc(HWND hwnd, UINT message, WPARAM wPara
         message != WM_NCHITTEST &&
         message != WM_GETTEXT &&
         message != WM_MDIGETACTIVE &&
-        message != WM_GETICON &&
-        message != WM_DEVICECHANGE)
+        !ignore_message( message ))
     {
-        trace("mdi client: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
-
-        switch (message)
-        {
-            case WM_WINDOWPOSCHANGING:
-            case WM_WINDOWPOSCHANGED:
-            {
-                WINDOWPOS *winpos = (WINDOWPOS *)lParam;
-
-                trace("%s\n", (message == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
-                trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
-                      winpos->hwnd, winpos->hwndInsertAfter,
-                      winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
-                dump_winpos_flags(winpos->flags);
-
-                /* Log only documented flags, win2k uses 0x1000 and 0x2000
-                 * in the high word for internal purposes
-                 */
-                wParam = winpos->flags & 0xffff;
-                /* We are not interested in the flags that don't match under XP and Win9x */
-                wParam &= ~(SWP_NOZORDER);
-                break;
-            }
-        }
-
+        msg.hwnd = hwnd;
         msg.message = message;
         msg.flags = sent|wparam|lparam;
         msg.wParam = wParam;
         msg.lParam = lParam;
+        msg.descr = "mdi client";
         add_message(&msg);
     }
 
@@ -2814,9 +3243,9 @@ static LRESULT WINAPI mdi_client_hook_proc(HWND hwnd, UINT message, WPARAM wPara
 
 static LRESULT WINAPI mdi_child_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    static long defwndproc_counter = 0;
+    static LONG defwndproc_counter = 0;
     LRESULT ret;
-    struct message msg;
+    struct recvd_message msg;
 
     /* do not log painting messages */
     if (message != WM_PAINT &&
@@ -2825,33 +3254,10 @@ static LRESULT WINAPI mdi_child_wnd_proc(HWND hwnd, UINT message, WPARAM wParam,
         message != WM_ERASEBKGND &&
         message != WM_NCHITTEST &&
         message != WM_GETTEXT &&
-        message != WM_GETICON &&
-        message != WM_DEVICECHANGE)
+        !ignore_message( message ))
     {
-        trace("mdi child: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
-
         switch (message)
         {
-            case WM_WINDOWPOSCHANGING:
-            case WM_WINDOWPOSCHANGED:
-            {
-                WINDOWPOS *winpos = (WINDOWPOS *)lParam;
-
-                trace("%s\n", (message == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
-                trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
-                      winpos->hwnd, winpos->hwndInsertAfter,
-                      winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
-                dump_winpos_flags(winpos->flags);
-
-                /* Log only documented flags, win2k uses 0x1000 and 0x2000
-                 * in the high word for internal purposes
-                 */
-                wParam = winpos->flags & 0xffff;
-                /* We are not interested in the flags that don't match under XP and Win9x */
-                wParam &= ~(SWP_NOZORDER);
-                break;
-            }
-
             case WM_MDIACTIVATE:
             {
                 HWND active, client = GetParent(hwnd);
@@ -2866,11 +3272,13 @@ static LRESULT WINAPI mdi_child_wnd_proc(HWND hwnd, UINT message, WPARAM wParam,
             }
         }
 
+        msg.hwnd = hwnd;
         msg.message = message;
         msg.flags = sent|wparam|lparam;
         if (defwndproc_counter) msg.flags |= defwinproc;
         msg.wParam = wParam;
         msg.lParam = lParam;
+        msg.descr = "mdi child";
         add_message(&msg);
     }
 
@@ -2883,9 +3291,9 @@ static LRESULT WINAPI mdi_child_wnd_proc(HWND hwnd, UINT message, WPARAM wParam,
 
 static LRESULT WINAPI mdi_frame_wnd_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    static long defwndproc_counter = 0;
+    static LONG defwndproc_counter = 0;
     LRESULT ret;
-    struct message msg;
+    struct recvd_message msg;
 
     /* do not log painting messages */
     if (message != WM_PAINT &&
@@ -2894,39 +3302,15 @@ static LRESULT WINAPI mdi_frame_wnd_proc(HWND hwnd, UINT message, WPARAM wParam,
         message != WM_ERASEBKGND &&
         message != WM_NCHITTEST &&
         message != WM_GETTEXT &&
-        message != WM_GETICON &&
-        message != WM_DEVICECHANGE)
+        !ignore_message( message ))
     {
-        trace("mdi frame: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
-
-        switch (message)
-        {
-            case WM_WINDOWPOSCHANGING:
-            case WM_WINDOWPOSCHANGED:
-            {
-                WINDOWPOS *winpos = (WINDOWPOS *)lParam;
-
-                trace("%s\n", (message == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
-                trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
-                      winpos->hwnd, winpos->hwndInsertAfter,
-                      winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
-                dump_winpos_flags(winpos->flags);
-
-                /* Log only documented flags, win2k uses 0x1000 and 0x2000
-                 * in the high word for internal purposes
-                 */
-                wParam = winpos->flags & 0xffff;
-                /* We are not interested in the flags that don't match under XP and Win9x */
-                wParam &= ~(SWP_NOZORDER);
-                break;
-            }
-        }
-
+        msg.hwnd = hwnd;
         msg.message = message;
         msg.flags = sent|wparam|lparam;
         if (defwndproc_counter) msg.flags |= defwinproc;
         msg.wParam = wParam;
         msg.lParam = lParam;
+        msg.descr = "mdi frame";
         add_message(&msg);
     }
 
@@ -2947,7 +3331,7 @@ static BOOL mdi_RegisterWindowClasses(void)
     cls.cbWndExtra = 0;
     cls.hInstance = GetModuleHandleA(0);
     cls.hIcon = 0;
-    cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
+    cls.hCursor = LoadCursorA(0, IDC_ARROW);
     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
     cls.lpszMenuName = NULL;
     cls.lpszClassName = "MDI_frame_class";
@@ -3324,7 +3708,7 @@ static void test_mdi_messages(void)
                                 0, 0, CW_USEDEFAULT, CW_USEDEFAULT,
                                 mdi_client, 0, GetModuleHandleA(0), NULL);
     assert(mdi_child2);
-    ok_sequence(WmCreateMDIchildInvisibleMaxSeq4, "Create maximized invisible MDI child window", TRUE);
+    ok_sequence(WmCreateMDIchildInvisibleMaxSeq4, "Create maximized invisible MDI child window", FALSE);
     ok(IsZoomed(mdi_child2), "MDI child should be maximized\n");
     ok(!(GetWindowLongA(mdi_child2, GWL_STYLE) & WS_VISIBLE), "MDI child should be not visible\n");
     ok(!IsWindowVisible(mdi_child2), "MDI child should be not visible\n");
@@ -3334,7 +3718,7 @@ static void test_mdi_messages(void)
      */
     active_child = (HWND)SendMessageA(mdi_client, WM_MDIGETACTIVE, 0, (LPARAM)&zoomed);
     ok(active_child == mdi_child || /* win2k */
-       !active_child, /* win9x */
+       !active_child || active_child == mdi_child2, /* win9x */
        "wrong active MDI child %p\n", active_child);
     flush_sequence();
 
@@ -3506,6 +3890,7 @@ static void test_WM_SETREDRAW(HWND hwnd)
 {
     DWORD style = GetWindowLongA(hwnd, GWL_STYLE);
 
+    flush_events();
     flush_sequence();
 
     SendMessageA(hwnd, WM_SETREDRAW, FALSE, 0);
@@ -3524,53 +3909,34 @@ static void test_WM_SETREDRAW(HWND hwnd)
     /* restore original WS_VISIBLE state */
     SetWindowLongA(hwnd, GWL_STYLE, style);
 
+    flush_events();
     flush_sequence();
 }
 
 static INT_PTR CALLBACK TestModalDlgProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    struct message msg;
-
-    trace("dialog: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
+    struct recvd_message msg;
 
-    /* explicitly ignore WM_GETICON message */
-    if (message == WM_GETICON) return 0;
+    if (ignore_message( message )) return 0;
 
     switch (message)
     {
        /* ignore */
        case WM_MOUSEMOVE:
+       case WM_NCMOUSEMOVE:
+       case WM_NCMOUSELEAVE:
        case WM_SETCURSOR:
-       case WM_DEVICECHANGE:
             return 0;
         case WM_NCHITTEST:
             return HTCLIENT;
-
-        case WM_WINDOWPOSCHANGING:
-        case WM_WINDOWPOSCHANGED:
-        {
-            WINDOWPOS *winpos = (WINDOWPOS *)lParam;
-
-            trace("%s\n", (message == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
-            trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
-                  winpos->hwnd, winpos->hwndInsertAfter,
-                  winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
-            dump_winpos_flags(winpos->flags);
-
-            /* Log only documented flags, win2k uses 0x1000 and 0x2000
-             * in the high word for internal purposes
-             */
-            wParam = winpos->flags & 0xffff;
-            /* We are not interested in the flags that don't match under XP and Win9x */
-            wParam &= ~(SWP_NOZORDER);
-            break;
-        }
     }
 
+    msg.hwnd = hwnd;
     msg.message = message;
     msg.flags = sent|wparam|lparam;
     msg.wParam = wParam;
     msg.lParam = lParam;
+    msg.descr = "dialog";
     add_message(&msg);
 
     if (message == WM_INITDIALOG) SetTimer( hwnd, 1, 100, NULL );
@@ -3610,7 +3976,6 @@ static void test_hv_scroll_1(HWND hwnd, INT ctl, DWORD clear, DWORD set, INT min
 
     xmin = 0xdeadbeef;
     xmax = 0xdeadbeef;
-    trace("Ignore GetScrollRange error below if you are on Win9x\n");
     ret = GetScrollRange(hwnd, ctl, &xmin, &xmax);
     ok( ret, "GetScrollRange(%d) error %d\n", ctl, GetLastError());
     ok_sequence(WmEmptySeq, "GetScrollRange(SB_HORZ/SB_VERT) empty sequence", FALSE);
@@ -3682,6 +4047,9 @@ static void test_scroll_messages(HWND hwnd)
     INT min, max;
     BOOL ret;
 
+    flush_events();
+    flush_sequence();
+
     min = 0xdeadbeef;
     max = 0xdeadbeef;
     ret = GetScrollRange(hwnd, SB_CTL, &min, &max);
@@ -3776,14 +4144,12 @@ static void test_showwindow(void)
     /* ShowWindow( SW_SHOWNA) for invisible top level window */
     trace("calling ShowWindow( SW_SHOWNA) for invisible top level window\n");
     ok( ShowWindow(hwnd, SW_SHOWNA) == FALSE, "ShowWindow: window was visible\n" );
-    ok_sequence(WmSHOWNATopInvisible, "ShowWindow(SW_SHOWNA) on invisible top level window", TRUE);
-    trace("done\n");
+    ok_sequence(WmSHOWNATopInvisible, "ShowWindow(SW_SHOWNA) on invisible top level window", FALSE);
 
     /* ShowWindow( SW_SHOWNA) for now visible top level window */
     trace("calling ShowWindow( SW_SHOWNA) for now visible top level window\n");
     ok( ShowWindow(hwnd, SW_SHOWNA) != FALSE, "ShowWindow: window was invisible\n" );
     ok_sequence(WmSHOWNATopVisible, "ShowWindow(SW_SHOWNA) on visible top level window", FALSE);
-    trace("done\n");
     /* back to invisible */
     ShowWindow(hchild, SW_HIDE);
     ShowWindow(hwnd, SW_HIDE);
@@ -3792,21 +4158,18 @@ static void test_showwindow(void)
     trace("calling ShowWindow( SW_SHOWNA) for invisible child with invisible parent\n");
     ok( ShowWindow(hchild, SW_SHOWNA) == FALSE, "ShowWindow: window was visible\n" );
     ok_sequence(WmSHOWNAChildInvisParInvis, "ShowWindow(SW_SHOWNA) invisible child and parent", FALSE);
-    trace("done\n");
     /* ShowWindow(SW_SHOWNA) with child visible and parent invisible */ 
     ok( ShowWindow(hchild, SW_SHOW) != FALSE, "ShowWindow: window was invisible\n" );
     flush_sequence();
     trace("calling ShowWindow( SW_SHOWNA) for the visible child and invisible parent\n");
     ok( ShowWindow(hchild, SW_SHOWNA) != FALSE, "ShowWindow: window was invisible\n" );
     ok_sequence(WmSHOWNAChildVisParInvis, "ShowWindow(SW_SHOWNA) visible child and invisible parent", FALSE);
-    trace("done\n");
     /* ShowWindow(SW_SHOWNA) with child visible and parent visible */
     ShowWindow( hwnd, SW_SHOW);
     flush_sequence();
     trace("calling ShowWindow( SW_SHOWNA) for the visible child and parent\n");
     ok( ShowWindow(hchild, SW_SHOWNA) != FALSE, "ShowWindow: window was invisible\n" );
     ok_sequence(WmSHOWNAChildVisParVis, "ShowWindow(SW_SHOWNA) for the visible child and parent", FALSE);
-    trace("done\n");
 
     /* ShowWindow(SW_SHOWNA) with child invisible and parent visible */
     ShowWindow( hchild, SW_HIDE);
@@ -3814,7 +4177,6 @@ static void test_showwindow(void)
     trace("calling ShowWindow( SW_SHOWNA) for the invisible child and visible parent\n");
     ok( ShowWindow(hchild, SW_SHOWNA) == FALSE, "ShowWindow: window was visible\n" );
     ok_sequence(WmSHOWNAChildInvisParVis, "ShowWindow(SW_SHOWNA) for the invisible child and visible parent", FALSE);
-    trace("done\n");
 
     SetCapture(hchild);
     ok(GetCapture() == hchild, "wrong capture window %p\n", GetCapture());
@@ -3836,7 +4198,6 @@ static void test_showwindow(void)
     ok (hwnd != 0, "Failed to create popup window\n");
     ok(IsZoomed(hwnd), "window should be maximized\n");
     ok_sequence(WmCreateInvisibleMaxPopupSeq, "CreateWindow(WS_MAXIMIZED):popup", FALSE);
-    trace("done\n");
 
     GetWindowRect(hwnd, &rc);
     ok( rc.right-rc.left == GetSystemMetrics(SM_CXSCREEN) &&
@@ -3852,7 +4213,6 @@ static void test_showwindow(void)
     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
     ok(IsZoomed(hwnd), "window should be maximized\n");
     ok_sequence(WmShowMaxPopupResizedSeq, "ShowWindow(SW_SHOWMAXIMIZED):invisible maximized and resized popup", FALSE);
-    trace("done\n");
 
     GetWindowRect(hwnd, &rc);
     ok( rc.right-rc.left == GetSystemMetrics(SM_CXSCREEN) &&
@@ -3872,13 +4232,11 @@ static void test_showwindow(void)
     ok (hwnd != 0, "Failed to create popup window\n");
     ok(IsZoomed(hwnd), "window should be maximized\n");
     ok_sequence(WmCreateInvisibleMaxPopupSeq, "CreateWindow(WS_MAXIMIZED):popup", FALSE);
-    trace("done\n");
 
     trace("calling ShowWindow( SW_SHOWMAXIMIZE ) for invisible maximized popup window\n");
     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
     ok(IsZoomed(hwnd), "window should be maximized\n");
     ok_sequence(WmShowMaxPopupSeq, "ShowWindow(SW_SHOWMAXIMIZED):invisible maximized popup", FALSE);
-    trace("done\n");
     DestroyWindow(hwnd);
     flush_sequence();
 
@@ -3891,7 +4249,6 @@ static void test_showwindow(void)
     ok (hwnd != 0, "Failed to create popup window\n");
     ok(IsZoomed(hwnd), "window should be maximized\n");
     ok_sequence(WmCreateMaxPopupSeq, "CreateWindow(WS_MAXIMIZED):popup", FALSE);
-    trace("done\n");
     DestroyWindow(hwnd);
     flush_sequence();
 
@@ -3905,13 +4262,11 @@ static void test_showwindow(void)
     ok (hwnd != 0, "Failed to create popup window\n");
     ok(!IsZoomed(hwnd), "window should NOT be maximized\n");
     ok_sequence(WmCreatePopupSeq, "CreateWindow(WS_VISIBLE):popup", FALSE);
-    trace("done\n");
 
     trace("calling ShowWindow( SW_SHOWMAXIMIZE ) for visible popup window\n");
     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
     ok(IsZoomed(hwnd), "window should be maximized\n");
     ok_sequence(WmShowVisMaxPopupSeq, "ShowWindow(SW_SHOWMAXIMIZED):popup", FALSE);
-    trace("done\n");
     DestroyWindow(hwnd);
     flush_sequence();
 }
@@ -4029,7 +4384,39 @@ static const struct message WmInitEndSession_4[] = {
 /* Sending undocumented 0x3B message with wparam = 0x80000001 */
 static const struct message WmInitEndSession_5[] = {
     { 0x003B, sent },
-    { WM_ENDSESSION, sent|defwinproc|wparam|lparam, 1, ENDSESSION_LOGOFF },
+    { WM_ENDSESSION, sent|defwinproc/*|wparam*/|lparam, 1, ENDSESSION_LOGOFF },
+    { 0 }
+};
+
+static const struct message WmOptionalPaint[] = {
+    { WM_PAINT, sent|optional },
+    { WM_NCPAINT, sent|beginpaint|optional },
+    { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
+    { 0 }
+};
+
+static const struct message WmZOrder[] = {
+    { WM_WINDOWPOSCHANGING, sent|wparam, 0, 0 },
+    { WM_GETMINMAXINFO, sent|defwinproc|wparam, 0, 0 },
+    { HCBT_ACTIVATE, hook },
+    { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
+    { WM_WINDOWPOSCHANGING, sent|wparam, 3, 0 },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOREDRAW|SWP_NOMOVE|SWP_NOSIZE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE, 0 },
+    { WM_GETTEXT, sent|optional },
+    { WM_NCCALCSIZE, sent|wparam|optional, 1 },
+    { WM_ACTIVATEAPP, sent|wparam, 1, 0 },
+    { WM_NCACTIVATE, sent|lparam, 1, 0 },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ACTIVATE, sent|wparam|lparam, 1, 0 },
+    { HCBT_SETFOCUS, hook },
+    { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
+    { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
+    { WM_SETFOCUS, sent|wparam|defwinproc, 0 },
+    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_GETTEXT, sent|optional },
+    { WM_NCCALCSIZE, sent|optional },
     { 0 }
 };
 
@@ -4046,7 +4433,7 @@ static void test_MsgWaitForMultipleObjects(HWND hwnd)
     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
     ok(ret == WAIT_OBJECT_0, "MsgWaitForMultipleObjects returned %x\n", ret);
 
-    ok(PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n");
+    ok(PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n");
     ok(msg.message == WM_USER, "got %04x instead of WM_USER\n", msg.message);
 
     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
@@ -4057,7 +4444,7 @@ static void test_MsgWaitForMultipleObjects(HWND hwnd)
     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
     ok(ret == WAIT_OBJECT_0, "MsgWaitForMultipleObjects returned %x\n", ret);
 
-    ok(PeekMessageW( &msg, 0, 0, 0, PM_NOREMOVE ), "PeekMessage should succeed\n");
+    ok(PeekMessageA( &msg, 0, 0, 0, PM_NOREMOVE ), "PeekMessage should succeed\n");
     ok(msg.message == WM_USER, "got %04x instead of WM_USER\n", msg.message);
 
     /* shows QS_POSTMESSAGE flag is cleared in the PeekMessage call */
@@ -4070,9 +4457,9 @@ static void test_MsgWaitForMultipleObjects(HWND hwnd)
     ret = MsgWaitForMultipleObjects(0, NULL, FALSE, 0, QS_POSTMESSAGE);
     ok(ret == WAIT_OBJECT_0, "MsgWaitForMultipleObjects returned %x\n", ret);
 
-    ok(PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n");
+    ok(PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n");
     ok(msg.message == WM_USER, "got %04x instead of WM_USER\n", msg.message);
-    ok(PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n");
+    ok(PeekMessageA( &msg, 0, 0, 0, PM_REMOVE ), "PeekMessage should succeed\n");
     ok(msg.message == WM_USER, "got %04x instead of WM_USER\n", msg.message);
 }
 
@@ -4100,40 +4487,54 @@ static void test_messages(void)
     test_WM_SETREDRAW(hwnd);
 
     SetWindowPos(hwnd, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
+    flush_events();
     ok_sequence(WmSWP_ShowOverlappedSeq, "SetWindowPos:SWP_SHOWWINDOW:overlapped", FALSE);
     ok(IsWindowVisible(hwnd), "window should be visible at this point\n");
 
     ok(GetActiveWindow() == hwnd, "window should be active\n");
     ok(GetFocus() == hwnd, "window should have input focus\n");
     ShowWindow(hwnd, SW_HIDE);
-    ok_sequence(WmHideOverlappedSeq, "ShowWindow(SW_HIDE):overlapped", TRUE);
+    flush_events();
+    ok_sequence(WmHideOverlappedSeq, "ShowWindow(SW_HIDE):overlapped", FALSE);
 
     ShowWindow(hwnd, SW_SHOW);
+    flush_events();
     ok_sequence(WmShowOverlappedSeq, "ShowWindow(SW_SHOW):overlapped", TRUE);
 
     ShowWindow(hwnd, SW_HIDE);
+    flush_events();
     ok_sequence(WmHideOverlappedSeq, "ShowWindow(SW_HIDE):overlapped", FALSE);
 
     ShowWindow(hwnd, SW_SHOWMAXIMIZED);
+    flush_events();
     ok_sequence(WmShowMaxOverlappedSeq, "ShowWindow(SW_SHOWMAXIMIZED):overlapped", TRUE);
-
-    ShowWindow(hwnd, SW_RESTORE);
-    /* FIXME: add ok_sequence() here */
     flush_sequence();
 
+    if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_MAXIMIZE)
+    {
+        ShowWindow(hwnd, SW_RESTORE);
+        flush_events();
+        ok_sequence(WmShowRestoreMaxOverlappedSeq, "ShowWindow(SW_RESTORE):overlapped", TRUE);
+        flush_sequence();
+    }
+
     ShowWindow(hwnd, SW_MINIMIZE);
+    flush_events();
     ok_sequence(WmShowMinOverlappedSeq, "ShowWindow(SW_SHOWMINIMIZED):overlapped", TRUE);
     flush_sequence();
 
-    ShowWindow(hwnd, SW_RESTORE);
-    /* FIXME: add ok_sequence() here */
-    flush_sequence();
+    if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_MINIMIZE)
+    {
+        ShowWindow(hwnd, SW_RESTORE);
+        flush_events();
+        ok_sequence(WmShowRestoreMinOverlappedSeq, "ShowWindow(SW_RESTORE):overlapped", TRUE);
+        flush_sequence();
+    }
 
     ShowWindow(hwnd, SW_SHOW);
-    ok_sequence(WmEmptySeq, "ShowWindow(SW_SHOW):overlapped already visible", FALSE);
+    flush_events();
+    ok_sequence(WmOptionalPaint, "ShowWindow(SW_SHOW):overlapped already visible", FALSE);
 
-    ok(GetActiveWindow() == hwnd, "window should be active\n");
-    ok(GetFocus() == hwnd, "window should have input focus\n");
     SetWindowPos(hwnd, 0,0,0,0,0, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE);
     ok_sequence(WmSWP_HideOverlappedSeq, "SetWindowPos:SWP_HIDEWINDOW:overlapped", FALSE);
     ok(!IsWindowVisible(hwnd), "window should not be visible at this point\n");
@@ -4141,6 +4542,7 @@ static void test_messages(void)
 
     /* test WM_SETREDRAW on a visible top level window */
     ShowWindow(hwnd, SW_SHOW);
+    flush_events();
     test_WM_SETREDRAW(hwnd);
 
     trace("testing scroll APIs on a visible top level window %p\n", hwnd);
@@ -4155,7 +4557,7 @@ static void test_messages(void)
     ok_sequence(WmSWP_MoveSeq, "SetWindowPos:Move", FALSE );
     flush_events();
     flush_sequence();
-    SetWindowPos( hwnd, 0, 200, 200, 250, 250, SWP_NOZORDER );
+    SetWindowPos( hwnd, 0, 200, 200, 250, 250, SWP_NOZORDER|SWP_NOACTIVATE );
     ok_sequence(WmSWP_ResizeNoZOrder, "SetWindowPos:WmSWP_ResizeNoZOrder", FALSE );
     flush_events();
     flush_sequence();
@@ -4283,7 +4685,7 @@ static void test_messages(void)
     ok_sequence(WmShowVisiblePopupSeq_2, "SetWindowPos:show_visible_popup_2", FALSE);
     flush_sequence();
     SetWindowPos(hchild, 0,0,0,0,0, SWP_SHOWWINDOW|SWP_NOSIZE|SWP_NOMOVE);
-    ok_sequence(WmShowVisiblePopupSeq_3, "SetWindowPos:show_visible_popup_3", TRUE);
+    ok_sequence(WmShowVisiblePopupSeq_3, "SetWindowPos:show_visible_popup_3", FALSE);
     DestroyWindow(hchild);
 
     /* this time add WS_VISIBLE for CreateWindowEx, but this fact actually
@@ -4460,47 +4862,75 @@ static void test_messages(void)
 
     flush_sequence();
     res = SendMessage(hwnd, 0x3B, 0x8000000b, 0);
+    if (!res)
+    {
+        todo_wine win_skip( "Message 0x3b not supported\n" );
+        goto done;
+    }
     ok_sequence(WmInitEndSession, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x8000000b", TRUE);
-    todo_wine
     ok(res == 1, "SendMessage(hwnd, 0x3B, 0x8000000b, 0) should have returned 1 instead of %ld\n", res);
     res = SendMessage(hwnd, 0x3B, 0x0000000b, 0);
     ok_sequence(WmInitEndSession_2, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x0000000b", TRUE);
-    todo_wine
     ok(res == 1, "SendMessage(hwnd, 0x3B, 0x0000000b, 0) should have returned 1 instead of %ld\n", res);
     res = SendMessage(hwnd, 0x3B, 0x0000000f, 0);
     ok_sequence(WmInitEndSession_2, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x0000000f", TRUE);
-    todo_wine
     ok(res == 1, "SendMessage(hwnd, 0x3B, 0x0000000f, 0) should have returned 1 instead of %ld\n", res);
 
     flush_sequence();
     res = SendMessage(hwnd, 0x3B, 0x80000008, 0);
     ok_sequence(WmInitEndSession_3, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x80000008", TRUE);
-    todo_wine
     ok(res == 2, "SendMessage(hwnd, 0x3B, 0x80000008, 0) should have returned 2 instead of %ld\n", res);
     res = SendMessage(hwnd, 0x3B, 0x00000008, 0);
     ok_sequence(WmInitEndSession_4, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x00000008", TRUE);
-    todo_wine
     ok(res == 2, "SendMessage(hwnd, 0x3B, 0x00000008, 0) should have returned 2 instead of %ld\n", res);
 
     res = SendMessage(hwnd, 0x3B, 0x80000004, 0);
     ok_sequence(WmInitEndSession_3, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x80000004", TRUE);
-    todo_wine
     ok(res == 2, "SendMessage(hwnd, 0x3B, 0x80000004, 0) should have returned 2 instead of %ld\n", res);
 
     res = SendMessage(hwnd, 0x3B, 0x80000001, 0);
     ok_sequence(WmInitEndSession_5, "Handling of undocumented 0x3B message by DefWindowProc wparam=0x80000001", TRUE);
-    todo_wine
     ok(res == 2, "SendMessage(hwnd, 0x3B, 0x80000001, 0) should have returned 2 instead of %ld\n", res);
 
+done:
     DestroyWindow(hwnd);
     flush_sequence();
 }
 
-static void invisible_parent_tests(void)
+static void test_setwindowpos(void)
 {
-    HWND hparent, hchild;
+    HWND hwnd;
+    RECT rc;
+    LRESULT res;
+    const INT winX = 100;
+    const INT winY = 100;
+    const INT sysX = GetSystemMetrics(SM_CXMINTRACK);
 
-    hparent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW,
+    hwnd = CreateWindowExA(0, "TestWindowClass", NULL, 0,
+                           0, 0, winX, winY, 0,
+                           NULL, NULL, 0);
+
+    GetWindowRect(hwnd, &rc);
+    expect(sysX, rc.right);
+    expect(winY, rc.bottom);
+
+    flush_events();
+    flush_sequence();
+    res = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, winX, winY, 0);
+    ok_sequence(WmZOrder, "Z-Order", TRUE);
+    ok(res == TRUE, "SetWindowPos expected TRUE, got %ld\n", res);
+
+    GetWindowRect(hwnd, &rc);
+    expect(sysX, rc.right);
+    expect(winY, rc.bottom);
+    DestroyWindow(hwnd);
+}
+
+static void invisible_parent_tests(void)
+{
+    HWND hparent, hchild;
+
+    hparent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW,
                               100, 100, 200, 200, 0, 0, 0, NULL);
     ok (hparent != 0, "Failed to create parent window\n");
     flush_sequence();
@@ -4662,6 +5092,8 @@ todo_wine {
 }
 
 /****************** button message test *************************/
+#define ID_BUTTON 0x000e
+
 static const struct message WmSetFocusButtonSeq[] =
 {
     { HCBT_SETFOCUS, hook },
@@ -4669,7 +5101,9 @@ static const struct message WmSetFocusButtonSeq[] =
     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_SETFOCUS, sent|wparam, 0 },
-    { WM_CTLCOLORBTN, sent|defwinproc },
+    { WM_CTLCOLORBTN, sent|parent },
+    { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_SETFOCUS) },
+    { WM_APP, sent|wparam|lparam, 0, 0 },
     { 0 }
 };
 static const struct message WmKillFocusButtonSeq[] =
@@ -4677,9 +5111,13 @@ static const struct message WmKillFocusButtonSeq[] =
     { HCBT_SETFOCUS, hook },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_KILLFOCUS, sent|wparam, 0 },
-    { WM_CTLCOLORBTN, sent|defwinproc },
+    { WM_CTLCOLORBTN, sent|parent },
+    { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_KILLFOCUS) },
     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
+    { WM_APP, sent|wparam|lparam, 0, 0 },
+    { WM_PAINT, sent },
+    { WM_CTLCOLORBTN, sent|parent },
     { 0 }
 };
 static const struct message WmSetFocusStaticSeq[] =
@@ -4689,7 +5127,10 @@ static const struct message WmSetFocusStaticSeq[] =
     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_SETFOCUS, sent|wparam, 0 },
-    { WM_CTLCOLORSTATIC, sent|defwinproc },
+    { WM_CTLCOLORSTATIC, sent|parent },
+    { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_SETFOCUS) },
+    { WM_COMMAND, sent|wparam|parent|optional, MAKEWPARAM(ID_BUTTON, BN_CLICKED) }, /* radio button */
+    { WM_APP, sent|wparam|lparam, 0, 0 },
     { 0 }
 };
 static const struct message WmKillFocusStaticSeq[] =
@@ -4697,9 +5138,42 @@ static const struct message WmKillFocusStaticSeq[] =
     { HCBT_SETFOCUS, hook },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_KILLFOCUS, sent|wparam, 0 },
-    { WM_CTLCOLORSTATIC, sent|defwinproc },
+    { WM_CTLCOLORSTATIC, sent|parent },
+    { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_KILLFOCUS) },
+    { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
+    { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
+    { WM_APP, sent|wparam|lparam, 0, 0 },
+    { WM_PAINT, sent },
+    { WM_CTLCOLORSTATIC, sent|parent },
+    { 0 }
+};
+static const struct message WmSetFocusOwnerdrawSeq[] =
+{
+    { HCBT_SETFOCUS, hook },
+    { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
+    { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
+    { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
+    { WM_SETFOCUS, sent|wparam, 0 },
+    { WM_CTLCOLORBTN, sent|parent },
+    { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x001040e4 },
+    { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_SETFOCUS) },
+    { WM_APP, sent|wparam|lparam, 0, 0 },
+    { 0 }
+};
+static const struct message WmKillFocusOwnerdrawSeq[] =
+{
+    { HCBT_SETFOCUS, hook },
+    { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
+    { WM_KILLFOCUS, sent|wparam, 0 },
+    { WM_CTLCOLORBTN, sent|parent },
+    { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000040e4 },
+    { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_KILLFOCUS) },
     { WM_IME_SETCONTEXT, sent|wparam|optional, 0 },
     { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
+    { WM_APP, sent|wparam|lparam, 0, 0 },
+    { WM_PAINT, sent },
+    { WM_CTLCOLORBTN, sent|parent },
+    { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000010e4 },
     { 0 }
 };
 static const struct message WmLButtonDownSeq[] =
@@ -4735,29 +5209,77 @@ static const struct message WmSetFontButtonSeq[] =
     { WM_CTLCOLORBTN, sent|defwinproc },
     { 0 }
 };
+static const struct message WmSetStyleButtonSeq[] =
+{
+    { BM_SETSTYLE, sent },
+    { WM_APP, sent|wparam|lparam, 0, 0 },
+    { WM_PAINT, sent },
+    { WM_NCPAINT, sent|defwinproc|optional }, /* FIXME: Wine sends it */
+    { WM_ERASEBKGND, sent|defwinproc|optional }, /* Win9x doesn't send it */
+    { WM_CTLCOLORBTN, sent|parent },
+    { 0 }
+};
+static const struct message WmSetStyleStaticSeq[] =
+{
+    { BM_SETSTYLE, sent },
+    { WM_APP, sent|wparam|lparam, 0, 0 },
+    { WM_PAINT, sent },
+    { WM_NCPAINT, sent|defwinproc|optional }, /* FIXME: Wine sends it */
+    { WM_ERASEBKGND, sent|defwinproc|optional }, /* Win9x doesn't send it */
+    { WM_CTLCOLORSTATIC, sent|parent },
+    { 0 }
+};
+static const struct message WmSetStyleUserSeq[] =
+{
+    { BM_SETSTYLE, sent },
+    { WM_APP, sent|wparam|lparam, 0, 0 },
+    { WM_PAINT, sent },
+    { WM_NCPAINT, sent|defwinproc|optional }, /* FIXME: Wine sends it */
+    { WM_ERASEBKGND, sent|defwinproc|optional }, /* Win9x doesn't send it */
+    { WM_CTLCOLORBTN, sent|parent },
+    { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_BUTTON, BN_PAINT) },
+    { 0 }
+};
+static const struct message WmSetStyleOwnerdrawSeq[] =
+{
+    { BM_SETSTYLE, sent },
+    { WM_APP, sent|wparam|lparam, 0, 0 },
+    { WM_PAINT, sent },
+    { WM_NCPAINT, sent|optional }, /* FIXME: Wine sends it */
+    { WM_ERASEBKGND, sent|defwinproc|optional }, /* Win9x doesn't send it */
+    { WM_CTLCOLORBTN, sent|parent },
+    { WM_CTLCOLORBTN, sent|parent|optional }, /* Win9x doesn't send it */
+    { WM_DRAWITEM, sent|wparam|lparam|parent, ID_BUTTON, 0x000010e4 },
+    { 0 }
+};
 
 static WNDPROC old_button_proc;
 
 static LRESULT CALLBACK button_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    static long defwndproc_counter = 0;
+    static LONG defwndproc_counter = 0;
     LRESULT ret;
-    struct message msg;
-
-    trace("button: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
-
-    /* explicitly ignore WM_GETICON message */
-    if (message == WM_GETICON) return 0;
+    struct recvd_message msg;
 
-    msg.message = message;
-    msg.flags = sent|wparam|lparam;
-    if (defwndproc_counter) msg.flags |= defwinproc;
-    msg.wParam = wParam;
-    msg.lParam = lParam;
-    add_message(&msg);
+    if (ignore_message( message )) return 0;
 
-    if (message == BM_SETSTATE)
+    switch (message)
+    {
+    case WM_SYNCPAINT:
+        break;
+    case BM_SETSTATE:
        ok(GetCapture() == hwnd, "GetCapture() = %p\n", GetCapture());
+        /* fall through */
+    default:
+        msg.hwnd = hwnd;
+        msg.message = message;
+        msg.flags = sent|wparam|lparam;
+        if (defwndproc_counter) msg.flags |= defwinproc;
+        msg.wParam = wParam;
+        msg.lParam = lParam;
+        msg.descr = "button";
+        add_message(&msg);
+    }
 
     defwndproc_counter++;
     ret = CallWindowProcA(old_button_proc, hwnd, message, wParam, lParam);
@@ -4789,67 +5311,122 @@ static void test_button_messages(void)
        DWORD dlg_code;
        const struct message *setfocus;
        const struct message *killfocus;
+       const struct message *setstyle;
     } button[] = {
        { BS_PUSHBUTTON, DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON,
-         WmSetFocusButtonSeq, WmKillFocusButtonSeq },
+         WmSetFocusButtonSeq, WmKillFocusButtonSeq, WmSetStyleButtonSeq },
        { BS_DEFPUSHBUTTON, DLGC_BUTTON | DLGC_DEFPUSHBUTTON,
-         WmSetFocusButtonSeq, WmKillFocusButtonSeq },
+         WmSetFocusButtonSeq, WmKillFocusButtonSeq, WmSetStyleButtonSeq },
        { BS_CHECKBOX, DLGC_BUTTON,
-         WmSetFocusStaticSeq, WmKillFocusStaticSeq },
+         WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq },
        { BS_AUTOCHECKBOX, DLGC_BUTTON,
-         WmSetFocusStaticSeq, WmKillFocusStaticSeq },
+         WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq },
        { BS_RADIOBUTTON, DLGC_BUTTON | DLGC_RADIOBUTTON,
-         WmSetFocusStaticSeq, WmKillFocusStaticSeq },
+         WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq },
        { BS_3STATE, DLGC_BUTTON,
-         WmSetFocusStaticSeq, WmKillFocusStaticSeq },
+         WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq },
        { BS_AUTO3STATE, DLGC_BUTTON,
-         WmSetFocusStaticSeq, WmKillFocusStaticSeq },
+         WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq },
        { BS_GROUPBOX, DLGC_STATIC,
-         WmSetFocusStaticSeq, WmKillFocusStaticSeq },
+         WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq },
        { BS_USERBUTTON, DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON,
-         WmSetFocusButtonSeq, WmKillFocusButtonSeq },
+         WmSetFocusButtonSeq, WmKillFocusButtonSeq, WmSetStyleUserSeq },
        { BS_AUTORADIOBUTTON, DLGC_BUTTON | DLGC_RADIOBUTTON,
-         WmSetFocusStaticSeq, WmKillFocusStaticSeq },
+         WmSetFocusStaticSeq, WmKillFocusStaticSeq, WmSetStyleStaticSeq },
        { BS_OWNERDRAW, DLGC_BUTTON,
-         WmSetFocusButtonSeq, WmKillFocusButtonSeq }
+         WmSetFocusOwnerdrawSeq, WmKillFocusOwnerdrawSeq, WmSetStyleOwnerdrawSeq }
     };
     unsigned int i;
-    HWND hwnd;
+    HWND hwnd, parent;
     DWORD dlg_code;
     HFONT zfont;
 
+    /* selection with VK_SPACE should capture button window */
+    hwnd = CreateWindowExA(0, "button", "test", BS_CHECKBOX | WS_VISIBLE | WS_POPUP,
+                           0, 0, 50, 14, 0, 0, 0, NULL);
+    ok(hwnd != 0, "Failed to create button window\n");
+    ReleaseCapture();
+    SetFocus(hwnd);
+    SendMessageA(hwnd, WM_KEYDOWN, VK_SPACE, 0);
+    ok(GetCapture() == hwnd, "Should be captured on VK_SPACE WM_KEYDOWN\n");
+    SendMessageA(hwnd, WM_KEYUP, VK_SPACE, 0);
+    DestroyWindow(hwnd);
+
     subclass_button();
 
+    parent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
+                             100, 100, 200, 200, 0, 0, 0, NULL);
+    ok(parent != 0, "Failed to create parent window\n");
+
     for (i = 0; i < sizeof(button)/sizeof(button[0]); i++)
     {
-       hwnd = CreateWindowExA(0, "my_button_class", "test", button[i].style | WS_POPUP,
-                              0, 0, 50, 14, 0, 0, 0, NULL);
+        MSG msg;
+        DWORD style;
+
+        trace("button style %08x\n", button[i].style);
+
+        hwnd = CreateWindowExA(0, "my_button_class", "test", button[i].style | WS_CHILD | BS_NOTIFY,
+                               0, 0, 50, 14, parent, (HMENU)ID_BUTTON, 0, NULL);
        ok(hwnd != 0, "Failed to create button window\n");
 
+        style = GetWindowLongA(hwnd, GWL_STYLE);
+        style &= ~(WS_CHILD | BS_NOTIFY);
+        /* XP turns a BS_USERBUTTON into BS_PUSHBUTTON */
+        if (button[i].style == BS_USERBUTTON)
+            ok(style == BS_PUSHBUTTON, "expected style BS_PUSHBUTTON got %x\n", style);
+        else
+            ok(style == button[i].style, "expected style %x got %x\n", button[i].style, style);
+
        dlg_code = SendMessageA(hwnd, WM_GETDLGCODE, 0, 0);
        ok(dlg_code == button[i].dlg_code, "%u: wrong dlg_code %08x\n", i, dlg_code);
 
        ShowWindow(hwnd, SW_SHOW);
        UpdateWindow(hwnd);
        SetFocus(0);
+       flush_events();
        flush_sequence();
 
-       trace("button style %08x\n", button[i].style);
+        log_all_parent_messages++;
+
+        ok(GetFocus() == 0, "expected focus 0, got %p\n", GetFocus());
        SetFocus(hwnd);
+        SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
+        while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
        ok_sequence(button[i].setfocus, "SetFocus(hwnd) on a button", FALSE);
 
        SetFocus(0);
+        SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
+        while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
        ok_sequence(button[i].killfocus, "SetFocus(0) on a button", FALSE);
 
+        ok(GetFocus() == 0, "expected focus 0, got %p\n", GetFocus());
+
+        SendMessage(hwnd, BM_SETSTYLE, button[i].style | BS_BOTTOM, TRUE);
+        SendMessage(hwnd, WM_APP, 0, 0); /* place a separator mark here */
+        while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
+        ok_sequence(button[i].setstyle, "BM_SETSTYLE on a button", FALSE);
+
+        style = GetWindowLongA(hwnd, GWL_STYLE);
+        style &= ~(WS_VISIBLE | WS_CHILD | BS_NOTIFY);
+        /* XP doesn't turn a BS_USERBUTTON into BS_PUSHBUTTON here! */
+        ok(style == button[i].style, "expected style %x got %x\n", button[i].style, style);
+
+        log_all_parent_messages--;
+
        DestroyWindow(hwnd);
     }
 
+    DestroyWindow(parent);
+
     hwnd = CreateWindowExA(0, "my_button_class", "test", BS_PUSHBUTTON | WS_POPUP | WS_VISIBLE,
                           0, 0, 50, 14, 0, 0, 0, NULL);
     ok(hwnd != 0, "Failed to create button window\n");
 
-    SetFocus(0);
+    SetForegroundWindow(hwnd);
     flush_events();
+
+    SetActiveWindow(hwnd);
+    SetFocus(0);
     flush_sequence();
 
     SendMessageA(hwnd, WM_LBUTTONDOWN, 0, 0);
@@ -4859,7 +5436,7 @@ static void test_button_messages(void)
     ok_sequence(WmLButtonUpSeq, "WM_LBUTTONUP on a button", FALSE);
 
     flush_sequence();
-    zfont = (HFONT)GetStockObject(SYSTEM_FONT);
+    zfont = GetStockObject(SYSTEM_FONT);
     SendMessageA(hwnd, WM_SETFONT, (WPARAM)zfont, TRUE);
     UpdateWindow(hwnd);
     ok_sequence(WmSetFontButtonSeq, "WM_SETFONT on a button", FALSE);
@@ -4871,9 +5448,9 @@ static void test_button_messages(void)
 static const struct message WmSetFontStaticSeq[] =
 {
     { WM_SETFONT, sent },
-    { WM_PAINT, sent|defwinproc },
+    { WM_PAINT, sent|defwinproc|optional },
     { WM_ERASEBKGND, sent|defwinproc|optional },
-    { WM_CTLCOLORSTATIC, sent|defwinproc },
+    { WM_CTLCOLORSTATIC, sent|defwinproc|optional },
     { 0 }
 };
 
@@ -4881,20 +5458,19 @@ static WNDPROC old_static_proc;
 
 static LRESULT CALLBACK static_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    static long defwndproc_counter = 0;
+    static LONG defwndproc_counter = 0;
     LRESULT ret;
-    struct message msg;
+    struct recvd_message msg;
 
-    trace("static: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
-
-    /* explicitly ignore WM_GETICON message */
-    if (message == WM_GETICON) return 0;
+    if (ignore_message( message )) return 0;
 
+    msg.hwnd = hwnd;
     msg.message = message;
     msg.flags = sent|wparam|lparam;
     if (defwndproc_counter) msg.flags |= defwinproc;
     msg.wParam = wParam;
     msg.lParam = lParam;
+    msg.descr = "static";
     add_message(&msg);
 
     defwndproc_counter++;
@@ -4977,9 +5553,9 @@ static WNDPROC old_combobox_proc;
 
 static LRESULT CALLBACK combobox_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    static long defwndproc_counter = 0;
+    static LONG defwndproc_counter = 0;
     LRESULT ret;
-    struct message msg;
+    struct recvd_message msg;
 
     /* do not log painting messages */
     if (message != WM_PAINT &&
@@ -4988,16 +5564,15 @@ static LRESULT CALLBACK combobox_hook_proc(HWND hwnd, UINT message, WPARAM wPara
         message != WM_ERASEBKGND &&
         message != WM_NCHITTEST &&
         message != WM_GETTEXT &&
-        message != WM_GETICON &&
-        message != WM_DEVICECHANGE)
+        !ignore_message( message ))
     {
-        trace("combo: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
-
+        msg.hwnd = hwnd;
         msg.message = message;
         msg.flags = sent|wparam|lparam;
         if (defwndproc_counter) msg.flags |= defwinproc;
         msg.wParam = wParam;
         msg.lParam = lParam;
+        msg.descr = "combo";
         add_message(&msg);
     }
 
@@ -5076,21 +5651,21 @@ static const struct message WmImeKeydownMsgSeq_0[] =
 
 static const struct message WmImeKeydownMsgSeq_1[] =
 {
-    { WM_KEYDOWN, wparam, VK_RETURN },
-    { WM_CHAR,    wparam, VK_RETURN },
+    { WM_KEYDOWN, optional|wparam, VK_RETURN },
+    { WM_CHAR,    optional|wparam, VK_RETURN },
     { 0 }
 };
 
 static LRESULT WINAPI wmime_keydown_procA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    struct message msg;
-
-    trace("wmime_keydown_procA: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
+    struct recvd_message msg;
 
+    msg.hwnd = hwnd;
     msg.message = message;
     msg.flags = wparam|lparam;
     msg.wParam = wParam;
     msg.lParam = lParam;
+    msg.descr = "wmime_keydown";
     add_message(&msg);
 
     return DefWindowProcA(hwnd, message, wParam, lParam);
@@ -5107,7 +5682,7 @@ static void register_wmime_keydown_class(void)
     if (!RegisterClassA(&cls)) assert(0);
 }
 
-void test_wmime_keydown_message(void)
+static void test_wmime_keydown_message(void)
 {
     HWND hwnd;
     MSG msg;
@@ -5232,7 +5807,7 @@ static const struct message WmInvalidateErasePaint[] = {
     { WM_PAINT, sent },
     { WM_NCPAINT, sent|wparam|beginpaint, 1 },
     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
-    { WM_ERASEBKGND, sent|beginpaint },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
     { 0 }
 };
 
@@ -5240,7 +5815,7 @@ static const struct message WmInvalidateErasePaint2[] = {
     { WM_PAINT, sent },
     { WM_NCPAINT, sent|beginpaint },
     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
-    { WM_ERASEBKGND, sent|beginpaint },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
     { 0 }
 };
 
@@ -5295,7 +5870,7 @@ static const struct message WmParentPaintNc[] = {
     { WM_PAINT, sent },
     { WM_NCPAINT, sent|beginpaint },
     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
-    { WM_ERASEBKGND, sent|beginpaint },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
     { 0 }
 };
 
@@ -5303,7 +5878,7 @@ static const struct message WmChildPaintNc[] = {
     { WM_PAINT, sent },
     { WM_NCPAINT, sent|beginpaint },
     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
-    { WM_ERASEBKGND, sent|beginpaint },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
     { 0 }
 };
 
@@ -5311,11 +5886,11 @@ static const struct message WmParentErasePaint[] = {
     { WM_PAINT, sent|parent },
     { WM_NCPAINT, sent|parent|beginpaint },
     { WM_GETTEXT, sent|parent|beginpaint|defwinproc|optional },
-    { WM_ERASEBKGND, sent|parent|beginpaint },
+    { WM_ERASEBKGND, sent|parent|beginpaint|optional },
     { WM_PAINT, sent },
     { WM_NCPAINT, sent|beginpaint },
     { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
-    { WM_ERASEBKGND, sent|beginpaint },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
     { 0 }
 };
 
@@ -5400,13 +5975,15 @@ static void test_paint_messages(void)
      */
     trace("testing ValidateRect(0, NULL)\n");
     SetRectEmpty( &rect );
-    ok(ValidateRect(0, &rect), "ValidateRect(0, &rc) should not fail\n");
-    check_update_rgn( hwnd, hrgn );
-    ok_sequence( WmInvalidateErase, "InvalidateErase", FALSE );
-    flush_events();
-    ok_sequence( WmPaint, "Paint", FALSE );
-    RedrawWindow( hwnd, NULL, NULL, RDW_VALIDATE );
-    check_update_rgn( hwnd, 0 );
+    if (ValidateRect(0, &rect))  /* not supported on Win9x */
+    {
+        check_update_rgn( hwnd, hrgn );
+        ok_sequence( WmInvalidateErase, "InvalidateErase", FALSE );
+        flush_events();
+        ok_sequence( WmPaint, "Paint", FALSE );
+        RedrawWindow( hwnd, NULL, NULL, RDW_VALIDATE );
+        check_update_rgn( hwnd, 0 );
+    }
 
     trace("testing InvalidateRgn(0, NULL, FALSE)\n");
     SetLastError(0xdeadbeef);
@@ -5420,7 +5997,9 @@ static void test_paint_messages(void)
     trace("testing ValidateRgn(0, NULL)\n");
     SetLastError(0xdeadbeef);
     ok(!ValidateRgn(0, NULL), "ValidateRgn(0, NULL) should fail\n");
-    ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "wrong error code %d\n", GetLastError());
+    ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE ||
+       broken( GetLastError() == 0xdeadbeef ) /* win9x */,
+       "wrong error code %d\n", GetLastError());
     check_update_rgn( hwnd, 0 );
     flush_events();
     ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
@@ -5777,10 +6356,90 @@ static void test_paint_messages(void)
     flush_events();
     ok_sequence(WmSWP_FrameChangedDeferErase, "SetWindowPos:FrameChangedDeferErase", FALSE );
 
+    ok(GetWindowLong( hparent, GWL_STYLE ) & WS_VISIBLE, "parent should be visible\n");
+    ok(GetWindowLong( hchild, GWL_STYLE ) & WS_VISIBLE, "child should be visible\n");
+
+    UpdateWindow( hparent );
+    flush_events();
+    flush_sequence();
+    trace("testing SetWindowPos(-10000, -10000) on child\n");
+    SetWindowPos( hchild, 0, -10000, -10000, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER );
+    check_update_rgn( hchild, 0 );
+    flush_events();
+
+#if 0 /* this one doesn't pass under Wine yet */
+    UpdateWindow( hparent );
+    flush_events();
+    flush_sequence();
+    trace("testing ShowWindow(SW_MINIMIZE) on child\n");
+    ShowWindow( hchild, SW_MINIMIZE );
+    check_update_rgn( hchild, 0 );
+    flush_events();
+#endif
+
+    UpdateWindow( hparent );
+    flush_events();
+    flush_sequence();
+    trace("testing SetWindowPos(-10000, -10000) on parent\n");
+    SetWindowPos( hparent, 0, -10000, -10000, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER );
+    check_update_rgn( hparent, 0 );
+    flush_events();
+
     log_all_parent_messages--;
     DestroyWindow( hparent );
     ok(!IsWindow(hchild), "child must be destroyed with its parent\n");
 
+    /* tests for moving windows off-screen (needs simple WS_POPUP windows) */
+
+    hparent = CreateWindowExA(0, "TestParentClass", "Test parent", WS_POPUP | WS_VISIBLE,
+                              100, 100, 200, 200, 0, 0, 0, NULL);
+    ok (hparent != 0, "Failed to create parent window\n");
+
+    hchild = CreateWindowExA(0, "TestWindowClass", "Test child", WS_CHILD | WS_VISIBLE,
+                           10, 10, 100, 100, hparent, 0, 0, NULL);
+    ok (hchild != 0, "Failed to create child window\n");
+
+    ShowWindow( hparent, SW_SHOW );
+    UpdateWindow( hparent );
+    UpdateWindow( hchild );
+    flush_events();
+    flush_sequence();
+
+    /* moving child outside of parent boundaries changes update region */
+    SetRect( &rect, 0, 0, 40, 40 );
+    RedrawWindow( hchild, &rect, 0, RDW_INVALIDATE | RDW_ERASE );
+    SetRectRgn( hrgn, 0, 0, 40, 40 );
+    check_update_rgn( hchild, hrgn );
+    MoveWindow( hchild, -10, 10, 100, 100, FALSE );
+    SetRectRgn( hrgn, 10, 0, 40, 40 );
+    check_update_rgn( hchild, hrgn );
+    MoveWindow( hchild, -10, -10, 100, 100, FALSE );
+    SetRectRgn( hrgn, 10, 10, 40, 40 );
+    check_update_rgn( hchild, hrgn );
+
+    /* moving parent off-screen does too */
+    SetRect( &rect, 0, 0, 100, 100 );
+    RedrawWindow( hparent, &rect, 0, RDW_INVALIDATE | RDW_ERASE | RDW_NOCHILDREN );
+    SetRectRgn( hrgn, 0, 0, 100, 100 );
+    check_update_rgn( hparent, hrgn );
+    SetRectRgn( hrgn, 10, 10, 40, 40 );
+    check_update_rgn( hchild, hrgn );
+    MoveWindow( hparent, -20, -20, 200, 200, FALSE );
+    SetRectRgn( hrgn, 20, 20, 100, 100 );
+    check_update_rgn( hparent, hrgn );
+    SetRectRgn( hrgn, 30, 30, 40, 40 );
+    check_update_rgn( hchild, hrgn );
+
+    /* invalidated region is cropped by the parent rects */
+    SetRect( &rect, 0, 0, 50, 50 );
+    RedrawWindow( hchild, &rect, 0, RDW_INVALIDATE | RDW_ERASE );
+    SetRectRgn( hrgn, 30, 30, 50, 50 );
+    check_update_rgn( hchild, hrgn );
+
+    DestroyWindow( hparent );
+    ok(!IsWindow(hchild), "child must be destroyed with its parent\n");
+    flush_sequence();
+
     DeleteObject( hrgn );
     DeleteObject( hrgn2 );
 }
@@ -5788,19 +6447,21 @@ static void test_paint_messages(void)
 struct wnd_event
 {
     HWND hwnd;
-    HANDLE event;
+    HANDLE grand_child;
+    HANDLE start_event;
+    HANDLE stop_event;
 };
 
 static DWORD WINAPI thread_proc(void *param)
 {
     MSG msg;
-    struct wnd_event *wnd_event = (struct wnd_event *)param;
+    struct wnd_event *wnd_event = param;
 
     wnd_event->hwnd = CreateWindowExA(0, "TestWindowClass", "window caption text", WS_OVERLAPPEDWINDOW,
                                       100, 100, 200, 200, 0, 0, 0, NULL);
     ok(wnd_event->hwnd != 0, "Failed to create overlapped window\n");
 
-    SetEvent(wnd_event->event);
+    SetEvent(wnd_event->start_event);
 
     while (GetMessage(&msg, 0, 0, 0))
     {
@@ -5813,6 +6474,54 @@ static DWORD WINAPI thread_proc(void *param)
     return 0;
 }
 
+static DWORD CALLBACK create_grand_child_thread( void *param )
+{
+    struct wnd_event *wnd_event = param;
+    HWND hchild;
+    MSG msg;
+
+    hchild = CreateWindowExA(0, "TestWindowClass", "Test child",
+                             WS_CHILD | WS_VISIBLE, 0, 0, 10, 10, wnd_event->hwnd, 0, 0, NULL);
+    ok (hchild != 0, "Failed to create child window\n");
+    flush_events();
+    flush_sequence();
+    SetEvent( wnd_event->start_event );
+
+    for (;;)
+    {
+        MsgWaitForMultipleObjects(0, NULL, FALSE, 1000, QS_ALLINPUT);
+        if (!IsWindow( hchild )) break;  /* will be destroyed when parent thread exits */
+        while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
+    }
+    return 0;
+}
+
+static DWORD CALLBACK create_child_thread( void *param )
+{
+    struct wnd_event *wnd_event = param;
+    struct wnd_event child_event;
+    DWORD ret, tid;
+    MSG msg;
+
+    child_event.hwnd = CreateWindowExA(0, "TestWindowClass", "Test child",
+                             WS_CHILD | WS_VISIBLE, 0, 0, 10, 10, wnd_event->hwnd, 0, 0, NULL);
+    ok (child_event.hwnd != 0, "Failed to create child window\n");
+    SetFocus( child_event.hwnd );
+    flush_events();
+    flush_sequence();
+    child_event.start_event = wnd_event->start_event;
+    wnd_event->grand_child = CreateThread(NULL, 0, create_grand_child_thread, &child_event, 0, &tid);
+    for (;;)
+    {
+        DWORD ret = MsgWaitForMultipleObjects(1, &child_event.start_event, FALSE, 1000, QS_SENDMESSAGE);
+        if (ret != 1) break;
+        while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
+    }
+    ret = WaitForSingleObject( wnd_event->stop_event, 5000 );
+    ok( !ret, "WaitForSingleObject failed %x\n", ret );
+    return 0;
+}
+
 static void test_interthread_messages(void)
 {
     HANDLE hThread;
@@ -5824,19 +6533,19 @@ static void test_interthread_messages(void)
     struct wnd_event wnd_event;
     BOOL ret;
 
-    wnd_event.event = CreateEventW(NULL, 0, 0, NULL);
-    if (!wnd_event.event)
+    wnd_event.start_event = CreateEventW(NULL, 0, 0, NULL);
+    if (!wnd_event.start_event)
     {
-        trace("skipping interthread message test under win9x\n");
+        win_skip("skipping interthread message test under win9x\n");
         return;
     }
 
     hThread = CreateThread(NULL, 0, thread_proc, &wnd_event, 0, &tid);
     ok(hThread != NULL, "CreateThread failed, error %d\n", GetLastError());
 
-    ok(WaitForSingleObject(wnd_event.event, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
+    ok(WaitForSingleObject(wnd_event.start_event, INFINITE) == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
 
-    CloseHandle(wnd_event.event);
+    CloseHandle(wnd_event.start_event);
 
     SetLastError(0xdeadbeef);
     ok(!DestroyWindow(wnd_event.hwnd), "DestroyWindow succeded\n");
@@ -5860,7 +6569,7 @@ static void test_interthread_messages(void)
     memset(buf, 0, sizeof(buf));
     SetLastError(0xdeadbeef);
     len = DispatchMessageA(&msg);
-    ok(!len && GetLastError() == ERROR_MESSAGE_SYNC_ONLY,
+    ok((!len && GetLastError() == ERROR_MESSAGE_SYNC_ONLY) || broken(len), /* nt4 */
        "DispatchMessageA(WM_GETTEXT) succeded on another thread window: ret %d, error %d\n", len, GetLastError());
 
     /* the following test causes an exception in user.exe under win9x */
@@ -5880,6 +6589,38 @@ static void test_interthread_messages(void)
     CloseHandle(hThread);
 
     ok(!IsWindow(wnd_event.hwnd), "window should be destroyed on thread exit\n");
+
+    wnd_event.hwnd = CreateWindowExA(0, "TestParentClass", "Test parent", WS_OVERLAPPEDWINDOW | WS_VISIBLE,
+                              100, 100, 200, 200, 0, 0, 0, NULL);
+    ok (wnd_event.hwnd != 0, "Failed to create parent window\n");
+    flush_sequence();
+    log_all_parent_messages++;
+    wnd_event.start_event = CreateEventA( NULL, TRUE, FALSE, NULL );
+    wnd_event.stop_event = CreateEventA( NULL, TRUE, FALSE, NULL );
+    hThread = CreateThread( NULL, 0, create_child_thread, &wnd_event, 0, &tid );
+    for (;;)
+    {
+        ret = MsgWaitForMultipleObjects(1, &wnd_event.start_event, FALSE, 1000, QS_SENDMESSAGE);
+        if (ret != 1) break;
+        while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
+    }
+    ok( !ret, "MsgWaitForMultipleObjects failed %x\n", ret );
+    /* now wait for the thread without processing messages; this shouldn't deadlock */
+    SetEvent( wnd_event.stop_event );
+    ret = WaitForSingleObject( hThread, 5000 );
+    ok( !ret, "WaitForSingleObject failed %x\n", ret );
+    CloseHandle( hThread );
+
+    ret = WaitForSingleObject( wnd_event.grand_child, 5000 );
+    ok( !ret, "WaitForSingleObject failed %x\n", ret );
+    CloseHandle( wnd_event.grand_child );
+
+    CloseHandle( wnd_event.start_event );
+    CloseHandle( wnd_event.stop_event );
+    flush_events();
+    ok_sequence(WmExitThreadSeq, "destroy child on thread exit", FALSE);
+    log_all_parent_messages--;
+    DestroyWindow( wnd_event.hwnd );
 }
 
 
@@ -6001,6 +6742,8 @@ static const struct message WmCtrlAltVkN[] = {
     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0x20000001 }, /* XP */
     { WM_KEYDOWN, wparam|lparam, 'N', 0x20000001 },
     { WM_KEYDOWN, sent|wparam|lparam, 'N', 0x20000001 },
+    { WM_CHAR, optional },
+    { WM_CHAR, sent|optional },
     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xe0000001 }, /* XP */
     { WM_KEYUP, wparam|lparam, 'N', 0xe0000001 },
     { WM_KEYUP, sent|wparam|lparam, 'N', 0xe0000001 },
@@ -6089,19 +6832,19 @@ static const struct message WmAltPressRelease[] = {
     { WM_SYSKEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
     { 0 }
 };
-static const struct message WmAltMouseButton[] = {
-    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 }, /* XP */
-    { WM_SYSKEYDOWN, wparam|lparam, VK_MENU, 0x20000001 },
-    { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
+static const struct message WmShiftMouseButton[] = {
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 1 }, /* XP */
+    { WM_KEYDOWN, wparam|lparam, VK_SHIFT, 1 },
+    { WM_KEYDOWN, sent|wparam|lparam, VK_SHIFT, 1 },
     { WM_MOUSEMOVE, wparam|optional, 0, 0 },
     { WM_MOUSEMOVE, sent|wparam|optional, 0, 0 },
-    { WM_LBUTTONDOWN, wparam, MK_LBUTTON, 0 },
-    { WM_LBUTTONDOWN, sent|wparam, MK_LBUTTON, 0 },
-    { WM_LBUTTONUP, wparam, 0, 0 },
-    { WM_LBUTTONUP, sent|wparam, 0, 0 },
-    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xc0000001 }, /* XP */
-    { WM_SYSKEYUP, wparam|lparam, VK_MENU, 0xc0000001 },
-    { WM_SYSKEYUP, sent|wparam|lparam, VK_MENU, 0xc0000001 },
+    { WM_LBUTTONDOWN, wparam, MK_LBUTTON|MK_SHIFT, 0 },
+    { WM_LBUTTONDOWN, sent|wparam, MK_LBUTTON|MK_SHIFT, 0 },
+    { WM_LBUTTONUP, wparam, MK_SHIFT, 0 },
+    { WM_LBUTTONUP, sent|wparam, MK_SHIFT, 0 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_SHIFT, 0xc0000001 }, /* XP */
+    { WM_KEYUP, wparam|lparam, VK_SHIFT, 0xc0000001 },
+    { WM_KEYUP, sent|wparam|lparam, VK_SHIFT, 0xc0000001 },
     { 0 }
 };
 static const struct message WmF1Seq[] = {
@@ -6127,6 +6870,36 @@ static const struct message WmVkAppsSeq[] = {
     { WM_CONTEXTMENU, sent|lparam, /*hwnd*/0, (LPARAM)-1 },
     { 0 }
 };
+static const struct message WmVkF10Seq[] = {
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 1 }, /* XP */
+    { WM_SYSKEYDOWN, wparam|lparam, VK_F10, 1 },
+    { WM_SYSKEYDOWN, sent|wparam|lparam, VK_F10, 0x00000001 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 0xc0000001 }, /* XP */
+    { WM_SYSKEYUP, wparam|lparam, VK_F10, 0xc0000001 },
+    { WM_SYSKEYUP, sent|wparam|lparam, VK_F10, 0xc0000001 },
+    { WM_SYSCOMMAND, sent|defwinproc|wparam, SC_KEYMENU },
+    { HCBT_SYSCOMMAND, hook },
+    { WM_ENTERMENULOOP, sent|defwinproc|wparam|lparam, 0, 0 },
+    { EVENT_SYSTEM_CAPTURESTART, winevent_hook|wparam|lparam, 0, 0 },
+    { WM_INITMENU, sent|defwinproc },
+    { EVENT_SYSTEM_MENUSTART, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
+    { WM_MENUSELECT, sent|defwinproc|wparam, MAKEWPARAM(0,MF_SYSMENU|MF_POPUP|MF_HILITE) },
+    { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_SYSMENU, 1 },
+
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 0x10000001 }, /* XP */
+
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 1 }, /* XP */
+    { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
+    { EVENT_SYSTEM_CAPTUREEND, winevent_hook|wparam|lparam, 0, 0, },
+    { WM_CAPTURECHANGED, sent|defwinproc },
+    { WM_MENUSELECT, sent|defwinproc|wparam|optional, MAKEWPARAM(0,0xffff) },
+    { EVENT_SYSTEM_MENUEND, winevent_hook|wparam|lparam, OBJID_SYSMENU, 0 },
+    { WM_EXITMENULOOP, sent|defwinproc },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_F10, 0xc0000001 }, /* XP */
+    { WM_SYSKEYUP, wparam|lparam, VK_F10, 0xc0000001 },
+    { WM_SYSKEYUP, sent|wparam|lparam, VK_F10, 0xc0000001 },
+    { 0 }
+};
 
 static void pump_msg_loop(HWND hwnd, HACCEL hAccel)
 {
@@ -6134,20 +6907,20 @@ static void pump_msg_loop(HWND hwnd, HACCEL hAccel)
 
     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE))
     {
-        struct message log_msg;
-
-        trace("accel: %p, %04x, %08lx, %08lx\n", msg.hwnd, msg.message, msg.wParam, msg.lParam);
+        struct recvd_message log_msg;
 
         /* ignore some unwanted messages */
         if (msg.message == WM_MOUSEMOVE ||
-            msg.message == WM_GETICON ||
-            msg.message == WM_DEVICECHANGE)
+            msg.message == WM_TIMER ||
+            ignore_message( msg.message ))
             continue;
 
+        log_msg.hwnd = msg.hwnd;
         log_msg.message = msg.message;
         log_msg.flags = wparam|lparam;
         log_msg.wParam = msg.wParam;
         log_msg.lParam = msg.lParam;
+        log_msg.descr = "accel";
         add_message(&log_msg);
 
         if (!hAccel || !TranslateAccelerator(hwnd, hAccel, &msg))
@@ -6161,6 +6934,7 @@ static void pump_msg_loop(HWND hwnd, HACCEL hAccel)
 static void test_accelerators(void)
 {
     RECT rc;
+    POINT pt;
     SHORT state;
     HACCEL hAccel;
     HWND hwnd = CreateWindowExA(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
@@ -6183,6 +6957,7 @@ static void test_accelerators(void)
     hAccel = LoadAccelerators(GetModuleHandleA(0), MAKEINTRESOURCE(1));
     assert(hAccel != 0);
 
+    flush_events();
     pump_msg_loop(hwnd, 0);
     flush_sequence();
 
@@ -6191,6 +6966,11 @@ static void test_accelerators(void)
     keybd_event('N', 0, 0, 0);
     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
     pump_msg_loop(hwnd, hAccel);
+    if (!sequence_cnt)  /* we didn't get any message */
+    {
+        skip( "queuing key events not supported\n" );
+        goto done;
+    }
     ok_sequence(WmVkN, "VK_N press/release", FALSE);
 
     trace("testing Shift+VK_N press/release\n");
@@ -6308,6 +7088,7 @@ static void test_accelerators(void)
 
     ret = DestroyAcceleratorTable(hAccel);
     ok( ret, "DestroyAcceleratorTable error %d\n", GetLastError());
+    hAccel = 0;
 
     trace("testing Alt press/release\n");
     flush_sequence();
@@ -6319,23 +7100,6 @@ static void test_accelerators(void)
     /* this test doesn't pass in Wine for managed windows */
     ok_sequence(WmAltPressRelease, "Alt press/release", TRUE);
 
-    trace("testing Alt+MouseButton press/release\n");
-    /* first, move mouse pointer inside of the window client area */
-    GetClientRect(hwnd, &rc);
-    MapWindowPoints(hwnd, 0, (LPPOINT)&rc, 2);
-    rc.left += (rc.right - rc.left)/2;
-    rc.top += (rc.bottom - rc.top)/2;
-    SetCursorPos(rc.left, rc.top);
-
-    flush_events();
-    flush_sequence();
-    keybd_event(VK_MENU, 0, 0, 0);
-    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
-    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
-    keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
-    pump_msg_loop(hwnd, 0);
-    ok_sequence(WmAltMouseButton, "Alt+MouseButton press/release", FALSE);
-
     trace("testing VK_F1 press/release\n");
     keybd_event(VK_F1, 0, 0, 0);
     keybd_event(VK_F1, 0, KEYEVENTF_KEYUP, 0);
@@ -6348,23 +7112,57 @@ static void test_accelerators(void)
     pump_msg_loop(hwnd, 0);
     ok_sequence(WmVkAppsSeq, "VK_APPS press/release", FALSE);
 
-    DestroyWindow(hwnd);
-}
-
-/************* window procedures ********************/
+    trace("testing VK_F10 press/release\n");
+    keybd_event(VK_F10, 0, 0, 0);
+    keybd_event(VK_F10, 0, KEYEVENTF_KEYUP, 0);
+    keybd_event(VK_F10, 0, 0, 0);
+    keybd_event(VK_F10, 0, KEYEVENTF_KEYUP, 0);
+    pump_msg_loop(hwnd, 0);
+    ok_sequence(WmVkF10Seq, "VK_F10 press/release", TRUE);
 
-static LRESULT MsgCheckProc (BOOL unicode, HWND hwnd, UINT message, 
+    trace("testing Shift+MouseButton press/release\n");
+    /* first, move mouse pointer inside of the window client area */
+    GetClientRect(hwnd, &rc);
+    MapWindowPoints(hwnd, 0, (LPPOINT)&rc, 2);
+    rc.left += (rc.right - rc.left)/2;
+    rc.top += (rc.bottom - rc.top)/2;
+    SetCursorPos(rc.left, rc.top);
+    SetActiveWindow(hwnd);
+
+    flush_events();
+    flush_sequence();
+    GetCursorPos(&pt);
+    if (pt.x == rc.left && pt.y == rc.top)
+    {
+        int i;
+        keybd_event(VK_SHIFT, 0, 0, 0);
+        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
+        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
+        keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
+        pump_msg_loop(hwnd, 0);
+        for (i = 0; i < sequence_cnt; i++) if (sequence[i].message == WM_LBUTTONDOWN) break;
+        if (i < sequence_cnt)
+            ok_sequence(WmShiftMouseButton, "Shift+MouseButton press/release", FALSE);
+        else
+            skip( "Shift+MouseButton event didn't get to the window\n" );
+    }
+
+done:
+    if (hAccel) DestroyAcceleratorTable(hAccel);
+    DestroyWindow(hwnd);
+}
+
+/************* window procedures ********************/
+
+static LRESULT MsgCheckProc (BOOL unicode, HWND hwnd, UINT message, 
                             WPARAM wParam, LPARAM lParam)
 {
-    static long defwndproc_counter = 0;
-    static long beginpaint_counter = 0;
+    static LONG defwndproc_counter = 0;
+    static LONG beginpaint_counter = 0;
     LRESULT ret;
-    struct message msg;
-
-    trace("%p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
+    struct recvd_message msg;
 
-    /* explicitly ignore WM_GETICON message */
-    if (message == WM_GETICON) return 0;
+    if (ignore_message( message )) return 0;
 
     switch (message)
     {
@@ -6421,40 +7219,24 @@ static LRESULT MsgCheckProc (BOOL unicode, HWND hwnd, UINT message,
        /* test_accelerators() depends on this */
        case WM_NCHITTEST:
            return HTCLIENT;
-    
+
        /* ignore */
        case WM_MOUSEMOVE:
+       case WM_MOUSEACTIVATE:
+       case WM_NCMOUSEMOVE:
        case WM_SETCURSOR:
-       case WM_DEVICECHANGE:
+       case WM_IME_SELECT:
            return 0;
-
-        case WM_WINDOWPOSCHANGING:
-        case WM_WINDOWPOSCHANGED:
-        {
-            WINDOWPOS *winpos = (WINDOWPOS *)lParam;
-
-            trace("%s\n", (message == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
-            trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
-                  winpos->hwnd, winpos->hwndInsertAfter,
-                  winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
-            dump_winpos_flags(winpos->flags);
-
-            /* Log only documented flags, win2k uses 0x1000 and 0x2000
-             * in the high word for internal purposes
-             */
-            wParam = winpos->flags & 0xffff;
-            /* We are not interested in the flags that don't match under XP and Win9x */
-            wParam &= ~(SWP_NOZORDER);
-            break;
-        }
     }
 
+    msg.hwnd = hwnd;
     msg.message = message;
     msg.flags = sent|wparam|lparam;
     if (defwndproc_counter) msg.flags |= defwinproc;
     if (beginpaint_counter) msg.flags |= beginpaint;
     msg.wParam = wParam;
     msg.lParam = lParam;
+    msg.descr = "MsgCheckProc";
     add_message(&msg);
 
     if (message == WM_GETMINMAXINFO && (GetWindowLongA(hwnd, GWL_STYLE) & WS_CHILD))
@@ -6465,17 +7247,12 @@ static LRESULT MsgCheckProc (BOOL unicode, HWND hwnd, UINT message,
 
        GetClientRect(parent, &rc);
        trace("parent %p client size = (%d x %d)\n", parent, rc.right, rc.bottom);
-
-       trace("ptReserved = (%d,%d)\n"
-              "ptMaxSize = (%d,%d)\n"
-              "ptMaxPosition = (%d,%d)\n"
-              "ptMinTrackSize = (%d,%d)\n"
-              "ptMaxTrackSize = (%d,%d)\n",
-             minmax->ptReserved.x, minmax->ptReserved.y,
-             minmax->ptMaxSize.x, minmax->ptMaxSize.y,
-             minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
-             minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
-             minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
+        trace("Reserved=%d,%d MaxSize=%d,%d MaxPos=%d,%d MinTrack=%d,%d MaxTrack=%d,%d\n",
+              minmax->ptReserved.x, minmax->ptReserved.y,
+              minmax->ptMaxSize.x, minmax->ptMaxSize.y,
+              minmax->ptMaxPosition.x, minmax->ptMaxPosition.y,
+              minmax->ptMinTrackSize.x, minmax->ptMinTrackSize.y,
+              minmax->ptMaxTrackSize.x, minmax->ptMaxTrackSize.y);
 
        ok(minmax->ptMaxSize.x == rc.right, "default width of maximized child %d != %d\n",
           minmax->ptMaxSize.x, rc.right);
@@ -6513,20 +7290,27 @@ static LRESULT WINAPI MsgCheckProcW(HWND hwnd, UINT message, WPARAM wParam, LPAR
 
 static LRESULT WINAPI PopupMsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    static long defwndproc_counter = 0;
+    static LONG defwndproc_counter = 0;
     LRESULT ret;
-    struct message msg;
+    struct recvd_message msg;
 
-    trace("popup: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
+    if (ignore_message( message )) return 0;
 
-    /* explicitly ignore WM_GETICON message */
-    if (message == WM_GETICON) return 0;
+    switch (message)
+    {
+    case WM_QUERYENDSESSION:
+    case WM_ENDSESSION:
+        lParam &= ~0x01;  /* Vista adds a 0x01 flag */
+        break;
+    }
 
+    msg.hwnd = hwnd;
     msg.message = message;
     msg.flags = sent|wparam|lparam;
     if (defwndproc_counter) msg.flags |= defwinproc;
     msg.wParam = wParam;
     msg.lParam = lParam;
+    msg.descr = "popup";
     add_message(&msg);
 
     if (message == WM_CREATE)
@@ -6544,23 +7328,18 @@ static LRESULT WINAPI PopupMsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam,
 
 static LRESULT WINAPI ParentMsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    static long defwndproc_counter = 0;
-    static long beginpaint_counter = 0;
+    static LONG defwndproc_counter = 0;
+    static LONG beginpaint_counter = 0;
     LRESULT ret;
-    struct message msg;
-    LPARAM logged_lParam;
+    struct recvd_message msg;
 
-    trace("parent: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
+    if (ignore_message( message )) return 0;
 
-    /* explicitly ignore WM_GETICON message */
-    if (message == WM_GETICON) return 0;
-
-    logged_lParam=lParam;
     if (log_all_parent_messages ||
         message == WM_PARENTNOTIFY || message == WM_CANCELMODE ||
        message == WM_SETFOCUS || message == WM_KILLFOCUS ||
        message == WM_ENABLE || message == WM_ENTERIDLE ||
-        message == WM_DRAWITEM ||
+       message == WM_DRAWITEM || message == WM_COMMAND ||
        message == WM_IME_SETCONTEXT)
     {
         switch (message)
@@ -6570,6 +7349,7 @@ static LRESULT WINAPI ParentMsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam
                 return HTCLIENT;
             case WM_SETCURSOR:
             case WM_MOUSEMOVE:
+            case WM_NCMOUSEMOVE:
                 return 0;
 
             case WM_ERASEBKGND:
@@ -6581,53 +7361,16 @@ static LRESULT WINAPI ParentMsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam
                        ret, rc.left, rc.top, rc.right, rc.bottom);
                 break;
             }
-
-            case WM_WINDOWPOSCHANGING:
-            case WM_WINDOWPOSCHANGED:
-            {
-                WINDOWPOS *winpos = (WINDOWPOS *)lParam;
-
-                trace("%s\n", (message == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
-                trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
-                      winpos->hwnd, winpos->hwndInsertAfter,
-                      winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
-                dump_winpos_flags(winpos->flags);
-
-                /* Log only documented flags, win2k uses 0x1000 and 0x2000
-                 * in the high word for internal purposes
-                 */
-                wParam = winpos->flags & 0xffff;
-                /* We are not interested in the flags that don't match under XP and Win9x */
-                wParam &= ~(SWP_NOZORDER);
-                break;
-            }
-
-            case WM_DRAWITEM:
-            {
-                /* encode DRAWITEMSTRUCT into an LPARAM */
-                DRAW_ITEM_STRUCT di;
-                DRAWITEMSTRUCT *dis = (DRAWITEMSTRUCT *)lParam;
-
-                trace("WM_DRAWITEM: type %x, ctl_id %x, item_id %x, action %x, state %x\n",
-                      dis->CtlType, dis->CtlID, dis->itemID, dis->itemAction, dis->itemState);
-
-                di.u.item.type = dis->CtlType;
-                di.u.item.ctl_id = dis->CtlID;
-                di.u.item.item_id = dis->itemID;
-                di.u.item.action = dis->itemAction;
-                di.u.item.state = dis->itemState;
-
-                logged_lParam = di.u.lp;
-                break;
-            }
         }
 
+        msg.hwnd = hwnd;
         msg.message = message;
         msg.flags = sent|parent|wparam|lparam;
         if (defwndproc_counter) msg.flags |= defwinproc;
         if (beginpaint_counter) msg.flags |= beginpaint;
         msg.wParam = wParam;
-        msg.lParam = logged_lParam;
+        msg.lParam = lParam;
+        msg.descr = "parent";
         add_message(&msg);
     }
 
@@ -6650,14 +7393,11 @@ static LRESULT WINAPI ParentMsgCheckProcA(HWND hwnd, UINT message, WPARAM wParam
 
 static LRESULT WINAPI TestDlgProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    static long defwndproc_counter = 0;
+    static LONG defwndproc_counter = 0;
     LRESULT ret;
-    struct message msg;
-
-    trace("dialog: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
+    struct recvd_message msg;
 
-    /* explicitly ignore WM_GETICON message */
-    if (message == WM_GETICON) return 0;
+    if (ignore_message( message )) return 0;
 
     if (test_def_id)
     {
@@ -6669,34 +7409,13 @@ static LRESULT WINAPI TestDlgProcA(HWND hwnd, UINT message, WPARAM wParam, LPARA
             ok(HIWORD(ret) == DC_HASDEFID, "DM_GETDEFID should return DC_HASDEFID, got %lx\n", ret);
     }
 
-    switch (message)
-    {
-        case WM_WINDOWPOSCHANGING:
-        case WM_WINDOWPOSCHANGED:
-        {
-            WINDOWPOS *winpos = (WINDOWPOS *)lParam;
-
-            trace("%s\n", (message == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
-            trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
-                  winpos->hwnd, winpos->hwndInsertAfter,
-                  winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
-            dump_winpos_flags(winpos->flags);
-
-            /* Log only documented flags, win2k uses 0x1000 and 0x2000
-             * in the high word for internal purposes
-             */
-            wParam = winpos->flags & 0xffff;
-            /* We are not interested in the flags that don't match under XP and Win9x */
-            wParam &= ~(SWP_NOZORDER);
-            break;
-        }
-    }
-
+    msg.hwnd = hwnd;
     msg.message = message;
     msg.flags = sent|wparam|lparam;
     if (defwndproc_counter) msg.flags |= defwinproc;
     msg.wParam = wParam;
     msg.lParam = lParam;
+    msg.descr = "dialog";
     add_message(&msg);
 
     defwndproc_counter++;
@@ -6706,53 +7425,11 @@ static LRESULT WINAPI TestDlgProcA(HWND hwnd, UINT message, WPARAM wParam, LPARA
     return ret;
 }
 
-static void dump_winpos_flags(UINT flags)
-{
-    if (!winetest_debug) return;
-
-    if (flags & SWP_SHOWWINDOW) printf("|SWP_SHOWWINDOW");
-    if (flags & SWP_HIDEWINDOW) printf("|SWP_HIDEWINDOW");
-    if (flags & SWP_NOACTIVATE) printf("|SWP_NOACTIVATE");
-    if (flags & SWP_FRAMECHANGED) printf("|SWP_FRAMECHANGED");
-    if (flags & SWP_NOCOPYBITS) printf("|SWP_NOCOPYBITS");
-    if (flags & SWP_NOOWNERZORDER) printf("|SWP_NOOWNERZORDER");
-    if (flags & SWP_NOSENDCHANGING) printf("|SWP_NOSENDCHANGING");
-    if (flags & SWP_DEFERERASE) printf("|SWP_DEFERERASE");
-    if (flags & SWP_ASYNCWINDOWPOS) printf("|SWP_ASYNCWINDOWPOS");
-    if (flags & SWP_NOZORDER) printf("|SWP_NOZORDER");
-    if (flags & SWP_NOREDRAW) printf("|SWP_NOREDRAW");
-    if (flags & SWP_NOSIZE) printf("|SWP_NOSIZE");
-    if (flags & SWP_NOMOVE) printf("|SWP_NOMOVE");
-    if (flags & SWP_NOCLIENTSIZE) printf("|SWP_NOCLIENTSIZE");
-    if (flags & SWP_NOCLIENTMOVE) printf("|SWP_NOCLIENTMOVE");
-
-#define DUMPED_FLAGS \
-    (SWP_NOSIZE | \
-    SWP_NOMOVE | \
-    SWP_NOZORDER | \
-    SWP_NOREDRAW | \
-    SWP_NOACTIVATE | \
-    SWP_FRAMECHANGED | \
-    SWP_SHOWWINDOW | \
-    SWP_HIDEWINDOW | \
-    SWP_NOCOPYBITS | \
-    SWP_NOOWNERZORDER | \
-    SWP_NOSENDCHANGING | \
-    SWP_DEFERERASE | \
-    SWP_ASYNCWINDOWPOS | \
-    SWP_NOCLIENTSIZE | \
-    SWP_NOCLIENTMOVE)
-
-    if(flags & ~DUMPED_FLAGS) printf("|0x%04x", flags & ~DUMPED_FLAGS);
-    printf("\n");
-#undef DUMPED_FLAGS
-}
-
 static LRESULT WINAPI ShowWindowProcA(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    static long defwndproc_counter = 0;
+    static LONG defwndproc_counter = 0;
     LRESULT ret;
-    struct message msg;
+    struct recvd_message msg;
 
     /* log only specific messages we are interested in */
     switch (message)
@@ -6763,49 +7440,25 @@ static LRESULT WINAPI ShowWindowProcA(HWND hwnd, UINT message, WPARAM wParam, LP
     case WM_KILLFOCUS:
 #endif
     case WM_SHOWWINDOW:
-        trace("WM_SHOWWINDOW %ld\n", wParam);
-        break;
     case WM_SIZE:
-        trace("WM_SIZE %ld\n", wParam);
-        break;
     case WM_MOVE:
-        trace("WM_MOVE\n");
-        break;
     case WM_GETMINMAXINFO:
-        trace("WM_GETMINMAXINFO\n");
-        break;
-
     case WM_WINDOWPOSCHANGING:
     case WM_WINDOWPOSCHANGED:
-    {
-        WINDOWPOS *winpos = (WINDOWPOS *)lParam;
-
-        trace("%s\n", (message == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
-        trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
-              winpos->hwnd, winpos->hwndInsertAfter,
-              winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
-        trace("flags: ");
-        dump_winpos_flags(winpos->flags);
-
-        /* Log only documented flags, win2k uses 0x1000 and 0x2000
-         * in the high word for internal purposes
-         */
-        wParam = winpos->flags & 0xffff;
-        /* We are not interested in the flags that don't match under XP and Win9x */
-        wParam &= ~(SWP_NOZORDER);
         break;
-    }
 
     default: /* ignore */
         /*trace("showwindow: %p, %04x, %08x, %08lx\n", hwnd, message, wParam, lParam);*/
         return DefWindowProcA(hwnd, message, wParam, lParam);
     }
 
+    msg.hwnd = hwnd;
     msg.message = message;
     msg.flags = sent|wparam|lparam;
     if (defwndproc_counter) msg.flags |= defwinproc;
     msg.wParam = wParam;
     msg.lParam = lParam;
+    msg.descr = "show";
     add_message(&msg);
 
     defwndproc_counter++;
@@ -6815,6 +7468,35 @@ static LRESULT WINAPI ShowWindowProcA(HWND hwnd, UINT message, WPARAM wParam, LP
     return ret;
 }
 
+static LRESULT WINAPI PaintLoopProcA(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
+{
+    switch (msg)
+    {
+        case WM_CREATE: return 0;
+        case WM_PAINT:
+        {
+            MSG msg2;
+            static int i = 0;
+
+            if (i < 256)
+            {
+                i++;
+                if (PeekMessageA(&msg2, 0, 0, 0, 1))
+                {
+                    TranslateMessage(&msg2);
+                    DispatchMessage(&msg2);
+                }
+                i--;
+            }
+            else ok(broken(1), "infinite loop\n");
+            if ( i == 0)
+                paint_loop_done = 1;
+            return DefWindowProcA(hWnd,msg,wParam,lParam);
+        }
+    }
+    return DefWindowProcA(hWnd,msg,wParam,lParam);
+}
+
 static BOOL RegisterWindowClasses(void)
 {
     WNDCLASSA cls;
@@ -6826,7 +7508,7 @@ static BOOL RegisterWindowClasses(void)
     cls.cbWndExtra = 0;
     cls.hInstance = GetModuleHandleA(0);
     cls.hIcon = 0;
-    cls.hCursor = LoadCursorA(0, (LPSTR)IDC_ARROW);
+    cls.hCursor = LoadCursorA(0, IDC_ARROW);
     cls.hbrBackground = GetStockObject(WHITE_BRUSH);
     cls.lpszMenuName = NULL;
     cls.lpszClassName = "TestWindowClass";
@@ -6848,6 +7530,10 @@ static BOOL RegisterWindowClasses(void)
     cls.lpszClassName = "SimpleWindowClass";
     if(!RegisterClassA(&cls)) return FALSE;
 
+    cls.lpfnWndProc = PaintLoopProcA;
+    cls.lpszClassName = "PaintLoopWindowClass";
+    if(!RegisterClassA(&cls)) return FALSE;
+
     cls.style = CS_NOCLOSE;
     cls.lpszClassName = "NoCloseWindowClass";
     if(!RegisterClassA(&cls)) return FALSE;
@@ -6875,27 +7561,37 @@ static BOOL RegisterWindowClasses(void)
     return TRUE;
 }
 
-static HHOOK hCBT_hook;
-static DWORD cbt_hook_thread_id;
+static BOOL is_our_logged_class(HWND hwnd)
+{
+    char buf[256];
+
+    if (GetClassNameA(hwnd, buf, sizeof(buf)))
+    {
+       if (!lstrcmpiA(buf, "TestWindowClass") ||
+           !lstrcmpiA(buf, "ShowWindowClass") ||
+           !lstrcmpiA(buf, "TestParentClass") ||
+           !lstrcmpiA(buf, "TestPopupClass") ||
+           !lstrcmpiA(buf, "SimpleWindowClass") ||
+           !lstrcmpiA(buf, "TestDialogClass") ||
+           !lstrcmpiA(buf, "MDI_frame_class") ||
+           !lstrcmpiA(buf, "MDI_client_class") ||
+           !lstrcmpiA(buf, "MDI_child_class") ||
+           !lstrcmpiA(buf, "my_button_class") ||
+           !lstrcmpiA(buf, "my_edit_class") ||
+           !lstrcmpiA(buf, "static") ||
+           !lstrcmpiA(buf, "ListBox") ||
+           !lstrcmpiA(buf, "ComboBox") ||
+           !lstrcmpiA(buf, "MyDialogClass") ||
+           !lstrcmpiA(buf, "#32770") ||
+           !lstrcmpiA(buf, "#32768"))
+        return TRUE;
+    }
+    return FALSE;
+}
 
 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam) 
 { 
-    static const char * const CBT_code_name[10] = {
-       "HCBT_MOVESIZE",
-       "HCBT_MINMAX",
-       "HCBT_QS",
-       "HCBT_CREATEWND",
-       "HCBT_DESTROYWND",
-       "HCBT_ACTIVATE",
-       "HCBT_CLICKSKIPPED",
-       "HCBT_KEYSKIPPED",
-       "HCBT_SYSCOMMAND",
-       "HCBT_SETFOCUS" };
-    const char *code_name = (nCode >= 0 && nCode <= HCBT_SETFOCUS) ? CBT_code_name[nCode] : "Unknown";
     HWND hwnd;
-    char buf[256];
-
-    trace("CBT: %d (%s), %08lx, %08lx\n", nCode, code_name, wParam, lParam);
 
     ok(cbt_hook_thread_id == GetCurrentThreadId(), "we didn't ask for events from other threads\n");
 
@@ -6907,12 +7603,14 @@ static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
 
     if (nCode == HCBT_SYSCOMMAND || nCode == HCBT_KEYSKIPPED)
     {
-       struct message msg;
+       struct recvd_message msg;
 
+        msg.hwnd = 0;
        msg.message = nCode;
        msg.flags = hook|wparam|lparam;
        msg.wParam = wParam;
        msg.lParam = lParam;
+        msg.descr = "CBT";
        add_message(&msg);
 
        return CallNextHookEx(hCBT_hook, nCode, wParam, lParam);
@@ -6935,33 +7633,17 @@ static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
     /* Log also SetFocus(0) calls */
     hwnd = wParam ? (HWND)wParam : (HWND)lParam;
 
-    if (GetClassNameA(hwnd, buf, sizeof(buf)))
+    if (is_our_logged_class(hwnd))
     {
-       if (!lstrcmpiA(buf, "TestWindowClass") ||
-           !lstrcmpiA(buf, "ShowWindowClass") ||
-           !lstrcmpiA(buf, "TestParentClass") ||
-           !lstrcmpiA(buf, "TestPopupClass") ||
-           !lstrcmpiA(buf, "SimpleWindowClass") ||
-           !lstrcmpiA(buf, "TestDialogClass") ||
-           !lstrcmpiA(buf, "MDI_frame_class") ||
-           !lstrcmpiA(buf, "MDI_client_class") ||
-           !lstrcmpiA(buf, "MDI_child_class") ||
-           !lstrcmpiA(buf, "my_button_class") ||
-           !lstrcmpiA(buf, "my_edit_class") ||
-           !lstrcmpiA(buf, "static") ||
-           !lstrcmpiA(buf, "ListBox") ||
-           !lstrcmpiA(buf, "ComboBox") ||
-           !lstrcmpiA(buf, "MyDialogClass") ||
-           !lstrcmpiA(buf, "#32770"))
-       {
-           struct message msg;
+        struct recvd_message msg;
 
-           msg.message = nCode;
-           msg.flags = hook|wparam|lparam;
-           msg.wParam = wParam;
-           msg.lParam = lParam;
-           add_message(&msg);
-       }
+        msg.hwnd = hwnd;
+        msg.message = nCode;
+        msg.flags = hook|wparam|lparam;
+        msg.wParam = wParam;
+        msg.lParam = lParam;
+        msg.descr = "CBT";
+        add_message(&msg);
     }
     return CallNextHookEx(hCBT_hook, nCode, wParam, lParam);
 }
@@ -6974,43 +7656,22 @@ static void CALLBACK win_event_proc(HWINEVENTHOOK hevent,
                                    DWORD thread_id,
                                    DWORD event_time)
 {
-    char buf[256];
-
-    trace("WEH:%p,event %08x,hwnd %p,obj %08x,id %08x,thread %08x,time %08x\n",
-          hevent, event, hwnd, object_id, child_id, thread_id, event_time);
-
     ok(thread_id == GetCurrentThreadId(), "we didn't ask for events from other threads\n");
 
     /* ignore mouse cursor events */
     if (object_id == OBJID_CURSOR) return;
 
-    if (!hwnd || GetClassNameA(hwnd, buf, sizeof(buf)))
+    if (!hwnd || is_our_logged_class(hwnd))
     {
-       if (!hwnd ||
-           !lstrcmpiA(buf, "TestWindowClass") ||
-           !lstrcmpiA(buf, "TestParentClass") ||
-           !lstrcmpiA(buf, "TestPopupClass") ||
-           !lstrcmpiA(buf, "SimpleWindowClass") ||
-           !lstrcmpiA(buf, "TestDialogClass") ||
-           !lstrcmpiA(buf, "MDI_frame_class") ||
-           !lstrcmpiA(buf, "MDI_client_class") ||
-           !lstrcmpiA(buf, "MDI_child_class") ||
-           !lstrcmpiA(buf, "my_button_class") ||
-           !lstrcmpiA(buf, "my_edit_class") ||
-           !lstrcmpiA(buf, "static") ||
-           !lstrcmpiA(buf, "ListBox") ||
-           !lstrcmpiA(buf, "ComboBox") ||
-           !lstrcmpiA(buf, "MyDialogClass") ||
-           !lstrcmpiA(buf, "#32770"))
-       {
-           struct message msg;
-
-           msg.message = event;
-           msg.flags = winevent_hook|wparam|lparam;
-           msg.wParam = object_id;
-           msg.lParam = child_id;
-           add_message(&msg);
-       }
+        struct recvd_message msg;
+
+        msg.hwnd = hwnd;
+        msg.message = event;
+        msg.flags = winevent_hook|wparam|lparam;
+        msg.wParam = object_id;
+        msg.lParam = child_id;
+        msg.descr = "WEH";
+        add_message(&msg);
     }
 }
 
@@ -7034,7 +7695,7 @@ static LRESULT CALLBACK MsgConversionProcW(HWND hwnd, UINT uMsg, WPARAM wParam,
 
 static const struct message WmGetTextLengthAfromW[] = {
     { WM_GETTEXTLENGTH, sent },
-    { WM_GETTEXT, sent },
+    { WM_GETTEXT, sent|optional },
     { 0 }
 };
 
@@ -7164,13 +7825,15 @@ static void test_message_conversion(void)
     newproc = (WNDPROC)GetWindowLongPtrA( hwnd, GWLP_WNDPROC );
     lRes = CallWindowProcA( newproc, hwnd, WM_GETTEXTLENGTH, 0, 0 );
     ok( lRes == WideCharToMultiByte( CP_ACP, 0, dummy_window_text, lstrlenW(dummy_window_text),
-                                     NULL, 0, NULL, NULL ),
+                                     NULL, 0, NULL, NULL ) ||
+        broken(lRes == lstrlenW(dummy_window_text) + 37),
         "got bad length %ld\n", lRes );
 
     SetWindowLongPtrW( hwnd, GWLP_WNDPROC, (LONG_PTR)wndproc );  /* restore old wnd proc */
     lRes = CallWindowProcA( newproc, hwnd, WM_GETTEXTLENGTH, 0, 0 );
     ok( lRes == WideCharToMultiByte( CP_ACP, 0, dummy_window_text, lstrlenW(dummy_window_text),
-                                     NULL, 0, NULL, NULL ),
+                                     NULL, 0, NULL, NULL ) ||
+        broken(lRes == lstrlenW(dummy_window_text) + 37),
         "got bad length %ld\n", lRes );
 
     ret = DestroyWindow(hwnd);
@@ -7188,6 +7851,12 @@ static VOID CALLBACK tfunc(HWND hwnd, UINT uMsg, UINT_PTR id, DWORD dwTime)
 {
 }
 
+static VOID CALLBACK tfunc_crash(HWND hwnd, UINT uMsg, UINT_PTR id, DWORD dwTime)
+{
+    /* Crash on purpose */
+    *(volatile int *)0 = 2;
+}
+
 #define TIMER_ID  0x19
 
 static DWORD WINAPI timer_thread_proc(LPVOID x)
@@ -7209,6 +7878,7 @@ static void test_timers(void)
 {
     struct timer_info info;
     DWORD id;
+    MSG msg;
 
     info.hWnd = CreateWindow ("TestWindowClass", NULL,
        WS_OVERLAPPEDWINDOW ,
@@ -7231,6 +7901,26 @@ static void test_timers(void)
     ok( KillTimer(info.hWnd, TIMER_ID), "KillTimer failed\n");
 
     ok(DestroyWindow(info.hWnd), "failed to destroy window\n");
+
+    /* Test timer callback with crash */
+    SetLastError(0xdeadbeef);
+    info.hWnd = CreateWindowW(testWindowClassW, NULL,
+                              WS_OVERLAPPEDWINDOW ,
+                              CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
+                              NULL, NULL, 0);
+    if ((!info.hWnd && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) || /* Win9x/Me */
+        (!pGetMenuInfo)) /* Win95/NT4 */
+    {
+        win_skip("Test would crash on Win9x/WinMe/NT4\n");
+        DestroyWindow(info.hWnd);
+        return;
+    }
+    info.id = SetTimer(info.hWnd, TIMER_ID, 0, tfunc_crash);
+    ok(info.id, "SetTimer failed\n");
+    Sleep(150);
+    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
+
+    ok(DestroyWindow(info.hWnd), "failed to destroy window\n");
 }
 
 static int count = 0;
@@ -7351,20 +8041,19 @@ static void CALLBACK win_event_global_hook_proc(HWINEVENTHOOK hevent,
 {
     char buf[256];
 
-    trace("WEH_2:%p,event %08x,hwnd %p,obj %08x,id %08x,thread %08x,time %08x\n",
-          hevent, event, hwnd, object_id, child_id, thread_id, event_time);
-
     if (GetClassNameA(hwnd, buf, sizeof(buf)))
     {
        if (!lstrcmpiA(buf, "TestWindowClass") ||
            !lstrcmpiA(buf, "static"))
        {
-           struct message msg;
+           struct recvd_message msg;
 
+            msg.hwnd = hwnd;
            msg.message = event;
            msg.flags = winevent_hook|wparam|lparam;
            msg.wParam = object_id;
            msg.lParam = (thread_id == GetCurrentThreadId()) ? child_id : (child_id + 2);
+            msg.descr = "WEH_2";
            add_message(&msg);
        }
     }
@@ -7378,16 +8067,16 @@ static LRESULT CALLBACK cbt_global_hook_proc(int nCode, WPARAM wParam, LPARAM lP
     HWND hwnd;
     char buf[256];
 
-    trace("CBT_2: %d, %08lx, %08lx\n", nCode, wParam, lParam);
-
     if (nCode == HCBT_SYSCOMMAND)
     {
-       struct message msg;
+       struct recvd_message msg;
 
+        msg.hwnd = 0;
        msg.message = nCode;
        msg.flags = hook|wparam|lparam;
        msg.wParam = wParam;
        msg.lParam = (cbt_global_hook_thread_id == GetCurrentThreadId()) ? 1 : 2;
+        msg.descr = "CBT_2";
        add_message(&msg);
 
        return CallNextHookEx(hCBT_global_hook, nCode, wParam, lParam);
@@ -7400,11 +8089,12 @@ static LRESULT CALLBACK cbt_global_hook_proc(int nCode, WPARAM wParam, LPARAM lP
         /* we can't test for real mouse events */
         if (mhll->flags & LLMHF_INJECTED)
         {
-           struct message msg;
+           struct recvd_message msg;
 
            memset (&msg, 0, sizeof (msg));
            msg.message = wParam;
            msg.flags = hook;
+            msg.descr = "CBT_2";
            add_message(&msg);
         }
        return CallNextHookEx(hCBT_global_hook, nCode, wParam, lParam);
@@ -7418,12 +8108,14 @@ static LRESULT CALLBACK cbt_global_hook_proc(int nCode, WPARAM wParam, LPARAM lP
        if (!lstrcmpiA(buf, "TestWindowClass") ||
            !lstrcmpiA(buf, "static"))
        {
-           struct message msg;
+           struct recvd_message msg;
 
+            msg.hwnd = hwnd;
            msg.message = nCode;
            msg.flags = hook|wparam|lparam;
            msg.wParam = wParam;
            msg.lParam = (cbt_global_hook_thread_id == GetCurrentThreadId()) ? 1 : 2;
+            msg.descr = "CBT_2";
            add_message(&msg);
        }
     }
@@ -7554,7 +8246,7 @@ static void test_winevents(void)
 
     hevent = CreateEventA(NULL, 0, 0, NULL);
     assert(hevent);
-    hwnd2 = (HWND)hevent;
+    hwnd2 = hevent;
 
     hthread = CreateThread(NULL, 0, cbt_global_hook_thread_proc, &hwnd2, 0, &tid);
     ok(hthread != NULL, "CreateThread failed, error %d\n", GetLastError());
@@ -7607,7 +8299,7 @@ static void test_winevents(void)
     ok_sequence(WmWinEventsSeq, "notify winevents", FALSE);
 
     /****** start of event filtering test *************/
-    hhook = (HWINEVENTHOOK)pSetWinEventHook(
+    hhook = pSetWinEventHook(
        EVENT_OBJECT_SHOW, /* 0x8002 */
        EVENT_OBJECT_LOCATIONCHANGE, /* 0x800B */
        GetModuleHandleA(0), win_event_global_hook_proc,
@@ -7617,7 +8309,7 @@ static void test_winevents(void)
 
     hevent = CreateEventA(NULL, 0, 0, NULL);
     assert(hevent);
-    hwnd2 = (HWND)hevent;
+    hwnd2 = hevent;
 
     hthread = CreateThread(NULL, 0, win_event_global_thread_proc, &hwnd2, 0, &tid);
     ok(hthread != NULL, "CreateThread failed, error %d\n", GetLastError());
@@ -7646,16 +8338,14 @@ static void test_winevents(void)
     /****** end of event filtering test *************/
 
     /****** start of out of context event test *************/
-    hhook = (HWINEVENTHOOK)pSetWinEventHook(
-       EVENT_MIN, EVENT_MAX,
-       0, win_event_global_hook_proc,
-       GetCurrentProcessId(), 0,
+    hhook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0,
+        win_event_global_hook_proc, GetCurrentProcessId(), 0,
        WINEVENT_OUTOFCONTEXT);
     ok(hhook != 0, "SetWinEventHook error %d\n", GetLastError());
 
     hevent = CreateEventA(NULL, 0, 0, NULL);
     assert(hevent);
-    hwnd2 = (HWND)hevent;
+    hwnd2 = hevent;
 
     flush_sequence();
 
@@ -7696,13 +8386,13 @@ static void test_winevents(void)
     /* WH_MOUSE_LL is not supported on Win9x platforms */
     if (!hCBT_global_hook)
     {
-        trace("Skipping WH_MOUSE_LL test on this platform\n");
+        win_skip("Skipping WH_MOUSE_LL test on this platform\n");
         goto skip_mouse_ll_hook_test;
     }
 
     hevent = CreateEventA(NULL, 0, 0, NULL);
     assert(hevent);
-    hwnd2 = (HWND)hevent;
+    hwnd2 = hevent;
 
     hthread = CreateThread(NULL, 0, mouse_ll_global_thread_proc, &hwnd2, 0, &tid);
     ok(hthread != NULL, "CreateThread failed, error %d\n", GetLastError());
@@ -7769,8 +8459,8 @@ static void test_set_hook(void)
 
     /* even process local incontext hooks require hmodule */
     SetLastError(0xdeadbeef);
-    hwinevent_hook = (HWINEVENTHOOK)pSetWinEventHook(EVENT_MIN, EVENT_MAX,
-       0, win_event_proc, GetCurrentProcessId(), 0, WINEVENT_INCONTEXT);
+    hwinevent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0, win_event_proc,
+        GetCurrentProcessId(), 0, WINEVENT_INCONTEXT);
     ok(!hwinevent_hook, "WINEVENT_INCONTEXT requires hModule != 0\n");
     ok(GetLastError() == ERROR_HOOK_NEEDS_HMOD || /* Win2k */
        GetLastError() == 0xdeadbeef, /* Win9x */
@@ -7778,8 +8468,8 @@ static void test_set_hook(void)
 
     /* even thread local incontext hooks require hmodule */
     SetLastError(0xdeadbeef);
-    hwinevent_hook = (HWINEVENTHOOK)pSetWinEventHook(EVENT_MIN, EVENT_MAX,
-       0, win_event_proc, GetCurrentProcessId(), GetCurrentThreadId(), WINEVENT_INCONTEXT);
+    hwinevent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0, win_event_proc,
+        GetCurrentProcessId(), GetCurrentThreadId(), WINEVENT_INCONTEXT);
     ok(!hwinevent_hook, "WINEVENT_INCONTEXT requires hModule != 0\n");
     ok(GetLastError() == ERROR_HOOK_NEEDS_HMOD || /* Win2k */
        GetLastError() == 0xdeadbeef, /* Win9x */
@@ -7789,27 +8479,27 @@ static void test_set_hook(void)
     {
     /* these 3 tests don't pass under Win9x */
     SetLastError(0xdeadbeef);
-    hwinevent_hook = (HWINEVENTHOOK)pSetWinEventHook(1, 0,
-       0, win_event_proc, GetCurrentProcessId(), 0, WINEVENT_OUTOFCONTEXT);
+    hwinevent_hook = pSetWinEventHook(1, 0, 0, win_event_proc,
+        GetCurrentProcessId(), 0, WINEVENT_OUTOFCONTEXT);
     ok(!hwinevent_hook, "SetWinEventHook with invalid event range should fail\n");
     ok(GetLastError() == ERROR_INVALID_HOOK_FILTER, "unexpected error %d\n", GetLastError());
 
     SetLastError(0xdeadbeef);
-    hwinevent_hook = (HWINEVENTHOOK)pSetWinEventHook(-1, 1,
-       0, win_event_proc, GetCurrentProcessId(), 0, WINEVENT_OUTOFCONTEXT);
+    hwinevent_hook = pSetWinEventHook(-1, 1, 0, win_event_proc,
+        GetCurrentProcessId(), 0, WINEVENT_OUTOFCONTEXT);
     ok(!hwinevent_hook, "SetWinEventHook with invalid event range should fail\n");
     ok(GetLastError() == ERROR_INVALID_HOOK_FILTER, "unexpected error %d\n", GetLastError());
 
     SetLastError(0xdeadbeef);
-    hwinevent_hook = (HWINEVENTHOOK)pSetWinEventHook(EVENT_MIN, EVENT_MAX,
-       0, win_event_proc, 0, 0xdeadbeef, WINEVENT_OUTOFCONTEXT);
+    hwinevent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0, win_event_proc,
+        0, 0xdeadbeef, WINEVENT_OUTOFCONTEXT);
     ok(!hwinevent_hook, "SetWinEventHook with invalid tid should fail\n");
     ok(GetLastError() == ERROR_INVALID_THREAD_ID, "unexpected error %d\n", GetLastError());
     }
 
     SetLastError(0xdeadbeef);
-    hwinevent_hook = (HWINEVENTHOOK)pSetWinEventHook(0, 0,
-       0, win_event_proc, GetCurrentProcessId(), 0, WINEVENT_OUTOFCONTEXT);
+    hwinevent_hook = pSetWinEventHook(0, 0, 0, win_event_proc,
+        GetCurrentProcessId(), 0, WINEVENT_OUTOFCONTEXT);
     ok(hwinevent_hook != 0, "SetWinEventHook error %d\n", GetLastError());
     ok(GetLastError() == 0xdeadbeef, "unexpected error %d\n", GetLastError());
     ret = pUnhookWinEvent(hwinevent_hook);
@@ -7819,8 +8509,8 @@ todo_wine {
     /* This call succeeds under win2k SP4, but fails under Wine.
        Does win2k test/use passed process id? */
     SetLastError(0xdeadbeef);
-    hwinevent_hook = (HWINEVENTHOOK)pSetWinEventHook(EVENT_MIN, EVENT_MAX,
-       0, win_event_proc, 0xdeadbeef, 0, WINEVENT_OUTOFCONTEXT);
+    hwinevent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX, 0, win_event_proc,
+        0xdeadbeef, 0, WINEVENT_OUTOFCONTEXT);
     ok(hwinevent_hook != 0, "SetWinEventHook error %d\n", GetLastError());
     ok(GetLastError() == 0xdeadbeef, "unexpected error %d\n", GetLastError());
     ret = pUnhookWinEvent(hwinevent_hook);
@@ -7837,6 +8527,14 @@ todo_wine {
 static const struct message ScrollWindowPaint1[] = {
     { WM_PAINT, sent },
     { WM_ERASEBKGND, sent|beginpaint },
+    { WM_GETTEXTLENGTH, sent|optional },
+    { WM_PAINT, sent|optional },
+    { WM_NCPAINT, sent|beginpaint|optional },
+    { WM_GETTEXT, sent|beginpaint|optional },
+    { WM_GETTEXT, sent|beginpaint|optional },
+    { WM_GETTEXT, sent|beginpaint|optional },
+    { WM_GETTEXT, sent|beginpaint|defwinproc|optional },
+    { WM_ERASEBKGND, sent|beginpaint|optional },
     { 0 }
 };
 
@@ -7889,10 +8587,9 @@ static void test_scrollwindowex(void)
     trace("start scroll\n");
     ScrollWindowEx( hwnd, 10, 10, &rect, NULL, NULL, NULL,
             SW_SCROLLCHILDREN|SW_ERASE|SW_INVALIDATE);
-    todo_wine { /* wine sends WM_POSCHANGING, WM_POSCHANGED messages */
-                /* windows sometimes a WM_MOVE */
-        ok_sequence(WmEmptySeq, "ScrollWindowEx", 0);
-    }
+    /* wine sends WM_POSCHANGING, WM_POSCHANGED messages */
+    /* windows sometimes a WM_MOVE */
+    ok_sequence(WmEmptySeq, "ScrollWindowEx", TRUE);
     trace("end scroll\n");
     flush_sequence();
     flush_events();
@@ -7939,7 +8636,7 @@ static void test_DestroyWindow(void)
 {
     BOOL ret;
     HWND parent, child1, child2, child3, child4, test;
-    UINT child_id = WND_CHILD_ID + 1;
+    UINT_PTR child_id = WND_CHILD_ID + 1;
 
     parent = CreateWindowExA(0, "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
                             100, 100, 200, 200, 0, 0, 0, NULL);
@@ -8137,8 +8834,11 @@ struct sendmsg_info
 static DWORD CALLBACK send_msg_thread( LPVOID arg )
 {
     struct sendmsg_info *info = arg;
+    SetLastError( 0xdeadbeef );
     info->ret = SendMessageTimeoutA( info->hwnd, WM_USER, 0, 0, 0, info->timeout, NULL );
-    if (!info->ret) ok( GetLastError() == ERROR_TIMEOUT, "unexpected error %d\n", GetLastError());
+    if (!info->ret) ok( GetLastError() == ERROR_TIMEOUT ||
+                        broken(GetLastError() == 0),  /* win9x */
+                        "unexpected error %d\n", GetLastError());
     return 0;
 }
 
@@ -8162,6 +8862,7 @@ static void test_SendMessageTimeout(void)
     HANDLE thread;
     struct sendmsg_info info;
     DWORD tid;
+    BOOL is_win9x;
 
     info.hwnd = CreateWindowA( "TestWindowClass", NULL, WS_OVERLAPPEDWINDOW,
                                100, 100, 200, 200, 0, 0, 0, NULL);
@@ -8185,17 +8886,18 @@ static void test_SendMessageTimeout(void)
     ok( info.ret == 0, "SendMessageTimeout succeeded\n" );
     ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
 
-    /* 0 means infinite timeout */
+    /* 0 means infinite timeout (but not on win9x) */
     info.timeout = 0;
     info.ret = 0xdeadbeef;
     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
     Sleep(100);
     wait_for_thread( thread );
     CloseHandle( thread );
-    ok( info.ret == 1, "SendMessageTimeout failed\n" );
-    ok_sequence( WmUser, "WmUser", FALSE );
+    is_win9x = !info.ret;
+    if (is_win9x) ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
+    else ok_sequence( WmUser, "WmUser", FALSE );
 
-    /* timeout is treated as signed despite the prototype */
+    /* timeout is treated as signed despite the prototype (but not on win9x) */
     info.timeout = 0x7fffffff;
     info.ret = 0xdeadbeef;
     thread = CreateThread( NULL, 0, send_msg_thread, &info, 0, &tid );
@@ -8211,8 +8913,16 @@ static void test_SendMessageTimeout(void)
     Sleep(100);
     wait_for_thread( thread );
     CloseHandle( thread );
-    ok( info.ret == 0, "SendMessageTimeout succeeded\n" );
-    ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
+    if (is_win9x)
+    {
+        ok( info.ret == 1, "SendMessageTimeout failed\n" );
+        ok_sequence( WmUser, "WmUser", FALSE );
+    }
+    else
+    {
+        ok( info.ret == 0, "SendMessageTimeout succeeded\n" );
+        ok_sequence( WmEmptySeq, "WmEmptySeq", FALSE );
+    }
 
     /* now check for timeout during message processing */
     SetWindowLongPtrA( info.hwnd, GWLP_WNDPROC, (LONG_PTR)send_msg_delay_proc );
@@ -8339,20 +9049,19 @@ static WNDPROC old_edit_proc;
 
 static LRESULT CALLBACK edit_hook_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    static long defwndproc_counter = 0;
+    static LONG defwndproc_counter = 0;
     LRESULT ret;
-    struct message msg;
-
-    trace("edit: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
+    struct recvd_message msg;
 
-    /* explicitly ignore WM_GETICON message */
-    if (message == WM_GETICON) return 0;
+    if (ignore_message( message )) return 0;
 
+    msg.hwnd = hwnd;
     msg.message = message;
     msg.flags = sent|wparam|lparam;
     if (defwndproc_counter) msg.flags |= defwinproc;
     msg.wParam = wParam;
     msg.lParam = lParam;
+    msg.descr = "edit";
     add_message(&msg);
 
     defwndproc_counter++;
@@ -8477,13 +9186,24 @@ static const struct message WmKeyDownSkippedSeq[] =
     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 1 }, /* XP */
     { 0 }
 };
+static const struct message WmKeyDownWasDownSkippedSeq[] =
+{
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0x40000001 }, /* XP */
+    { 0 }
+};
 static const struct message WmKeyUpSkippedSeq[] =
 {
     { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
     { 0 }
 };
+static const struct message WmUserKeyUpSkippedSeq[] =
+{
+    { WM_USER, sent },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'N', 0xc0000001 }, /* XP */
+    { 0 }
+};
 
-#define EV_START_STOP 0
+#define EV_STOP 0
 #define EV_SENDMSG 1
 #define EV_ACK 2
 
@@ -8498,9 +9218,8 @@ static DWORD CALLBACK send_msg_thread_2(void *param)
     DWORD ret;
     struct peekmsg_info *info = param;
 
-    trace("thread: waiting for start\n");
-    WaitForSingleObject(info->hevent[EV_START_STOP], INFINITE);
     trace("thread: looping\n");
+    SetEvent(info->hevent[EV_ACK]);
 
     while (1)
     {
@@ -8508,13 +9227,14 @@ static DWORD CALLBACK send_msg_thread_2(void *param)
 
         switch (ret)
         {
-        case WAIT_OBJECT_0 + EV_START_STOP:
+        case WAIT_OBJECT_0 + EV_STOP:
             trace("thread: exiting\n");
             return 0;
 
         case WAIT_OBJECT_0 + EV_SENDMSG:
             trace("thread: sending message\n");
-            SendNotifyMessageA(info->hwnd, WM_USER, 0, 0);
+            ok( SendNotifyMessageA(info->hwnd, WM_USER, 0, 0),
+                "SendNotifyMessageA failed error %u\n", GetLastError());
             SetEvent(info->hevent[EV_ACK]);
             break;
 
@@ -8544,15 +9264,12 @@ static void test_PeekMessage(void)
     UpdateWindow(info.hwnd);
     SetFocus(info.hwnd);
 
-    info.hevent[EV_START_STOP] = CreateEventA(NULL, 0, 0, NULL);
+    info.hevent[EV_STOP] = CreateEventA(NULL, 0, 0, NULL);
     info.hevent[EV_SENDMSG] = CreateEventA(NULL, 0, 0, NULL);
     info.hevent[EV_ACK] = CreateEventA(NULL, 0, 0, NULL);
 
     hthread = CreateThread(NULL, 0, send_msg_thread_2, &info, 0, &tid);
-    Sleep(100);
-
-    trace("signalling to start looping\n");
-    SetEvent(info.hevent[EV_START_STOP]);
+    WaitForSingleObject(info.hevent[EV_ACK], 10000);
 
     flush_events();
     flush_sequence();
@@ -8565,6 +9282,11 @@ static void test_PeekMessage(void)
         qs_all_input &= ~QS_RAWINPUT;
         qs_input &= ~QS_RAWINPUT;
     }
+    if (qstatus & QS_POSTMESSAGE)
+    {
+        while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) /* nothing */ ;
+        qstatus = GetQueueStatus(qs_all_input);
+    }
     ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
 
     trace("signalling to send message\n");
@@ -8574,10 +9296,13 @@ static void test_PeekMessage(void)
     /* pass invalid QS_xxxx flags */
     SetLastError(0xdeadbeef);
     qstatus = GetQueueStatus(0xffffffff);
-    ok(qstatus == 0, "GetQueueStatus should fail: %08x\n", qstatus);
-    ok(GetLastError() == ERROR_INVALID_FLAGS, "wrong error %d\n", GetLastError());
-
-    qstatus = GetQueueStatus(qs_all_input);
+    ok(qstatus == 0 || broken(qstatus)  /* win9x */, "GetQueueStatus should fail: %08x\n", qstatus);
+    if (!qstatus)
+    {
+        ok(GetLastError() == ERROR_INVALID_FLAGS, "wrong error %d\n", GetLastError());
+        qstatus = GetQueueStatus(qs_all_input);
+    }
+    qstatus &= ~MAKELONG( 0x4000, 0x4000 );  /* sometimes set on Win95 */
     ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE),
        "wrong qstatus %08x\n", qstatus);
 
@@ -8594,6 +9319,11 @@ static void test_PeekMessage(void)
     keybd_event('N', 0, 0, 0);
     keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
     qstatus = GetQueueStatus(qs_all_input);
+    if (!(qstatus & MAKELONG(QS_KEY, QS_KEY)))
+    {
+        skip( "queuing key events not supported\n" );
+        goto done;
+    }
     ok(qstatus == MAKELONG(QS_KEY, QS_KEY),
        "wrong qstatus %08x\n", qstatus);
 
@@ -8617,9 +9347,19 @@ static void test_PeekMessage(void)
 
     msg.message = 0;
     ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | (qs_input << 16));
+    if (ret && msg.message == WM_CHAR)
+    {
+        win_skip( "PM_QS_* flags not supported in PeekMessage\n" );
+        goto done;
+    }
     ok(!ret,
        "PeekMessageA should have returned FALSE instead of msg %04x\n",
         msg.message);
+    if (!sequence_cnt)  /* nt4 doesn't fetch anything with PM_QS_* flags */
+    {
+        win_skip( "PM_QS_* flags not supported in PeekMessage\n" );
+        goto done;
+    }
     ok_sequence(WmUser, "WmUser", FALSE);
 
     qstatus = GetQueueStatus(qs_all_input);
@@ -8635,7 +9375,7 @@ static void test_PeekMessage(void)
        "wrong qstatus %08x\n", qstatus);
 
     msg.message = 0;
-    ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_POSTMESSAGE);
+    ret = PeekMessageA(&msg, 0, 0, 0, PM_REMOVE | PM_QS_POSTMESSAGE );
     ok(!ret,
        "PeekMessageA should have returned FALSE instead of msg %04x\n",
         msg.message);
@@ -8861,34 +9601,131 @@ todo_wine {
     ok(qstatus == 0,
        "wrong qstatus %08x\n", qstatus);
 
-    trace("signalling to exit\n");
-    SetEvent(info.hevent[EV_START_STOP]);
-
-    WaitForSingleObject(hthread, INFINITE);
-
-    CloseHandle(hthread);
-    CloseHandle(info.hevent[0]);
-    CloseHandle(info.hevent[1]);
-    CloseHandle(info.hevent[2]);
+    /* some GetMessage tests */
 
-    DestroyWindow(info.hwnd);
-}
+    keybd_event('N', 0, 0, 0);
+    qstatus = GetQueueStatus(qs_all_input);
+    ok(qstatus == MAKELONG(QS_KEY, QS_KEY), "wrong qstatus %08x\n", qstatus);
 
-static void wait_move_event(HWND hwnd, int x, int y)
-{
-    MSG msg;
-    DWORD time;
-    BOOL  ret;
-    int go = 0;
+    PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
+    qstatus = GetQueueStatus(qs_all_input);
+    ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY), "wrong qstatus %08x\n", qstatus);
 
-    time = GetTickCount();
-    while (GetTickCount() - time < 200 && !go) {
-       ret = PeekMessageA(&msg, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_NOREMOVE);
-       go  = ret && msg.pt.x > x && msg.pt.y > y;
+    if (qstatus)
+    {
+        ret = GetMessageA( &msg, 0, 0, 0 );
+        ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
+           "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
+           ret, msg.message, msg.wParam);
+        qstatus = GetQueueStatus(qs_all_input);
+        ok(qstatus == MAKELONG(0, QS_KEY), "wrong qstatus %08x\n", qstatus);
     }
-}
 
-#define STEP 20
+    if (qstatus)
+    {
+        ret = GetMessageA( &msg, 0, 0, 0 );
+        ok(ret && msg.message == WM_KEYDOWN && msg.wParam == 'N',
+           "got %d and %04x wParam %08lx instead of TRUE and WM_KEYDOWN wParam 'N'\n",
+           ret, msg.message, msg.wParam);
+        ok_sequence(WmKeyDownSkippedSeq, "WmKeyDownSkippedSeq", FALSE);
+        qstatus = GetQueueStatus(qs_all_input);
+        ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
+    }
+
+    keybd_event('N', 0, 0, 0);
+    qstatus = GetQueueStatus(qs_all_input);
+    ok(qstatus == MAKELONG(QS_KEY, QS_KEY), "wrong qstatus %08x\n", qstatus);
+
+    PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
+    qstatus = GetQueueStatus(qs_all_input);
+    ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY), "wrong qstatus %08x\n", qstatus);
+
+    if (qstatus & (QS_KEY << 16))
+    {
+        ret = GetMessageA( &msg, 0, WM_KEYDOWN, WM_KEYUP );
+        ok(ret && msg.message == WM_KEYDOWN && msg.wParam == 'N',
+           "got %d and %04x wParam %08lx instead of TRUE and WM_KEYDOWN wParam 'N'\n",
+           ret, msg.message, msg.wParam);
+        ok_sequence(WmKeyDownWasDownSkippedSeq, "WmKeyDownWasDownSkippedSeq", FALSE);
+        qstatus = GetQueueStatus(qs_all_input);
+        ok(qstatus == MAKELONG(0, QS_POSTMESSAGE), "wrong qstatus %08x\n", qstatus);
+    }
+
+    if (qstatus)
+    {
+        ret = GetMessageA( &msg, 0, WM_CHAR, WM_CHAR );
+        ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
+           "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
+           ret, msg.message, msg.wParam);
+        qstatus = GetQueueStatus(qs_all_input);
+        ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
+    }
+
+    keybd_event('N', 0, KEYEVENTF_KEYUP, 0);
+    qstatus = GetQueueStatus(qs_all_input);
+    ok(qstatus == MAKELONG(QS_KEY, QS_KEY), "wrong qstatus %08x\n", qstatus);
+
+    PostMessageA(info.hwnd, WM_CHAR, 'z', 0);
+    qstatus = GetQueueStatus(qs_all_input);
+    ok(qstatus == MAKELONG(QS_POSTMESSAGE, QS_POSTMESSAGE|QS_KEY), "wrong qstatus %08x\n", qstatus);
+
+    trace("signalling to send message\n");
+    SetEvent(info.hevent[EV_SENDMSG]);
+    WaitForSingleObject(info.hevent[EV_ACK], INFINITE);
+    qstatus = GetQueueStatus(qs_all_input);
+    ok(qstatus == MAKELONG(QS_SENDMESSAGE, QS_SENDMESSAGE|QS_POSTMESSAGE|QS_KEY),
+       "wrong qstatus %08x\n", qstatus);
+
+    if (qstatus & (QS_KEY << 16))
+    {
+        ret = GetMessageA( &msg, 0, WM_KEYDOWN, WM_KEYUP );
+        ok(ret && msg.message == WM_KEYUP && msg.wParam == 'N',
+           "got %d and %04x wParam %08lx instead of TRUE and WM_KEYDOWN wParam 'N'\n",
+           ret, msg.message, msg.wParam);
+        ok_sequence(WmUserKeyUpSkippedSeq, "WmUserKeyUpSkippedSeq", FALSE);
+        qstatus = GetQueueStatus(qs_all_input);
+        ok(qstatus == MAKELONG(0, QS_POSTMESSAGE), "wrong qstatus %08x\n", qstatus);
+    }
+
+    if (qstatus)
+    {
+        ret = GetMessageA( &msg, 0, WM_CHAR, WM_CHAR );
+        ok(ret && msg.message == WM_CHAR && msg.wParam == 'z',
+           "got %d and %04x wParam %08lx instead of TRUE and WM_CHAR wParam 'z'\n",
+           ret, msg.message, msg.wParam);
+        qstatus = GetQueueStatus(qs_all_input);
+        ok(qstatus == 0, "wrong qstatus %08x\n", qstatus);
+    }
+done:
+    trace("signalling to exit\n");
+    SetEvent(info.hevent[EV_STOP]);
+
+    WaitForSingleObject(hthread, INFINITE);
+
+    CloseHandle(hthread);
+    CloseHandle(info.hevent[0]);
+    CloseHandle(info.hevent[1]);
+    CloseHandle(info.hevent[2]);
+
+    DestroyWindow(info.hwnd);
+}
+
+static void wait_move_event(HWND hwnd, int x, int y)
+{
+    MSG msg;
+    DWORD time;
+    BOOL  ret;
+    int go = 0;
+
+    time = GetTickCount();
+    while (GetTickCount() - time < 200 && !go) {
+       ret = PeekMessageA(&msg, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_NOREMOVE);
+       go  = ret && msg.pt.x > x && msg.pt.y > y;
+        if (!ret) MsgWaitForMultipleObjects( 0, NULL, FALSE, GetTickCount() - time, QS_ALLINPUT );
+    }
+}
+
+#define STEP 5
 static void test_PeekMessage2(void)
 {
     HWND hwnd;
@@ -8918,11 +9755,16 @@ static void test_PeekMessage2(void)
     /* Do initial mousemove, wait until we can see it
        and then do our test peek with PM_NOREMOVE. */
     mouse_event(MOUSEEVENTF_MOVE, STEP, STEP, 0, 0);
-    wait_move_event(hwnd, 80, 80);
+    wait_move_event(hwnd, 100-STEP, 100-STEP);
 
     ret = PeekMessageA(&msg, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_NOREMOVE);
-    ok(ret, "no message available\n");
-    if (ret) {
+    if (!ret)
+    {
+        skip( "queuing mouse events not supported\n" );
+        goto done;
+    }
+    else
+    {
        trace("1st move event: %04x %x %d %d\n", msg.message, msg.time, msg.pt.x, msg.pt.y);
        message = msg.message;
        time1 = msg.time;
@@ -8936,7 +9778,7 @@ static void test_PeekMessage2(void)
      * Although the previous mousemove message was never removed, the
      * mousemove we now peek should reflect the recent mouse movements
      * because the input queue will merge the move events. */
-    Sleep(2);
+    Sleep(100);
     mouse_event(MOUSEEVENTF_MOVE, STEP, STEP, 0, 0);
     wait_move_event(hwnd, x1, y1);
 
@@ -8954,7 +9796,7 @@ static void test_PeekMessage2(void)
     }
 
     /* Have another go, to drive the point home */
-    Sleep(2);
+    Sleep(100);
     mouse_event(MOUSEEVENTF_MOVE, STEP, STEP, 0, 0);
     wait_move_event(hwnd, x2, y2);
 
@@ -8971,6 +9813,7 @@ static void test_PeekMessage2(void)
        ok(x3 != x2 && y3 != y2, "coords not changed: (%d %d) (%d %d)\n", x2, y2, x3, y3);
     }
 
+done:
     DestroyWindow(hwnd);
     SetCursorPos(pos.x, pos.y);
     flush_events();
@@ -8982,6 +9825,7 @@ static void test_quit_message(void)
     BOOL ret;
 
     /* test using PostQuitMessage */
+    flush_events();
     PostQuitMessage(0xbeef);
 
     ret = PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
@@ -9042,8 +9886,8 @@ static void pump_msg_loop_timeout(DWORD timeout, BOOL inject_mouse_move)
     DWORD start_ticks, end_ticks;
 
     start_ticks = GetTickCount();
-    /* add some deviation (5%) to cover not expected delays */
-    start_ticks += timeout / 20;
+    /* add some deviation (50%) to cover not expected delays */
+    start_ticks += timeout / 2;
 
     do
     {
@@ -9054,12 +9898,14 @@ static void pump_msg_loop_timeout(DWORD timeout, BOOL inject_mouse_move)
              */
             if (msg.message == WM_TIMER || msg.message == WM_SYSTIMER)
             {
-                struct message s_msg;
+                struct recvd_message s_msg;
 
+                s_msg.hwnd = msg.hwnd;
                 s_msg.message = msg.message;
                 s_msg.flags = sent|wparam|lparam;
                 s_msg.wParam = msg.wParam;
                 s_msg.lParam = msg.lParam;
+                s_msg.descr = "msg_loop";
                 add_message(&s_msg);
             }
             DispatchMessage(&msg);
@@ -9123,17 +9969,20 @@ static void test_TrackMouseEvent(void)
     default_hover_time = 0xdeadbeef;
     SetLastError(0xdeadbeef);
     ret = SystemParametersInfo(SPI_GETMOUSEHOVERTIME, 0, &default_hover_time, 0);
-    ok(ret, "SystemParametersInfo(SPI_GETMOUSEHOVERTIME) error %u\n", GetLastError());
+    ok(ret || broken(GetLastError() == 0xdeadbeef),  /* win9x */
+       "SystemParametersInfo(SPI_GETMOUSEHOVERTIME) error %u\n", GetLastError());
     if (!ret) default_hover_time = 400;
     trace("SPI_GETMOUSEHOVERTIME returned %u ms\n", default_hover_time);
 
     SetLastError(0xdeadbeef);
     ret = SystemParametersInfo(SPI_GETMOUSEHOVERWIDTH, 0, &hover_width, 0);
-    ok(ret, "SystemParametersInfo(SPI_GETMOUSEHOVERWIDTH) error %u\n", GetLastError());
+    ok(ret || broken(GetLastError() == 0xdeadbeef),  /* win9x */
+       "SystemParametersInfo(SPI_GETMOUSEHOVERWIDTH) error %u\n", GetLastError());
     if (!ret) hover_width = 4;
     SetLastError(0xdeadbeef);
     ret = SystemParametersInfo(SPI_GETMOUSEHOVERHEIGHT, 0, &hover_height, 0);
-    ok(ret, "SystemParametersInfo(SPI_GETMOUSEHOVERHEIGHT) error %u\n", GetLastError());
+    ok(ret || broken(GetLastError() == 0xdeadbeef),  /* win9x */
+       "SystemParametersInfo(SPI_GETMOUSEHOVERHEIGHT) error %u\n", GetLastError());
     if (!ret) hover_height = 4;
     trace("hover rect is %u x %d\n", hover_width, hover_height);
 
@@ -9149,6 +9998,7 @@ static void test_TrackMouseEvent(void)
                          NULL, NULL, 0);
     assert(hchild);
 
+    SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE );
     flush_events();
     flush_sequence();
 
@@ -9159,7 +10009,8 @@ static void test_TrackMouseEvent(void)
     SetLastError(0xdeadbeef);
     ret = pTrackMouseEvent(&tme);
     ok(!ret, "TrackMouseEvent should fail\n");
-    ok(GetLastError() == ERROR_INVALID_PARAMETER, "not expected error %d\n", GetLastError());
+    ok(GetLastError() == ERROR_INVALID_PARAMETER || broken(GetLastError() == 0xdeadbeef),
+       "not expected error %u\n", GetLastError());
 
     tme.cbSize = sizeof(tme);
     tme.dwFlags = TME_HOVER;
@@ -9168,7 +10019,8 @@ static void test_TrackMouseEvent(void)
     SetLastError(0xdeadbeef);
     ret = pTrackMouseEvent(&tme);
     ok(!ret, "TrackMouseEvent should fail\n");
-    ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "not expected error %d\n", GetLastError());
+    ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || broken(GetLastError() == 0xdeadbeef),
+       "not expected error %u\n", GetLastError());
 
     tme.cbSize = sizeof(tme);
     tme.dwFlags = TME_HOVER | TME_CANCEL;
@@ -9177,7 +10029,8 @@ static void test_TrackMouseEvent(void)
     SetLastError(0xdeadbeef);
     ret = pTrackMouseEvent(&tme);
     ok(!ret, "TrackMouseEvent should fail\n");
-    ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE, "not expected error %d\n", GetLastError());
+    ok(GetLastError() == ERROR_INVALID_WINDOW_HANDLE || broken(GetLastError() == 0xdeadbeef),
+       "not expected error %u\n", GetLastError());
 
     GetWindowRect(hwnd, &rc_parent);
     GetWindowRect(hchild, &rc_child);
@@ -9198,7 +10051,24 @@ static void test_TrackMouseEvent(void)
     flush_sequence();
 
     track_hover(hwnd, 0);
-    track_query(TME_HOVER, hwnd, default_hover_time);
+    tme.cbSize = sizeof(tme);
+    tme.dwFlags = TME_QUERY;
+    tme.hwndTrack = (HWND)0xdeadbeef;
+    tme.dwHoverTime = 0xdeadbeef;
+    SetLastError(0xdeadbeef);
+    ret = pTrackMouseEvent(&tme);
+    ok(ret, "TrackMouseEvent(TME_QUERY) error %d\n", GetLastError());
+    ok(tme.cbSize == sizeof(tme), "wrong tme.cbSize %u\n", tme.cbSize);
+    if (!tme.dwFlags)
+    {
+        skip( "Cursor not inside window, skipping TrackMouseEvent tests\n" );
+        DestroyWindow( hwnd );
+        return;
+    }
+    ok(tme.dwFlags == TME_HOVER, "wrong tme.dwFlags %08x, expected TME_HOVER\n", tme.dwFlags);
+    ok(tme.hwndTrack == hwnd, "wrong tme.hwndTrack %p, expected %p\n", tme.hwndTrack, hwnd);
+    ok(tme.dwHoverTime == default_hover_time, "wrong tme.dwHoverTime %u, expected %u\n",
+       tme.dwHoverTime, default_hover_time);
 
     pump_msg_loop_timeout(default_hover_time, FALSE);
     ok_sequence(WmMouseHoverSeq, "WmMouseHoverSeq", FALSE);
@@ -9214,7 +10084,7 @@ static void test_TrackMouseEvent(void)
 
     track_query(TME_HOVER, hwnd, default_hover_time);
 
-    pump_msg_loop_timeout(default_hover_time / 2, FALSE);
+    pump_msg_loop_timeout(default_hover_time, FALSE);
     ok_sequence(WmMouseHoverSeq, "WmMouseHoverSeq", FALSE);
 
     track_query(0, NULL, 0);
@@ -9242,9 +10112,9 @@ static void test_TrackMouseEvent(void)
 static const struct message WmSetWindowRgn[] = {
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },
     { WM_NCCALCSIZE, sent|wparam, 1 },
-    { WM_NCPAINT, sent }, /* wparam != 1 */
+    { WM_NCPAINT, sent|optional }, /* wparam != 1 */
     { WM_GETTEXT, sent|defwinproc|optional },
-    { WM_ERASEBKGND, sent|optional }, /* FIXME: remove optional once Wine is fixed */
+    { WM_ERASEBKGND, sent|optional },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
     { 0 }
@@ -9259,14 +10129,24 @@ static const struct message WmSetWindowRgn_no_redraw[] = {
 };
 
 static const struct message WmSetWindowRgn_clear[] = {
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_WINDOWPOSCHANGING, sent/*|wparam*/, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE/*|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE only on some Windows versions */ },
     { WM_NCCALCSIZE, sent|wparam, 1 },
-    { WM_NCPAINT, sent }, /* wparam != 1 */
+    { WM_NCPAINT, sent|optional },
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_ERASEBKGND, sent|optional }, /* FIXME: remove optional once Wine is fixed */
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },
     { WM_NCCALCSIZE, sent|wparam|optional, 1 },
-    { WM_NCPAINT, sent|optional }, /* wparam != 1 */
+    { WM_NCPAINT, sent|optional },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ERASEBKGND, sent|optional },
+    { WM_WINDOWPOSCHANGING, sent|optional },
+    { WM_NCCALCSIZE, sent|optional|wparam, 1 },
+    { WM_NCPAINT, sent|optional },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ERASEBKGND, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|optional|wparam, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_NCCALCSIZE, sent|optional|wparam, 1 },
+    { WM_NCPAINT, sent|optional },
     { WM_GETTEXT, sent|defwinproc|optional },
     { WM_ERASEBKGND, sent|optional },
     { EVENT_OBJECT_LOCATIONCHANGE, winevent_hook|wparam|lparam, 0, 0 },
@@ -9326,9 +10206,9 @@ static const struct message WmShow[] = {
 };
 static const struct message WmShowNoActivate_1[] = {
     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWNOACTIVATE },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_STATECHANGED },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_STATECHANGED },
-    { WM_MOVE, sent|defwinproc },
+    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_MOVE, sent|defwinproc|optional },
     { WM_SIZE, sent|wparam|defwinproc, SIZE_RESTORED },
     { 0 }
 };
@@ -9341,6 +10221,7 @@ static const struct message WmShowNoActivate_2[] = {
     { HCBT_SETFOCUS, hook|optional },
     { HCBT_ACTIVATE, hook|optional }, /* win2003 doesn't send it */
     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2003 doesn't send it */
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { HCBT_SETFOCUS, hook|optional }, /* win2003 doesn't send it */
     { 0 }
 };
@@ -9353,6 +10234,7 @@ static const struct message WmShowNA_1[] = {
 static const struct message WmShowNA_2[] = {
     { WM_SHOWWINDOW, sent|wparam, 1 },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
     { 0 }
 };
 static const struct message WmRestore_1[] = {
@@ -9390,25 +10272,28 @@ static const struct message WmRestore_3[] = {
     { 0 }
 };
 static const struct message WmRestore_4[] = {
-    { HCBT_MINMAX, hook|lparam, 0, SW_RESTORE },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
-    { WM_MOVE, sent|defwinproc },
-    { WM_SIZE, sent|wparam|defwinproc, SIZE_RESTORED },
+    { HCBT_MINMAX, hook|lparam|optional, 0, SW_RESTORE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_MOVE, sent|defwinproc|optional },
+    { WM_SIZE, sent|wparam|defwinproc|optional, SIZE_RESTORED },
     { 0 }
 };
 static const struct message WmRestore_5[] = {
-    { HCBT_MINMAX, hook|lparam, 0, SW_SHOWNORMAL },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
-    { WM_MOVE, sent|defwinproc },
-    { WM_SIZE, sent|wparam|defwinproc, SIZE_RESTORED },
+    { HCBT_MINMAX, hook|lparam|optional, 0, SW_SHOWNORMAL },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
+    { HCBT_ACTIVATE, hook|optional },
+    { HCBT_SETFOCUS, hook|optional },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_MOVE, sent|defwinproc|optional },
+    { WM_SIZE, sent|wparam|defwinproc|optional, SIZE_RESTORED },
     { 0 }
 };
 static const struct message WmHide_1[] = {
     { WM_SHOWWINDOW, sent|wparam, 0 },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { HCBT_ACTIVATE, hook|optional },
     { HCBT_SETFOCUS, hook|optional }, /* win2000 sends it */
     { 0 }
 };
@@ -9416,13 +10301,14 @@ static const struct message WmHide_2[] = {
     { WM_SHOWWINDOW, sent|wparam, 0 },
     { WM_WINDOWPOSCHANGING, sent /*|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE*/ }, /* win2000 doesn't add SWP_NOACTIVATE */
     { WM_WINDOWPOSCHANGED, sent /*|wparam, SWP_HIDEWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE*/ }, /* win2000 doesn't add SWP_NOACTIVATE */
+    { HCBT_ACTIVATE, hook|optional },
     { 0 }
 };
 static const struct message WmHide_3[] = {
     { WM_SHOWWINDOW, sent|wparam, 0 },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_HIDEWINDOW|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
-    { HCBT_SETFOCUS, hook },
+    { HCBT_SETFOCUS, hook|optional },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { 0 }
 };
@@ -9433,7 +10319,7 @@ static const struct message WmShowMinimized_1[] = {
     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
     { WM_MOVE, sent|defwinproc },
-    { WM_SIZE, sent|wparam|defwinproc, SIZE_MINIMIZED },
+    { WM_SIZE, sent|wparam|lparam|defwinproc, SIZE_MINIMIZED, 0 },
     { 0 }
 };
 static const struct message WmMinimize_1[] = {
@@ -9443,15 +10329,16 @@ static const struct message WmMinimize_1[] = {
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
     { WM_MOVE, sent|defwinproc },
-    { WM_SIZE, sent|wparam|defwinproc, SIZE_MINIMIZED },
+    { WM_SIZE, sent|wparam|lparam|defwinproc, SIZE_MINIMIZED, 0 },
     { 0 }
 };
 static const struct message WmMinimize_2[] = {
     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
+    { HCBT_SETFOCUS, hook|optional },
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
     { WM_MOVE, sent|defwinproc },
-    { WM_SIZE, sent|wparam|defwinproc, SIZE_MINIMIZED },
+    { WM_SIZE, sent|wparam|lparam|defwinproc, SIZE_MINIMIZED, 0 },
     { 0 }
 };
 static const struct message WmMinimize_3[] = {
@@ -9459,13 +10346,15 @@ static const struct message WmMinimize_3[] = {
     { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
     { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
     { WM_MOVE, sent|defwinproc },
-    { WM_SIZE, sent|wparam|defwinproc, SIZE_MINIMIZED },
+    { WM_SIZE, sent|wparam|lparam|defwinproc, SIZE_MINIMIZED, 0 },
     { 0 }
 };
 static const struct message WmShowMinNoActivate[] = {
     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMINNOACTIVE },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_WINDOWPOSCHANGING, sent },
+    { WM_WINDOWPOSCHANGED, sent },
+    { WM_MOVE, sent|defwinproc|optional },
+    { WM_SIZE, sent|wparam|lparam|defwinproc|optional, SIZE_MINIMIZED, 0 },
     { 0 }
 };
 static const struct message WmMinMax_1[] = {
@@ -9474,10 +10363,24 @@ static const struct message WmMinMax_1[] = {
 };
 static const struct message WmMinMax_2[] = {
     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMAXIMIZED },
+    { WM_GETMINMAXINFO, sent|optional },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED },
+    { HCBT_ACTIVATE, hook|optional },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
+    { HCBT_SETFOCUS, hook|optional },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_MOVE, sent|defwinproc|optional },
+    { WM_SIZE, sent|wparam|defwinproc|optional, SIZE_MAXIMIZED },
+    { HCBT_SETFOCUS, hook|optional },
     { 0 }
 };
 static const struct message WmMinMax_3[] = {
     { HCBT_MINMAX, hook|lparam, 0, SW_MINIMIZE },
+    { HCBT_SETFOCUS, hook|optional },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_SHOWWINDOW|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
+    { WM_MOVE, sent|defwinproc|optional },
+    { WM_SIZE, sent|wparam|lparam|defwinproc|optional, SIZE_MINIMIZED, 0 },
     { 0 }
 };
 static const struct message WmMinMax_4[] = {
@@ -9500,26 +10403,28 @@ static const struct message WmShowMaximized_1[] = {
 static const struct message WmShowMaximized_2[] = {
     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMAXIMIZED },
     { WM_GETMINMAXINFO, sent },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_WINDOWPOSCHANGING, sent|optional },
+    { HCBT_ACTIVATE, hook|optional },
+    { WM_WINDOWPOSCHANGED, sent|optional },
     { WM_MOVE, sent|optional }, /* Win9x doesn't send it */
     { WM_SIZE, sent|wparam|optional, SIZE_MAXIMIZED }, /* Win9x doesn't send it */
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
+    { WM_WINDOWPOSCHANGING, sent|optional },
+    { HCBT_SETFOCUS, hook|optional },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_FRAMECHANGED|SWP_NOCOPYBITS|SWP_STATECHANGED },
     { WM_MOVE, sent|defwinproc },
     { WM_SIZE, sent|wparam|defwinproc, SIZE_MAXIMIZED },
-    { HCBT_SETFOCUS, hook },
+    { HCBT_SETFOCUS, hook|optional },
     { 0 }
 };
 static const struct message WmShowMaximized_3[] = {
     { HCBT_MINMAX, hook|lparam, 0, SW_SHOWMAXIMIZED },
-    { WM_GETMINMAXINFO, sent },
-    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
+    { WM_GETMINMAXINFO, sent|optional },
+    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
     { HCBT_ACTIVATE, hook|optional }, /* win2000 doesn't send it */
     { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE }, /* win2000 doesn't send it */
     { HCBT_SETFOCUS, hook|optional }, /* win2000 doesn't send it */
-    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED },
-    { WM_MOVE, sent|defwinproc },
+    { WM_WINDOWPOSCHANGED, sent|wparam, SWP_FRAMECHANGED|SWP_STATECHANGED, 0, SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE|SWP_NOSIZE|SWP_NOMOVE },
+    { WM_MOVE, sent|defwinproc|optional },
     { WM_SIZE, sent|wparam|defwinproc, SIZE_MAXIMIZED },
     { 0 }
 };
@@ -9567,7 +10472,7 @@ static void test_ShowWindow(void)
 /* 29 */ { SW_HIDE, FALSE, WS_MINIMIZE, WmEmptySeq, FALSE },
 /* 30 */ { SW_RESTORE, FALSE, WS_VISIBLE, WmRestore_1, FALSE },
 /* 31 */ { SW_RESTORE, TRUE, WS_VISIBLE, WmEmptySeq, FALSE },
-/* 32 */ { SW_HIDE, TRUE, 0, WmHide_3, TRUE },
+/* 32 */ { SW_HIDE, TRUE, 0, WmHide_3, FALSE },
 /* 33 */ { SW_HIDE, FALSE, 0, WmEmptySeq, FALSE },
 /* 34 */ { SW_NORMALNA, FALSE, 0, WmEmptySeq, FALSE }, /* what does this mean?! */
 /* 35 */ { SW_NORMALNA, FALSE, 0, WmEmptySeq, FALSE },
@@ -9577,7 +10482,7 @@ static void test_ShowWindow(void)
 /* 39 */ { SW_SHOWNOACTIVATE, TRUE, WS_VISIBLE, WmEmptySeq, FALSE },
 /* 40 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinimize_2, FALSE },
 /* 41 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinMax_3, FALSE },
-/* 42 */ { SW_SHOWMAXIMIZED, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmShowMaximized_2, TRUE },
+/* 42 */ { SW_SHOWMAXIMIZED, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmShowMaximized_2, FALSE },
 /* 43 */ { SW_SHOWMAXIMIZED, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmMinMax_2, FALSE },
 /* 44 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinimize_1, FALSE },
 /* 45 */ { SW_MINIMIZE, TRUE, WS_VISIBLE|WS_MINIMIZE, WmMinMax_3, FALSE },
@@ -9586,7 +10491,7 @@ static void test_ShowWindow(void)
 /* 48 */ { SW_SHOWMAXIMIZED, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmShowMaximized_3, FALSE },
 /* 49 */ { SW_SHOW, TRUE, WS_VISIBLE|WS_MAXIMIZE, WmEmptySeq, FALSE },
 /* 50 */ { SW_SHOWNORMAL, TRUE, WS_VISIBLE, WmRestore_5, FALSE },
-/* 51 */ { SW_SHOWNORMAL, TRUE, WS_VISIBLE, WmEmptySeq, FALSE },
+/* 51 */ { SW_SHOWNORMAL, TRUE, WS_VISIBLE, WmRestore_5, FALSE },
 /* 52 */ { SW_HIDE, TRUE, 0, WmHide_1, FALSE },
 /* 53 */ { SW_HIDE, FALSE, 0, WmEmptySeq, FALSE },
 /* 54 */ { SW_MINIMIZE, FALSE, WS_VISIBLE|WS_MINIMIZE, WmMinimize_3, FALSE },
@@ -9644,41 +10549,16 @@ static void test_ShowWindow(void)
 
 static INT_PTR WINAPI test_dlg_proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    struct message msg;
-
-    trace("dialog: %p, %04x, %08lx, %08lx\n", hwnd, message, wParam, lParam);
-
-    switch (message)
-    {
-    case WM_WINDOWPOSCHANGING:
-    case WM_WINDOWPOSCHANGED:
-    {
-        WINDOWPOS *winpos = (WINDOWPOS *)lParam;
-
-        trace("%s\n", (message == WM_WINDOWPOSCHANGING) ? "WM_WINDOWPOSCHANGING" : "WM_WINDOWPOSCHANGED");
-        trace("%p after %p, x %d, y %d, cx %d, cy %d flags %08x\n",
-              winpos->hwnd, winpos->hwndInsertAfter,
-              winpos->x, winpos->y, winpos->cx, winpos->cy, winpos->flags);
-        dump_winpos_flags(winpos->flags);
-
-        /* Log only documented flags, win2k uses 0x1000 and 0x2000
-         * in the high word for internal purposes
-         */
-        wParam = winpos->flags & 0xffff;
-        /* We are not interested in the flags that don't match under XP and Win9x */
-        wParam &= ~(SWP_NOZORDER);
-        break;
-    }
+    struct recvd_message msg;
 
-    /* explicitly ignore WM_GETICON message */
-    case WM_GETICON:
-        return 0;
-    }
+    if (ignore_message( message )) return 0;
 
+    msg.hwnd = hwnd;
     msg.message = message;
     msg.flags = sent|wparam|lparam;
     msg.wParam = wParam;
     msg.lParam = lParam;
+    msg.descr = "dialog";
     add_message(&msg);
 
     /* calling DefDlgProc leads to a recursion under XP */
@@ -9868,6 +10748,167 @@ static void test_nullCallback(void)
     DestroyWindow(hwnd);
 }
 
+/* SetActiveWindow( 0 ) hwnd visible */
+static const struct message SetActiveWindowSeq0[] =
+{
+    { HCBT_ACTIVATE, hook|optional },
+    { WM_NCACTIVATE, sent|wparam, 0 },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ACTIVATE, sent|wparam, 0 },
+    { WM_ACTIVATEAPP, sent|wparam|optional, 0 },
+    { WM_ACTIVATEAPP, sent|wparam|optional, 0 },
+    { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
+    { WM_KILLFOCUS, sent|optional },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_NCACTIVATE, sent|wparam|optional, 1 },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ACTIVATE, sent|wparam|optional, 1 },
+    { HCBT_SETFOCUS, hook|optional },
+    { WM_KILLFOCUS, sent|defwinproc|optional },
+    { WM_IME_SETCONTEXT, sent|defwinproc|optional },
+    { WM_IME_SETCONTEXT, sent|defwinproc|optional },
+    { WM_IME_SETCONTEXT, sent|optional },
+    { WM_IME_SETCONTEXT, sent|optional },
+    { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
+    { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
+    { WM_SETFOCUS, sent|defwinproc|optional },
+    { WM_GETTEXT, sent|optional },
+    { 0 }
+};
+/* SetActiveWindow( hwnd ) hwnd visible */
+static const struct message SetActiveWindowSeq1[] =
+{
+    { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
+    { 0 }
+};
+/* SetActiveWindow( popup ) hwnd visible, popup visible */
+static const struct message SetActiveWindowSeq2[] =
+{
+    { HCBT_ACTIVATE, hook },
+    { WM_NCACTIVATE, sent|wparam, 0 },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ACTIVATE, sent|wparam, 0 },
+    { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
+    { WM_NCPAINT, sent|optional },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ERASEBKGND, sent|optional },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ACTIVATE, sent|wparam, 1 },
+    { HCBT_SETFOCUS, hook },
+    { WM_KILLFOCUS, sent|defwinproc },
+    { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 0 },
+    { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
+    { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
+    { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
+    { WM_SETFOCUS, sent|defwinproc },
+    { WM_GETTEXT, sent|optional },
+    { 0 }
+};
+
+/* SetActiveWindow( hwnd ) hwnd not visible */
+static const struct message SetActiveWindowSeq3[] =
+{
+    { HCBT_ACTIVATE, hook },
+    { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
+    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
+    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOACTIVATE|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_ACTIVATEAPP, sent|wparam, 1 },
+    { WM_ACTIVATEAPP, sent|wparam, 1 },
+    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_ACTIVATE, sent|wparam, 1 },
+    { HCBT_SETFOCUS, hook },
+    { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
+    { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
+    { WM_SETFOCUS, sent|defwinproc },
+    { 0 }
+};
+/* SetActiveWindow( popup ) hwnd not visible, popup not visible */
+static const struct message SetActiveWindowSeq4[] =
+{
+    { HCBT_ACTIVATE, hook },
+    { WM_NCACTIVATE, sent|wparam, 0 },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ACTIVATE, sent|wparam, 0 },
+    { WM_QUERYNEWPALETTE, sent|wparam|lparam|optional, 0, 0 },
+    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE },
+    { WM_WINDOWPOSCHANGING, sent|wparam, SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE },
+    { WM_WINDOWPOSCHANGED, sent|wparam|optional, SWP_NOSIZE|SWP_NOMOVE|SWP_NOREDRAW|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE },
+    { WM_NCACTIVATE, sent|wparam, 1 },
+    { WM_GETTEXT, sent|defwinproc|optional },
+    { WM_ACTIVATE, sent|wparam, 1 },
+    { HCBT_SETFOCUS, hook },
+    { WM_KILLFOCUS, sent|defwinproc },
+    { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 0 },
+    { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
+    { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 1 },
+    { WM_IME_NOTIFY, sent|wparam|defwinproc|optional, 2 },
+    { WM_SETFOCUS, sent|defwinproc },
+    { 0 }
+};
+
+
+static void test_SetActiveWindow(void)
+{
+    HWND hwnd, popup, ret;
+
+    hwnd = CreateWindowExA(0, "TestWindowClass", "Test SetActiveWindow",
+                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
+                           100, 100, 200, 200, 0, 0, 0, NULL);
+
+    popup = CreateWindowExA(0, "TestWindowClass", "Test SetActiveWindow",
+                           WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_POPUP,
+                           100, 100, 200, 200, hwnd, 0, 0, NULL);
+
+    ok(hwnd != 0, "Failed to create overlapped window\n");
+    ok(popup != 0, "Failed to create popup window\n");
+    SetForegroundWindow( popup );
+    flush_sequence();
+
+    trace("SetActiveWindow(0)\n");
+    ret = SetActiveWindow(0);
+    ok( ret == popup, "Failed to SetActiveWindow(0)\n");
+    ok_sequence(SetActiveWindowSeq0, "SetActiveWindow(0)", FALSE);
+    flush_sequence();
+
+    trace("SetActiveWindow(hwnd), hwnd visible\n");
+    ret = SetActiveWindow(hwnd);
+    if (ret == hwnd) ok_sequence(SetActiveWindowSeq1, "SetActiveWindow(hwnd), hwnd visible", TRUE);
+    flush_sequence();
+
+    trace("SetActiveWindow(popup), hwnd visible, popup visible\n");
+    ret = SetActiveWindow(popup);
+    ok( ret == hwnd, "Failed to SetActiveWindow(popup), popup visible\n");
+    ok_sequence(SetActiveWindowSeq2, "SetActiveWindow(popup), hwnd visible, popup visible", FALSE);
+    flush_sequence();
+
+    ShowWindow(hwnd, SW_HIDE);
+    ShowWindow(popup, SW_HIDE);
+    flush_sequence();
+
+    trace("SetActiveWindow(hwnd), hwnd not visible\n");
+    ret = SetActiveWindow(hwnd);
+    ok( ret == NULL, "SetActiveWindow(hwnd), hwnd not visible, previous is %p\n", ret );
+    ok_sequence(SetActiveWindowSeq3, "SetActiveWindow(hwnd), hwnd not visible", TRUE);
+    flush_sequence();
+
+    trace("SetActiveWindow(popup), hwnd not visible, popup not visible\n");
+    ret = SetActiveWindow(popup);
+    ok( ret == hwnd, "Failed to SetActiveWindow(popup)\n");
+    ok_sequence(SetActiveWindowSeq4, "SetActiveWindow(popup), hwnd not visible, popup not visible", TRUE);
+    flush_sequence();
+
+    trace("done\n");
+
+    DestroyWindow(hwnd);
+}
+
 static const struct message SetForegroundWindowSeq[] =
 {
     { WM_NCACTIVATE, sent|wparam, 0 },
@@ -9888,6 +10929,7 @@ static void test_SetForegroundWindow(void)
                            WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                            100, 100, 200, 200, 0, 0, 0, NULL);
     ok (hwnd != 0, "Failed to create overlapped window\n");
+    SetForegroundWindow( hwnd );
     flush_sequence();
 
     trace("SetForegroundWindow( 0 )\n");
@@ -9917,7 +10959,7 @@ static void test_dbcs_wm_char(void)
 
     if (!pGetCPInfoExA)
     {
-        skip("GetCPInfoExA is not available\n");
+        win_skip("GetCPInfoExA is not available\n");
         return;
     }
 
@@ -10224,7 +11266,7 @@ static const struct message wm_lb_click_0[] =
     { HCBT_SETFOCUS, hook },
     { WM_KILLFOCUS, sent|parent },
     { WM_IME_SETCONTEXT, sent|wparam|optional|parent, 0 },
-    { WM_IME_SETCONTEXT, sent|wparam|optional, 1 },
+    { WM_IME_SETCONTEXT, sent|wparam|defwinproc|optional, 1 },
     { EVENT_OBJECT_FOCUS, winevent_hook|wparam|lparam, OBJID_CLIENT, 0 },
     { WM_SETFOCUS, sent|defwinproc },
 
@@ -10250,6 +11292,23 @@ static const struct message wm_lb_click_0[] =
     { WM_COMMAND, sent|wparam|parent, MAKEWPARAM(ID_LISTBOX, LBN_SELCHANGE) },
     { 0 }
 };
+static const struct message wm_lb_deletestring[] =
+{
+    { LB_DELETESTRING, sent|wparam|lparam, 0, 0 },
+    { WM_DELETEITEM, sent|wparam|parent|optional, ID_LISTBOX, 0 },
+    { WM_DRAWITEM, sent|wparam|parent|optional, ID_LISTBOX },
+    { WM_DRAWITEM, sent|wparam|parent|optional, ID_LISTBOX },
+    { 0 }
+};
+static const struct message wm_lb_deletestring_reset[] =
+{
+    { LB_DELETESTRING, sent|wparam|lparam, 0, 0 },
+    { LB_RESETCONTENT, sent|wparam|lparam|defwinproc|optional, 0, 0 },
+    { WM_DELETEITEM, sent|wparam|parent|optional, ID_LISTBOX, 0 },
+    { WM_DRAWITEM, sent|wparam|parent|optional, ID_LISTBOX },
+    { WM_DRAWITEM, sent|wparam|parent|optional, ID_LISTBOX },
+    { 0 }
+};
 
 #define check_lb_state(a1, a2, a3, a4, a5) check_lb_state_dbg(a1, a2, a3, a4, a5, __LINE__)
 
@@ -10257,9 +11316,9 @@ static LRESULT (WINAPI *listbox_orig_proc)(HWND, UINT, WPARAM, LPARAM);
 
 static LRESULT WINAPI listbox_hook_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
 {
-    static long defwndproc_counter = 0;
+    static LONG defwndproc_counter = 0;
     LRESULT ret;
-    struct message msg;
+    struct recvd_message msg;
 
     /* do not log painting messages */
     if (message != WM_PAINT &&
@@ -10268,16 +11327,15 @@ static LRESULT WINAPI listbox_hook_proc(HWND hwnd, UINT message, WPARAM wp, LPAR
         message != WM_ERASEBKGND &&
         message != WM_NCHITTEST &&
         message != WM_GETTEXT &&
-        message != WM_GETICON &&
-        message != WM_DEVICECHANGE)
+        !ignore_message( message ))
     {
-        trace("listbox: %p, %04x, %08lx, %08lx\n", hwnd, message, wp, lp);
-
+        msg.hwnd = hwnd;
         msg.message = message;
         msg.flags = sent|wparam|lparam;
         if (defwndproc_counter) msg.flags |= defwinproc;
         msg.wParam = wp;
         msg.lParam = lp;
+        msg.descr = "listbox";
         add_message(&msg);
     }
 
@@ -10299,7 +11357,9 @@ static void check_lb_state_dbg(HWND listbox, int count, int cur_sel,
     ret = CallWindowProcA(listbox_orig_proc, listbox, LB_GETCURSEL, 0, 0);
     ok_(__FILE__, line)(ret == cur_sel, "expected cur sel %d, got %ld\n", cur_sel, ret);
     ret = CallWindowProcA(listbox_orig_proc, listbox, LB_GETCARETINDEX, 0, 0);
-    ok_(__FILE__, line)(ret == caret_index, "expected caret index %d, got %ld\n", caret_index, ret);
+    ok_(__FILE__, line)(ret == caret_index ||
+                        broken(cur_sel == -1 && caret_index == 0 && ret == -1),  /* nt4 */
+                        "expected caret index %d, got %ld\n", caret_index, ret);
     ret = CallWindowProcA(listbox_orig_proc, listbox, LB_GETTOPINDEX, 0, 0);
     ok_(__FILE__, line)(ret == top_index, "expected top index %d, got %ld\n", top_index, ret);
 }
@@ -10359,16 +11419,605 @@ static void test_listbox_messages(void)
     check_lb_state(listbox, 3, 0, 0, 0);
     flush_sequence();
 
+    trace("deleting item 0\n");
+    ret = SendMessage(listbox, LB_DELETESTRING, 0, 0);
+    ok(ret == 2, "expected 2, got %ld\n", ret);
+    ok_sequence(wm_lb_deletestring, "LB_DELETESTRING 0", FALSE );
+    check_lb_state(listbox, 2, -1, 0, 0);
+    flush_sequence();
+
+    trace("deleting item 0\n");
+    ret = SendMessage(listbox, LB_DELETESTRING, 0, 0);
+    ok(ret == 1, "expected 1, got %ld\n", ret);
+    ok_sequence(wm_lb_deletestring, "LB_DELETESTRING 0", FALSE );
+    check_lb_state(listbox, 1, -1, 0, 0);
+    flush_sequence();
+
+    trace("deleting item 0\n");
+    ret = SendMessage(listbox, LB_DELETESTRING, 0, 0);
+    ok(ret == 0, "expected 0, got %ld\n", ret);
+    ok_sequence(wm_lb_deletestring_reset, "LB_DELETESTRING 0", FALSE );
+    check_lb_state(listbox, 0, -1, 0, 0);
+    flush_sequence();
+
+    trace("deleting item 0\n");
+    ret = SendMessage(listbox, LB_DELETESTRING, 0, 0);
+    ok(ret == LB_ERR, "expected LB_ERR, got %ld\n", ret);
+    check_lb_state(listbox, 0, -1, 0, 0);
+    flush_sequence();
+
     log_all_parent_messages--;
 
     DestroyWindow(listbox);
     DestroyWindow(parent);
 }
 
+/*************************** Menu test ******************************/
+static const struct message wm_popup_menu_1[] =
+{
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 },
+    { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'E', 0x20000001 },
+    { WM_SYSKEYDOWN, sent|wparam|lparam, 'E', 0x20000001 },
+    { WM_SYSCHAR, sent|wparam|lparam, 'e', 0x20000001 },
+    { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_KEYMENU, 'e' },
+    { WM_ENTERMENULOOP, sent|wparam|lparam, 0, 0 },
+    { WM_INITMENU, sent|lparam, 0, 0 },
+    { WM_MENUSELECT, sent|wparam, MAKEWPARAM(1,MF_HILITE|MF_POPUP) },
+    { WM_INITMENUPOPUP, sent|lparam, 0, 1 },
+    { HCBT_CREATEWND, hook|optional }, /* Win9x doesn't create a window */
+    { WM_MENUSELECT, sent|wparam, MAKEWPARAM(200,MF_HILITE) },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'E', 0xf0000001 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xd0000001 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0x10000001 },
+    { HCBT_DESTROYWND, hook|optional }, /* Win9x doesn't create a window */
+    { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
+    { WM_MENUSELECT, sent|wparam|lparam, MAKEWPARAM(0,0xffff), 0 },
+    { WM_EXITMENULOOP, sent|wparam|lparam, 0, 0 },
+    { WM_MENUCOMMAND, sent }, /* |wparam, 200 - Win9x */
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0xc0000001 },
+    { WM_KEYUP, sent|wparam|lparam, VK_RETURN, 0xc0000001 },
+    { 0 }
+};
+static const struct message wm_popup_menu_2[] =
+{
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 },
+    { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'F', 0x20000001 },
+    { WM_SYSKEYDOWN, sent|wparam|lparam, 'F', 0x20000001 },
+    { WM_SYSCHAR, sent|wparam|lparam, 'f', 0x20000001 },
+    { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_KEYMENU, 'f' },
+    { WM_ENTERMENULOOP, sent|wparam|lparam, 0, 0 },
+    { WM_INITMENU, sent|lparam, 0, 0 },
+    { WM_MENUSELECT, sent|wparam, MAKEWPARAM(0,MF_HILITE|MF_POPUP) },
+    { WM_INITMENUPOPUP, sent|lparam, 0, 0 },
+    { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(0,MF_HILITE|MF_POPUP) }, /* Win9x */
+    { WM_INITMENUPOPUP, sent|lparam|optional, 0, 0 }, /* Win9x */
+    { HCBT_CREATEWND, hook },
+    { WM_MENUSELECT, sent }, /*|wparam, MAKEWPARAM(0,MF_HILITE|MF_POPUP) - XP
+                               |wparam, MAKEWPARAM(100,MF_HILITE) - Win9x */
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'F', 0xf0000001 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xd0000001 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RIGHT, 0x10000001 },
+    { WM_INITMENUPOPUP, sent|lparam|optional, 0, 0 }, /* Win9x doesn't send it */
+    { HCBT_CREATEWND, hook|optional }, /* Win9x doesn't send it */
+    { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(100,MF_HILITE) },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RIGHT, 0xd0000001 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0x10000001 },
+    { HCBT_DESTROYWND, hook },
+    { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
+    { HCBT_DESTROYWND, hook|optional }, /* Win9x doesn't send it */
+    { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
+    { WM_MENUSELECT, sent|wparam|lparam, MAKEWPARAM(0,0xffff), 0 },
+    { WM_EXITMENULOOP, sent|wparam|lparam, 0, 0 },
+    { WM_MENUCOMMAND, sent }, /* |wparam, 100 - Win9x */
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0xc0000001 },
+    { WM_KEYUP, sent|wparam|lparam, VK_RETURN, 0xc0000001 },
+    { 0 }
+};
+static const struct message wm_popup_menu_3[] =
+{
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0x20000001 },
+    { WM_SYSKEYDOWN, sent|wparam|lparam, VK_MENU, 0x20000001 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'F', 0x20000001 },
+    { WM_SYSKEYDOWN, sent|wparam|lparam, 'F', 0x20000001 },
+    { WM_SYSCHAR, sent|wparam|lparam, 'f', 0x20000001 },
+    { HCBT_SYSCOMMAND, hook|wparam|lparam, SC_KEYMENU, 'f' },
+    { WM_ENTERMENULOOP, sent|wparam|lparam, 0, 0 },
+    { WM_INITMENU, sent|lparam, 0, 0 },
+    { WM_MENUSELECT, sent|wparam, MAKEWPARAM(0,MF_HILITE|MF_POPUP) },
+    { WM_INITMENUPOPUP, sent|lparam, 0, 0 },
+    { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(0,MF_HILITE|MF_POPUP) }, /* Win9x */
+    { WM_INITMENUPOPUP, sent|lparam|optional, 0, 0 }, /* Win9x */
+    { HCBT_CREATEWND, hook },
+    { WM_MENUSELECT, sent }, /*|wparam, MAKEWPARAM(0,MF_HILITE|MF_POPUP) - XP
+                               |wparam, MAKEWPARAM(100,MF_HILITE) - Win9x */
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, 'F', 0xf0000001 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_MENU, 0xd0000001 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RIGHT, 0x10000001 },
+    { WM_INITMENUPOPUP, sent|lparam|optional, 0, 0 }, /* Win9x doesn't send it */
+    { HCBT_CREATEWND, hook|optional }, /* Win9x doesn't send it */
+    { WM_MENUSELECT, sent|wparam|optional, MAKEWPARAM(100,MF_HILITE) },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RIGHT, 0xd0000001 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0x10000001 },
+    { HCBT_DESTROYWND, hook },
+    { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
+    { HCBT_DESTROYWND, hook|optional }, /* Win9x doesn't send it */
+    { WM_UNINITMENUPOPUP, sent|lparam, 0, 0 },
+    { WM_MENUSELECT, sent|wparam|lparam, MAKEWPARAM(0,0xffff), 0 },
+    { WM_EXITMENULOOP, sent|wparam|lparam, 0, 0 },
+    { WM_COMMAND, sent|wparam|lparam, 100, 0 },
+    { HCBT_KEYSKIPPED, hook|wparam|lparam|optional, VK_RETURN, 0xc0000001 },
+    { WM_KEYUP, sent|wparam|lparam, VK_RETURN, 0xc0000001 },
+    { 0 }
+};
+
+static LRESULT WINAPI parent_menu_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp)
+{
+    if (message == WM_ENTERIDLE ||
+        message == WM_INITMENU ||
+        message == WM_INITMENUPOPUP ||
+        message == WM_MENUSELECT ||
+        message == WM_PARENTNOTIFY ||
+        message == WM_ENTERMENULOOP ||
+        message == WM_EXITMENULOOP ||
+        message == WM_UNINITMENUPOPUP ||
+        message == WM_KEYDOWN ||
+        message == WM_KEYUP ||
+        message == WM_CHAR ||
+        message == WM_SYSKEYDOWN ||
+        message == WM_SYSKEYUP ||
+        message == WM_SYSCHAR ||
+        message == WM_COMMAND ||
+        message == WM_MENUCOMMAND)
+    {
+        struct recvd_message msg;
+
+        msg.hwnd = hwnd;
+        msg.message = message;
+        msg.flags = sent|wparam|lparam;
+        msg.wParam = wp;
+        msg.lParam = lp;
+        msg.descr = "parent_menu_proc";
+        add_message(&msg);
+    }
+
+    return DefWindowProcA(hwnd, message, wp, lp);
+}
+
+static void set_menu_style(HMENU hmenu, DWORD style)
+{
+    MENUINFO mi;
+    BOOL ret;
+
+    mi.cbSize = sizeof(mi);
+    mi.fMask = MIM_STYLE;
+    mi.dwStyle = style;
+    SetLastError(0xdeadbeef);
+    ret = pSetMenuInfo(hmenu, &mi);
+    ok(ret, "SetMenuInfo error %u\n", GetLastError());
+}
+
+static DWORD get_menu_style(HMENU hmenu)
+{
+    MENUINFO mi;
+    BOOL ret;
+
+    mi.cbSize = sizeof(mi);
+    mi.fMask = MIM_STYLE;
+    mi.dwStyle = 0;
+    SetLastError(0xdeadbeef);
+    ret = pGetMenuInfo(hmenu, &mi);
+    ok(ret, "GetMenuInfo error %u\n", GetLastError());
+
+    return mi.dwStyle;
+}
+
+static void test_menu_messages(void)
+{
+    MSG msg;
+    WNDCLASSA cls;
+    HMENU hmenu, hmenu_popup;
+    HWND hwnd;
+    DWORD style;
+
+    if (!pGetMenuInfo || !pSetMenuInfo)
+    {
+        win_skip("GetMenuInfo and/or SetMenuInfo are not available\n");
+        return;
+    }
+    cls.style = 0;
+    cls.lpfnWndProc = parent_menu_proc;
+    cls.cbClsExtra = 0;
+    cls.cbWndExtra = 0;
+    cls.hInstance = GetModuleHandleA(0);
+    cls.hIcon = 0;
+    cls.hCursor = LoadCursorA(0, IDC_ARROW);
+    cls.hbrBackground = GetStockObject(WHITE_BRUSH);
+    cls.lpszMenuName = NULL;
+    cls.lpszClassName = "TestMenuClass";
+    UnregisterClass(cls.lpszClassName, cls.hInstance);
+    if (!RegisterClassA(&cls)) assert(0);
+
+    SetLastError(0xdeadbeef);
+    hwnd = CreateWindowExA(0, "TestMenuClass", NULL, WS_OVERLAPPEDWINDOW | WS_VISIBLE,
+                           100, 100, 200, 200, 0, 0, 0, NULL);
+    ok(hwnd != 0, "LoadMenuA error %u\n", GetLastError());
+
+    SetLastError(0xdeadbeef);
+    hmenu = LoadMenuA(GetModuleHandle(0), MAKEINTRESOURCE(1));
+    ok(hmenu != 0, "LoadMenuA error %u\n", GetLastError());
+
+    SetMenu(hwnd, hmenu);
+    SetForegroundWindow( hwnd );
+
+    set_menu_style(hmenu, MNS_NOTIFYBYPOS);
+    style = get_menu_style(hmenu);
+    ok(style == MNS_NOTIFYBYPOS, "expected MNS_NOTIFYBYPOS, got %u\n", style);
+
+    hmenu_popup = GetSubMenu(hmenu, 0);
+    ok(hmenu_popup != 0, "GetSubMenu returned 0 for submenu 0\n");
+    style = get_menu_style(hmenu_popup);
+    ok(style == 0, "expected 0, got %u\n", style);
+
+    hmenu_popup = GetSubMenu(hmenu_popup, 0);
+    ok(hmenu_popup != 0, "GetSubMenu returned 0 for submenu 0\n");
+    style = get_menu_style(hmenu_popup);
+    ok(style == 0, "expected 0, got %u\n", style);
+
+    /* Alt+E, Enter */
+    trace("testing a popup menu command\n");
+    flush_sequence();
+    keybd_event(VK_MENU, 0, 0, 0);
+    keybd_event('E', 0, 0, 0);
+    keybd_event('E', 0, KEYEVENTF_KEYUP, 0);
+    keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
+    keybd_event(VK_RETURN, 0, 0, 0);
+    keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
+    while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
+    {
+        TranslateMessage(&msg);
+        DispatchMessage(&msg);
+    }
+    if (!sequence_cnt)  /* we didn't get any message */
+    {
+        skip( "queuing key events not supported\n" );
+        goto done;
+    }
+    /* win98 queues only a WM_KEYUP and doesn't start menu tracking */
+    if (sequence[0].message == WM_KEYUP && sequence[0].wParam == VK_MENU)
+    {
+        win_skip( "menu tracking through VK_MENU not supported\n" );
+        goto done;
+    }
+    ok_sequence(wm_popup_menu_1, "popup menu command", FALSE);
+
+    /* Alt+F, Right, Enter */
+    trace("testing submenu of a popup menu command\n");
+    flush_sequence();
+    keybd_event(VK_MENU, 0, 0, 0);
+    keybd_event('F', 0, 0, 0);
+    keybd_event('F', 0, KEYEVENTF_KEYUP, 0);
+    keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
+    keybd_event(VK_RIGHT, 0, 0, 0);
+    keybd_event(VK_RIGHT, 0, KEYEVENTF_KEYUP, 0);
+    keybd_event(VK_RETURN, 0, 0, 0);
+    keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
+    while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
+    {
+        TranslateMessage(&msg);
+        DispatchMessage(&msg);
+    }
+    ok_sequence(wm_popup_menu_2, "submenu of a popup menu command", FALSE);
+
+    set_menu_style(hmenu, 0);
+    style = get_menu_style(hmenu);
+    ok(style == 0, "expected 0, got %u\n", style);
+
+    hmenu_popup = GetSubMenu(hmenu, 0);
+    ok(hmenu_popup != 0, "GetSubMenu returned 0 for submenu 0\n");
+    set_menu_style(hmenu_popup, MNS_NOTIFYBYPOS);
+    style = get_menu_style(hmenu_popup);
+    ok(style == MNS_NOTIFYBYPOS, "expected MNS_NOTIFYBYPOS, got %u\n", style);
+
+    hmenu_popup = GetSubMenu(hmenu_popup, 0);
+    ok(hmenu_popup != 0, "GetSubMenu returned 0 for submenu 0\n");
+    style = get_menu_style(hmenu_popup);
+    ok(style == 0, "expected 0, got %u\n", style);
+
+    /* Alt+F, Right, Enter */
+    trace("testing submenu of a popup menu command\n");
+    flush_sequence();
+    keybd_event(VK_MENU, 0, 0, 0);
+    keybd_event('F', 0, 0, 0);
+    keybd_event('F', 0, KEYEVENTF_KEYUP, 0);
+    keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
+    keybd_event(VK_RIGHT, 0, 0, 0);
+    keybd_event(VK_RIGHT, 0, KEYEVENTF_KEYUP, 0);
+    keybd_event(VK_RETURN, 0, 0, 0);
+    keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
+    while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
+    {
+        TranslateMessage(&msg);
+        DispatchMessage(&msg);
+    }
+    ok_sequence(wm_popup_menu_3, "submenu of a popup menu command", FALSE);
+
+done:
+    DestroyWindow(hwnd);
+    DestroyMenu(hmenu);
+}
+
+
+static void test_paintingloop(void)
+{
+    HWND hwnd;
+
+    paint_loop_done = 0;
+    hwnd = CreateWindowExA(0x0,"PaintLoopWindowClass",
+                               "PaintLoopWindowClass",WS_OVERLAPPEDWINDOW,
+                                100, 100, 100, 100, 0, 0, 0, NULL );
+    ok(hwnd != 0, "PaintLoop window error %u\n", GetLastError());
+    ShowWindow(hwnd,SW_NORMAL);
+    SetFocus(hwnd);
+
+    while (!paint_loop_done)
+    {
+        MSG msg;
+        if (PeekMessageA(&msg, 0, 0, 0, 1))
+        {
+            TranslateMessage(&msg);
+            DispatchMessage(&msg);
+        }
+    }
+    DestroyWindow(hwnd);
+}
+
+static void test_defwinproc(void)
+{
+    HWND hwnd;
+    MSG msg;
+    int gotwmquit = FALSE;
+    hwnd = CreateWindowExA(0, "static", "test_defwndproc", WS_POPUP, 0,0,0,0,0,0,0, NULL);
+    assert(hwnd);
+    DefWindowProcA( hwnd, WM_ENDSESSION, 1, 0);
+    while (PeekMessageA( &msg, 0, 0, 0, PM_REMOVE )) {
+        if( msg.message == WM_QUIT) gotwmquit = TRUE;
+        DispatchMessageA( &msg );
+    }
+    ok( gotwmquit == FALSE, "Unexpected WM_QUIT message!\n");
+    DestroyWindow( hwnd);
+}
+
+#define clear_clipboard(hwnd)  clear_clipboard_(__LINE__, (hwnd))
+static void clear_clipboard_(int line, HWND hWnd)
+{
+    BOOL succ;
+    succ = OpenClipboard(hWnd);
+    ok_(__FILE__, line)(succ, "OpenClipboard failed, err=%u\n", GetLastError());
+    succ = EmptyClipboard();
+    ok_(__FILE__, line)(succ, "EmptyClipboard failed, err=%u\n", GetLastError());
+    succ = CloseClipboard();
+    ok_(__FILE__, line)(succ, "CloseClipboard failed, err=%u\n", GetLastError());
+}
+
+#define expect_HWND(expected, got) expect_HWND_(__LINE__, (expected), (got))
+static void expect_HWND_(int line, HWND expected, HWND got)
+{
+    ok_(__FILE__, line)(got==expected, "Expected %p, got %p\n", expected, got);
+}
+
+static WNDPROC pOldViewerProc;
+
+static LRESULT CALLBACK recursive_viewer_proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
+{
+    static BOOL recursion_guard;
+
+    if (message == WM_DRAWCLIPBOARD && !recursion_guard)
+    {
+        recursion_guard = TRUE;
+        clear_clipboard(hWnd);
+        recursion_guard = FALSE;
+    }
+    return CallWindowProcA(pOldViewerProc, hWnd, message, wParam, lParam);
+}
+
+static void test_clipboard_viewers(void)
+{
+    static struct message wm_change_cb_chain[] =
+    {
+        { WM_CHANGECBCHAIN, sent|wparam|lparam, 0, 0 },
+        { 0 }
+    };
+    static const struct message wm_clipboard_destroyed[] =
+    {
+        { WM_DESTROYCLIPBOARD, sent|wparam|lparam, 0, 0 },
+        { 0 }
+    };
+    static struct message wm_clipboard_changed[] =
+    {
+        { WM_DRAWCLIPBOARD, sent|wparam|lparam, 0, 0 },
+        { 0 }
+    };
+    static struct message wm_clipboard_changed_and_owned[] =
+    {
+        { WM_DESTROYCLIPBOARD, sent|wparam|lparam, 0, 0 },
+        { WM_DRAWCLIPBOARD, sent|wparam|lparam, 0, 0 },
+        { 0 }
+    };
+
+    HINSTANCE hInst = GetModuleHandleA(NULL);
+    HWND hWnd1, hWnd2, hWnd3;
+    HWND hOrigViewer;
+    HWND hRet;
+
+    hWnd1 = CreateWindowExA(0, "TestWindowClass", "Clipboard viewer test wnd 1",
+        WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
+        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
+        GetDesktopWindow(), NULL, hInst, NULL);
+    hWnd2 = CreateWindowExA(0, "SimpleWindowClass", "Clipboard viewer test wnd 2",
+        WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
+        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
+        GetDesktopWindow(), NULL, hInst, NULL);
+    hWnd3 = CreateWindowExA(0, "SimpleWindowClass", "Clipboard viewer test wnd 3",
+        WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX,
+        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
+        GetDesktopWindow(), NULL, hInst, NULL);
+    trace("clipbd viewers: hWnd1=%p, hWnd2=%p, hWnd3=%p\n", hWnd1, hWnd2, hWnd3);
+    assert(hWnd1 && hWnd2 && hWnd3);
+
+    flush_sequence();
+
+    /* Test getting the clipboard viewer and setting the viewer to NULL. */
+    hOrigViewer = GetClipboardViewer();
+    hRet = SetClipboardViewer(NULL);
+    ok_sequence(WmEmptySeq, "set viewer to NULL", FALSE);
+    expect_HWND(hOrigViewer, hRet);
+    expect_HWND(NULL, GetClipboardViewer());
+
+    /* Test registering hWnd1 as a viewer. */
+    hRet = SetClipboardViewer(hWnd1);
+    wm_clipboard_changed[0].wParam = (WPARAM) GetClipboardOwner();
+    ok_sequence(wm_clipboard_changed, "set viewer NULL->1", FALSE);
+    expect_HWND(NULL, hRet);
+    expect_HWND(hWnd1, GetClipboardViewer());
+
+    /* Test that changing the clipboard actually refreshes the registered viewer. */
+    clear_clipboard(hWnd1);
+    wm_clipboard_changed[0].wParam = (WPARAM) GetClipboardOwner();
+    ok_sequence(wm_clipboard_changed, "clear clipbd (viewer=owner=1)", FALSE);
+
+    /* Again, but with different owner. */
+    clear_clipboard(hWnd2);
+    wm_clipboard_changed_and_owned[1].wParam = (WPARAM) GetClipboardOwner();
+    ok_sequence(wm_clipboard_changed_and_owned, "clear clipbd (viewer=1, owner=2)", FALSE);
+
+    /* Test re-registering same window. */
+    hRet = SetClipboardViewer(hWnd1);
+    wm_clipboard_changed[0].wParam = (WPARAM) GetClipboardOwner();
+    ok_sequence(wm_clipboard_changed, "set viewer 1->1", FALSE);
+    expect_HWND(hWnd1, hRet);
+    expect_HWND(hWnd1, GetClipboardViewer());
+
+    /* Test ChangeClipboardChain. */
+    ChangeClipboardChain(hWnd2, hWnd3);
+    wm_change_cb_chain[0].wParam = (WPARAM) hWnd2;
+    wm_change_cb_chain[0].lParam = (LPARAM) hWnd3;
+    ok_sequence(wm_change_cb_chain, "change chain (viewer=1, remove=2, next=3)", FALSE);
+    expect_HWND(hWnd1, GetClipboardViewer());
+
+    ChangeClipboardChain(hWnd2, NULL);
+    wm_change_cb_chain[0].wParam = (WPARAM) hWnd2;
+    wm_change_cb_chain[0].lParam = 0;
+    ok_sequence(wm_change_cb_chain, "change chain (viewer=1, remove=2, next=NULL)", FALSE);
+    expect_HWND(hWnd1, GetClipboardViewer());
+
+    ChangeClipboardChain(NULL, hWnd2);
+    ok_sequence(WmEmptySeq, "change chain (viewer=1, remove=NULL, next=2)", TRUE);
+    expect_HWND(hWnd1, GetClipboardViewer());
+
+    /* Actually change clipboard viewer with ChangeClipboardChain. */
+    ChangeClipboardChain(hWnd1, hWnd2);
+    ok_sequence(WmEmptySeq, "change chain (viewer=remove=1, next=2)", FALSE);
+    expect_HWND(hWnd2, GetClipboardViewer());
+
+    /* Test that no refresh messages are sent when viewer has unregistered. */
+    clear_clipboard(hWnd2);
+    ok_sequence(WmEmptySeq, "clear clipd (viewer=2, owner=1)", FALSE);
+
+    /* Register hWnd1 again. */
+    ChangeClipboardChain(hWnd2, hWnd1);
+    ok_sequence(WmEmptySeq, "change chain (viewer=remove=2, next=1)", FALSE);
+    expect_HWND(hWnd1, GetClipboardViewer());
+
+    /* Subclass hWnd1 so that when it receives a WM_DRAWCLIPBOARD message, it
+     * changes the clipboard. When this happens, the system shouldn't send
+     * another WM_DRAWCLIPBOARD (as this could cause an infinite loop).
+     */
+    pOldViewerProc = (WNDPROC) SetWindowLongPtrA(hWnd1, GWLP_WNDPROC, (LONG_PTR) recursive_viewer_proc);
+    clear_clipboard(hWnd2);
+    /* The clipboard owner is changed in recursive_viewer_proc: */
+    wm_clipboard_changed[0].wParam = (WPARAM) hWnd2;
+    ok_sequence(wm_clipboard_changed, "recursive clear clipbd (viewer=1, owner=2)", TRUE);
+
+    /* Test unregistering. */
+    ChangeClipboardChain(hWnd1, NULL);
+    ok_sequence(WmEmptySeq, "change chain (viewer=remove=1, next=NULL)", FALSE);
+    expect_HWND(NULL, GetClipboardViewer());
+
+    clear_clipboard(hWnd1);
+    ok_sequence(wm_clipboard_destroyed, "clear clipbd (no viewer, owner=1)", FALSE);
+
+    DestroyWindow(hWnd1);
+    DestroyWindow(hWnd2);
+    DestroyWindow(hWnd3);
+    SetClipboardViewer(hOrigViewer);
+}
+
+static void test_PostMessage(void)
+{
+    static const struct
+    {
+        HWND hwnd;
+        BOOL ret;
+    } data[] =
+    {
+        { HWND_TOP /* 0 */, TRUE },
+        { HWND_BROADCAST, TRUE },
+        { HWND_BOTTOM, TRUE },
+        { HWND_TOPMOST, TRUE },
+        { HWND_NOTOPMOST, FALSE },
+        { HWND_MESSAGE, FALSE },
+        { (HWND)0xdeadbeef, FALSE }
+    };
+    int i;
+    HWND hwnd;
+    BOOL ret;
+    MSG msg;
+    static const WCHAR staticW[] = {'s','t','a','t','i','c',0};
+
+    SetLastError(0xdeadbeef);
+    hwnd = CreateWindowExW(0, staticW, NULL, WS_POPUP, 0,0,0,0,0,0,0, NULL);
+    if (!hwnd && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
+    {
+        win_skip("Skipping some PostMessage tests on Win9x/WinMe\n");
+        return;
+    }
+    assert(hwnd);
+
+    flush_events();
+
+    PostMessage(hwnd, WM_USER+1, 0x1234, 0x5678);
+    PostMessage(0, WM_USER+2, 0x5678, 0x1234);
+
+    for (i = 0; i < sizeof(data)/sizeof(data[0]); i++)
+    {
+        memset(&msg, 0xab, sizeof(msg));
+        ret = PeekMessageA(&msg, data[i].hwnd, 0, 0, PM_NOREMOVE);
+        ok(ret == data[i].ret, "%d: hwnd %p expected %d, got %d\n", i, data[i].hwnd, data[i].ret, ret);
+        if (data[i].ret)
+        {
+            if (data[i].hwnd)
+                ok(ret && msg.hwnd == 0 && msg.message == WM_USER+2 &&
+                   msg.wParam == 0x5678 && msg.lParam == 0x1234,
+                   "%d: got ret %d hwnd %p msg %04x wParam %08lx lParam %08lx instead of TRUE/0/WM_USER+2/0x5678/0x1234\n",
+                   i, ret, msg.hwnd, msg.message, msg.wParam, msg.lParam);
+            else
+                ok(ret && msg.hwnd == hwnd && msg.message == WM_USER+1 &&
+                   msg.wParam == 0x1234 && msg.lParam == 0x5678,
+                   "%d: got ret %d hwnd %p msg %04x wParam %08lx lParam %08lx instead of TRUE/%p/WM_USER+1/0x1234/0x5678\n",
+                   i, ret, msg.hwnd, msg.message, msg.wParam, msg.lParam, msg.hwnd);
+        }
+    }
+
+    DestroyWindow(hwnd);
+    flush_events();
+}
+
 START_TEST(msg)
 {
     BOOL ret;
-    FARPROC pIsWinEventHookInstalled = 0;/*GetProcAddress(user32, "IsWinEventHookInstalled");*/
+    BOOL (WINAPI *pIsWinEventHookInstalled)(DWORD)= 0;/*GetProcAddress(user32, "IsWinEventHookInstalled");*/
 
     init_procs();
 
@@ -10376,25 +12025,22 @@ START_TEST(msg)
 
     if (pSetWinEventHook)
     {
-       hEvent_hook = (HWINEVENTHOOK)pSetWinEventHook(EVENT_MIN, EVENT_MAX,
-                                                     GetModuleHandleA(0),
-                                                     win_event_proc,
-                                                     0,
-                                                     GetCurrentThreadId(),
-                                                     WINEVENT_INCONTEXT);
-       assert(hEvent_hook);
-
-       if (pIsWinEventHookInstalled)
+        hEvent_hook = pSetWinEventHook(EVENT_MIN, EVENT_MAX,
+                                       GetModuleHandleA(0), win_event_proc,
+                                       0, GetCurrentThreadId(),
+                                       WINEVENT_INCONTEXT);
+        if (pIsWinEventHookInstalled && hEvent_hook)
        {
            UINT event;
            for (event = EVENT_MIN; event <= EVENT_MAX; event++)
                ok(pIsWinEventHookInstalled(event), "IsWinEventHookInstalled(%u) failed\n", event);
        }
     }
+    if (!hEvent_hook) win_skip( "no win event hook support\n" );
 
     cbt_hook_thread_id = GetCurrentThreadId();
     hCBT_hook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
-    assert(hCBT_hook);
+    if (!hCBT_hook) win_skip( "cannot set global hook, will skip hook tests\n" );
 
     test_winevents();
 
@@ -10409,11 +12055,13 @@ START_TEST(msg)
     hEvent_hook = 0;
 #endif
 
+    test_PostMessage();
     test_ShowWindow();
     test_PeekMessage();
     test_PeekMessage2();
     test_scrollwindowex();
     test_messages();
+    test_setwindowpos();
     test_showwindow();
     invisible_parent_tests();
     test_mdi_messages();
@@ -10428,15 +12076,16 @@ START_TEST(msg)
     test_accelerators();
     test_timers();
     test_timers_no_wnd();
-    test_set_hook();
+    if (hCBT_hook) test_set_hook();
     test_DestroyWindow();
     test_DispatchMessage();
     test_SendMessageTimeout();
     test_edit_messages();
     test_quit_message();
+    test_SetActiveWindow();
 
     if (!pTrackMouseEvent)
-        skip("TrackMouseEvent is not available\n");
+        win_skip("TrackMouseEvent is not available\n");
     else
         test_TrackMouseEvent();
 
@@ -10444,11 +12093,18 @@ START_TEST(msg)
     test_sys_menu();
     test_dialog_messages();
     test_nullCallback();
-    test_SetForegroundWindow();
     test_dbcs_wm_char();
+    test_menu_messages();
+    test_paintingloop();
+    test_defwinproc();
+    test_clipboard_viewers();
+    /* keep it the last test, under Windows it tends to break the tests
+     * which rely on active/foreground windows being correct.
+     */
+    test_SetForegroundWindow();
 
     UnhookWindowsHookEx(hCBT_hook);
-    if (pUnhookWinEvent)
+    if (pUnhookWinEvent && hEvent_hook)
     {
        ret = pUnhookWinEvent(hEvent_hook);
        ok( ret, "UnhookWinEvent error %d\n", GetLastError());
@@ -10458,6 +12114,4 @@ START_TEST(msg)
           GetLastError() == 0xdeadbeef, /* Win9x */
            "unexpected error %d\n", GetLastError());
     }
-    else
-        skip("UnhookWinEvent is not available\n");
 }