Wie erstelle ich ein window ????



  • Mein Standard-Gerüst sieht folgendermaßen aus:

    #include <windows.h>
    #include <commctrl.h>
    //---------------------------------------------------------------------------
    //---------------------------------------------------------------------------
    
    // Global Variables
    LPCTSTR    g_lpszAppTitle = TEXT("<AppTitle>");
    LPCTSTR    g_lpszAppClass = TEXT("<AppWindowClass>");
    HINSTANCE  g_hInstance;
    HWND       g_hAppWindow;
    //---------------------------------------------------------------------------
    
    // Function Prototypes
    VOID              RegisterAppWindow();
    VOID              Create_GUI_Interface();
    LRESULT CALLBACK  AppWndProc(HWND, UINT, WPARAM, LPARAM);
    VOID              ShowMessage(LPCTSTR);
    VOID              ShowLastErrorString();
    //---------------------------------------------------------------------------
    //---------------------------------------------------------------------------
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, int)
    {
         MSG  msg;
    
         InitCommonControls();
         g_hInstance = hInstance;
         RegisterAppWindow();
         Create_GUI_Interface();
    
         // Message Loop
         while(GetMessage(&msg, NULL, 0, 0))
         {
               TranslateMessage(&msg);
               DispatchMessage(&msg);
         }
         return msg.wParam ;
    }
    //---------------------------------------------------------------------------
    
    VOID RegisterAppWindow()
    {
         WNDCLASSEX wndclass;
         wndclass.cbSize        = sizeof(WNDCLASSEX);
         wndclass.style         = 0;
         wndclass.lpfnWndProc   = AppWndProc;
         wndclass.cbClsExtra    = 0;
         wndclass.cbWndExtra    = 0;
         wndclass.hInstance     = g_hInstance;
         wndclass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
         wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
         wndclass.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
         wndclass.lpszMenuName  = NULL;
         wndclass.lpszClassName = g_lpszAppClass;
         wndclass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    
         RegisterClassEx(&wndclass);
    }
    //---------------------------------------------------------------------------
    
    VOID Create_GUI_Interface()
    {
       LONG style, ex_style;
    
       // Create Application Window
       style = WS_VISIBLE|WS_MINIMIZEBOX|WS_SYSMENU;
       ex_style = 0;
       g_hAppWindow = CreateWindowEx(ex_style, g_lpszAppClass, g_lpszAppTitle, style,
                                     100, 100, 300, 400,
                                     NULL, NULL, g_hInstance, NULL);
    }
    //---------------------------------------------------------------------------
    
    LRESULT CALLBACK AppWndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
    {
       switch(iMsg)
       {
          case WM_CREATE:
             break;
    
          case WM_COMMAND:
             switch(LOWORD(wParam))  // Switch between control IDs
             {
             }
             break;
    
          case WM_DESTROY:
             PostQuitMessage(0);
             break;
       }
    
       return DefWindowProc(hwnd, iMsg, wParam, lParam);
    }
    //---------------------------------------------------------------------------
    
    VOID ShowMessage(LPCTSTR str)
    {
       MessageBox(g_hAppWindow, str, g_lpszAppTitle,  MB_OK);
    }
    //---------------------------------------------------------------------------
    
    VOID ShowLastErrorString()
    {
       LPVOID lpMsgBuf;
       FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
                     FORMAT_MESSAGE_IGNORE_INSERTS,
                     NULL,
                     GetLastError(),
                     MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                     (LPTSTR) &lpMsgBuf,
                     0,
                     NULL
                    );
        MessageBox(g_hAppWindow, (LPCTSTR)lpMsgBuf, TEXT("Error"), MB_OK | MB_ICONERROR);
        LocalFree(lpMsgBuf);
    }
    //---------------------------------------------------------------------------
    


  • Ach ja, du benutzt ja den BCB, nicht wahr? Eventuell ist es da noch notwendig oder ratsam, eine weitere Datei zu inkludieren nämlich

    #include <condefs.h>
    

    Das ist zumindest in meinem BCB3 vonnöten. Vielleicht ja auch in deinem BCB6.



  • und was macht condefs.h ?

    nur noch mal ne frage HWND = der teil worunter der name meines Prog also Window in der Win Registry reg. wird oder? also zum beispiel unter knut, dann ist mein fenster unter knut reg? bzw. das fenster was ich create wird den programm knut zugeteilt, ist das richtig?



  • T0bi schrieb:

    ist das richtig?

    nein



  • und wie ist es dann richtig?



  • Ein HWND (Handle to a Window) hat mit der Windows-Registry rein garnichts zu tun. Du möchtest doch sicher 'mal ein Tutorial über die WinAPI lesen!?

    Greetz, Swordfish



  • huhu swordfish *g* bin gerade dabei eins zu lesen o_O.... nun beschaeftigt mich des naechste problem ich kann jetzt nen fenster erstellen und moechte nen text drin ausgeben lassen, alles sieht so aus:

    //---------------------------------------------------------------------------
    
    #include <windows.h>
    
    LRESULT CALLBACK WinProc ( HWND, UINT, WPARAM, LPARAM );
    const char szAppName[] = "Meine Fenster mit Text :)";
    
    WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    
        HWND hWnd;
        MSG messages;
        WNDCLASS wc;
    
        wc.style = CS_VREDRAW | CS_HREDRAW;
        wc.lpfnWndProc = WinProc;
        wc.hInstance = hInstance;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.lpszMenuName = NULL;
        wc.lpszClassName = szAppName;
        wc.hIcon = LoadIcon( NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor( NULL, IDC_ARROW );
    
        if( !(RegisterClass( &wc ) ) )
        return 0;
    
        hWnd = CreateWindow( szAppName,
                             szAppName,
                             WS_OVERLAPPEDWINDOW,
                             CW_USEDEFAULT, CW_USEDEFAULT,
                             500, 300,
                             NULL, NULL,
                             hInstance,
                             NULL );
    
        ShowWindow( hWnd, SW_SHOW );
        UpdateWindow( hWnd );
    
        while( GetMessage( &messages, NULL, 0, 0 ) ) {
    
            TranslateMessage( &messages );
            DispatchMessage( &messages );
        }
        return messages.wParam;
    }
    
    LRESULT CALLBACK WinPorc ( HWND hWnd, UINT messages, WPARAM wParam, LPARAM lParam ) {
    
            switch( messages ) {
    
                case WM_PAINT : {
    
                    PAINTSTRUCT ps;
                    HDC hDc;
    
                    const char szText[] = "Wer das liest is schön :P";
    
                    hDc = BeginPaint( hWnd, &ps );
                    {
                        TextOut( hDc, 50, 50, szText, sizeof( szText ) - 1 );
                    }
                    EndPaint( hWnd, &ps );
                    return 0;
                }
    
                case WM_DESTROY : {
    
                    PostQuitMessage( 0 );
                    return 0;
                }
            }
            return DefWindowProc( hWnd, messages, wParam, lParam );
    }
    

    aber ich bekomm vom linker ne warnung :

    [Linker Error] Unresolved external '__stdcall WinProc(void *, unsigned int, unsigned int, long)' referenced from C:\PROGRAMME\BORLAND\CBUILDER6\PROJECTS\UNIT1.OBJ
    

    was mus sich jetz tun???



  • Dann schau nochmal ganz genau auf deine Funktionsnamen. 😉
    Übrigens würde ich das eher so machen:

    LRESULT CALLBACK WinProc ( HWND hWnd, UINT messages, WPARAM wParam, LPARAM lParam ) 
    {
        static const char szText[] = "Wer das liest is schön :P";
    
            switch( messages ) {
    
                case WM_PAINT : {
    
                    PAINTSTRUCT ps;
                    HDC hDc;
    
                    hDc = BeginPaint( hWnd, &ps );
                    {
                        TextOut( hDc, 50, 50, szText, sizeof( szText ) - 1 );
                    }
                    EndPaint( hWnd, &ps );
                    return 0;
                }
    
                case WM_DESTROY : {
    
                    PostQuitMessage( 0 );
                    return 0;
                }
            }
            return DefWindowProc( hWnd, messages, wParam, lParam );
    }
    

    Wozu immer wieder neu den Text definieren. Außerdem nimmt man für statische Textausgaben meist Static-Controls und schreibt nicht auf's Fenster.



  • T0bi schrieb:

    was mus sich jetz tun???

    Genau ein 'o' mit einem 'r' vertauschen. 😉

    Greetz, Swordfish



  • *grrrrrrrr* is mir des shcon zum 2mal in dem programm passiert hrhrhr....



  • T0bi schrieb:

    WinPorc

    ⚠ != ⚠

    T0bi schrieb:

    WinProc


Anmelden zum Antworten