Thread: <windows.h>

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    <windows.h>

    hey i'm really curious about being able to program with windows interface. I just finished my C++ class but I'm an IS major so i really just learned how to do simple business apps. I tried compiling the sample code in the tutorial and it didn't work in microsoft visual C++ 6.0.

    could someone post a really simple program (producing a cool window to the screen) that I could copy and mess around with?

    Do IO streams and basic rules stay the same in a windows environment?

    thanks in advance.
    cout << "\aBIOTCH";

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "could someone post a really simple program"

    lol. Here's a windows programming Hello World program:
    Code:
    #include <windows.h>
    
    long WINAPI WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
    {
       WNDCLASS WindowClass;                 
    
       static char szAppName[] = "OFWin";    
       HWND hWnd;                           
       MSG msg;                              
    
       
       WindowClass.style   = CS_HREDRAW | CS_VREDRAW;
    
       
       WindowClass.lpfnWndProc = WindowProc;
    
       WindowClass.cbClsExtra = 0;          
       WindowClass.cbWndExtra = 0;          
    
       WindowClass.hInstance = hInstance;   
    
       
       WindowClass.hIcon = LoadIcon(0, IDI_APPLICATION);
    
       
       WindowClass.hCursor = LoadCursor(0, IDC_ARROW);
    
       
       WindowClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(GRAY_BRUSH));
    
       WindowClass.lpszMenuName = 0;           
    
       WindowClass.lpszClassName = szAppName;  
    
       
       RegisterClass(&WindowClass);
    
       
       hWnd = CreateWindow(
              szAppName,                       
              "A Basic Window the Hard Way",   
              WS_OVERLAPPEDWINDOW,             
              CW_USEDEFAULT,         
              CW_USEDEFAULT,         
              CW_USEDEFAULT,         
              CW_USEDEFAULT,         
              0,                     
              0,                     
              hInstance,             
              0                      
            );
    
       ShowWindow(hWnd, nCmdShow);  
       UpdateWindow(hWnd);           
    
       while(GetMessage(&msg, 0, 0, 0) == TRUE)   
       {
          TranslateMessage(&msg);                 
          DispatchMessage(&msg);                 
       }
    
       return msg.wParam;                        
    }
    
    
    
    long WINAPI WindowProc(HWND hWnd, UINT message, WPARAM wParam,
                           LPARAM lParam)
    {
       HDC hDC;                       
       PAINTSTRUCT PaintSt;           
       RECT aRect;                   
    
       switch(message)                
       {
          case WM_PAINT:                       
             hDC = BeginPaint(hWnd, &PaintSt); 
    
             
             GetClientRect(hWnd, &aRect);
    
             SetBkMode(hDC, TRANSPARENT);      
    
             
             DrawText(
                 hDC,                  
                 "But, soft! What light through yonder window breaks?",
                 -1,                   
                 &aRect,              
                 DT_SINGLELINE|        
                 DT_CENTER|            
                 DT_VCENTER);          
    
             EndPaint(hWnd, &PaintSt); 
             return 0;
    
          case WM_DESTROY:             
             PostQuitMessage(0);
             return 0;
    
          default:            
                               
             return DefWindowProc(hWnd, message, wParam, lParam);
       }
    }
    Last edited by 7stud; 04-29-2003 at 12:34 AM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    Code:
    Linking...
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/win.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    compiled fine but got this message when i tried to build..
    cout << "\aBIOTCH";

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Did you notice that there's no main() function?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    this isn't it?

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPSTR lpCmdLine, int nCmdShow)
    cout << "\aBIOTCH";

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    When you build Win32 Application you'll have a WinMain() as the entry point of the program, a Win32 Console Application will use main()

    Try rebuild the workspace with Win32 Application.
    Merc

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    If you go to the project settings you should be able to switch it in there. There should be a compiler option /subsystem:console just change that to /subsystem:windows

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    It's always frustrating getting a compiler set-up properly.

    Do IO streams and basic rules stay the same in a windows environment?
    Hmmm... The Visual C++ compiler "attempts" to be ANSI/ISO compliant. (Some people think that it's compliance sucks.) So, I would say the "basic rules" do stay the same. All the Windows functions are additional to the standard stuff.

    But, Windows does have it's own special ways of handling keyboard input, and output to the screen. (Notice that there is no cout in the example.)

    I recommend Charles Petzold's "Programming Windows" book.

  9. #9
    Registered User
    Join Date
    Feb 2002
    Posts
    98
    hmmm i get this when trying to build, what does it mean?is it because im using win 2000 pro?
    Code:
    --------------------Configuration: trash - Win32 Debug--------------------
    Linking...
    LINK : fatal error LNK1104: cannot open file "kernel32.lib"
    Error executing link.exe.
    
    trash.exe - 1 error(s), 0 warning(s)
    i got the directories set to that file, but wont open it?matter of fact, there are 3 differant kernal32.lib's on my computer, tried everyone of them and it wont open when trying to build.

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    When you install VC it sets up it's paths to things like kernel32. If you have changed them, then I suggest you put them back to what they were.

    To compile and link something simple like that posted above, you do not need to change anything, it should work out of the box.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Did you illegally copy VC++ 6.0?

  12. #12
    Registered User
    Join Date
    Feb 2002
    Posts
    98
    Change something on my computer? never! lol i changed the directories, but put them back to system , and yes there is a kernel32.lib that came with vc, but still wont compile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <windows.h>
    By TehClutchKiller in forum Windows Programming
    Replies: 6
    Last Post: 05-28-2008, 03:30 AM
  2. C - including the <windows.h> header file
    By Jolu42 in forum C Programming
    Replies: 4
    Last Post: 05-13-2007, 01:05 PM
  3. Errore opening the include <windows.h>
    By rasheed in forum Windows Programming
    Replies: 5
    Last Post: 05-21-2006, 11:23 PM
  4. <windows.h> compiler help..!
    By The Brain in forum Windows Programming
    Replies: 4
    Last Post: 05-11-2005, 08:47 PM
  5. <windows.h>?
    By bluehead in forum C++ Programming
    Replies: 6
    Last Post: 11-20-2001, 07:17 PM