CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2007

    Unhappy Win32 C++ Fix Window Size and Position

    Hi, I tried to Google this question and I searched the previous posts but couldn't find a solution. Currently, I have a resizable window with a filled-in vertically resizable box draw in it corresponding to the +/- keys. I would like to know how to fix the window. I don't want to give the user the option to resize, move, or minimize this window. All, I need to be able to is redraw the window when necessary and to close the window. How do I fix the window size and position?
    #include <windows.h>
    /*  Declare Windows procedure  */
    static int size = 400;
    static int top = 80;
    static int left = 100;
    static int right = 700;
    static int bottom = 480;
    HDC hdc;
    HRGN hrgn;
    RECT rc1, rc2;
    static bool flag = false;
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_BYTEALIGNWINDOW;           
        wincl.cbSize = sizeof (WNDCLASSEX);
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               800,                 /* The programs width */
               600,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
            /* Translate virtual-key messages into character messages */
            /* Send message to WindowProcedure */
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    /*  This function is called by the Windows function DispatchMessage()  */
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
        switch (message)                  /* handle the messages */
            case WM_CREATE:
                hdc = GetDC(hwnd);
           //     xPixel = GetDeviceCaps(hdc,ASPECTX);
            //    yPixel = GetDeviceCaps(hdc,ASPECTY);
            case WM_PAINT:
                hdc = BeginPaint(hwnd, &ps);
     			rc1.left = 0;
     			rc1.top = 0;
    	  	    rc1.right = 800;
       	        rc1.bottom = 600;
     			rc2.left = left;
     			rc2.top = top;
    	  	    rc2.right = right;
       	        rc2.bottom = bottom;
       	        FillRect(hdc, &rc1, (HBRUSH) GetStockObject(COLOR_BACKGROUND));
      		   	FillRect(hdc, &rc2, (HBRUSH) GetStockObject(BLACK_BRUSH));
    //            Rectangle(hdc, 100, 200, 700,size);
                EndPaint(hwnd, &ps);
    //            RedrawWindow(hwnd,&rc2, hrgn, RDW_UPDATENOW);
     InvalidateRect(hwnd, &rc2, TRUE);
     flag = false;
                return 0;
            case WM_KEYDOWN:
                     case VK_ADD:
                        //  size += 50;
                          top -=15;
                          bottom += 15;
                     case VK_SUBTRACT:
                       //   size -= 50;
                          top += 15;
                          bottom -= 15;
                 InvalidateRect(hwnd, &rc2, FALSE);
                 flag = true;
            //     SendMessage(hwnd, WM_PAINT, 0, 0);
                 return 0;
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        return 0;

  2. #2
    Join Date
    Nov 2007

    Re: Win32 C++ Fix Window Size and Position

    Use the WM_GETMINMAXINFO to set mandatory minimum and maximum sizes of the window. The user will be unable to resize the window out of these limits.

    Alternatively you can use the following messages: WM_SIZING, WM_MOVING. Using these messages you can restore the window back to the initial size and position size every time the user tryies to change them.

    You can create from start a non sizable window if you replace the OVERLAPPEDWINDOW parameter in the CreateWindowEx with a different window style. Look for a window style matching your needs.
    Last edited by srelu; November 26th, 2007 at 04:17 PM.

  3. #3
    Join Date
    Nov 2007

    Unhappy Re: Win32 C++ Fix Window Size and Position

    Yes, I want to fix the window size at the start of my program. I should have specified in my original message that I would like help choosing the window style. Unfortunately, Google and MSDN weren't much help here. Perhaps I missed it. Which window style does not allow resizing or repositioning the window?

  4. #4
    Join Date
    Nov 2007

    Exclamation Re: Win32 C++ Fix Window Size and Position

    I tried everything I could think of. The only result I was able to come up with to prevent window resizing is to use the MoveWindow function to set the size and position of the window upon receiving the WM_SIZE message. But this really isn't what I want. I want to fix the window size and position at the start of the program. I know that there is a method or window style that accomplishes this but I am unable to figure out which one, and I'm stuck. Please help!!!

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Hanover Germany

    Re: Win32 C++ Fix Window Size and Position

    Have a look at WM_WINDOWPOSCHANGING message and check these threads out:
    Victor Nijegorodov

  6. #6
    Join Date
    Nov 2007

    Exclamation Re: Win32 C++ Fix Window Size and Position

    Thank you Victor.
    That pointed me in the right direction.
    I wasn't primarilly concerned with window position, more so size.
    However, while looking up WM_WINDOWPOSCHANGING, I found a reference to WM_SIZING.
    That is pretty much what I was looking for.
    However, in a previous post, someone mentioned that there is a certain window style for disabling resize.
    Are you familiar with the particular window style?

  7. #7
    Join Date
    May 1999

    Re: Win32 C++ Fix Window Size and Position

    It is frame style. Do not use WS_THICKFRAME, use any other. WS_DLGFRAME seems to be the best. WS_OVERLAPPEDWINDOW is comprised of many styles WS_THICKFRAME included.


    Do not use WS_MINIMIZEBOX and WS_MAXIMIZEBOX if you want disable minimizing and maximizing.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  8. #8
    Join Date
    Feb 2003
    Iasi - Romania

    Re: Win32 C++ Fix Window Size and Position

    ... and, if you additionally want like in OP a window which cannot be moved by user, remove the WS_CAPTION style as well. Although there are also other methods to make that, any user seeing a window with a caption will be tempted to move it... and usually will be frustrated if cannot do that.

    Note: when creating an overlapped window (with neither WS_CHILD nor WS_POPUP style set) the WS_CAPTION style is automatically added. So, after creating that window you have to remove the caption (modifying the style with SetWindowLong) or, simpler and more appropriate for what you want to achieve, create directly a popup window (use WS_POPUP instead of WS_OVERLAPPED when calling CreateWindow or CreateWindowEx).

    [ redirected thread ]
    Last edited by ovidiucucu; November 27th, 2007 at 06:01 PM.
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.