多线程问题!急!
先把代码贴出来:
#include"windows.h"#include"process.h"HWND hwndChild[2] = {NULL,NULL} ;BOOL thread1 = TRUE,thread2 = TRUE;HWND hwnd;// 此代码模块中包含的函数的前向声明:ATOM MyRegisterClass(HINSTANCE hInstance);BOOL InitInstance(HINSTANCE, int);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){ // TODO: 在此放置代码。 MSG msg; //注册 MyRegisterClass(hInstance); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } // 主消息循环: while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg) ; } return (int) msg.wParam;}//ATOM MyRegisterClass(HINSTANCE hInstance){ WNDCLASSEX wndclass; wndclass.cbSize = sizeof(WNDCLASSEX); wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(hInstance, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = "多线程"; wndclass.hIconSm = NULL; return RegisterClassEx(&wndclass);}//创建窗口BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){ //hInst = hInstance; // 将实例句柄存储在全局变量中 hwnd = CreateWindowEx( 0, "多线程", "多线程之四个子窗口", WS_OVERLAPPEDWINDOW, 100, 100, 600, 600, NULL, NULL, hInstance, NULL); if (!hwnd) { return FALSE; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); return TRUE;}void Check (HWND hwnd){ InvalidateRect (hwnd, NULL, TRUE) ; UpdateWindow (hwnd) ; }VOID Thread1(PVOID pvoid){ HDC hdc ; int i ; while(thread1) { POINT pt ; ScreenToClient(hwnd,&pt) ; Check (hwndChild[1]) ; hdc = GetDC(hwndChild[1]) ; Rectangle(hdc,10,10,50,50) ; ReleaseDC(hwndChild[1],hdc) ; } _endthread() ;}LRESULT CALLBACK WndChildProc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ switch(message) { case WM_CREATE: _beginthread(Thread1,0,NULL) ; return 0 ; case WM_PAINT: return 0; case WM_DESTROY: thread1 = FALSE ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }VOID Thread2(PVOID pvoid){ HDC hdc ; int i ; int cx_1,cy_1,cy_2,cx_2 ; while(thread2) { POINT pt ; ScreenToClient(hwnd,&pt) ; Check (hwndChild[2]) ; hdc = GetDC(hwndChild[2]) ; Rectangle(hdc,10,10,50,50) ; ReleaseDC(hwndChild[2],hdc) ; } _endthread() ; }LRESULT CALLBACK WndChildProc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ switch(message) { case WM_CREATE: _beginthread(Thread2,0,NULL) ; return 0 ; case WM_PAINT: return 0; case WM_DESTROY: thread2 = FALSE ; return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ static TCHAR * szChildClass[] = { TEXT ("ChildWindow1"),TEXT ("ChildWindow2")} ; static WNDPROC ChildProc[] = { WndChildProc1, WndChildProc2} ; HINSTANCE hInstance ; int i, cxClient, cyClient ; WNDCLASSEX wndclass ; switch (message) { case WM_CREATE: //hInstance = ((LPCREATESTRUCT)lParam)->hInstance ; hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE) ; wndclass.cbClsExtra = 0 ; wndclass.cbSize = sizeof(WNDCLASSEX) ; wndclass.cbWndExtra = 0 ; wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hIcon = NULL ; wndclass.hIconSm = NULL ; wndclass.hInstance = hInstance ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpszMenuName = NULL ; for(i = 0 ; i < 2 ; i++) { wndclass.lpfnWndProc = ChildProc[i] ; wndclass.lpszClassName = szChildClass[i] ; RegisterClassEx (&wndclass) ; hwndChild[i] = CreateWindowEx ( 0, szChildClass[i], NULL, WS_CHILDWINDOW | WS_BORDER | WS_VISIBLE, 0, 0, 0, 0, hwnd, (HMENU) i, hInstance, NULL) ; } return 0 ; case WM_SIZE: cxClient = LOWORD(lParam) ; cyClient = HIWORD(lParam) ; for (i = 0 ; i < 2 ; i++) MoveWindow (hwndChild[i], (i % 2) * cxClient / 2, (i > 1) * cyClient,(i >= 1)?cxClient : cxClient / 2,cyClient, TRUE) ; return 0 ; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, message, wParam, lParam); } return 0;}