no output

This is a discussion on no output within the Windows Programming forums, part of the Platform Specific Boards category; The following code is not printing out the values of tm.tmAveCharWidth and tm.tmHeight + tm.tmExternalLeading in the WM_CREATE message. Why?? ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    384

    no output

    The following code is not printing out the values of tm.tmAveCharWidth and tm.tmHeight + tm.tmExternalLeading in the WM_CREATE message. Why??

    Code:
    /*------------------------------------------------------------
       HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
                     (c) Charles Petzold, 1998
      ------------------------------------------------------------*/
    
    #include "StdAfx.h"
    #include <windows.h>
    #include<wingdi.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("HelloWin") ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASS     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 ;
    
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
         
         hwnd = CreateWindow (szAppName,                  // window class name
                              TEXT ("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 message, WPARAM wParam, LPARAM lParam)
    {
         HDC         hdc ;
         PAINTSTRUCT ps ;
         RECT        rect ;
    	 static int cxChar, cyChar; int l;
    	 TCHAR s[40];
    
    
    
    	 TEXTMETRIC tm;
    
    	 switch (message)
         {
         case WM_CREATE:
         
    	 hdc = GetDC (hwnd) ;
    	 
    	 GetTextMetrics (hdc, &tm) ;
         cxChar = tm.tmAveCharWidth ;
         cyChar = tm.tmHeight + tm.tmExternalLeading ;
    	 GetClientRect (hwnd, &rect) ;
         TextOut(hdc,rect.left,rect.top,s,wsprintf(s,TEXT("%d %d"),cxChar,cyChar));
              
    
         ReleaseDC (hwnd, hdc) ;
    	 return 0 ;
    
    
              
         case WM_PAINT:
    		 InvalidateRect (hwnd, NULL, FALSE) ;
             hdc=BeginPaint(hwnd,&ps) ;
              
             GetClientRect (hwnd, &rect) ;
    		  
             DrawText (hdc, TEXT ("Hello, Windows!"), -1, &rect,
                      DT_SINGLELINE |DT_CENTER | DT_VCENTER ) ;
             EndPaint (hwnd, &ps) ;
    		 
    
             return 0 ;
              
         case WM_DESTROY:
    		  PostQuitMessage(0);
              return 0 ;
    	 }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Isn't the FALSE flag in call to InvalidateRect supposed to prevent BeginPaint from clearing the background??

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > TextOut(hdc,rect.left,rect.top,s,wsprintf(s,TEXT(" %d %d"),cxChar,cyChar));
    Why isn't this in WM_PAINT?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by Salem View Post
    > TextOut(hdc,rect.left,rect.top,s,wsprintf(s,TEXT(" %d %d"),cxChar,cyChar));
    Why isn't this in WM_PAINT?
    Why won't it work in WM_CREATE?

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Is it necessary that whenever we want to print some data on the screen we must do so in WM_PAINT???

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Perhaps read up the whole Windows message concept.
    WM_PAINT message
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    384
    Quote Originally Posted by Salem View Post
    Perhaps read up the whole Windows message concept.
    WM_PAINT message
    Done that already.
    Can you tell me how to prevent BeginPaint from erasing the background..

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by juice View Post
    Done that already.
    Can you tell me how to prevent BeginPaint from erasing the background..
    Don't include a background brush in your Window Class.

    You might also want to take a good read through this...

    The Forger's Tutorials

    Thing is, especially with windows, you can't read a few pages and get a head full of ideas and go off on your own. You will only confuse yourself beyond all redemption. Stay with the book/tutorial end to end... it is very likely that your questions will be answered as you read more... but it's also a protracted course of understanding in which concepts are introduced an an order that makes the most sense.

    One simple fact of programming... knowing how to write a loop does not make you a programmer.

    Finally... almost all IO activity in windows happens in controls. It is neither common nor desirable to write directly to a window. Want to display some text... use a static control. Want to list a bunch of items... use a list box. Want to get a number from the user, use an edit control.

    IOW... leave console mode thinking behind, it will only hold you back.

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    As it has not been mentioned....

    InvalidateRect() generates a WM_PAINT msg, so having a call to InvalidateRect() in WM_PAINT handler creates an form of infinte loop.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ overlapping output and adding extensions to output files
    By lordmorgul in forum Linux Programming
    Replies: 9
    Last Post: 05-11-2010, 08:26 AM
  2. How to edit output in struct and call for the output
    By andrewkho in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:28 PM
  3. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 09:46 PM
  4. output a string to a standard output
    By sh4k3 in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 05:59 AM
  5. Replies: 3
    Last Post: 02-19-2003, 07:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21