Thread: Is this how you guys change the font face and height on a Windows program?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Is this how you guys change the font face and height on a Windows program?

    I've been reading Mr. Petzold's book for about two days now and I'm on chapter three where he shows us a program that will display a window with Hello, Windows 98! on its center. As this is all new to me, I thought it was pretty cool but I didn't like the font face he used at all and decided that I'd try to change it. After doing some reading on his book and also on the internet, I came up with this code and I'd like to know if this is the correct way of doing what I wanted. Thanks for your time.

    Code:
    #include <windows.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("Hello Word") ;
         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,               
                              TEXT ("Hello world program"),
                              WS_OVERLAPPEDWINDOW,       
                              CW_USEDEFAULT,             
                              CW_USEDEFAULT,             
                              CW_USEDEFAULT,             
                              CW_USEDEFAULT,             
                              NULL,                       
                              NULL,                       
                              hInstance,                 
                              NULL) ;                     
         
         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)
    {
         LOGFONT    lf ;
         static TCHAR szText[] = TEXT("Hello world!");
         HDC         hdc ;
         PAINTSTRUCT ps ;
         RECT        rect ;
         
         switch (message)
         {   
         case WM_PAINT:
              hdc = BeginPaint (hwnd, &ps) ;
             
              GetClientRect (hwnd, &rect) ;
             
              lf.lfHeight = 10;
              strcpy(lf.lfFaceName, "Courier");
             
              SelectObject(hdc, CreateFontIndirect (&lf));
              TextOut(hdc, rect.left, rect.top, szText, lstrlen(szText));
             
              DeleteObject(SelectObject(hdc, GetStockObject(SYSTEM_FONT)));
             
              EndPaint (hwnd, &ps) ;
              return 0 ;
             
         case WM_DESTROY:
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    I've noticed one problem: if I move the window around the screen, the letters get all scrambled up. Why is this happening?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Nevermind. Problem solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  3. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM