Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include <windows.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #define ID_EDIT 101
  7. #define IDB_RADIO1 102
  8. #define IDB_RADIO2 103
  9. #define ID_STATIC 104
  10. wchar_t ClassName[] = L"MainWindowClass";
  11. wchar_t Bufor[100];
  12. #pragma warning(disable : 4996)
  13. HWND hEditField;
  14. HWND hWndButton1;
  15. HWND hWndButton2;
  16. HWND hStatic;
  17.  
  18. BOOL CalToCm = true;
  19. wchar_t x[200];
  20.  
  21. double ConvertToCal()
  22. {
  23. double temp, result;
  24. temp = _wtof(Bufor);
  25. result = temp / 2, 54;
  26. return result;
  27. }
  28.  
  29. double ConvertToCm()
  30. {
  31. double temp, result;
  32. temp = _wtof(Bufor);
  33. result = temp * 2, 54;
  34. return result;
  35. }
  36.  
  37.  
  38.  
  39. LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
  40. {
  41. switch (Msg)
  42. {
  43. case WM_CREATE:
  44. {
  45. HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
  46.  
  47. hEditField = CreateWindowEx(0, TEXT("edit"), TEXT("Wprowadź wartość"), WS_VISIBLE | WS_CHILD | WS_BORDER, 10, 10, 160, 20, hWnd, (HMENU)ID_EDIT, hInstance, NULL);
  48. hWndButton1 = CreateWindowEx(0, TEXT("BUTTON"), TEXT("cm -> cale"), WS_VISIBLE | WS_CHILD | BS_RADIOBUTTON, 10, 40, 160, 20, hWnd, (HMENU)IDB_RADIO1, hInstance, NULL);
  49. hWndButton2 = CreateWindowEx(0, TEXT("BUTTON"), TEXT("cale -> cm"), WS_VISIBLE | WS_CHILD | BS_RADIOBUTTON, 10, 70, 160, 20, hWnd, (HMENU)IDB_RADIO2, hInstance, NULL);
  50. hStatic = CreateWindowEx(0, TEXT("static"), TEXT("Wynik"), WS_VISIBLE | WS_CHILD | WS_BORDER, 10, 100, 160, 20, hWnd, (HMENU)ID_STATIC, hInstance, NULL);
  51. }
  52. break;
  53.  
  54. case WM_COMMAND:
  55. {
  56. switch (LOWORD(wParam))
  57. {
  58. case ID_EDIT:
  59. {
  60. switch (HIWORD(wParam))
  61. {
  62. case EN_CHANGE:
  63. {
  64. //DWORD dlugosc = GetWindowTextLength(hEditField);
  65. //Bufor = (LPWSTR)GlobalAlloc(GPTR, dlugosc + 1);
  66. GetWindowText(hEditField, Bufor, 100);
  67.  
  68. if (CalToCm == 'true')
  69. {
  70. //double liczba = ConvertToCm(Bufor);
  71. //swprintf_s(Bufor, 100, "%f", liczba);
  72. swprintf(Bufor, L"%f", ConvertToCm());
  73. SetWindowText(hStatic, Bufor);
  74. }
  75. else
  76. {
  77. swprintf(Bufor, L"%f", ConvertToCal());
  78. SetWindowText(hStatic, Bufor);
  79. }
  80. }
  81. }
  82. }
  83. break;
  84. case IDB_RADIO1:
  85. {
  86. switch (HIWORD(wParam))
  87. {
  88. case BN_CLICKED:
  89. if (SendDlgItemMessage(hWnd, IDB_RADIO1, BM_GETCHECK, 0, 0) == 0)
  90. {
  91. SendDlgItemMessage(hWnd, IDB_RADIO1, BM_SETCHECK, 1, 0);
  92. SendDlgItemMessage(hWnd, IDB_RADIO2, BM_SETCHECK, 0, 0);
  93.  
  94. CalToCm = 1;
  95. swprintf(Bufor, L"%f", ConvertToCm());
  96. SetWindowText(hStatic, Bufor);
  97. }
  98. break;
  99. }
  100.  
  101. }
  102. break;
  103.  
  104. case IDB_RADIO2:
  105. {
  106. switch (HIWORD(wParam))
  107. {
  108. case BN_CLICKED:
  109. if (SendDlgItemMessage(hWnd, IDB_RADIO2, BM_GETCHECK, 0, 0) == 0)
  110. {
  111. SendDlgItemMessage(hWnd, IDB_RADIO2, BM_SETCHECK, 1, 0);
  112. SendDlgItemMessage(hWnd, IDB_RADIO1, BM_SETCHECK, 0, 0);
  113.  
  114. CalToCm = 0;
  115. swprintf(Bufor, L"%f", ConvertToCal());
  116. SetWindowText(hStatic, Bufor);
  117. }
  118. break;
  119. }
  120.  
  121.  
  122. }
  123. break;
  124. }
  125.  
  126. return 0;
  127. }
  128. break;
  129.  
  130. case WM_CLOSE:
  131. //GlobalFree(Bufor);
  132. DestroyWindow(hWnd);
  133. break;
  134.  
  135. case WM_DESTROY:
  136. PostQuitMessage(0);
  137. break;
  138.  
  139. default:
  140. return (DefWindowProc(hWnd, Msg, wParam, lParam));
  141. }
  142.  
  143. return 0;
  144. }
  145.  
  146.  
  147. INT WINAPI WinMain(HINSTANCE hInstance,
  148. HINSTANCE hPrevInstance,
  149. LPSTR lpCmdLine,
  150. INT nCmdShow)
  151. {
  152.  
  153. WNDCLASSEX wc;
  154. wc.cbSize = sizeof(WNDCLASSEX);
  155. wc.style = 0;
  156. wc.lpfnWndProc = (WNDPROC)WndProc;
  157. wc.cbClsExtra = 0;
  158. wc.cbWndExtra = 0;
  159. wc.hInstance = hInstance;
  160. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  161. wc.hIconSm = 0;
  162. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  163. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  164. wc.lpszMenuName = NULL;
  165. wc.lpszClassName = ClassName;
  166.  
  167. if (!RegisterClassEx(&wc))
  168. {
  169. MessageBox(NULL, TEXT("Failed To Register The Window Class."), TEXT("Error"), MB_OK | MB_ICONERROR);
  170. return 0;
  171. }
  172.  
  173. HWND hWnd = CreateWindowEx(
  174. WS_EX_CLIENTEDGE,
  175. ClassName,
  176. TEXT("Radio Buttons"),
  177. WS_OVERLAPPEDWINDOW,
  178. CW_USEDEFAULT,
  179. CW_USEDEFAULT,
  180. 400,
  181. 300,
  182. NULL,
  183. NULL,
  184. hInstance,
  185. NULL);
  186.  
  187. if (!hWnd)
  188. {
  189. MessageBox(NULL, TEXT("Window Creation Failed."), TEXT("Error"), MB_OK | MB_ICONERROR);
  190. return 0;
  191. }
  192.  
  193. ShowWindow(hWnd, SW_SHOW);
  194. UpdateWindow(hWnd);
  195.  
  196. MSG Msg;
  197. while (GetMessage(&Msg, NULL, 0, 0))
  198.  
  199. {
  200. TranslateMessage(&Msg);
  201. DispatchMessage(&Msg);
  202. }
  203.  
  204. return Msg.wParam;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement