не создается окно

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by fire64, 29 Nov 2008.

  1. fire64

    fire64 Elder - Старейшина

    Joined:
    1 Apr 2008
    Messages:
    251
    Likes Received:
    22
    Reputations:
    5
    подскажите что не так в этом коде

    дело в том что выполняется условие

    if (!hWnd)
    {
    return false;
    }

    и окно не создается



    PHP:
    #include <windows.h>

    #include "stdafx.h"

    extern HMODULE hClientApp;

    LPCTSTR lpszAppName  "MyApp";
    LPCTSTR lpszTitle    "My Application"



    bool Wind_Init() 
    {


       
    MSG        msg;
       
    HWND       hWnd
       
    WNDCLASSEX wc;

       
    // Register the application window class.
       //............................................
       
    wc.style         CS_HREDRAW CS_VREDRAW;
     
    //  wc.lpfnWndProc   = (WNDPROC)WndProc;       
       
    wc.cbClsExtra    0;                      
       
    wc.cbWndExtra    0;                      
       
    wc.hInstance     = (HINSTANCE)hClientApp;              
       
    wc.hIcon         LoadIcon( (HINSTANCE)hClientApplpszAppName ); 
       
    wc.hCursor       LoadCursor(NULLIDC_ARROW);
       
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
       
    wc.lpszMenuName  lpszAppName;              
       
    wc.lpszClassName lpszAppName;              
       
    wc.cbSize        sizeof(WNDCLASSEX);
    //   wc.hIconSm       = LoadImage( (HINSTANCE)hClientApp, lpszAppName, 
      //                               IMAGE_ICON, 16, 16,
        //                             LR_DEFAULTCOLOR );
        
    if (!RegisterClassEx( &wc )) 
        {
            return 
    false;
        }

        
    hWnd CreateWindowlpszAppName
                            
    lpszTitle,    
                            
    WS_OVERLAPPEDWINDOW

                            
    //size windows

                            
    400,   // координаты окна по ширине
                            
    300,   // координаты окна по высоте
                            
    170,  //  ширина окна
                            
    350,   //  высота окна
                            

                            
    NULL,              
                            
    NULL,              
                            (
    HINSTANCE)hClientApp,         
                            
    NULL               
                          
    );

        if (!
    hWnd
        {
            return 
    false;
        }    
        
       
    ShowWindowhWnd); 
       
    UpdateWindowhWnd );

       while( 
    GetMessage( &msgNULL00) )   
       {
          
    TranslateMessage( &msg ); 
          
    DispatchMessage( &msg );  
       }

        return 
    true;
    }

     
  2. 0verbreaK

    0verbreaK Elder - Старейшина

    Joined:
    30 Apr 2008
    Messages:
    318
    Likes Received:
    42
    Reputations:
    -3
    GetLastError что говорит?
     
  3. bons

    bons Elder - Старейшина

    Joined:
    20 Dec 2007
    Messages:
    286
    Likes Received:
    121
    Reputations:
    21
    вот работающий код минимального окна

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
         {
         static char szAppName[] = "HelloWin" ;
         HWND        hwnd ;
         MSG         msg ;
         WNDCLASSEX  wndclass ;
    
         wndclass.cbSize        = sizeof (wndclass) ;
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;
         wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
    
         RegisterClassEx (&wndclass) ;
    
         hwnd = CreateWindow (szAppName,         // window class name
    		            "The Hello Program",     // window caption
                        WS_OVERLAPPEDWINDOW,     // window style
                        CW_USEDEFAULT,           // initial x position
                        CW_USEDEFAULT,           // initial y position
                        CW_USEDEFAULT,           // initial x size
                        CW_USEDEFAULT,           // initial y size
                        NULL,                    // parent window handle
                        NULL,                    // window menu handle
                        hInstance,               // program instance handle
    		            NULL) ;		             // creation parameters
    
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
    
         while (GetMessage (&msg, NULL, 0, 0))
              {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
              }
         return msg.wParam ;
         }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
         {
         HDC         hdc ;
         PAINTSTRUCT ps ;
         RECT        rect ;
    
         switch (iMsg)
              {
    
              case WM_PAINT :
    	           hdc = BeginPaint (hwnd, &ps) ;
    
                   GetClientRect (hwnd, &rect) ;
    
                   DrawText (hdc, "Hello, world", -1, &rect,
    			             DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
    
    	           EndPaint (hwnd, &ps) ;
                   return 0 ;
    
              case WM_DESTROY :
                   PostQuitMessage (0) ;
                   return 0 ;
              }
    
         return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
         }
     
  4. t04

    t04 Elder - Старейшина

    Joined:
    10 Jan 2007
    Messages:
    137
    Likes Received:
    40
    Reputations:
    8
    Code:
    procedure WndProc(hWnd,uMsg:LongWord;wParam,lParam:Longint) stdcall;
    begin
      case uMsg of
        WM_DESTROY: PostQuitMessage(0);
      DefWindowProc(hWnd,uMsg,wParam,lParam);
    end;
    
    ...
    в заполнении WNDCLASSEX
      wc.lpfnWndProc:=@WndProc;
    Code:
    ShowWindow( hWnd, 0 );
    
    const
      SW_HIDE = 0;
      SW_NORMAL = 1;
    
    сделай
    ShowWindow( hWnd, SW_NORMAL );
    или 
    ShowWindow( hWnd, 1 );
    убери
    Code:
        if (!hWnd)  
        { 
            return false; 
        }    
    
    так никто не делает
    сделай так

    if (hWnd=INVALID_HANDLE_VALUE)or(hWnd<1) then
    exit;
     
    #4 t04, 30 Nov 2008
    Last edited: 30 Nov 2008
  5. Dian

    Dian Elder - Старейшина

    Joined:
    2 Sep 2008
    Messages:
    57
    Likes Received:
    11
    Reputations:
    4
    Закомменчено присвайвание указателя на оконную процедуру. Без нее работать не может.
    А вообще обычно лучше смотреть код ошибки
     
  6. 0verbreaK

    0verbreaK Elder - Старейшина

    Joined:
    30 Apr 2008
    Messages:
    318
    Likes Received:
    42
    Reputations:
    -3
    Dian без обработчика тоже должно работать, для окна не совсем нужен он.
     
  7. t04

    t04 Elder - Старейшина

    Joined:
    10 Jan 2007
    Messages:
    137
    Likes Received:
    40
    Reputations:
    8
    думаю что ошибка в каком то ненормальном if операторе

    никогда не видел чтобы писали такое

    делфи даже не скомпилирует такое "чудо"
     
  8. bons

    bons Elder - Старейшина

    Joined:
    20 Dec 2007
    Messages:
    286
    Likes Received:
    121
    Reputations:
    21
    :))
    потому что это не делфи;)
     
  9. De-visible

    De-visible [NDC] Network develope c0ders

    Joined:
    6 Jan 2008
    Messages:
    916
    Likes Received:
    550
    Reputations:
    66
    t04, ты помоему темой ошибся.
     
  10. sn0w

    sn0w Статус пользователя:

    Joined:
    26 Jul 2005
    Messages:
    1,021
    Likes Received:
    1,200
    Reputations:
    327
    топикстартер, а как ты предлагаешь окну работать если в твоем коде:

    // wc.lpfnWndProc = (WNDPROC)WndProc;

    ?!?!?