Advertisement
Guest User

Thanks Malcolm

a guest
Oct 21st, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. #include <windows.h>
  2. #include<cmath>
  3. HWND text_box1; // declare before the switch otherwise you have cross initialisation
  4. HWND text_box2; // declare before the switch otherwise you have cross initialisation
  5. HWND text_box3; // declare before the switch otherwise you have cross initialisation
  6. HWND text_box4; // declare before the switch otherwise you have cross initialisation
  7.  
  8. HWND button1; // declare before the switch otherwise you have cross initialisation
  9. HWND button2; // declare before the switch otherwise you have cross initialisation
  10. HWND button3; // declare before the switch otherwise you have cross initialisation
  11. HWND button4; // declare before the switch otherwise you have cross initialisation
  12.  
  13.  
  14. void convert_to_bin(int myint,char buff[])
  15. { int index=1;
  16. for (int j=0;j<=31;j++)
  17. {
  18. if ( ( myint & index ) !=0 ) buff[31-j]='1';
  19. else buff[31-j]='0';
  20. index=index<<1;
  21. }//for
  22. }// convert operation
  23.  
  24. int back_to_int(char buff[])
  25. {
  26. int digit,num=0;
  27. for (int j=1;j<=31;j++)
  28. {
  29. if (buff[j]=='1') digit=1;
  30. else digit=0;
  31. num=2*num+digit;
  32. }
  33. if (buff[0]=='1'){ num=num+(int)pow(2,31)+1; } // negative numbers
  34. return num;
  35. }
  36. void floatToBinary(float my_float, char buff[])
  37. {
  38.  
  39. int *ptrToTheFloat = (int*) &my_float;
  40. int indexX = 1;
  41. for(int i = 0; i < 31; i++)
  42. {
  43. buff[i] = ((*ptrToTheFloat) & indexX)/indexX + '0';
  44. indexX *= 2;
  45. }
  46.  
  47.  
  48.  
  49. }
  50. /*
  51. void binaryToFloat(float *theNumber, char array[], const int *sizeOfTheArray)
  52. {
  53. cout <<"enter a binary" << endl;
  54. cin >> array;
  55. int *ptrToTheFloat = (int*) &(*theNumber);
  56. *theNumber = 0;
  57. for(int i = 0; i < *sizeOfTheArray; i++)
  58. {
  59. *ptrToTheFloat = ((*ptrToTheFloat)*2) + array[i] -'0';
  60. }
  61.  
  62. cout << *theNumber << endl;
  63.  
  64. }
  65.  
  66.  
  67.  
  68. */
  69.  
  70.  
  71.  
  72. /* This is where all the input to the window goes to */
  73. LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
  74. int i_text1=1;
  75. int i_text2=1;
  76. int i_text3=1;
  77. int i_text4=1;
  78. int i_button1=3;
  79. int i_button2=3;
  80. int i_button3=3;
  81. int i_button4=3;
  82.  
  83.  
  84. switch(Message) {
  85.  
  86. /* Upon destruction, tell the main thread to stop */
  87. case WM_DESTROY:
  88. PostQuitMessage(0);
  89. break;
  90.  
  91. case WM_CREATE: // after case WM_destroy
  92. // 1. Creating Text box
  93. text_box1= CreateWindow(TEXT("EDIT"), TEXT(" myint"),WS_VISIBLE
  94. | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
  95. 20,10,200,25,hwnd, (HMENU)i_text1, NULL,NULL);
  96.  
  97. text_box2= CreateWindow(TEXT("EDIT"), TEXT(" myBinary"),WS_VISIBLE
  98. | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
  99. 250,10,300,25,hwnd, (HMENU)i_text2, NULL,NULL);
  100.  
  101.  
  102. text_box3= CreateWindow(TEXT("EDIT"), TEXT(" myFloat"),WS_VISIBLE
  103. | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
  104. 20,200,200,25,hwnd, (HMENU)i_text3, NULL,NULL);
  105.  
  106. text_box4= CreateWindow(TEXT("EDIT"), TEXT(" myBinary"),WS_VISIBLE
  107. | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
  108. 250,200,300,25,hwnd, (HMENU)i_text4, NULL,NULL);
  109.  
  110.  
  111. button1= CreateWindow(TEXT("BUTTON"), TEXT(" To Binary"),WS_VISIBLE
  112. | WS_BORDER | WS_CHILD,
  113. 20,100,80,25,hwnd, (HMENU)i_button1, NULL,NULL);
  114.  
  115. button2= CreateWindow(TEXT("BUTTON"), TEXT(" To Integer"),WS_VISIBLE
  116. | WS_BORDER | WS_CHILD,
  117. 250,100,80,25,hwnd, (HMENU)i_button2, NULL,NULL);
  118.  
  119. button3= CreateWindow(TEXT("BUTTON"), TEXT(" To Binary"),WS_VISIBLE
  120. | WS_BORDER | WS_CHILD,
  121. 20,300,80,25,hwnd, (HMENU)i_button3, NULL,NULL);
  122.  
  123. button4= CreateWindow(TEXT("BUTTON"), TEXT(" To float"),WS_VISIBLE
  124. | WS_BORDER | WS_CHILD,
  125. 250,300,80,25,hwnd, (HMENU)i_button4, NULL,NULL);
  126.  
  127.  
  128.  
  129.  
  130. break;
  131. case WM_COMMAND:
  132. if (LOWORD(wParam)==i_button1)
  133. {MessageBox(hwnd,"I wil do it for you, don't worry","title",
  134. MB_OK|MB_ICONINFORMATION);
  135. char my_int_text[80];
  136. char buff[33];
  137. buff[32]='\0';
  138. int bufsize = GetWindowTextLength(text_box1) ;
  139. int i=GetWindowText(text_box1 , (char *)my_int_text,bufsize+1);
  140. int my_int = atoi(my_int_text);
  141. convert_to_bin(my_int,buff); //convert to binary
  142. SetWindowText(text_box2, buff);
  143. } //button1
  144.  
  145.  
  146. if(LOWORD(wParam)==i_button2)
  147. {
  148. char buff[33];
  149. buff[32]='\0';
  150. int bufsize = GetWindowTextLength(text_box2) ;
  151. int i=GetWindowText(text_box2 , (char *)buff,bufsize+1);
  152. int my_int = back_to_int(buff);
  153. char int_text[10];
  154. itoa(my_int,int_text,9);
  155. SetWindowText(text_box1, int_text);
  156. }
  157.  
  158. //float
  159. if (LOWORD(wParam)==i_button3)
  160. {MessageBox(hwnd,"I wil do it for you, don't worry","title",
  161. MB_OK|MB_ICONINFORMATION);
  162. char my_float_text[80];
  163. char buff[33];
  164. buff[32]='\0';
  165. int bufsize = GetWindowTextLength(text_box3) ;
  166. int i=GetWindowText(text_box3 , (char *)my_float_text,bufsize+1);
  167. float my_float = atof(my_float_text);
  168. floatToBinary(my_float,buff); //convert to binary
  169. SetWindowText(text_box4, buff);
  170. } //button1
  171.  
  172. if(LOWORD(wParam)==i_button4)
  173. {
  174. char buff[33];
  175. buff[32]='\0';
  176. int bufsize = GetWindowTextLength(text_box4) ;
  177. int i=GetWindowText(text_box4 , (char *)buff,bufsize+1);
  178. int my_int = back_to_int(buff);
  179. char int_text[10];
  180. itoa(my_int,int_text,9);
  181. SetWindowText(text_box3, int_text);
  182.  
  183. }
  184. break;
  185. /* All other messages (a lot of them) are processed using default procedures */
  186. default:
  187. return DefWindowProc(hwnd, Message, wParam, lParam);
  188. }
  189. return 0;
  190. }
  191.  
  192. /* The 'main' function of Win32 GUI programs: this is where execution starts */
  193. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  194. WNDCLASSEX wc; /* A properties struct of our window */
  195. HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
  196. MSG msg; /* A temporary location for all messages */
  197.  
  198. /* zero out the struct and set the stuff we want to modify */
  199. memset(&wc,0,sizeof(wc));
  200. wc.cbSize = sizeof(WNDCLASSEX);
  201. wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
  202. wc.hInstance = hInstance;
  203. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  204.  
  205. /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
  206. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  207. wc.lpszClassName = "WindowClass";
  208. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
  209. wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
  210.  
  211. if(!RegisterClassEx(&wc)) {
  212. MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
  213. return 0;
  214. }
  215.  
  216. hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
  217. CW_USEDEFAULT, /* x */
  218. CW_USEDEFAULT, /* y */
  219. 640, /* width */
  220. 480, /* height */
  221. NULL,NULL,hInstance,NULL);
  222.  
  223. if(hwnd == NULL) {
  224. MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
  225. return 0;
  226. }
  227.  
  228. /*
  229. This is the heart of our program where all input is processed and
  230. sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
  231. this loop will not produce unreasonably high CPU usage
  232. */
  233. while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
  234. TranslateMessage(&msg); /* Translate key codes to chars if present */
  235. DispatchMessage(&msg); /* Send it to WndProc */
  236. }
  237. return msg.wParam;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement