HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId );lpThreadAttributes には、セキュリティ属性構造体へのポインタを指定します
#include <windows.h> DWORD WINAPI ThreadFunc(LPVOID vdParam) { HDC hdc; unsigned int iCount = 0; TCHAR strCount[128]; while (TRUE) { hdc = GetDC((HWND)vdParam); InvalidateRect((HWND)vdParam , NULL , TRUE); wsprintf(strCount , "Count = %d" , iCount); TextOut(hdc , 10 , 10 , strCount , lstrlen(strCount)); ReleaseDC((HWND)vdParam , hdc); iCount = iCount == 0xFFFFFFFF ? 0 : iCount + 1; } } LRESULT CALLBACK WndProc(HWND hWnd , UINT msg , WPARAM wp , LPARAM lp) { DWORD dwID; switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CREATE: CreateThread(NULL , 0 , ThreadFunc , (LPVOID)hWnd , 0 , &dwID); return 0; } return DefWindowProc(hWnd , msg , wp , lp); } int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow) { HWND hWnd; MSG msg; WNDCLASS winc; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); winc.hCursor = LoadCursor(NULL , IDC_ARROW); winc.hbrBackground = GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("KITTY"); if (!RegisterClass(&winc)) return 1; hWnd = CreateWindow( TEXT("KITTY") , TEXT("Kitty on your lap") , WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL ); if (hWnd == NULL) return 1; while (GetMessage(&msg , NULL , 0 , 0 )) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }このプログラムを実行すると、0 から順にカウントする文字が表示されます
#include <windows.h> DWORD WINAPI ThreadFunc(LPVOID hWnd) { HDC hdc; int iRed = 0 , iX = 0 , iY = 0; RECT rect; while (TRUE) { hdc = GetDC(hWnd); GetClientRect(hWnd , &rect); SetPixel(hdc , iX , iY , RGB(iRed , 0 , 0)); ReleaseDC(hWnd , hdc); if (iX < rect.right) iX++; else if (iY < rect.bottom) { iX = 0; iY++; } else break; iRed = iRed == 255 ? 0 : iRed + 1; } ExitThread(TRUE); } LRESULT CALLBACK WndProc(HWND hWnd , UINT msg , WPARAM wp , LPARAM lp) { DWORD dwParam; TCHAR str[128]; static HANDLE hThread; switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CREATE: hThread = CreateThread( NULL , 0 , ThreadFunc , hWnd , 0 , &dwParam); return 0; case WM_RBUTTONUP: GetExitCodeThread(hThread , &dwParam); if (dwParam == STILL_ACTIVE) MessageBox(hWnd , TEXT("Thread is still alive") , TEXT("スレッドは生きています") , MB_OK); else { wsprintf(str , TEXT("Code = %d") , dwParam); MessageBox(hWnd , str , TEXT("コード") , MB_OK); } return 0; } return DefWindowProc(hWnd , msg , wp , lp); } int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow) { HWND hWnd; MSG msg; WNDCLASS winc; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); winc.hCursor = LoadCursor(NULL , IDC_ARROW); winc.hbrBackground = GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("KITTY"); if (!RegisterClass(&winc)) return 1; hWnd = CreateWindow( TEXT("KITTY") , TEXT("Kitty on your lap") , WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL ); if (hWnd == NULL) return 1; while (GetMessage(&msg , NULL , 0 , 0 )) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }このプログラムは、副スレッドがウィンドウの左上隅から右下隅まで
#include <windows.h> DWORD WINAPI ThreadFunc(LPVOID hWnd) { HDC hdc; int iRed = 0 , iX = 0 , iY = 0; RECT rect; while (TRUE) { hdc = GetDC(hWnd); GetClientRect(hWnd , &rect); SetPixel(hdc , iX , iY , RGB(iRed , 0 , 0)); ReleaseDC(hWnd , hdc); if (iX < rect.right) iX++; else if (iY < rect.bottom) { iX = 0; iY++; } else break; iRed = iRed == 255 ? 0 : iRed + 1; } ExitThread(TRUE); } LRESULT CALLBACK WndProc(HWND hWnd , UINT msg , WPARAM wp , LPARAM lp) { DWORD dwParam; static HANDLE hThread; switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CREATE: hThread = CreateThread( NULL , 0 , ThreadFunc , hWnd , 0 , &dwParam); return 0; case WM_RBUTTONUP: if (!hThread) return 0; GetExitCodeThread(hThread , &dwParam); if (dwParam == STILL_ACTIVE) { TerminateThread(hThread , FALSE); CloseHandle(hThread); } return 0; } return DefWindowProc(hWnd , msg , wp , lp); } int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow) { HWND hWnd; MSG msg; WNDCLASS winc; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); winc.hCursor = LoadCursor(NULL , IDC_ARROW); winc.hbrBackground = GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("KITTY"); if (!RegisterClass(&winc)) return 1; hWnd = CreateWindow( TEXT("KITTY") , TEXT("Kitty on your lap") , WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL ); if (hWnd == NULL) return 1; while (GetMessage(&msg , NULL , 0 , 0 )) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }このプログラムは、先ほどのプログラムの一部を改良したものです
HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, DWORD dwCreationFlags, LPDWORD lpThreadId );新しいスレッドを作成します
定数 | 意味 |
---|---|
0 | スレッドを作成後、即座に実行させます |
CREATE_SUSPENDED | 新しいスレッドをサスペンド状態で作成します スレッドを実行するには、ResumeThread() 関数を使います |