Text im Fenster, wenn Button gedrück wird



  • Moin moin,

    ich möchte, wenn man einen Button drückt, dass ein Text im Hauptfenster erscheint

    case WM_COMMAND:
      switch(LOWORD(wParam))
      {
      case ID_START:
         hdc = BeginPaint (hwnd, &ps);  // Text
         TextOut (hdc, 15, 10, "Ich bin ein Text" !!!", 35);
         EndPaint (hwnd, &ps);
      break;
    

    so dachte ich mir das zuerst, doch dann bekam ich den heißen tipp:

    Das darf nicht im WM_COMMAND Handler stehen sondern nur im WM_PAINT Handler.

    In WM_COMMAND setzt du nur eine Variable und rufst dann InvalidateRect auf.

    case WM_COMMAND:
       ZeigeText = true;
       InvalidateRect(hWnd, NULL, TRUE);
    

    daaurf hin machte ich folgendes:

    switch(LOWORD(wParam))
    {
       case ID_START: // Aktion bei Button "Sart"   
       start = true;
          InvalidateRect(hwnd, NULL, TRUE);          
    }
    

    und in WM_PAINT

    if(start == true)
             TextOut (hdc, 20, 100, "Hier konmmt die Zahl !!!", 24);
    

    doch es funzt nicht, ich brauche eure hilfe, ich weiß nich mehr weiter...

    Es kann gut sein, dass ich etwas grundlegendes nicht verstanden habe oder so, aba schickt mir doch bitte einen kompletten quelltext (nicht wie oben), damit ich das endlich verstehe 🙄



  • ist start static oder global?



  • zeig mal den ganzen quelltext damit ich das bei mir compilieren kann



  • hier is der gesamte code (start ist static)

    #include <windows.h>
    
    #define ID_START   1
    #define ID_ENDE    2
    
    #define ID_RAHMEN  3
    #define ID_LEVEL1  4
    #define ID_LEVEL2  5
    #define ID_LEVEL3  6
    #define ID_RAHMEN2 7
    #define ID_LISTE   8
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    
    void winButtons		  (HWND, HWND, HWND, HWND, LPARAM);
    void winButtonsAktion (HWND, WPARAM, bool&);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
       static TCHAR szAppName[] = TEXT ("Klassenname");
       HWND         hwnd;
       MSG          msg;
       WNDCLASSEX   wndclassex = {0};
    
       wndclassex.cbSize        = sizeof(WNDCLASSEX);
       wndclassex.style         = CS_HREDRAW | CS_VREDRAW;
       wndclassex.lpfnWndProc   = WndProc;
       wndclassex.cbClsExtra    = 0;
       wndclassex.cbWndExtra    = 0;
       wndclassex.hInstance     = hInstance;
       wndclassex.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
       wndclassex.hCursor       = LoadCursor (NULL, IDC_ARROW);
       wndclassex.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
       wndclassex.lpszMenuName  = NULL;
       wndclassex.lpszClassName = szAppName;
       wndclassex.hIconSm       = wndclassex.hIcon;
    
       if (!RegisterClassEx (&wndclassex))
       {
          MessageBox (NULL, TEXT ("RegisterClassEx fehlgeschlagen!"),
                      szAppName, MB_ICONERROR);
          return 0;
       }
    
       hwnd = CreateWindowEx (WS_EX_OVERLAPPEDWINDOW, // erweiterter Fensterstil
                      szAppName, // Name der Fensterklasse
                      TEXT ("Schnelle Zahlen"), // Fenstertitel
                      WS_OVERLAPPEDWINDOW, // Fensterstil
                      CW_USEDEFAULT, // X-Position des Fensters                      
                      CW_USEDEFAULT, // Y-Position des Fensters       
                      500, // Fensterbreite                 
                      500, // Fensterhöhe                
                      NULL, // übergeordnetes Fenster
                      NULL, // Menü           
                      hInstance, // Programm-Kopiezähler (Programm-ID)            
                      NULL); // zusätzliche Parameter
    
       ShowWindow (hwnd, iCmdShow);
       UpdateWindow (hwnd);
    
       while (GetMessage (&msg, NULL, 0, 0))
       {
          TranslateMessage (&msg);
          DispatchMessage (&msg);
       }
       return msg.wParam;
    }
    
    // Die Hauptnachrichtenschleife
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
       HDC hdc, hdcMem;
       static HBITMAP hBitMap;
       static BITMAP bitmap ;
       PAINTSTRUCT ps;
       static HWND ButtonHandle, ListHandle, EingHandle;
       int nModePrev;
       static bool start = false;
    
       switch (message)
       {
       case WM_CREATE:
    	  // bild
    	  hBitMap = (HBITMAP)LoadImage(0,"zahlen.bmp",IMAGE_BITMAP, 0, 0,
                     LR_DEFAULTSIZE|LR_LOADFROMFILE|LR_CREATEDIBSECTION);
    
          if(hBitMap==NULL)
          {
              MessageBox(hwnd,"Bild konnte nicht geladen werden","Error",0);
              return -1;
          }  
          GetObject (hBitMap, sizeof (BITMAP), &bitmap);  
    
    	  winButtons (hwnd, ButtonHandle, ListHandle, EingHandle, lParam);	// Buttons		
          break;
    
       case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps);
    
          hdcMem = CreateCompatibleDC (hdc);
          SelectObject (hdcMem, hBitMap);
          StretchBlt (hdc, 0, 0, bitmap.bmWidth, bitmap.bmHeight, 
    		       	  hdcMem, 0, 0, 500, 500, SRCCOPY);
    
          DeleteDC (hdcMem); 		 
    
          nModePrev = SetBkMode(hdc, TRANSPARENT);
          TextOut (hdc, 15, 10, "Willkommen zu \"Schnelle Zahlen\" !!!", 35);
          TextOut (hdc, 320, 283, "Ihre Eingabe...",15);
    
          if(start == true)
             TextOut (hdc, 180, 100, "Hier konmmt die Zahl !!!", 24);
    
          EndPaint (hwnd, &ps);
          break;
    
       case WM_COMMAND:   	  
    	  winButtonsAktion (hwnd, wParam, start);
    	  break;
    
       case WM_DESTROY:
          PostQuitMessage (0);
          break;
       }
    
       return DefWindowProc (hwnd, message, wParam, lParam);
    }
    
    void winButtons (HWND hwnd, HWND ButtonHandle, HWND ListHandle, HWND EingHandle, LPARAM lParam)
    {
       // ------------ Start / Ende ----------------
       ButtonHandle = CreateWindow("BUTTON", "Start", WS_CHILD |    // Mein Button
    	  						   WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER, 
    							   15, 50, 100, 100, hwnd, (HMENU) ID_START, // <-- Kennziffer
    							   GetModuleHandle(NULL), NULL);	   
       ButtonHandle = CreateWindow("BUTTON", "Ende", WS_CHILD |    // Mein Button 
    	  						   WS_VISIBLE | BS_PUSHBUTTON | BS_CENTER, 
    							   15, 350, 100, 100, hwnd, (HMENU) ID_ENDE,  // <-- Kennziffer 
    							   GetModuleHandle(NULL), NULL);
       // ------------ Start / Ende ----------------
    
       // ------------ Levelauswahl ----------------
       ButtonHandle = CreateWindow("BUTTON", "Level", WS_CHILD |    // Rahmen
    	  						   WS_VISIBLE | BS_GROUPBOX | BS_CENTER, 
    							   15, 200, 100, 100, hwnd, (HMENU) ID_RAHMEN,
    							   GetModuleHandle(NULL), NULL);
       ButtonHandle = CreateWindow("BUTTON", "Leicht", WS_CHILD |    // Auswahlmöglichkeit 1
    	  						   WS_VISIBLE | BS_AUTORADIOBUTTON | BS_CENTER, 
    							   30, 230, 70, 20, hwnd, (HMENU) ID_LEVEL1,
    							   GetModuleHandle(NULL), NULL);							   	
       ButtonHandle = CreateWindow("BUTTON", "Mittel", WS_CHILD |    // Auswahlmöglichkeit 2
    	  						   WS_VISIBLE | BS_AUTORADIOBUTTON | BS_CENTER, 
    							   30, 250, 70, 20, hwnd, (HMENU) ID_LEVEL2,
    							   GetModuleHandle(NULL), NULL);
       ButtonHandle = CreateWindow("BUTTON", "Schwer", WS_CHILD |    // Auswahlmöglichkeit 3
    	  						   WS_VISIBLE | BS_AUTORADIOBUTTON | BS_CENTER, 
    							   30, 270, 70, 20, hwnd, (HMENU) ID_LEVEL3,
    							   GetModuleHandle(NULL), NULL);
       // ------------ Levelauswahl ----------------
    
       // ------------ Eingabe ----------------   
       EingHandle =  CreateWindowEx(WS_EX_LEFT, "edit", "", WS_CHILD | WS_VISIBLE,
       			  	 				420, 280, 50, 20, hwnd, NULL,
    								((LPCREATESTRUCT) lParam) -> hInstance, NULL);
       // ------------ Eingabe ----------------
    
       // ------------ Highscore ----------------
       ButtonHandle = CreateWindow("BUTTON", "Highscore", WS_CHILD |    // Rahmen
    	  						   WS_VISIBLE | BS_GROUPBOX | BS_CENTER, 
    							   200, 320, 215, 130, hwnd, (HMENU) ID_RAHMEN2,
    							   GetModuleHandle(NULL), NULL);
       ListHandle = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("LISTBOX"), NULL,
       			  				   WS_CHILD | WS_VISIBLE , 213 , 343 ,190 ,100 ,
    							   hwnd , (HMENU) ID_LISTE , ((LPCREATESTRUCT)lParam)->hInstance,NULL);
       // ------------ Highscore ----------------
    }
    
    void winButtonsAktion (HWND hwnd, WPARAM wParam, bool &start)
    {	
       switch(LOWORD(wParam))
       {
       case ID_START: // Aktion bei Button "Sart"
          HWND hButton;
    
       	  hButton = GetDlgItem(hwnd, ID_LEVEL1);
       	  if (SendMessage(hButton , BM_GETCHECK, 0, 0)==BST_CHECKED)
    	  {
    	     MessageBox(NULL, "Spiel wird mit leichter Schwierigkeitsstufe gestartet...",
    		 		    "Aktion...", MB_OK | MB_SETFOREGROUND);
    		 start = true;		    
    	  }
    	  hButton = GetDlgItem(hwnd, ID_LEVEL2);
       	  if (SendMessage(hButton , BM_GETCHECK, 0, 0)==BST_CHECKED)
    	  {
    	     MessageBox(NULL, "Spiel wird mit mittlerer Schwierigkeitsstufe gestartet...",
    		 		    "Aktion...", MB_OK | MB_SETFOREGROUND);
    		 start = true;
    	  }
    	  hButton = GetDlgItem(hwnd, ID_LEVEL3);
       	  if (SendMessage(hButton , BM_GETCHECK, 0, 0)==BST_CHECKED)
    	  {
    	     MessageBox(NULL, "Spiel wird mit leichter Schwirigkeitsstufe gestartet...",
    		 		    "Aktion...", MB_OK | MB_SETFOREGROUND);
    		 start = true;
    	  }
    
    	  if(start == false)
    	  { 
    	  	 MessageBox(NULL, "Bitte Schwierigkeitsstufe auswählen!!!",
    		 		 	"Aktion...", MB_OK | MB_SETFOREGROUND);
    	  }
          break;
    
       case ID_ENDE: // Aktion bei Button "Ende"
    	  MessageBox(NULL, "Programm wird beendet",
    		 		 "Aktion...", MB_OK | MB_SETFOREGROUND);
    	  PostQuitMessage (0);			
          break;	
       } 
    }
    

    Ich hab versucht, ein paar aufgaben in funktionen zu packen (programmier eigendlich nur c++). Ich bitte euch mich nicht mit verbesserungsvorschlägen zuzuschmeißen, sondern nur dieses eine problem zu beschprechen (sonst blick ich garnich mehr durch^^)

    das programm ist noch lange noch nicht fertig (ich muss noch rauskriegen, wie man variabeln in das "TextOut" reinbekommt usw., aber die grundzuüge sind schon fertig^^

    um das zu compilieren brauch man die datei "logo.bmp" um exe-ordner



  • ahh, "zahlen.bmp" ^^



  • Bitte kürze deinen Code auf das Entscheidene!



  • hat sich erledigt, es klappt



  • woran lag es?


Anmelden zum Antworten