1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <Windows.h>
#define ErrorMessageBox(a,b) MessageBox(a,b,"Error:",MB_ICONWARNING)
#define WS_CHILD_WINDOW WS_CHILD|WS_VISIBLE|WS_BORDER
#define WS_NO_RESIZE WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX
#define INPUT 1000
#define CHECK 1001
bool SetUpWindowClass (char*, int, int, int);
bool CheckIfPrimeNumber (int);
LRESULT CALLBACK WindowProcedure (HWND, unsigned int, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpsCmdLine, int iCmdShow) {
if (!SetUpWindowClass ("1", 255, 255, 255)) {
ErrorMessageBox (NULL, "Window class registration failed");
return 0;
}
HWND hWnd = CreateWindow ("1", "Prime Number Checker", WS_NO_RESIZE, 500, 200, 270, 80, NULL, NULL, hInstance, NULL);
if (!hWnd) {
ErrorMessageBox (NULL, "Window handle is NULL");
return 0;
}
ShowWindow (hWnd, SW_SHOW);
MSG uMsg;
while (GetMessage (&uMsg, NULL, 0, 0) > 0) {
TranslateMessage (&uMsg);
DispatchMessage (&uMsg);
}
return 0;
}
bool SetUpWindowClass (char *cpTitle, int iR, int iG, int iB) {
WNDCLASSEX WindowClass;
WindowClass.cbClsExtra = 0;
WindowClass.cbWndExtra = 0;
WindowClass.cbSize = sizeof (WNDCLASSEX);
WindowClass.style = 0;
WindowClass.lpszClassName = cpTitle;
WindowClass.lpszMenuName = NULL;
WindowClass.lpfnWndProc = WindowProcedure;
WindowClass.hInstance = GetModuleHandle (NULL);
WindowClass.hbrBackground = CreateSolidBrush (RGB (iR, iG, iB));
WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
if (RegisterClassEx (&WindowClass)) return true;
else return false;
}
bool CheckIfPrimeNumber (int iNumber) {
bool bIsPrime = true;
for (int iLoopCounter = 2; iLoopCounter < iNumber; iLoopCounter++) {
if (iNumber % iLoopCounter == 0 ) {
bIsPrime = false;
break;
}
}
if (bIsPrime) return true;
else return false;
}
LRESULT CALLBACK WindowProcedure (HWND hWnd, unsigned int uiMsg, WPARAM wParam, LPARAM lParam) {
switch (uiMsg) {
case WM_CLOSE: DestroyWindow (hWnd); break;
case WM_DESTROY: PostQuitMessage (0); break;
case WM_CREATE: {
HINSTANCE hInstance = GetModuleHandle (NULL);
CreateWindow ("edit", "", WS_CHILD_WINDOW, 5, 5, 50, 30, hWnd, (HMENU) INPUT, hInstance, NULL);
CreateWindow ("button", "Check", WS_CHILD_WINDOW, 60, 5, 50, 30, hWnd, (HMENU) CHECK, hInstance, NULL);
}
case WM_COMMAND:
switch (LOWORD (wParam)) {
case CHECK: {
TCHAR tcaInput [256];
GetWindowText (GetDlgItem (hWnd, INPUT), tcaInput, 256);
int iValue = atoi (tcaInput);
if (CheckIfPrimeNumber (iValue)) MessageBox (hWnd, "Number is prime", "Result:", MB_OK);
else MessageBox (hWnd, "Number is NOT prime", "Result:", MB_OK);
}
break;
}
break;
}
return DefWindowProc (hWnd, uiMsg, wParam, lParam);
}
|