Click to See Complete Forum and Search --> : [RESOLVED] Resize window seems to snap to grid


John_M_Gough
November 14th, 2007, 09:58 AM
hi I have created a method to auto resize controlls when the client window is resized, the only problem is that when i resize the controls it only resizes when the window resizes by a considerable amount, it seems that there is a snap to grid when resizing controls. im happy to post my code if you think its a math problem.
im using Dev c++
Any help would be apreciated greatley.
thankyou

Boris K K
November 14th, 2007, 10:51 AM
Yes, it would be better to post the code or at least tell us which message you are monitoring.

John_M_Gough
November 14th, 2007, 11:28 AM
ok hears my code

/*
Name: CTL_MakeControls.h
Author: John Gough
Date: 11/11/07 00:26
Description: To effectively arrange GUI controls
*/

#include <iostream>
//#include "JHeaders.h"

#include <windows.h>
#define CTL_PERCENT "PERCENT"
using namespace std;

struct CONTROL
{
public:
int iControlID;
HWND hControl;
HWND hGroup;
POINT pTopLeft;
POINT pBottomRight;
bool bPercentage;
};

enum Flags
{
// Controlls
CTL_EDIT =01,
CTL_COMBO =010,
CTL_BUTTON =0100,
CTL_CHECKBOX =01000,
CTL_GROUPBOX =010000,
CTL_LISTBOX =0100000,
CTL_TEXT =01000000,
CTL_TREEVIEW =010000000,
CTL_MENUITEM =0100000000,

CTL_MULTILINE =02,
CTL_SLIM =020,
CTL_ICON =0200,
};
struct CTL_MakeControls
{
public:
HWND hOwner;
int iControlAmount;
/****************************/
//Constructer
CTL_MakeControls(HWND hOwnerWindow,int iAmount)
{
hOwner=hOwnerWindow;
iControlAmount=iAmount;
iControlNumber=0;
ctlControl = new CONTROL[iControlAmount];
}
//Distructor
~CTL_MakeControls()
{
delete [] hOwner;
delete [] &iControlAmount;
}
HWND ControlCreate(HWND hGroup,int sControlType,int id,int iPosX,int iPosY,int iSizeX,int iSizeY,LPSTR sPercent="",int iIconID=0,LPSTR lpsText="")
{
if (iControlNumber >= iControlAmount)
{
//ErrorBox("Sorry un-able to create control\nMemory Not Allocated");
}
else{
ctlControl[iControlNumber].iControlID=iControlNumber;
ctlControl[iControlNumber].hGroup=hGroup;
ctlControl[iControlNumber].pTopLeft.x=iPosX;
ctlControl[iControlNumber].pTopLeft.y=iPosY;
ctlControl[iControlNumber].pBottomRight.x=iSizeX;
ctlControl[iControlNumber].pBottomRight.y=iSizeY;
X = iPosX;
Y = iPosY;
XX= iSizeX;
YY= iSizeX;
if (sPercent==CTL_PERCENT)
{
ctlControl[iControlNumber].bPercentage=true;
}
if(sControlType==CTL_EDIT)
{
lpsClassName="EDIT";
dwExStyle=0;
dwStyle=WS_VISIBLE|WS_CHILD|WS_BORDER;
}
if(sControlType==CTL_EDIT+CTL_MULTILINE)
{
lpsClassName="EDIT";
dwExStyle=0;
dwStyle=WS_VISIBLE|WS_CHILD|WS_BORDER|WS_HSCROLL|WS_VSCROLL|ES_MULTILINE;
}
if(sControlType==CTL_COMBO)
{
lpsClassName="COMBOBOX";
dwExStyle=0;
dwStyle=WS_VISIBLE|WS_CHILD|WS_BORDER|WS_HSCROLL|WS_VSCROLL|CBS_SIMPLE;
}
if(sControlType==CTL_COMBO+CTL_SLIM)
{
lpsClassName="COMBOBOX";
dwExStyle=0;
dwStyle=WS_VISIBLE|WS_CHILD|WS_BORDER|WS_HSCROLL|WS_VSCROLL|CBS_DROPDOWN;
}
if(sControlType==CTL_LISTBOX)
{
lpsClassName="LISTBOX";
dwExStyle=0;
dwStyle=WS_VISIBLE|WS_CHILD|WS_BORDER|WS_HSCROLL|WS_VSCROLL|ES_MULTILINE;
}
if(sControlType==CTL_GROUPBOX)
{
lpsClassName="STATIC";
dwExStyle=0;
dwStyle=WS_VISIBLE|WS_CHILD|SS_ETCHEDFRAME ;
}
if(sControlType==CTL_BUTTON)
{
lpsClassName="BUTTON";
dwExStyle=0;
dwStyle=WS_VISIBLE|WS_CHILD;
}
if(sControlType==CTL_BUTTON+CTL_ICON)
{
lpsClassName="BUTTON";
dwExStyle=0;
dwStyle=WS_VISIBLE|WS_CHILD|BS_BITMAP;
}
hMade=CreateWindowEx(dwExStyle,
lpsClassName,
lpsText,
dwStyle,
X,
Y,
XX,
YY,
hOwner,
(HMENU)id,
GetModuleHandle(NULL),
0);
ctlControl[iControlNumber].hControl=hMade;


if(sControlType==CTL_BUTTON+CTL_ICON)
{
HBITMAP hImage;
hImage=(HBITMAP)LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(iIconID));
SendMessage(ctlControl[iControlNumber].hControl,BM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hImage);
}
iControlNumber++;
/*>>>> NOTE: NEEED TO ADD MORE CONTROLS!!!!!!!*/
}
return hMade;
};

