Thread: How to resize with WS_POPUP style?

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    How to resize with WS_POPUP style?

    I have a window with the WS_POPUP style, and I wanted to implement my own resizing system. However, the only method I can think of would be to use WM_MOUSEMOVE capturing, but those messages aren't sent unless the mouse cursor is over the window.

    Does anyone know how to change window styles during runtime, or how to make a custom resizing system?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>how to change window styles during runtime<<

    Use SetWindowLong with the GWL_STYLE flag. You may need to follow that up with SetWindowPos to ensure the changes are properly drawn.

    >>make a custom resizing system?<<

    Handle WM_NCHITTEST, determine where the cursor is on your window 'border' ( or wherever you want the 'sizing' event to be fired) and return a suitable flag (see msdn for available flags) from your handler. That way, the system does the drawing which is a lot smoother than MoveWindow or SetWindowPos in response to mouse move events. Note that you can return HTCAPTION from the same handler if you are wanting to 'drag' the window around by its client area or by some region that you have designated as its 'caption' area.

    edit: typos
    Last edited by Ken Fitlike; 09-28-2003 at 04:59 AM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Thanks for the tip Ken, but I already dug up that info from a post of yours a while ago.

    Code:
    case ID_SIZE:
    	{
     		SetWindowLong(hwnd, GWL_STYLE,WS_OVERLAPPED|WS_CAPTION);
     		SetWindowPos(hwnd,NULL,0,0,0,0, SWP_NOZORDER|SWP_NOSIZE|SWP_NOMOVE);
    		break;
    	}
    I call that code in response to an accelerator. What happens is, I lose control of the window, and it stops repainting itself.

    Could you elaborate on the custom resizer a little? I sort of see where you're coming from, but I dont understand completely.
    Last edited by bennyandthejets; 09-28-2003 at 07:01 AM.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Originally posted by bennyandthejets
    Could you elaborate on the custom resizer a little? I sort of see where you're coming from, but I dont understand completely.
    When you handle the WM_NCHITTEST message and return, for example HTCAPTION, if you left-click on the client area of the window, the system assumes you've done so in the caption area of that window and acts accordingly - ie allows you to drag that window. The same goes for the other flags; if you return 'HTLEFT' then the system assumes the mouse is over the left border of your window, changes the cursor to the proper re-sizing cursor and will permit resizing of that window if you left-click and drag it about. So, all you need do to get this default behaviour is decide where you want it fired. If you have, for example, an 8-pixel wide border then it's simply a case of determining where the cursor is when the message is fired - if it's on the left hand side within that 8 pixel margin then you would return 'HTLEFT', if it's on the right within your 8-pixel border then you'd return 'HTRIGHT' etc. The only other thing you may have to consider is handling the WM_GETMINMAXINFO message to prevent the user from reducing your window's size so that they don't inadvertently shrink it to, eg, 1x1 pixel.

    Example WM_NCHITTEST handler where BORDERWIDTH is some value in pixels you've designated as the width of your window border.
    Code:
    case WM_NCHITTEST:
      {
      POINT pt;
      RECT rc;
      GetClientRect(hwnd,&rc);
    
      pt.x=LOWORD(lParam);
      pt.y=HIWORD(lParam);
      ScreenToClient(hwnd,&pt);
      /*top-left, top and top-right*/
      if (pt.y<BORDERWIDTH)
        {
        if (pt.x<BORDERWIDTH)
          {
          return HTTOPLEFT;
          }
        else if (pt.x>(rc.right-BORDERWIDTH))
          {
          return HTTOPRIGHT;
          }
        return HTTOP;
        }
      /*bottom-left, bottom and bottom-right */
      if (pt.y>(rc.bottom-BORDERWIDTH))
        {
        if (pt.x<BORDERWIDTH)
          {
          return HTBOTTOMLEFT;
          }
        else if (pt.x>(rc.right-BORDERWIDTH))
          {
          return HTBOTTOMRIGHT;
          }
        return HTBOTTOM;
        }
      if (pt.x<BORDERWIDTH)
        {
        return HTLEFT;
        }
      if (pt.x>(rc.right-BORDERWIDTH))
        {
        return HTRIGHT;
        }
      return HTCAPTION;
      }
    Hope that helps.

    edit: editing
    Last edited by Ken Fitlike; 09-29-2003 at 03:19 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I tried your code Ken, but it didn't work. I think it may be because an edit control covers the entire client area. Should I try to use the code somewhere else?

    And did you work out how to change window styles during runtime yet?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Originally posted by bennyandthejets
    I tried your code Ken, but it didn't work. I think it may be because an edit control covers the entire client area. Should I try to use the code somewhere else?
    Just resize your edit control so that it doesn't obscure the border (WM_SIZE, MoveWindow). You'll lose the ability to drag the window about by the parent's client, though.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>And did you work out how to change window styles during runtime yet?

    Use GetWindowLong() to find current styles so we don't loose them.
    Code:
    then test if the style is present 
     if not 
             add to the required style 
    Set the new style with SetWindowLong()
    Ask window to be redrawn
    "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

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Thanks, it works now Ken.

    novacain, I took your advice, and it works better now, but not completely. Here's my code:

    Code:
    	LONG style;
    	RECT rc;
    
    	GetWindowRect(hwnd,&rc);
    	style=GetWindowLong(hwnd,GWL_STYLE);
    				style|=WS_CAPTION|WS_OVERLAPPED|WS_SYSMENU;
    	style|=(~WS_POPUP);
    
    	SetWindowLong(hwnd,GWL_STYLE,style);
    	SetWindowPos(hwnd,NULL,rc.left, rc.top,rc.right-rc.left, rc.bottom-rc.top,SWP_NOZORDER);
    The initial style was set (with CreateWindowEx) as WS_EX_TOOLWINDOW and WS_POPUP. When I run the code, the whole window becomes just a caption bar. Here is a pic:
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I just found something odd. In my code, when I use GetWindowLong() to get the window style, the value is -1811939328. Is that normal? I would have thought the value would be something a bit simpler.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    If you don't modify the style (ie just comment out the code that changes the style) does the window behave normally?
    "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

  11. #11
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Yeah it's fine. Hey, I made some code that works alright:

    Code:
    {
    	LONG style;
    	LONG exStyle;
    	RECT rc;
    
    	GetWindowRect(hwnd,&rc);
    	style=GetWindowLong(hwnd,GWL_STYLE);
    	exStyle=GetWindowLong(hwnd,GWL_EXSTYLE);
    
    	style|=WS_CAPTION;
    			
    								
    	SetWindowLong(hwnd,GWL_STYLE,style);
    	SetWindowPos(hwnd,NULL,rc.left-1,rc.top,rc.right-rc.left, rc.bottom-rc.top,SWP_NOZORDER);
    }
    It just puts a caption bar and border around the window. The trick I think is to actually move the window with SetWindowPos, or change it somehow.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >style|=(~WS_POPUP);

    If you are trying to turn off the WS_POPUP style I think you want &=. This is currently turning on every style except WS_POPUP.

  13. #13
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Ok, everything pretty much works for me. However, I sent it to a friend, and it didn't work. Can someone try it for me and see?

    Open the program, and press alt-c (make sure caps lock is off). It should change styles.

    EDIT: After you try it, can you send me the specs.dat file that it produces?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  14. #14
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Originally posted by bennyandthejets
    I just found something odd. In my code, when I use GetWindowLong() to get the window style, the value is -1811939328. Is that normal? I would have thought the value would be something a bit simpler.
    I figured it out. Here's that number in hex:FFFFFFFF94000000. The flags are contained in the LOWORD/HIWORD (i don't know which one, but I'm happy with this bit now.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  15. #15
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Okay, I've found out that there's some issue with the Windows Classic style in Windows XP. The program works well on Windows XP style (as far as I know), but it doesn't want to change styles in Windows classic style. The GWL_STYLE value and GWL_EXSTYLE values change the same way in both styles, but they only effect a visual change in Windows XP style.

    Does anyone know why this is?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. png Image Resize
    By bhupesh.kec in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 07:52 AM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 11:21 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM