John_M_Gough
November 12th, 2007, 12:54 PM
I am creating a struct that makes it easy to create controls, it has an option to resizes the controls compared to a given percentage and the window size. when the window resizes and the controls resize two.
hears an example of using my struct.
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hGroupBox;
static CTL_MakeControls cControls(hwnd,5);
switch (message) /* handle the messages */
{
case WM_CREATE:
hGroupBox=cControls.ControlCreate(hwnd,CTL_GROUPBOX,0,5,5,90,90,CTL_PERCENT);
cControls.ControlCreate(hGroupBox,CTL_EDIT|CTL_MULTILINE,0,10,10,40,40,CTL_PERCENT);
cControls.ControlCreate(hGroupBox,CTL_BUTTON|CTL_ICON,0,60,60,40,40,CTL_PERCENT,ID_ICONONE);
cControls.ControlCreate(hGroupBox,CTL_COMBO|CTL_SLIM,0,10,60,40,40,CTL_PERCENT);
cControls.ControlCreate(hGroupBox,CTL_GROUPBOX,0,60,10,40,40,CTL_PERCENT);
break;
case WM_WINDOWPOSCHANGING://
cControls.ReSizeAll();
InvalidateRgn(hwnd, NULL, true);
break;
case WM_EXITSIZEMOVE://
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);
}
return 0;
}
The problem i am having is if i were to create a controll on a group box and wish to resize the controls in comarison to the group box. how can i do this.
Ide assume that:
GetWindowRect(pControl[n].hGroup,&rRect)
but this seems to return the rect of the window and not the controll
hear is my struct; PS im still learning so this probebly isnt the best way, but im doing it as a learning exercise,
#include <windows.h>
#define CTL_PERCENT "PERCENT"
using namespace std;
class 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_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)
{
if (!hGroup)hGroup=hOwner;
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=iSizeY;
if (sPercent==CTL_PERCENT)
{
ctlControl[iControlNumber].bPercentage=true;
GetWindowRect(ctlControl[iControlNumber].hGroup,&rRect);
X=(rRect.right-rRect.left)/100*iPosX;
Y=(rRect.bottom-rRect.top)/100*iPosX;
XX=(rRect.right-rRect.left)/100*iSizeX;
YY=(rRect.bottom-rRect.top)/100*iSizeY;
}
cout <<"\t"<<sControlType<<endl;
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;
}
ctlControl[iControlNumber].hControl= CreateWindowEx(dwExStyle,
lpsClassName,
"",
dwStyle,
X,
YY,
XX,
YY,
hOwner,
(HMENU)id,
GetModuleHandle(NULL),
0);
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 ctlControl[iControlNumber].hControl;
};
bool ReSizeAll()
{
int iPosX;
int iPosY;
int iSizeX;
int iSizeY;
int bBool;
for (n=0 ;n != iControlAmount; n++)
{
GetWindowRect(ctlControl[n].hGroup,&rRect);
bBool=ctlControl[n].bPercentage;
if (bBool==true)
{
iPosX=ctlControl[n].pTopLeft.x;
iPosY=ctlControl[n].pTopLeft.y;
iSizeX=ctlControl[n].pBottomRight.x;
iSizeY=ctlControl[n].pBottomRight.y;
X=((rRect.right-rRect.left)/100)*iPosX;
Y=((rRect.bottom-rRect.top)/100)*iPosY;
XX=((rRect.right-rRect.left)/100)*iSizeX;
YY=((rRect.bottom-rRect.top)/100)*iSizeY;
SetWindowPos(ctlControl[n].hControl,HWND_TOP,X,Y,XX,YY,SWP_SHOWWINDOW);
cout << Y<<endl;
}
}
return true;
}
private:
int n;
int iControlNumber;
int X;
int XX;
int Y;
int YY;
RECT rRect;
CONTROL* ctlControl;
DWORD dwStyle;
DWORD dwExStyle;
LPSTR lpsClassName;
};
hears an example of using my struct.
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hGroupBox;
static CTL_MakeControls cControls(hwnd,5);
switch (message) /* handle the messages */
{
case WM_CREATE:
hGroupBox=cControls.ControlCreate(hwnd,CTL_GROUPBOX,0,5,5,90,90,CTL_PERCENT);
cControls.ControlCreate(hGroupBox,CTL_EDIT|CTL_MULTILINE,0,10,10,40,40,CTL_PERCENT);
cControls.ControlCreate(hGroupBox,CTL_BUTTON|CTL_ICON,0,60,60,40,40,CTL_PERCENT,ID_ICONONE);
cControls.ControlCreate(hGroupBox,CTL_COMBO|CTL_SLIM,0,10,60,40,40,CTL_PERCENT);
cControls.ControlCreate(hGroupBox,CTL_GROUPBOX,0,60,10,40,40,CTL_PERCENT);
break;
case WM_WINDOWPOSCHANGING://
cControls.ReSizeAll();
InvalidateRgn(hwnd, NULL, true);
break;
case WM_EXITSIZEMOVE://
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);
}
return 0;
}
The problem i am having is if i were to create a controll on a group box and wish to resize the controls in comarison to the group box. how can i do this.
Ide assume that:
GetWindowRect(pControl[n].hGroup,&rRect)
but this seems to return the rect of the window and not the controll
hear is my struct; PS im still learning so this probebly isnt the best way, but im doing it as a learning exercise,
#include <windows.h>
#define CTL_PERCENT "PERCENT"
using namespace std;
class 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_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)
{
if (!hGroup)hGroup=hOwner;
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=iSizeY;
if (sPercent==CTL_PERCENT)
{
ctlControl[iControlNumber].bPercentage=true;
GetWindowRect(ctlControl[iControlNumber].hGroup,&rRect);
X=(rRect.right-rRect.left)/100*iPosX;
Y=(rRect.bottom-rRect.top)/100*iPosX;
XX=(rRect.right-rRect.left)/100*iSizeX;
YY=(rRect.bottom-rRect.top)/100*iSizeY;
}
cout <<"\t"<<sControlType<<endl;
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;
}
ctlControl[iControlNumber].hControl= CreateWindowEx(dwExStyle,
lpsClassName,
"",
dwStyle,
X,
YY,
XX,
YY,
hOwner,
(HMENU)id,
GetModuleHandle(NULL),
0);
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 ctlControl[iControlNumber].hControl;
};
bool ReSizeAll()
{
int iPosX;
int iPosY;
int iSizeX;
int iSizeY;
int bBool;
for (n=0 ;n != iControlAmount; n++)
{
GetWindowRect(ctlControl[n].hGroup,&rRect);
bBool=ctlControl[n].bPercentage;
if (bBool==true)
{
iPosX=ctlControl[n].pTopLeft.x;
iPosY=ctlControl[n].pTopLeft.y;
iSizeX=ctlControl[n].pBottomRight.x;
iSizeY=ctlControl[n].pBottomRight.y;
X=((rRect.right-rRect.left)/100)*iPosX;
Y=((rRect.bottom-rRect.top)/100)*iPosY;
XX=((rRect.right-rRect.left)/100)*iSizeX;
YY=((rRect.bottom-rRect.top)/100)*iSizeY;
SetWindowPos(ctlControl[n].hControl,HWND_TOP,X,Y,XX,YY,SWP_SHOWWINDOW);
cout << Y<<endl;
}
}
return true;
}
private:
int n;
int iControlNumber;
int X;
int XX;
int Y;
int YY;
RECT rRect;
CONTROL* ctlControl;
DWORD dwStyle;
DWORD dwExStyle;
LPSTR lpsClassName;
};