您的位置:首页 > 编程语言 > C语言/C++

VC++ win32 多线程 一边画圆一边画矩形

2013-09-24 23:20 337 查看
// WinThreadTest.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "WinThreadTest.h"
#include <windows.h>
#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                    // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR    lpCmdLine,
int       nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WINTHREADTEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINTHREADTEST));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style            = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc    = WndProc;
wcex.cbClsExtra        = 0;
wcex.cbWndExtra        = 0;
wcex.hInstance        = hInstance;
wcex.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINTHREADTEST));
wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName    = MAKEINTRESOURCE(IDC_WINTHREADTEST);
wcex.lpszClassName    = szWindowClass;
wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

DWORD WINAPI tProc(LPVOID pParam){
//TODO.................
HDC hdc;
hdc=(HDC)pParam;
MoveToEx(hdc, 50, 50, NULL);
for(int i=1;i<=50;i++){
LineTo(hdc, 50+i,50);
Sleep(5);
}
MoveToEx(hdc, 100, 50, NULL);
for(int i=1;i<=50;i++){
LineTo(hdc, 100,50+i);
Sleep(5);
}
for(int i=1;i<=50;i++){
LineTo(hdc, 100-i,100);
Sleep(5);
}
for(int i=1;i<=50;i++){
LineTo(hdc, 50,100-i);
Sleep(5);
}
return 0;
}

DWORD WINAPI tProc2(LPVOID pParam){
//TODO.................
HDC hdc;
hdc=(HDC)pParam;
for(int i=1;i<=25;i++){
Pie(hdc,150,150,200,200,175,150,175-i,150);
Sleep(3);
}
for(int i=1;i<=25;i++){
Pie(hdc,150,150,200,200,175,150,150,150+i);
Sleep(3);
}
for(int i=1;i<=25;i++){
Pie(hdc,150,150,200,200,175,150,150,175+i);
Sleep(3);
}
for(int i=1;i<=25;i++){
Pie(hdc,150,150,200,200,175,150,150+i,200);
Sleep(3);
}
for(int i=1;i<=25;i++){
Pie(hdc,150,150,200,200,175,150,175+i,200);
Sleep(3);
}
for(int i=1;i<=25;i++){
Pie(hdc,150,150,200,200,175,150,200,200-i);
Sleep(3);
}
for(int i=1;i<=25;i++){
Pie(hdc,150,150,200,200,175,150,200,175-i);
Sleep(3);
}
for(int i=1;i<=25;i++){
Pie(hdc,150,150,200,200,175,150,200-i,150);
Sleep(3);
}
return 0;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND    - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY    - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
static int cxClient, cyClient;
PAINTSTRUCT ps;
HDC hdc;

//rakus code==================
HPEN hPen, hPenOld;//定义画笔
HBRUSH hBrush,HBruOld;//定义画刷
TEXTMETRIC tm;//用于获取字体信息
int x=10;
int y=150;
//end of rakus code=====================

switch (message)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_COMMAND:
wmId    = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...

//this is rakus code
//划横线
MoveToEx(hdc, 0, cyClient / 2, NULL);
LineTo(hdc, cxClient, cyClient / 2);
// 画竖线
MoveToEx(hdc, cxClient / 2, 0, NULL);
LineTo(hdc, cxClient / 2, cyClient);
// 画椭圆
Ellipse(hdc, cxClient / 2, 0, cxClient, cyClient / 2);
// 画圆矩形
RoundRect(hdc, cxClient / 8, 5 * cyClient / 8,
cxClient / 2 - cxClient / 8, cyClient - cyClient / 8,
cxClient / 16, cyClient / 16);

DWORD threadID;
HANDLE hThread;
hThread=CreateThread(NULL,0,tProc,hdc,0,&threadID);

DWORD threadID2;
HANDLE hThread2;
hThread2=CreateThread(NULL,0,tProc2,hdc,0,&threadID2);

HANDLE threads[2];
threads[0]=hThread;
threads[1]=hThread2;
WaitForMultipleObjects(2,threads,TRUE,INFINITE);////处理多个线程用这个方法最好

EndPaint(hWnd, &ps);
//end of rakus code
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
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) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: