Thread: Simple AdjustWindowRect() problem

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    57

    Simple AdjustWindowRect() problem

    I'm trying to make a window that is exactly 10 lines by 40 characters. (The below program is simplified)

    The program works fine if I use WS_POPUP|WS_CAPTION|WS_SYSMENU

    But if I use WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX, I get 8 1/2 lines by 39 1/2 characters.

    I may be using AdjustWindowRect incorrectly. Am I?

    Thank you.


    Code:
    //gcc prog.c -mwindows -o prog.exe
    
    #include <windows.h>
    
    DWORD dwStyle = WS_POPUP|WS_CAPTION|WS_SYSMENU;
    //DWORD dwStyle = WS_OVERLAPPED|WS_SYSMENU|WS_MINIMIZEBOX;
    
    int    nLines = 10;
    int    nCols  = 40;
    HINSTANCE  hInst;
    HWND   hwnd;
    HFONT  hFont;
    int    cxChar,cyChar;
    
    int initMainWindow(void);
    int initWindowPos(void);
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    
    /*************************************************************************/
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrev,LPSTR args,int nShow)
    {
       MSG  msg ;
    
       hInst = hInstance;
    
       initMainWindow();
       initWindowPos();
    
       ShowWindow(hwnd, nShow);
       UpdateWindow(hwnd);
    
       while( GetMessage(&msg,0,0,0) > 0 ) {
         TranslateMessage(&msg);
         DispatchMessage(&msg);
       }
       return (int)msg.wParam;
    }
    
    /*************************************************************************/
    int initMainWindow(void) {
       WNDCLASS wc = {0};
    
       wc.lpszClassName = "myClass";
       wc.hInstance     = hInst ;
       wc.hbrBackground = (HBRUSH)(COLOR_WINDOW);
       wc.lpfnWndProc   = WndProc ;
       wc.hCursor       = LoadCursor(0,IDC_ARROW);
    
       RegisterClass(&wc);
    
       hwnd = CreateWindow("myClass","test",
                   dwStyle,
                   0,0,0,0,
                   0,0,hInst,0);
    
       return 0;
    }
    
    /*************************************************************************/
    int initWindowPos(void) {
       HDC         hdc;
       TEXTMETRIC  tm;
       int         cx,cy;
       RECT        rc;
    
       hFont = CreateFont(
                16,0,0,0,FW_DONTCARE, 
                0,0,0,ANSI_CHARSET,
                OUT_DEFAULT_PRECIS,
                CLIP_DEFAULT_PRECIS,
                DEFAULT_QUALITY,
                DEFAULT_PITCH | FF_DONTCARE,
                "Courier New");
       // Extract font dimensions from the text metrics. 
       hdc = GetDC(hwnd); 
       SelectObject(hdc,hFont);
       GetTextMetrics(hdc,&tm); 
       ReleaseDC(hwnd,hdc);
    
       cxChar = tm.tmAveCharWidth;
       cyChar = tm.tmHeight + tm.tmExternalLeading; 
       
       cx = nCols  * cxChar;
       cy = nLines * cyChar;
       
       rc.left   = 0;
       rc.top    = 0;
       rc.right  = cx;
       rc.bottom = cy;
       AdjustWindowRect( &rc, dwStyle, FALSE );    
      
       SetWindowPos(hwnd,HWND_TOPMOST,0,0,rc.right-rc.left,rc.bottom-rc.top,SWP_NOZORDER);
            
       return 0;
    }
    
    /*************************************************************************/
    LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
    {
       PAINTSTRUCT ps;
       HDC hdc;
       int ii;
       char abc[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
       switch(msg) {
          case WM_PAINT:
               hdc = BeginPaint(hwnd,&ps);
               SelectObject(hdc,hFont);
                for (ii=0;ii<nLines;ii++) {
                   TextOut(hdc,0,ii*cyChar,abc,strlen(abc));
                }
               EndPaint(hwnd,&ps);
               return 0;
          case WM_DESTROY:
             PostQuitMessage(0); 
             return 0;
          default:
             return DefWindowProc(hwnd,msg,wParam,lParam);
       }
    }

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by MSDN AdjustWindowRect
    dwStyle [in] Specifies the window style of the window whose required size is to be calculated. Note that you cannot specify the WS_OVERLAPPED style.
    Apparently, AdjustWindowRect doesn't take into account the title bar added to the window with WS_OVERLAPPED. I'd trying adding WS_CAPTION to your list of styles.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    anonytmouse, thank you.
    I replaced WS_OVERLAPPED with WS_CAPTION and the program worked fine.
    Thanks very much for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  3. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 10:49 AM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM