ReactOS  0.4.14-dev-98-gb0d4763
multithrdwin.c
Go to the documentation of this file.
1 #include <windows.h>
2 #include <stdio.h>
3 
5 
6 typedef struct _THRDCREATEWIN
7 {
17 
20 static THRDCREATEWIN wnds[3];
21 
23 
26 {
27  MSG msg;
28  char caption[64];
30 
31  sprintf(caption, cw->Caption, GetCurrentThreadId());
32 
33  cw->Window = CreateWindow("MultiClass",
34  caption,
35  cw->Style | WS_VISIBLE,
36  cw->Position.x,
37  cw->Position.y,
38  cw->Size.cx,
39  cw->Size.cy,
40  (cw->Parent ? *(cw->Parent) : 0),
41  NULL,
43  NULL);
44 
46 
47  if(!cw->Window)
48  {
49  fprintf(stderr, "CreateWindow failed (last error 0x%lX)\n",
50  GetLastError());
51  return 1;
52  }
53  CreateWindow("BUTTON","Sleep",WS_CHILD|WS_VISIBLE|WS_TABSTOP|WS_GROUP|BS_PUSHBUTTON, 10, 10, 70, 23, cw->Window, (PVOID)1, hAppInstance, NULL);
57  while(GetMessage(&msg, NULL, 0, 0))
58  {
61  }
62 
63  return 0;
64 }
65 
66 int WINAPI
68  HINSTANCE hPrevInstance,
69  LPSTR lpszCmdLine,
70  int nCmdShow)
71 {
72  WNDCLASS wc;
73  int i;
74  HANDLE Threads[3];
75 
77 
79  FALSE,
80  FALSE,
81  NULL);
82 
83  if(!WinCreatedEvent)
84  {
85  fprintf(stderr, "Failed to create event (last error 0x%lX)\n",
86  GetLastError());
87  return 1;
88  }
89 
90  wc.lpszClassName = "MultiClass";
93  wc.hInstance = hInstance;
96  wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
97  wc.lpszMenuName = NULL;
98  wc.cbClsExtra = 0;
99  wc.cbWndExtra = 0;
100  if (RegisterClass(&wc) == 0)
101  {
102  fprintf(stderr, "RegisterClass failed (last error 0x%lX)\n",
103  GetLastError());
104  return(1);
105  }
106 
107  wnds[0].Caption = "TopLevel1 (ThreadID: %d)";
108  wnds[0].Parent = NULL;
110  wnds[0].Position.x = wnds[0].Position.y = 0;
111  wnds[0].Size.cx = 320;
112  wnds[0].Size.cy = 240;
113 
114  wnds[1].Caption = "Child1 of TopLevel1 (ThreadID: %d)";
115  wnds[1].Parent = &wnds[0].Window;
117  wnds[1].Position.x = 20;
118  wnds[1].Position.y = 120;
119  wnds[1].Size.cx = wnds[1].Size.cy = 240;
120 
121  wnds[2].Caption = "TopLevel2 (ThreadID: %d)";
122  wnds[2].Parent = NULL;
124  wnds[2].Position.x = 400;
125  wnds[2].Position.y = 0;
126  wnds[2].Size.cx = 160;
127  wnds[2].Size.cy = 490;
128 
129  for(i = 0; i < (sizeof(wnds) / sizeof(THRDCREATEWIN)); i++)
130  {
132  0,
134  &wnds[i],
135  0,
136  &wnds[i].ThreadId);
137  Threads[i] = wnds[i].hThread;
138  if(!wnds[i].hThread)
139  {
140  fprintf(stderr, "CreateThread #%i failed (last error 0x%lX)\n",
141  i, GetLastError());
142  return 1;
143  }
145  }
146 
147  WaitForMultipleObjects(sizeof(Threads) / sizeof(HANDLE), &Threads[0], TRUE, INFINITE);
148 
149  UnregisterClass("MultiClass", hInstance);
150 
151  return 0;
152 }
153 
155 {
156  PAINTSTRUCT ps;
157  HDC hDC;
158  RECT Client;
159  HBRUSH Brush;
160  DWORD_PTR Ret;
161 
162  static COLORREF Colors[] =
163  {
164  RGB(0x00, 0x00, 0x00),
165  RGB(0x80, 0x00, 0x00),
166  RGB(0x00, 0x80, 0x00),
167  RGB(0x00, 0x00, 0x80),
168  RGB(0x80, 0x80, 0x00),
169  RGB(0x80, 0x00, 0x80),
170  RGB(0x00, 0x80, 0x80),
171  RGB(0x80, 0x80, 0x80),
172  RGB(0xff, 0x00, 0x00),
173  RGB(0x00, 0xff, 0x00),
174  RGB(0x00, 0x00, 0xff),
175  RGB(0xff, 0xff, 0x00),
176  RGB(0xff, 0x00, 0xff),
177  RGB(0x00, 0xff, 0xff),
178  RGB(0xff, 0xff, 0xff)
179  };
180  static unsigned CurrentColor = 0;
181 
182  switch(msg)
183  {
184  case WM_PAINT:
185  hDC = BeginPaint(hWnd, &ps);
187  Brush = CreateSolidBrush(Colors[CurrentColor]);
188  FillRect(hDC, &Client, Brush);
190  CurrentColor++;
191  if (sizeof(Colors) / sizeof(Colors[0]) <= CurrentColor)
192  {
193  CurrentColor = 0;
194  }
195  EndPaint(hWnd, &ps);
196  break;
197 
198  case WM_COMMAND:
199  switch(LOWORD(wParam))
200  {
201  case 1:
202  Sleep(20000);
203  break;
204  case 2:
205  case 3:
206  case 4:
207  if(SendMessageTimeout(wnds[LOWORD(wParam) - 2].Window, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 1000, &Ret))
208  {
209  DbgPrint("SendMessageTimeout() OK");
210  MessageBox(hWnd, "SendMessageTimeout() OK", NULL, 0);
211  }
212  else
213  {
214  if(GetLastError() == ERROR_TIMEOUT)
215  {
216  DbgPrint("SendMessageTimeout() Timeout");
217  MessageBox(hWnd, "SendMessageTimeout() Timeout", NULL, 0);
218  }
219  else
220  {
221  DbgPrint("SendMessageTimeout() Failed");
222  MessageBox(hWnd, "SendMessageTimeout() Failed", NULL, 0);
223  }
224  }
225  break;
226  }
227  break;
228 
229  case WM_DESTROY:
230  PostQuitMessage(0);
231  break;
232 
233  default:
234  return DefWindowProc(hWnd, msg, wParam, lParam);
235  }
236 
237  return 0;
238 }
#define CreateEvent
Definition: winbase.h:3572
#define WS_GROUP
Definition: pedump.c:633
#define WS_CLIPSIBLINGS
Definition: pedump.c:618
#define WM_NULL
Definition: winuser.h:1589
HANDLE hThread
Definition: multithrdwin.c:8
#define DispatchMessage
Definition: winuser.h:5666
BOOL WINAPI TranslateMessage(_In_ const MSG *)
struct _THRDCREATEWIN * PTHRDCREATEWIN
#define TRUE
Definition: types.h:120
Definition: client.c:29
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
long y
Definition: polytest.cpp:48
#define CreateWindow
Definition: winuser.h:5655
long x
Definition: polytest.cpp:48
#define ERROR_TIMEOUT
Definition: winerror.h:941
TW_UINT32 TW_UINT16 TW_UINT16 MSG
Definition: twain.h:1827
static HDC
Definition: imagelist.c:92
#define CALLBACK
Definition: compat.h:27
HWND hWnd
Definition: settings.c:17
int cbClsExtra
Definition: winuser.h:3139
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
#define CS_HREDRAW
Definition: winuser.h:648
LPCSTR lpszMenuName
Definition: winuser.h:3145
#define IDI_APPLICATION
Definition: winuser.h:699
UINT_PTR WPARAM
Definition: windef.h:207
HBRUSH hbrBackground
Definition: winuser.h:3144
#define WS_CHILD
Definition: pedump.c:617
BOOL WINAPI EndPaint(_In_ HWND, _In_ const PAINTSTRUCT *)
char * LPSTR
Definition: xmlstorage.h:182
#define WS_CLIPCHILDREN
Definition: pedump.c:619
DWORD WINAPI WaitForMultipleObjects(IN DWORD nCount, IN CONST HANDLE *lpHandles, IN BOOL bWaitAll, IN DWORD dwMilliseconds)
Definition: synch.c:151
#define COLOR_WINDOW
Definition: winuser.h:908
WPARAM wParam
Definition: combotst.c:138
#define SMTO_ABORTIFHUNG
Definition: winuser.h:1209
#define sprintf(buf, format,...)
Definition: sprintf.c:55
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
Definition: window.c:29
int cbWndExtra
Definition: winuser.h:3140
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
HINSTANCE hInstance
Definition: charmap.c:20
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
HDC WINAPI BeginPaint(_In_ HWND, _Out_ LPPAINTSTRUCT)
HCURSOR hCursor
Definition: winuser.h:3143
CHAR * PCH
Definition: ntbasedef.h:398
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:136
void Brush(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2, COLORREF color, LONG style)
Definition: drawing.cpp:160
smooth NULL
Definition: ftsmooth.c:416
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
Definition: multithrdwin.c:67
LONG cx
Definition: windef.h:319
LONG_PTR LPARAM
Definition: windef.h:208
static THRDCREATEWIN wnds[3]
Definition: multithrdwin.c:20
DWORD WINAPI GetCurrentThreadId(VOID)
Definition: thread.c:458
#define DefWindowProc
Definition: ros2win.h:31
#define WM_DESTROY
Definition: winuser.h:1591
LRESULT WINAPI MultiWndProc(HWND, UINT, WPARAM, LPARAM)
Definition: multithrdwin.c:154
#define WS_CAPTION
Definition: pedump.c:624
#define CS_VREDRAW
Definition: winuser.h:653
#define SendMessageTimeout
Definition: winuser.h:5746
DWORD COLORREF
Definition: windef.h:285
Colors
Definition: ansiprsr.h:4
#define RGB(r, g, b)
Definition: wingdi.h:2935
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD WINAPI WindowThreadProc(LPVOID lpParameter)
Definition: multithrdwin.c:25
#define WM_PAINT
Definition: winuser.h:1602
static HINSTANCE hAppInstance
Definition: multithrdwin.c:18
struct _THRDCREATEWIN THRDCREATEWIN
LPVOID lpParameter
Definition: kernel32.h:241
#define MessageBox
Definition: winuser.h:5723
#define WS_TABSTOP
Definition: pedump.c:634
#define WM_COMMAND
Definition: winuser.h:1722
LPCSTR lpszClassName
Definition: winuser.h:3146
uint32_t DWORD_PTR
Definition: typedefs.h:63
#define LoadCursor
Definition: winuser.h:5713
#define BS_PUSHBUTTON
Definition: pedump.c:651
UINT style
Definition: winuser.h:3137
static HDC hDC
Definition: 3dtext.c:33
BOOL WINAPI GetClientRect(_In_ HWND, _Out_ LPRECT)
#define WS_BORDER
Definition: pedump.c:625
static HANDLE WinCreatedEvent
Definition: multithrdwin.c:19
HINSTANCE hInstance
Definition: winuser.h:3141
unsigned int UINT
Definition: ndis.h:50
HBRUSH WINAPI CreateSolidBrush(_In_ COLORREF)
#define GetMessage
Definition: winuser.h:5691
HANDLE hThread
Definition: wizard.c:27
ULONG DbgPrint(PCH Format,...)
WNDPROC lpfnWndProc
Definition: winuser.h:3138
#define msg(x)
Definition: auth_time.c:54
#define IDC_ARROW
Definition: winuser.h:682
#define WS_OVERLAPPEDWINDOW
Definition: pedump.c:637
unsigned int ULONG
Definition: retypes.h:1
FILE * stderr
#define RegisterClass
Definition: winuser.h:5737
#define LoadIcon
Definition: winuser.h:5714
LONG_PTR LRESULT
Definition: windef.h:209
#define INFINITE
Definition: serial.h:102
int WINAPI FillRect(HDC, LPCRECT, HBRUSH)
#define WS_VISIBLE
Definition: pedump.c:620
#define UnregisterClass
Definition: winuser.h:5762
#define WS_SYSMENU
Definition: pedump.c:629
LONG cy
Definition: windef.h:320
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
__analysis_noreturn void WINAPI PostQuitMessage(_In_ int)
HICON hIcon
Definition: winuser.h:3142