winapi programm - createDebugConsole



  • hi leute! ich denke sowas könnte jemand mal brauchen, deshalb poste ich mal den code...
    mit dem hauptfenster wird ein konsolenfenster erstellt, recht nützlich wenn man auf debug fehlersuche ist...man kann dann ganz normal mit cout arbeiten

    viel spaß!

    cu

    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
       static char szAppName[] = TEXT ("Klassenname");
       HWND        hwnd;
       MSG         msg;
    
       WNDCLASSEX wndclassex;
       memset (&wndclassex, 0, sizeof (WNDCLASSEX));
    
       wndclassex.cbSize        = sizeof(WNDCLASSEX);
       wndclassex.style         = CS_HREDRAW | CS_VREDRAW;
       wndclassex.lpfnWndProc   = WndProc;
    
       wndclassex.hInstance     = GetModuleHandle(NULL);
       wndclassex.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
       wndclassex.hCursor       = LoadCursor (NULL, IDC_ARROW);
       wndclassex.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    
       wndclassex.lpszMenuName  = (LPSTR)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 ("Filetransfer"), // Fenstertitel
    						  WS_OVERLAPPEDWINDOW, // Fensterstil
    						  CW_USEDEFAULT, // X-Position des Fensters                      
    						  CW_USEDEFAULT, // Y-Position des Fensters       
    					          CW_USEDEFAULT, // Fensterbreite                 
    						  CW_USEDEFAULT, // Fensterhöhe                
    						  (HWND)NULL, // übergeordnetes Fenster
    						  (HMENU)NULL, // Menü           
    						  GetModuleHandle (NULL), // Programm-Kopiezähler (Programm-ID)            
    						  NULL); // zusätzliche Parameter
    
       ShowWindow (hwnd, SW_SHOW);
       UpdateWindow (hwnd);
    
       while (GetMessage (&msg, NULL, 0, 0))
       {
          TranslateMessage (&msg);
          DispatchMessage (&msg);
       }
       return msg.wParam;
    }
    
    void createDebugConsole()
    {
      AllocConsole();
    
      freopen("CONOUT$", "w", stdout);
      freopen("CONOUT$", "w", stderr);
      freopen("CONIN$", "r", stdin);
    }
    
    // Die Hauptnachrichtenschleife
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
       HDC hdc;
       PAINTSTRUCT ps;
    
       switch (message)
       {
       case WM_CREATE:
    	    createDebugConsole();
    		break;
    
       case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps);
          TextOut (hdc, 0, 0, "Fenster", 7);
          EndPaint (hwnd, &ps);
          return (0);
    
       case WM_DESTROY:
          PostQuitMessage (0);
          return (0);
    
       default:
    	   return DefWindowProc (hwnd, message, wParam, lParam);
       }
    }
    

Anmelden zum Antworten