We help IT Professionals succeed at work.

Resizing a snappable dialog

672 Views
Last Modified: 2013-12-04
Hi all,
  I have a View which is a parent of a dialog. This view has a grid on it. Whenever someone moves the dialog, it snaps to the grid. I got that part working fine. But what I can't seem to figure out is how to resize the dialog, having it snap to the grid as it is being resized. I have tried overloading the OnSize message handler of my dialog, and putting this sample line of code in there:

this->MoveWindow(nLeft, nTop, cx, cy);  //cx and cy have been modified corresponding to fit grid.

However, this does not work at all. It does not resize; It only flickers.

Does anyone have any suggestions??

Comment
Watch Question

I doubt, something you have done in OnSize that is getting triggered while you are calling the MoveWindow (or SetWindowPos), thatz why the flicker happens. Please check some part of the code is again doing some wrong resize operation...
Good Luck
~Roshan

Author

Commented:
Roshan
  Even when I comment out all the code in OnMove, I still cannot resize the dialog. There is no other place in the code that messes with the size or position. Are you saying that "MoveWindow(nLeft, nTop, cx, cy);"  in OnSize should work??
Not sure may be some mistake like this is there in the code

void CMyDialog::OnSize(UINT nType, int cx, int cy)
{
      this->MoveWindow(nLeft, nTop, yourCX, yourCY);

      CDialog::OnSize(nType, cx, cy);            <--- Again, parent is resetting the value to the old one...

}

If that is the case, try this way

void CMyDialog::OnSize(UINT nType, int cx, int cy)
{
      //this->MoveWindow(nLeft, nTop, yourCX, yourCY); NO NEED OF MOVE WINDOW HERE

      CDialog::OnSize(nType, yourCX, yourCY);            <--- Again, parent is resetting the value to the old one...

}

Or you can try SetWindowPos, no need to bother abou the position....


void CMyDialog::OnSize(UINT nType, int cx, int cy)
{

    CDialog::OnSize(nType, cx, cy);


    this->SetWindowPos(NULL, 0, 0, newWidth, newHeight, SWP_NOMOVE | SWP_NOZORDER);

}


Good Luck
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Be very careful with code like this

In the OnSize you call Move which generates a WM_SIZE (calls OnSize) which calls Move which ....
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
If I understand correctly you want it to be sized to cells in the grid ON THE PARENT VIEW.

For that you should post a message to the parent view instructing it to snap the dialog to the grid and let that then finalise the size of the dialog.
>>>> void CMyDialog::OnSize(UINT nType, int cx, int cy)
The OnSize is the handler for the WM_SIZE message. It was called *after* the window was resized. It is really a *bad* place to make resizing as you can't prevent from flickering and have to watch not to do that infinitely as Andy has told you.

You'd better handle the WM_SIZING message which was called before resizing. And don't call the baseclass function if you handled a message. There are only a few messages where the baseclass handler needs to be called additionally, e. g. WM_INITDIALOG, but WM_SIZE definitively is none of these.

Author

Commented:
Nothing is working. Every time I call "this->SetWindowPos.....", in OnSize it calls OnSize once again.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
>>Every time I call "this->SetWindowPos.....", in OnSize it calls OnSize once again.

Read my very first comment.  Then my second telling you how to one way to get around it.

Author

Commented:
AndyAinscow, if I do that, then I will not have the snapping affect while resizing
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
One of us misunderstands the other.

If I understand you then you want the dialog to fit into one (or more cells) on the parent view exactly.  No partial covering of a cell.  If that is the case then do what I suggested, post a message to the parent view and let that resize the dialog appropriately.

If that isn't what you require then please explain what I am missing.


( I have a feeling that your dialog shouldn't even be resized by the user - the view should fake it. )

Author

Commented:
AndyAinscow, yes, that is true, exactly as you say. Whether resizing or moving, to always fit exactly in the cells of a parent view. I don't really know where in the parent view I would do that; Like what event to do it in.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Do you want the resizing to only happen in 'jumps' ?  
Or can the dialog be partially covering a cell and then fits to the grid after the user has finished the resizing ?

Author

Commented:
AndyAinscow,
  Well, ideally, I would like to happen in jumps.
Freelance programmer / Consultant
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.