int MsgBoxTimeout(HWND h, LPCTSTR text, LPCTSTR cap, UINT style, UINT timeout) {
 unsigned id;
 HANDLE cth = GetCurrentThreadId(),
 th = _beginthreadex(NULL, 0,
  lambda __stdcall(UINT timeout) int {
  HWND hmb = 0;
  Sleep(timeout);
  EnumThreadWindows(cth,
   lambda __stdcall(HWND h, LPARAM l) BOOL {
    char buf[256];
    int Ret = GetClassName(h, buf, 256);
    return (Ret && !strcmp(buf, "#32770"), hmb = h) ? 0 : 1; }
  , NULL);
  if (hmb && IsWindow(hmb))
    EndDialog(hmb, 100), SendMessage(hmb,WM_CLOSE,0,0);
 }, timeout, 0, &id);
 int r = MessageBox(h,text,cap,style);
 if (th) CloseHandle(th);
 return r;
}

20行だけど。