Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include<cmath>
- HWND text_box1; // declare before the switch otherwise you have cross initialisation
- HWND text_box2; // declare before the switch otherwise you have cross initialisation
- HWND text_box3; // declare before the switch otherwise you have cross initialisation
- HWND text_box4; // declare before the switch otherwise you have cross initialisation
- HWND button1; // declare before the switch otherwise you have cross initialisation
- HWND button2; // declare before the switch otherwise you have cross initialisation
- HWND button3; // declare before the switch otherwise you have cross initialisation
- HWND button4; // declare before the switch otherwise you have cross initialisation
- void convert_to_bin(int myint,char buff[])
- { int index=1;
- for (int j=0;j<=31;j++)
- {
- if ( ( myint & index ) !=0 ) buff[31-j]='1';
- else buff[31-j]='0';
- index=index<<1;
- }//for
- }// convert operation
- int back_to_int(char buff[])
- {
- int digit,num=0;
- for (int j=1;j<=31;j++)
- {
- if (buff[j]=='1') digit=1;
- else digit=0;
- num=2*num+digit;
- }
- if (buff[0]=='1'){ num=num+(int)pow(2,31)+1; } // negative numbers
- return num;
- }
- void floatToBinary(float my_float, char buff[])
- {
- int *ptrToTheFloat = (int*) &my_float;
- int indexX = 1;
- for(int i = 0; i < 31; i++)
- {
- buff[i] = ((*ptrToTheFloat) & indexX)/indexX + '0';
- indexX *= 2;
- }
- }
- /*
- void binaryToFloat(float *theNumber, char array[], const int *sizeOfTheArray)
- {
- cout <<"enter a binary" << endl;
- cin >> array;
- int *ptrToTheFloat = (int*) &(*theNumber);
- *theNumber = 0;
- for(int i = 0; i < *sizeOfTheArray; i++)
- {
- *ptrToTheFloat = ((*ptrToTheFloat)*2) + array[i] -'0';
- }
- cout << *theNumber << endl;
- }
- */
- /* This is where all the input to the window goes to */
- LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
- int i_text1=1;
- int i_text2=1;
- int i_text3=1;
- int i_text4=1;
- int i_button1=3;
- int i_button2=3;
- int i_button3=3;
- int i_button4=3;
- switch(Message) {
- /* Upon destruction, tell the main thread to stop */
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_CREATE: // after case WM_destroy
- // 1. Creating Text box
- text_box1= CreateWindow(TEXT("EDIT"), TEXT(" myint"),WS_VISIBLE
- | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
- 20,10,200,25,hwnd, (HMENU)i_text1, NULL,NULL);
- text_box2= CreateWindow(TEXT("EDIT"), TEXT(" myBinary"),WS_VISIBLE
- | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
- 250,10,300,25,hwnd, (HMENU)i_text2, NULL,NULL);
- text_box3= CreateWindow(TEXT("EDIT"), TEXT(" myFloat"),WS_VISIBLE
- | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
- 20,200,200,25,hwnd, (HMENU)i_text3, NULL,NULL);
- text_box4= CreateWindow(TEXT("EDIT"), TEXT(" myBinary"),WS_VISIBLE
- | WS_BORDER | WS_CHILD |ES_AUTOHSCROLL,
- 250,200,300,25,hwnd, (HMENU)i_text4, NULL,NULL);
- button1= CreateWindow(TEXT("BUTTON"), TEXT(" To Binary"),WS_VISIBLE
- | WS_BORDER | WS_CHILD,
- 20,100,80,25,hwnd, (HMENU)i_button1, NULL,NULL);
- button2= CreateWindow(TEXT("BUTTON"), TEXT(" To Integer"),WS_VISIBLE
- | WS_BORDER | WS_CHILD,
- 250,100,80,25,hwnd, (HMENU)i_button2, NULL,NULL);
- button3= CreateWindow(TEXT("BUTTON"), TEXT(" To Binary"),WS_VISIBLE
- | WS_BORDER | WS_CHILD,
- 20,300,80,25,hwnd, (HMENU)i_button3, NULL,NULL);
- button4= CreateWindow(TEXT("BUTTON"), TEXT(" To float"),WS_VISIBLE
- | WS_BORDER | WS_CHILD,
- 250,300,80,25,hwnd, (HMENU)i_button4, NULL,NULL);
- break;
- case WM_COMMAND:
- if (LOWORD(wParam)==i_button1)
- {MessageBox(hwnd,"I wil do it for you, don't worry","title",
- MB_OK|MB_ICONINFORMATION);
- char my_int_text[80];
- char buff[33];
- buff[32]='\0';
- int bufsize = GetWindowTextLength(text_box1) ;
- int i=GetWindowText(text_box1 , (char *)my_int_text,bufsize+1);
- int my_int = atoi(my_int_text);
- convert_to_bin(my_int,buff); //convert to binary
- SetWindowText(text_box2, buff);
- } //button1
- if(LOWORD(wParam)==i_button2)
- {
- char buff[33];
- buff[32]='\0';
- int bufsize = GetWindowTextLength(text_box2) ;
- int i=GetWindowText(text_box2 , (char *)buff,bufsize+1);
- int my_int = back_to_int(buff);
- char int_text[10];
- itoa(my_int,int_text,9);
- SetWindowText(text_box1, int_text);
- }
- //float
- if (LOWORD(wParam)==i_button3)
- {MessageBox(hwnd,"I wil do it for you, don't worry","title",
- MB_OK|MB_ICONINFORMATION);
- char my_float_text[80];
- char buff[33];
- buff[32]='\0';
- int bufsize = GetWindowTextLength(text_box3) ;
- int i=GetWindowText(text_box3 , (char *)my_float_text,bufsize+1);
- float my_float = atof(my_float_text);
- floatToBinary(my_float,buff); //convert to binary
- SetWindowText(text_box4, buff);
- } //button1
- if(LOWORD(wParam)==i_button4)
- {
- char buff[33];
- buff[32]='\0';
- int bufsize = GetWindowTextLength(text_box4) ;
- int i=GetWindowText(text_box4 , (char *)buff,bufsize+1);
- int my_int = back_to_int(buff);
- char int_text[10];
- itoa(my_int,int_text,9);
- SetWindowText(text_box3, int_text);
- }
- break;
- /* All other messages (a lot of them) are processed using default procedures */
- default:
- return DefWindowProc(hwnd, Message, wParam, lParam);
- }
- return 0;
- }
- /* The 'main' function of Win32 GUI programs: this is where execution starts */
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
- WNDCLASSEX wc; /* A properties struct of our window */
- HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
- MSG msg; /* A temporary location for all messages */
- /* zero out the struct and set the stuff we want to modify */
- memset(&wc,0,sizeof(wc));
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.lpfnWndProc = WndProc; /* This is where we will send messages to */
- wc.hInstance = hInstance;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- /* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wc.lpszClassName = "WindowClass";
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */
- if(!RegisterClassEx(&wc)) {
- MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
- return 0;
- }
- hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, /* x */
- CW_USEDEFAULT, /* y */
- 640, /* width */
- 480, /* height */
- NULL,NULL,hInstance,NULL);
- if(hwnd == NULL) {
- MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
- return 0;
- }
- /*
- This is the heart of our program where all input is processed and
- sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
- this loop will not produce unreasonably high CPU usage
- */
- while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
- TranslateMessage(&msg); /* Translate key codes to chars if present */
- DispatchMessage(&msg); /* Send it to WndProc */
- }
- return msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement