windows多线程窗口程序设计

掌握windows基于消息驱动的窗口应用程序设计的基本方法,掌握窗口程序资源的概念与设计,掌握常用的消息的程序处理方法,掌握文字图形输出相关函数编程。掌握设计的基本方法(选项),掌握时钟消息设计动画程序(选项)

利用windows基于消息驱动的窗口应用程序设计的基本方法,利用常用的消息程序处理方法,设计一个多线程程序,然后用时钟动画将各线程的执行过程直观的表现出来。

操作环境:安装有Windows操作系统PC机一台,实验环境为VS(任何版本都行).

 

1.在vs中,生成一个空白的win32项目,了解win32的消息驱动,学习窗口程序资源的概念与设计,并为程序设计一个位图图标。

2. 观察win32程序基本结构,了解常用的消息的程序处理方法,掌握文字图形输出相关函数编程,并练习文字与图形的输出。

3.设计并构建程序用户界面

菜单栏

线程设置对话框

菜单与对话框函数如下:

// 分析菜单选择:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

    int wmId, wmEvent;

    PAINTSTRUCT ps;

    HDC hdc;

    int pausecount;

 

    switch (message)

    {

    case WM_COMMAND:

        wmId = LOWORD(wParam);

        wmEvent = HIWORD(wParam);

        // 分析菜单选择:

        switch (wmId)

        {

        case IDM_ABOUT:

            switch (DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About))

            {

            case IDSTART:

                CreateThreadStart(hWnd);

                //GetSecurityInfo

                break;

 

            case IDPAUSE:

                pausecount=SuspendThread((HANDLE)ThreadId[treatwith]);

                pausecount=SuspendThread(ThreadHandle[treatwith]);

                if(pausecount==-1)

                {

                    pausecount=GetLastError();

                }

                break;

            case IDRESET:

                pausecount=ResumeThread((HANDLE)ThreadId[treatwith]);

                pausecount=ResumeThread(ThreadHandle[treatwith]);

                if(pausecount==-1)

                {

                    pausecount=GetLastError();

                }

                break;

            }

            break;

        

        case IDM_EXIT:

            DestroyWindow(hWnd);

            break;

        default:

            return DefWindowProc(hWnd, message, wParam, lParam);

        }

        break;

    case WM_SIZE:

        wndWidthSize=LOWORD(lParam);

        wndHighSize=HIWORD(lParam);

        break;

    case WM_PAINT:

        hdc = BeginPaint(hWnd, &ps);

        //TextOut(hdc,100,50,_T("SOUTH WEST UNIVERSITY"),wcslen(_T("SOUTH WEST UNIVERSITY")));

        // TODO: 在此添加任意绘图代码...

        EndPaint(hWnd, &ps);

        break;

    case WM_DESTROY:

        PostQuitMessage(0);

        break;

    default:

        return DefWindowProc(hWnd, message, wParam, lParam);

    }

    return 0;

}

 

// "开始"对话框的消息处理程序。

INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)

{

    UNREFERENCED_PARAMETER(lParam);

    switch (message)

    {

    case WM_INITDIALOG:

        return (INT_PTR)TRUE;

 

    case WM_COMMAND:

        if (LOWORD(wParam) == IDCANCEL)

        {

            EndDialog(hDlg, LOWORD(wParam));

            return (INT_PTR)TRUE;

        }

        if (LOWORD(wParam) == IDPAUSE)

        {

            treatwith=GetDlgItemInt(hDlg,IDC_EDIT2,NULL,TRUE);

            EndDialog(hDlg, LOWORD(wParam));

            return (INT_PTR)TRUE;

        }

        if (LOWORD(wParam) == IDRESET)

        {

            treatwith=GetDlgItemInt(hDlg,IDC_EDIT2,NULL,TRUE);

            EndDialog(hDlg, LOWORD(wParam));

            return (INT_PTR)TRUE;

        }

        if (LOWORD(wParam) == IDSTART)

        {

            threadNum=GetDlgItemInt(hDlg,IDC_EDIT1,NULL,TRUE);

            EndDialog(hDlg, LOWORD(wParam));

            return (INT_PTR)TRUE;

        }

        break;

    }

    return (INT_PTR)FALSE;

}

4.编写多线程创建函数,创建多个线程,编写动画绘图函数绘制图形,每个线程都调用绘图函数,在用户区中绘制动画,以直观表现线程的运行。具体代码如下:

//线程创建

void CreateThreadStart(HWND hWnd)

{

    bool SecurityDescriptorSuc=true;

    threadStruct *pthreadparam;

    UINT threadXuHao=0;

    int i=0;

    HANDLE handleRes;

    while(threadXuHao<threadNum)

    {

        pthreadparam=new threadStruct;//到线程里面去释放

        pthreadparam->hWnd=hWnd;

        pthreadparam->threadXuHao=threadXuHao;

        if(SecurityDescriptorSuc==false)

        {

            //handleRes=CreateThread(&sa,0,PaintThread,

            //    (LPVOID)pthreadparam,0,&ThreadId[threadXuHao]);

            ThreadHandle[threadXuHao]=handleRes;

        }

        else

        {

            handleRes=CreateThread(NULL,0,PaintThread,

                (LPVOID)pthreadparam,0,&ThreadId[threadXuHao]);

            ThreadHandle[threadXuHao]=handleRes;

        }

        threadXuHao++;

    }

}

 

//绘图

DWORD WINAPI PaintThread(LPVOID lpParam)

{

    UINT threadXuHao=((threadStruct*)lpParam)->threadXuHao;

    HWND hWnd=((threadStruct*)lpParam)->hWnd;

    delete lpParam;

 

    HDC hdc=GetDC(hWnd);

    /*UINT currentHigh=wndHighSize-1;*/

    wchar_t str[100];

    swprintf(str,_T("this is the %d thread"),threadXuHao);

    TextOut(hdc, 50 * (threadXuHao + 1) - 40 , 50 * (threadXuHao + 1) - 40 + (threadXuHao + 1) * 5, str, wcslen(str));

    

    /*UINT rectWidth;

    rectWidth=wndWidthSize/threadNum;*/

    //HFONT X=CreateHfont

    /*HPEN logicPen=CreatePen(PS_SOLID,3,logicColor[threadXuHao]);*/

    HPEN logicPen = CreatePen(PS_SOLID, 3, RGB(50 * threadXuHao - 80, 20 * threadXuHao, 10*threadXuHao+150));

    HPEN oldPenObj;

    RECT rc;

    GetClientRect(hWnd, &rc);

    int n = 0;

    int x=0;

    int y = 0;

    while (x<wndWidthSize)

    {

        x = 50 * (threadXuHao + 1) -40+150;

        y = 50 * (threadXuHao+1)-40;

            /*if (y > wndHighSize)

                y -= wndHighSize;*/

        int m = 10*(threadXuHao+1);

        

        //MoveToEx(hdc,rectWidth*threadXuHao,currentHigh,NULL);

        oldPenObj=(HPEN)SelectObject(hdc,(HGDIOBJ)logicPen);

        //LineTo(hdc,(rectWidth-30)*(threadXuHao+1),currentHigh);

        Ellipse(hdc, x+n ,y, x + m+n , y+m);

        SelectObject(hdc,(HGDIOBJ)oldPenObj);

        //currentHigh=currentHigh-3;

        n += (threadXuHao + 1) * 5;

        Sleep((threadXuHao+1)*200);

    }

    ReleaseDC(hWnd,hdc);

    return 1;

}

 

五. 实验结果

程序运行结果如下:

其中,进程3被暂停。

posted @ 2016-07-23 14:57 左昱_leftshine 阅读(...) 评论(...) 编辑 收藏