读书人

多线程有关问题!

发布时间: 2012-02-26 20:19:45 作者: rapoo

多线程问题!急!
先把代码贴出来:

C/C++ code
#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;} 


我想达到的目的是:有一个600*600的窗口,然后再其中创建了两个300*600的子窗口,实际上就是将窗口左右一分为二形成两个子窗口,然后再这两个子窗口中各画了一个矩形,用多线程实现。但是问题出现了,出现的问题如图:

问题:有一个小矩形在创建的窗口之外,有一个小矩形在右边的子窗口中,但是整个屏幕不停的闪(也是不停地重画),在外面的那个小矩形引起的整个屏幕的闪烁。
求各位高手解答,小弟在此谢谢,刚接触多线程,还望各位高手之间一二,谢谢。
请各位高手指出小弟代码中不正确的地方,谢谢。

[解决办法]
不要在多线程里操作界面!

[解决办法]
窗口只能在创建它的线程里面画,不能在别的线程中直接访问。别的线程要访问一般通过SendMessage/PostMessage
[解决办法]
感觉你的不停的闪烁的问题 是线程中的while循环搞的 把循环去掉就OK了
[解决办法]
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) ;

你这里试试把子窗口大小用常数指定试一下,我感觉有时候这个窗口初始化的时候size在变,然后它就一直处理WM_SIZE消息,你试试常数行不行,我也不是很清楚,但是以前有遇到过这种类似问题。

读书人网 >C++

热点推荐