bool ReSizeAll()
{
RECT rRect;
int iPosX;
int iPosY;
int iSizeX;
int iSizeY;
int bBool;
for (n=0 ;n != iControlAmount; n++)
{

bBool=ctlControl[n].bPercentage;
if (bBool==true)
{
GetWindowRect(hOwner,&rRect);

//////////////////////////////////////
if (ctlControl[n].hGroup != hOwner)
{
//find the group handle
nn=0;
while (ctlControl[nn].hControl != ctlControl[n].hGroup)
{
nn++;

}
//GetWindowRect(ctlControl[n].hGroup,&rRectGroup);
// find the xy position of the parent
iPosX=((rRect.right-rRect.left)/100)*ctlControl[nn].pTopLeft.x;
iPosY=((rRect.bottom-rRect.top)/100)*ctlControl[nn].pTopLeft.y;
// find the size of the parent
iSizeX=((rRect.right-rRect.left)/100)*ctlControl[nn].pBottomRight.x;
iSizeY=((rRect.bottom-rRect.top)/100)*ctlControl[nn].pBottomRight.y;

// calculate the xy of current controll
X=iPosX+((iSizeX/100)*ctlControl[n].pTopLeft.x);
Y=iPosY+((iSizeY/100)*ctlControl[n].pTopLeft.y);

XX=(iSizeX/100)*ctlControl[n].pBottomRight.x;
YY=(iSizeY/100)*ctlControl[n].pBottomRight.y;

}
else
{
X= ((rRect.right-rRect.left)/100)*ctlControl[n].pTopLeft.x;
Y= ((rRect.bottom-rRect.top)/100)*ctlControl[n].pTopLeft.y;
XX=((rRect.right-rRect.left)/100)*ctlControl[n].pBottomRight.x;
YY=((rRect.bottom-rRect.top)/100)*ctlControl[n].pBottomRight.y;
}
SetWindowPos(ctlControl[n].hControl,HWND_TOP,X,Y,XX,YY,SWP_SHOWWINDOW);
}
cout << endl;
}
return true;
}
private:
int n;
int nn;
int iControlNumber;
int X;
int XX;
int Y;
int YY;

CONTROL* ctlControl;
DWORD dwStyle;
DWORD dwExStyle;
LPSTR lpsClassName;
HWND hMade;
};



and hears my usage


static HWND hGroupBox;
static CTL_MakeControls cControls(hwnd,5);
switch (message)
{
case WM_CREATE:
hGroupBox=cControls.ControlCreate(hwnd,CTL_GROUPBOX, 0, 5, 5, 90, 90,CTL_PERCENT);
cControls.ControlCreate(hGroupBox,CTL_EDIT|CTL_MULTILINE,0, 0, 0, 50, 50,CTL_PERCENT,0,"ITS AN EDIT CONTROLL");
cControls.ControlCreate(hGroupBox,CTL_BUTTON, 0, 50, 50, 50, 50,CTL_PERCENT);
cControls.ControlCreate(hGroupBox,CTL_COMBO|CTL_SLIM, 0, 0, 50, 50, 50,CTL_PERCENT);
cControls.ControlCreate(hGroupBox,CTL_COMBO, 0, 50, 0, 50, 50,CTL_PERCENT);

break;
case WM_SIZE://
cControls.ReSizeAll();
InvalidateRgn(hwnd, NULL, true);
break;
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

Boris K K
November 15th, 2007, 10:49 AM
1. Try using DeferWindowPos (http://blogs.msdn.com/oldnewthing/archive/2005/07/06/436043.aspx).

2. Try without InvalidateRgn.

VladimirF
November 16th, 2007, 09:14 AM
ok hears my code...Your prblem is - integr division. Cast your varables to double befoe dividing by 100.

John_M_Gough
November 18th, 2007, 07:43 PM
Your prblem is - integr division. Cast your varables to double befoe dividing by 100.

Yes. thank you. it works fine now.

Boris K K
November 20th, 2007, 12:24 AM
VladimirF is correct about the cause. Another solution without using floating-point arithmetics is to use, for example

iPosX=((rRect.right-rRect.left) * ctlControl[nn].pTopLeft.x + 50) / 100;

1. Make the division the last operation.
2. [Optional] add half of the divisor to the divident to achieve rounding towards the nearest integer (integer division rounds towards zero).