#include #include #include #include #include #include char *buffer = NULL; char string[900]; float Zahl[100]; int i=0; int Zeichen=0; float Ergebnis; char Zwischen[100]; HWND hButton1; HWND hButton2; HWND hButton3; HWND hButton4; HWND hButton5; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HWND hWnd; MSG msg; WNDCLASS wc; WNDCLASSEX WindowClass; // Die Fensterklasse const char szAppName[] = "Rechner"; wc.style = CS_HREDRAW | CS_VREDRAW; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE); wc.lpszMenuName = NULL; wc.lpfnWndProc = WndProc; wc.lpszClassName = szAppName; RegisterClass(&wc); hWnd = CreateWindow( szAppName, "Taschenrechner", WS_OVERLAPPEDWINDOW| WS_VISIBLE, 200, 200, 300, 200, NULL, NULL, hInstance, NULL ); ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd); hButton1 = CreateWindow("BUTTON", "+", WS_VISIBLE | WS_CHILD, 20, 50, 30, 30, hWnd, NULL, hInstance, NULL); if(!hButton1) return FALSE; hButton2 = CreateWindow("BUTTON", "-", WS_VISIBLE | WS_CHILD, 60, 50, 30, 30, hWnd, NULL, hInstance, NULL); if(!hButton1) return FALSE; hButton3= CreateWindow("BUTTON", "*", WS_VISIBLE | WS_CHILD, 100, 50, 30, 30, hWnd, NULL, hInstance, NULL); if(!hButton1) return FALSE; hButton4 = CreateWindow("BUTTON", "/", WS_VISIBLE | WS_CHILD, 140, 50, 30, 30, hWnd, NULL, hInstance, NULL); if(!hButton1) return FALSE; hButton5 = CreateWindow("BUTTON", "=", WS_VISIBLE | WS_CHILD, 180, 50, 30, 30, hWnd, NULL, hInstance, NULL); if(!hButton1) return FALSE; SendMessage(hButton1, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE); SendMessage(hButton2, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE); SendMessage(hButton3, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE); SendMessage(hButton4, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE); SendMessage(hButton5, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE); while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { static HWND hEdit; static HWND hText5; static HWND hText6; static HWND hTextf; switch(message) { case WM_CREATE: { hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "edit", buffer, // <- das ist der Inhalt der Editfelds WS_CHILD | WS_VISIBLE, 20, 20, 200, 20, hWnd, NULL, ((LPCREATESTRUCT) lParam) -> hInstance, NULL); free(buffer); SendMessage(hEdit, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), TRUE); return 0; } case WM_COMMAND: { // Ein Button wurde gedrückt, welcher? if(lParam == (LPARAM)hButton1) // + { int iLength; iLength = GetWindowTextLength(hEdit); buffer = malloc(iLength); GetWindowText(hEdit, buffer, iLength+1); strcpy(string,buffer); sscanf(string,"%f",&Zahl[i]); Zeichen=1; i++; SendMessage(hEdit, WM_SETTEXT, 0,(LPARAM)""); SetFocus(hEdit); free(buffer); } if(lParam == (LPARAM)hButton2) // - { int iLength; iLength = GetWindowTextLength(hEdit); buffer = malloc(iLength); GetWindowText(hEdit, buffer, iLength+1); strcpy(string,buffer); sscanf(string,"%f",&Zahl[i]); Zeichen=2; i++; SendMessage(hEdit, WM_SETTEXT, 0,(LPARAM)""); SetFocus(hEdit); free(buffer); } if(lParam == (LPARAM)hButton3) // * { int iLength; iLength = GetWindowTextLength(hEdit); buffer = malloc(iLength); GetWindowText(hEdit, buffer, iLength+1); strcpy(string,buffer); sscanf(string,"%f",&Zahl[i]); Zeichen=3; i++; SendMessage(hEdit, WM_SETTEXT, 0,(LPARAM)""); SetFocus(hEdit); free(buffer); } if(lParam == (LPARAM)hButton4) // / { int iLength; iLength = GetWindowTextLength(hEdit); buffer = malloc(iLength); GetWindowText(hEdit, buffer, iLength+1); strcpy(string,buffer); sscanf(string,"%f",&Zahl[i]); Zeichen=4; i++; SendMessage(hEdit, WM_SETTEXT, 0,(LPARAM)""); SetFocus(hEdit); free(buffer); } if(lParam == (LPARAM)hButton5) // = { int iLength; iLength = GetWindowTextLength(hEdit); buffer = malloc(iLength); GetWindowText(hEdit, buffer, iLength+1); strcpy(string,buffer); sscanf(string,"%f",&Zahl[i]); i++; SendMessage(hEdit, WM_SETTEXT, 0,(LPARAM)""); free(buffer); if(Zeichen==1) { Ergebnis = Zahl[i-2]+Zahl[i-1]; } if(Zeichen==2) { Ergebnis = Zahl[i-2]-Zahl[i-1]; } if(Zeichen==3) { Ergebnis = Zahl[i-2]*Zahl[i-1]; } if(Zeichen==4) { Ergebnis = Zahl[i-2]/Zahl[i-1]; } sprintf(Zwischen,"%g",Ergebnis); SendMessage(hEdit, WM_SETTEXT, 0,(LPARAM)Zwischen); SetFocus(hEdit); i=0; Zeichen=0; } } break; case WM_CLOSE: { DestroyWindow(hWnd); return 0; } case WM_DESTROY: { PostQuitMessage(0); return 0; } } return DefWindowProc(hWnd, message, wParam, lParam); }