Thread: Basic Question...

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question Basic Question...

    I just got petzolds book... (about a month ago)... I've been avoiding it and programming for console because of one thing I couldn't get answered... where does the code go? I use borland v4.52 and I can make a window... but... does the code go after the window creation or within it? if you have the book it comes with the cd including examples... the example is chapter 4 i think.

  2. #2
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    Has anyone read petzolds win32 api book? can anyone answer my question???

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Depends on what you are doing. Do you want to draw stuff? Put your code in the WM_PAINT message in the wndproc message handler. You have a main message loop right? So, you pretty much need to have your code in the message proc in response to messages sent/posted by your application. Maybe if you tell me something specific you are trying to do I can help more.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    So let's say I wanted to put simple text... I'd have to put it in the text section thingy?... Then how does it all come together?

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You could do something like this in your WM_PAINT message in your window proc.

    Code:
    case WM_PAINT:
    {
      HDC hDC = GetDC( hWnd );
      char foo[] = "Hello World";
    
      TextOut( hDC, 200, 200, foo, strlen( foo ) );
    
      ReleaseDC( hWnd, hDC );
    
      return 0;
    }
    Thats a pretty simple example but it will get something to the screen. Everytime the application receives a WM_PAINT message it will output that.
    Last edited by MrWizard; 10-10-2002 at 05:59 PM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    But what is the order of the functions? Do I have any control over it?

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    If I'm understanding you correctly , you just want to write a few functions for an application? That's no problem, just write a regular function and call it BEFORE the main message loop but after window creation etc.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  8. #8
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    I'm sooo confused... don't mind me.

  9. #9
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    Can someone post an example?

  10. #10
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    Can anyone tell me why win32 api is so cryptic? I mean the change from console to win32 api is like positioning myself to smack my head on the wall. <sarcasm>Not a very good analogie... I wood have thought of a better one if I just hadn't smacked my head too many times...</sarcasm>

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Here I through this together in two seconds. It's sloppy but you get the idea.

    Code:
    #include <windows.h> 
    
    #define APPNAME     "TEST"
    #define CLASSNAME   "MyClass"
    
    LRESULT CALLBACK MyWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {
      switch( msg )
      {
        case WM_DESTROY:
        case WM_CLOSE:
          PostQuitMessage( 0 ); // Exit our program
          return 0;
    
        case WM_KEYUP:
          if( wParam == 27 ) // 27 = ESC key
            PostQuitMessage( 0 );
          
          return 0;
    
        case WM_PAINT:
        {
          // Obtain our device context
          HDC hDC = GetDC( hWnd );
    
          // Output a text string on screen
          // Params: HDC, XPOS, YPOS, string, string length
          TextOut( hDC, 50, 50, "Hello World", 11 );
    
          // Give back our device context
          ReleaseDC( hWnd, hDC );
    
          return 0;
        }
      }
    
      // Let the default proc handle messages we don't
      return DefWindowProc( hWnd, msg, wParam, lParam );
    }
    
    BOOL RegisterWindow( HINSTANCE hInst )
    {
      WNDCLASSEX wndclex = { 0 };
    
      wndclex.cbSize = sizeof( WNDCLASSEX );
      wndclex.hInstance = hInst;
      wndclex.style = 0;
      wndclex.hIcon = NULL; // Default icon
      wndclex.hCursor = LoadCursor( NULL, IDC_ARROW );
      wndclex.cbClsExtra = 0;
      wndclex.cbWndExtra = 0;
      wndclex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
      wndclex.lpszMenuName = 0;
      wndclex.lpfnWndProc = MyWndProc;
      wndclex.lpszClassName = CLASSNAME;
    
      if( RegisterClassEx( &wndclex ) == 0 )
        return FALSE;
    
      return TRUE;
    }
    
    int __stdcall WinMain( HINSTANCE hInst,
                           HINSTANCE hPrev,
                           LPSTR lpCmdLine,
                           int nShowCmd )
    {
      HWND hWnd;
      MSG  msg;
    
      if( RegisterWindow( hInst ) == FALSE )
      {
        MessageBox( 0, "Register Window Failed", "Failure", MB_OK );
        return 0;
      }
    
      // Here we create our main window
      hWnd = CreateWindowEx( WS_EX_TOPMOST,
                             CLASSNAME,
                             APPNAME,
                             WS_OVERLAPPEDWINDOW,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             NULL,
                             NULL,
                             NULL,
                             NULL );
    
      if( hWnd == NULL )
      {
        MessageBox( 0, "CreateWindowEx Failed", "Failure", MB_OK );
        return 0;
      }
    
      // Update the window then display it
      UpdateWindow( hWnd );
      ShowWindow( hWnd, SW_SHOW );
    
      // Main message pump
      while( GetMessage( &msg, 0, 0, 0 ) )
      {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
        
        Sleep(1);
      }
    
      // Quit the program
      return msg.wParam;
    }

  12. #12
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    my question is now... how do you select order if it is always the same when creating windows?

  13. #13
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    You don't select order. You do things based on messaging (didn't you read the book's introduction? )

    See, windows is a message-based system. That means you can't really do anything in order, you just roll with the punches and work it that way.

    You can still rewrite programs that you've written in DOS. Let's say you have some program that does a fancy text conversion to spanish from english, and it's all in console. You'd simply create an edit box and a button. The person inputs the text to be translated into the edit box, and clicks the button. That button then sends a message to your program telling you that it's been pressed. Now here's where you come in and tell the program what to DO after that button's been pressed - in this case you'd get the text from the edit boxes, and use your conversion code from your DOS program to convert it (and output it somehow - possibly clear & send it to the eidt box).

    In WinAPI you don't have to do much more than Think Different (Copyright - apple computers) about how you're going to input and output everything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic math programming question
    By hebali in forum C Programming
    Replies: 38
    Last Post: 02-25-2008, 04:18 PM
  2. Basic question about GSL ODE func RK4
    By cosmich in forum Game Programming
    Replies: 1
    Last Post: 05-07-2007, 02:27 AM
  3. Basic question about RK4
    By cosmich in forum C++ Programming
    Replies: 0
    Last Post: 05-07-2007, 02:24 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM