#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
const char *szClassName = "PRZYKLAD";

HWND hButton, hText, hOutputText;

DWORD WINAPI ThreadFun(LPVOID lpParam)
{
	int n = 20000, i, p, lp;
	bool t;

	lp = 0;
	p = 2;

	while (lp < n)
	{
		t = true;
		for (i = 2; i < p; i++)
			if (p % i == 0)
			{
				t = false;
				break;
			}

		if (t)
		{
			char buffer[20];
			_itoa_s(p, buffer, 20, 10);
			LPCSTR q = buffer;
			SetWindowText(hOutputText, q);
			lp++;
		}
		p++;
	}

	SetWindowText(hOutputText, "Koniec");


	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	LPSTR lpCmdLine, int nShowCmd)
{
	HWND hwnd;
	MSG messages;
	WNDCLASSEX wincl;

	wincl.hInstance = hInstance;
	wincl.lpszClassName = szClassName;
	wincl.lpfnWndProc = WindowProcedure;
	wincl.style = CS_DBLCLKS;
	wincl.cbSize = sizeof(WNDCLASSEX);
	wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
	wincl.lpszMenuName = NULL;
	wincl.cbClsExtra = 0;
	wincl.cbWndExtra = 0;
	wincl.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);

	if (!RegisterClassEx(&wincl)) return 0;
	hwnd = CreateWindowEx(
		0, szClassName,
		"Mrożenie okna",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		512, 512,
		HWND_DESKTOP, NULL,
		hInstance, NULL);

	hButton = CreateWindowEx(0, "BUTTON", "Licz", WS_CHILD | WS_VISIBLE,
		10, 10, 150, 30, hwnd, NULL, hInstance, NULL);

	hText = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER |
		WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
		10, 50, 350, 30, hwnd, NULL, hInstance, NULL);

	hOutputText = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER,
		10, 100, 350, 30, hwnd, NULL, hInstance, NULL);

	ShowWindow(hwnd, nShowCmd);

	while (GetMessage(&messages, NULL, 0, 0))
	{
		TranslateMessage(&messages);
		DispatchMessage(&messages);
	}

	return messages.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message,
	WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{


	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_COMMAND:

		if ((HWND)lParam == hButton)
		{
			HANDLE hThread1 = CreateThread(NULL, 0, ThreadFun, NULL, 0, 0);
			CloseHandle(hThread1);
		}

		break;
	default:
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	return 0;
}


