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

时间:2023-03-08 17:07:29
// 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, , ))
{
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 = ;
wcex.cbWndExtra = ;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINTHREADTEST));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+);
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, , CW_USEDEFAULT, , 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, , , NULL);
for(int i=;i<=;i++){
LineTo(hdc, +i,);
Sleep();
}
MoveToEx(hdc, , , NULL);
for(int i=;i<=;i++){
LineTo(hdc, ,+i);
Sleep();
}
for(int i=;i<=;i++){
LineTo(hdc, -i,);
Sleep();
}
for(int i=;i<=;i++){
LineTo(hdc, ,-i);
Sleep();
}
return ;
} DWORD WINAPI tProc2(LPVOID pParam){
//TODO.................
HDC hdc;
hdc=(HDC)pParam;
for(int i=;i<=;i++){
Pie(hdc,,,,,,,-i,);
Sleep();
}
for(int i=;i<=;i++){
Pie(hdc,,,,,,,,+i);
Sleep();
}
for(int i=;i<=;i++){
Pie(hdc,,,,,,,,+i);
Sleep();
}
for(int i=;i<=;i++){
Pie(hdc,,,,,,,+i,);
Sleep();
}
for(int i=;i<=;i++){
Pie(hdc,,,,,,,+i,);
Sleep();
}
for(int i=;i<=;i++){
Pie(hdc,,,,,,,,-i);
Sleep();
}
for(int i=;i<=;i++){
Pie(hdc,,,,,,,,-i);
Sleep();
}
for(int i=;i<=;i++){
Pie(hdc,,,,,,,-i,);
Sleep();
}
return ;
} //
// 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=;
int y=;
//end of rakus code===================== switch (message)
{
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return ;
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, , cyClient / , NULL);
LineTo(hdc, cxClient, cyClient / );
// 画竖线
MoveToEx(hdc, cxClient / , , NULL);
LineTo(hdc, cxClient / , cyClient);
// 画椭圆
Ellipse(hdc, cxClient / , , cxClient, cyClient / );
// 画圆矩形
RoundRect(hdc, cxClient / , * cyClient / ,
cxClient / - cxClient / , cyClient - cyClient / ,
cxClient / , cyClient / ); DWORD threadID;
HANDLE hThread;
hThread=CreateThread(NULL,,tProc,hdc,,&threadID); DWORD threadID2;
HANDLE hThread2;
hThread2=CreateThread(NULL,,tProc2,hdc,,&threadID2); HANDLE threads[];
threads[]=hThread;
threads[]=hThread2;
WaitForMultipleObjects(,threads,TRUE,INFINITE);////处理多个线程用这个方法最好 EndPaint(hWnd, &ps);
//end of rakus code
break;
case WM_DESTROY:
PostQuitMessage();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return ;
} // 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;
}