Codelänge?
-
Hallo - wollte mal fragen, ob ich was falsch mache, oder ob es Normal ist, dass ein WinApi Code eines einfachen Programmes ( paar buttons , editboxes ) 250 Zeilen lang ist??
Weil ich verlier da immer bissle den Überblick... also muss ich da was ändern , oder mich an den langen Code gewöhnen??
MfG Lorun
-
Es kommt drauf an was du genau machst. Wenn du aber nur fragst wie jemand heißt und dann seinen Namen ausgibst, ist das ziemlich viel.
-
Hmm nunja^^ Ich habe ein "Mathe Programm".
Oben 5 Buttons : "Restart" "Beenden" "Quadratische Gleichungen" "Taschenrechner" "Parabeln" die zu "Unterprogrammen" führen ( sprich was anderes "malen" ) in den "unterprogrammen" nochmal jeweils 2-3 buttons / editboxes.
Und eben für jedes "Unterprogramm" den Code, der beschreibt, was es tun soll.
Also denk ich mal , das dass normal ist^^

Und kannst du mir sagen, wie ich so ne art "Checkbox" machen kann?
MfG Lorun
-
Mach einen Button und setz anstatt der Eigenschaft BS_PUSHBUTTON, die Eigenschaft BS_AUTOCHECKBOX oder BS_CHECKBOX.
Also für das alles ist die Sourcelänge normal, ja.
Mfg Ominion
-
hButtonSend = CreateWindow( "button", "Ausrechnen", WS_CHILD, rect.left + 400, rect.top + 85, iWidth + 20, iHeight, hWnd, NULL, ((LPCREATESTRUCT) lParam) -> hInstance, NULL ); else if ( lParam == (LPARAM)hButtonSend && HIWORD(wParam) == BN_CLICKED ) { iLength = GetWindowTextLength(hEditA); GetWindowText(hEditA, cValue_1, iLength+1); iLength = GetWindowTextLength(hEditB); GetWindowText(hEditB, cValue_2, iLength+1); iLength = GetWindowTextLength(hEditC); GetWindowText(hEditC, cValue_3, iLength+1); fDiskriminante = ( atof(cValue_2)*atof(cValue_2) ) - 4 * atof(cValue_1) * atof(cValue_3); InvalidateRect(hWnd, NULL, TRUE); }^^ so sehen meine DEfinitionen und meine Abfrage aus... geht dass so oder muss ich da was komplett anders machen?
-
Wenn ich das richtig sehe hast du nur einen else if-Zweig aber keinen if-Zweig, oder?
-
Das ist nur ein ausschnitt^^
Das ist der Komplette Code :
#include <windows.h> #include <cstdio> #include <cmath> LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); const int width = 700, height = 500; const char szAppName[] = "MatheProgramm"; static HWND hButtonClose, hButtonQuadGlei, hButtonRestart, hButtonSend, hButtonCalc, hButtonPlus; // Buttons static HWND hButtonMinus, hButtonMulti, hButtonDevide, hButtonParabels; static HWND hEditA, hEditB, hEditC, hEditCalc; // Editboxes static HWND hComboParabels_1; // Comboboxes static HWND hWnd; // Mainwindow MSG msg; WNDCLASS wc; int ixKoordButton = 10; int iyKoordButton = 10; int iHeight = 25; int iWidth = 80; int iLength = 0; int iProgramm = 0; char cValue_1[20], cValue_2[20], cValue_3[20], cResult_1[50], cResult_2[50]; float fDiskriminante = 0.0, fValue_1 = 0.0, fValue_2= 0.0, fValue_3= 0.0, fResult_1= 0.0, fResult_2= 0.0; void restart(bool); void restart(bool a) { if ( a == TRUE ) { InvalidateRect(hWnd, NULL, TRUE); } ShowWindow(hEditA, SW_HIDE); ShowWindow(hEditB, SW_HIDE); ShowWindow(hEditC, SW_HIDE); ShowWindow(hButtonSend, SW_HIDE); ShowWindow(hEditCalc, SW_HIDE); ShowWindow(hButtonPlus, SW_HIDE); ShowWindow(hButtonMinus, SW_HIDE); ShowWindow(hButtonMulti, SW_HIDE); ShowWindow(hButtonDevide, SW_HIDE); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); wc.lpszMenuName = 0; wc.lpszClassName = szAppName; RegisterClass(&wc); hWnd = CreateWindow( szAppName, szAppName, WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, hInstance, NULL ); ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd); 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 RECT rect; switch(message) { case WM_CREATE: { rect.left = 110; rect.top = 60; rect.right = 670; rect.bottom = 450; #include "mainbuttons.h" #include "quadglei.h" #include "calc.h" #include "parabels.h" break; } case WM_COMMAND: { if ( lParam == (LPARAM)hButtonClose && HIWORD(wParam) == BN_CLICKED ) { const char cClose[] = "Wollen Sie das Programm wirklich beenden?"; int iClose = MessageBox(hWnd, cClose, "Sicher?", MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON1); if ( iClose == IDYES ) { PostQuitMessage(0); } } else if ( lParam == (LPARAM)hButtonRestart && HIWORD(wParam) == BN_CLICKED ) { iProgramm = 0; restart(TRUE); } else if ( lParam == (LPARAM)hButtonQuadGlei && HIWORD(wParam) == BN_CLICKED ) { iProgramm = 1; restart(TRUE); ShowWindow(hEditA, SW_SHOW); ShowWindow(hEditB, SW_SHOW); ShowWindow(hEditC, SW_SHOW); ShowWindow(hButtonSend, SW_SHOW); } else if ( lParam == (LPARAM)hButtonCalc && HIWORD(wParam) == BN_CLICKED ) { iProgramm = 2; restart(TRUE); ShowWindow(hEditCalc, SW_SHOW); ShowWindow(hButtonPlus, SW_SHOW); ShowWindow(hButtonMinus, SW_SHOW); ShowWindow(hButtonMulti, SW_SHOW); ShowWindow(hButtonDevide, SW_SHOW); } else if ( lParam == (LPARAM)hButtonPlus && HIWORD(wParam) == BN_CLICKED ) { iLength = GetWindowTextLength(hEditCalc); GetWindowText(hEditCalc, cValue_1, iLength+1); SetWindowText(hEditCalc, NULL); fResult_1 += atof(cValue_1); InvalidateRect(hWnd, NULL, TRUE); } else if ( lParam == (LPARAM)hButtonMinus && HIWORD(wParam) == BN_CLICKED ) { iLength = GetWindowTextLength(hEditCalc); GetWindowText(hEditCalc, cValue_1, iLength+1); SetWindowText(hEditCalc, NULL); fResult_1 -= atof(cValue_1); InvalidateRect(hWnd, NULL, TRUE); } else if ( lParam == (LPARAM)hButtonMulti && HIWORD(wParam) == BN_CLICKED ) { fResult_1 = (fResult_1 == 0) ? 1 : fResult_1; iLength = GetWindowTextLength(hEditCalc); GetWindowText(hEditCalc, cValue_1, iLength+1); SetWindowText(hEditCalc, NULL); fResult_1 *= atof(cValue_1); InvalidateRect(hWnd, NULL, TRUE); } else if ( lParam == (LPARAM)hButtonDevide && HIWORD(wParam) == BN_CLICKED ) { iLength = GetWindowTextLength(hEditCalc); GetWindowText(hEditCalc, cValue_1, iLength+1); SetWindowText(hEditCalc, NULL); if ( atof(cValue_1) == 0 || !cValue_1 ) { break; } fResult_1 /= atof(cValue_1); InvalidateRect(hWnd, NULL, TRUE); } else if ( lParam == (LPARAM)hButtonSend && HIWORD(wParam) == BN_CLICKED ) { iLength = GetWindowTextLength(hEditA); GetWindowText(hEditA, cValue_1, iLength+1); iLength = GetWindowTextLength(hEditB); GetWindowText(hEditB, cValue_2, iLength+1); iLength = GetWindowTextLength(hEditC); GetWindowText(hEditC, cValue_3, iLength+1); fDiskriminante = ( atof(cValue_2)*atof(cValue_2) ) - 4 * atof(cValue_1) * atof(cValue_3); InvalidateRect(hWnd, NULL, TRUE); } else if ( lParam == (LPARAM)hButtonParabels && HIWORD(wParam) == BN_CLICKED ) { iProgramm = 3; restart(TRUE); ShowWindow(hComboParabels_1, SW_SHOW); } } case WM_PAINT: { PAINTSTRUCT ps; HDC hDC; hDC = BeginPaint(hWnd, &ps); RoundRect(hDC, 100, 50, 680, 460, 40, 40); switch(iProgramm) { case 0: { const char cWelcome[] = "Herzlich Willkommen. \n\n" "Mit diesem Programm können Sie Mathematische Probleme lösen.\n" "Unteranderem stehen Funktionen für :\n\n" "- Quadratische Gleichungen\n" "- einen einfachen Taschenrechner\n" "- Parabeln\n" "- zzz\n\n" "zur Verfügung."; DrawText(hDC, cWelcome, strlen(cWelcome), &rect, DT_WORDBREAK | DT_LEFT); HBRUSH hWelcomeBrush = (HBRUSH)SelectObject(hDC,CreateSolidBrush(RGB(255,0,0))); Ellipse(hDC, 300, 200, 500, 400); DeleteObject(hWelcomeBrush); HBRUSH hWelcomeBrush_2 = (HBRUSH)SelectObject(hDC,CreateSolidBrush(RGB(0,255,0))); Ellipse(hDC, 380, 150, 580, 300); DeleteObject(hWelcomeBrush_2); HBRUSH hWelcomeBrush_3 = (HBRUSH)SelectObject(hDC,CreateSolidBrush(RGB(0,0,255))); Ellipse(hDC, 400, 250, 600, 430); DeleteObject(hWelcomeBrush_3); break; } case 1: { const char cTopic[] = "Quadratische Gleichung"; DrawText(hDC, cTopic, strlen(cTopic), &rect, DT_CENTER); const char cQuadDesc_1[] = "Form der Gleichung : ax² + bx + c"; TextOut(hDC, rect.left, rect.top + 40, cQuadDesc_1, strlen(cQuadDesc_1)); MoveToEx(hDC, rect.left + 30, rect.top + 70, NULL); LineTo(hDC, rect.right - 30, rect.top + 70); RoundRect(hDC, rect.left + 135, rect.top + 145, rect.left + 450, rect.top + 170, 10, 10); RoundRect(hDC, rect.left + 135, rect.top + 185, rect.left + 450, rect.top + 210, 10, 10); if ( fDiskriminante == 0 ) { fResult_1 = ( - atof(cValue_2) ) / 2 * atof(cValue_1); sprintf(cResult_1, "Die Nullstelle ist : %f", fResult_1); TextOut(hDC, rect.left + 150, rect.top + 150, cResult_1, strlen(cResult_1)); } else if ( fDiskriminante < 0 ) { sprintf(cResult_1, "Es gibt keine reele Nullstelle"); TextOut(hDC, rect.left + 150, rect.top + 150, cResult_1, strlen(cResult_1)); } else if ( fDiskriminante > 0 ) { fResult_1 = ( - atof(cValue_2) + sqrt(fDiskriminante) ) / 2 * atof(cValue_1); fResult_2 = ( - atof(cValue_2) - sqrt(fDiskriminante) ) / 2 * atof(cValue_1); sprintf(cResult_1, "Die erste Nullstelle ist : %f", fResult_1); sprintf(cResult_2, "Die zweite Nullstelle ist : %f", fResult_2); TextOut(hDC, rect.left + 150, rect.top + 150, cResult_1, strlen(cResult_1)); TextOut(hDC, rect.left + 150, rect.top + 190, cResult_2, strlen(cResult_2)); } break; } case 2: { const char cTopic[] = "Taschenrechner"; DrawText(hDC, cTopic, strlen(cTopic), &rect, DT_CENTER); RoundRect(hDC, rect.left + 135, rect.top + 345, rect.left + 450, rect.top + 370, 10, 10); sprintf(cResult_1, "Die Ergebnis ist : %f", fResult_1); TextOut(hDC, rect.left + 150, rect.top + 350, cResult_1, strlen(cResult_1)); break; } case 3: { const char cTopic[] = "Parabeln"; DrawText(hDC, cTopic, strlen(cTopic), &rect, DT_CENTER); break; } } EndPaint(hWnd, &ps); break; } case WM_DESTROY: { PostQuitMessage(0); break; } } return DefWindowProc(hWnd, message, wParam, lParam); }
-
Ich finde nichts, was man nicht so machen kann. Ich persönlich würde statt DrawText zwar TextOut benutzen, aber das liegt im eigenem Ermessen.
Mfg Ominion
-
ok danke
dann bin ich beruhig 
MfG Lorun
-
Mir ist doch was aufgefallen: du benutzt die Funktion sprintf? Dann musst du noch den Header stdio.h einbinden!
Mfg Ominion
[EDIT]Mein Fehler, tschuldigung[/EDIT]
-
Verwendet statt sprintf wsprintf! Damit gehst Du auch gleich noch sicher, das Dein Code Unicode sicher ist. Nur mal so am Rande bemerkt und noch was:
Statt char - TCHAR und wenn Du Texte schreibst, setze das TEXT-Makro davor, also: TCHAR szTest[] = TEXT ( "Mein Test fuer heute" );
Damit gehst Du ganz sicher, das Dein Quellcode Unicodevorbereitet ist.
-
Man sollte eher "_stprintf" verwenden, wenn man schon auf TCHAR hinweist!