Algorithm Alley
by John Keogh


Listing One
/* holds an identifier and the initial RECT of the control */
typedef struct
    {
    HWND hControl;
    RECT rect;
    } resizeinfo;

Listing Two
/* iterate through the resize array and get the correct information*/
resizeinfo GetResizeInfo(HWND hwnd)
    {
    resizeinfo ri;
    int i;
    for(i=0; i<iChildren; i++)
        {
        ri=pri[i];
        if(ri.hControl==hwnd)
            return ri;
        }
    /*should never get here*/
    return ri;
    }


Listing Three
/*this is where the children are resized*/
BOOL CALLBACK ResizeEnumProc (HWND hwnd, LPARAM lParam)
    {
    char szClassName [31];
    RECT   rect;
    int iLeft, iTop, iWidth, iHeight;
    POINT pTop, pBottom;
    resizeinfo ri;

    /*counting children*/
    if(lParam==0L)
        {iChildren++; return TRUE;}
    if(!GetWindowRect(hwnd, &rect))
        {;/*can't do anything if this fails*/ }
    /*getting the initial child size*/
    else if(lParam==1L)
        {
        ri.hControl=hwnd;
        pTop.x=rect.left;
        pTop.y=rect.top;
        pBottom.x=rect.right;
        pBottom.y=rect.bottom;
        ScreenToClient(GetParent(hwnd), &pTop);
        ScreenToClient(GetParent(hwnd), &pBottom);

        rect.left=pTop.x;
        rect.top=pTop.y;
        rect.right=pBottom.x;
        rect.bottom=pBottom.y;

        ri.rect=rect;
        pri[iCurrentChild++]=ri;
        }
    else/*resizing the controls*/
        {
        /*get the correct control*/
        ri=GetResizeInfo(hwnd);

        /*fix the horizontal size and position*/
        iLeft=(int)(dHResize*(double)ri.rect.left);
        iWidth=(int)(dHResize*(double)(ri.rect.right-ri.rect.left));

        /*fix the vertical size and position*/
        iTop=(int)(dVResize*(double)ri.rect.top);

        /*if this is anything other that a single line
          text edit, resize height (for demonstration)*/
        GetClassName(hwnd, szClassName, 30);
        AnsiUpper(szClassName);
        /*show that we can resize based on class*/
        if(strcmp(szClassName, "EDIT")==0)
            {
            LONG l=GetWindowLong(hwnd, GWL_STYLE);
            /*only ES_MULTILINE EDIT controls get resized*/
            if(l&ES_MULTILINE)
                iHeight=(int)(dVResize*
                              (double)(ri.rect.bottom-ri.rect.top));
            else
                iHeight=ri.rect.bottom-ri.rect.top;
            }
        else
            iHeight=(int)(dVResize*
                          (double)(ri.rect.bottom-ri.rect.top));
        /*move the window*/
        MoveWindow(hwnd, iLeft, iTop, iWidth, iHeight, TRUE);
        }
    return TRUE;
    }

Listing Four
/*resize.rc */

#include <windows.h>
#include "resize.rh"

/* this icon is external, you need an icon named resize.ico
  in the directory for the resource to compile correctly
*/
resizeicon ICON "resize.ico"

/* menu */
resizemenu MENU
{
    POPUP "&File"
    {
      MENUITEM "&Quit...", IDC_QUIT
    }
    POPUP "&Help"
    {
      MENUITEM "&About...", IDC_ABOUT
    }
}
/* Client Dialog */
resizedialog DIALOG 3, 14, 305, 75
STYLE WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | 
                                           WS_MINIMIZEBOX | WS_MAXIMIZEBOX
CLASS "resize"
CAPTION "Resizable Dialog Interface"
FONT 8, "MS Sans Serif"
{
 CONTROL "About", IDC_ABOUT, "BUTTON", BS_PUSHBUTTON | WS_CHILD |
                                  WS_VISIBLE | WS_TABSTOP, 228, 28, 65, 12
 CONTROL "Quit", IDC_QUIT, "BUTTON", BS_PUSHBUTTON | WS_CHILD |
                                  WS_VISIBLE | WS_TABSTOP, 228, 41, 65, 12
 CONTROL "", IDC_TEXTAREA, "EDIT", ES_LEFT | ES_MULTILINE |
                                   WS_CHILD | WS_VISIBLE |
                                  WS_BORDER | WS_TABSTOP, 10, 10, 198, 46
 CONTROL "", IDC_TEXTFIELD, "edit", ES_LEFT | WS_CHILD |
                                    WS_VISIBLE | WS_BORDER |
                                    WS_TABSTOP, 228, 10, 66, 14
}
/* conventional about box */
AboutBox DIALOG 31, 24, 160, 52
STYLE WS_POPUP | WS_DLGFRAME
{
   CONTROL "Resizable Dialog Client", -1, "STATIC", SS_CENTER |
                                                    WS_CHILD |
                                                    WS_VISIBLE |
                                                    WS_GROUP, 16, 10, 128, 12
   CONTROL "OK", IDOK, "BUTTON", BS_DEFPUSHBUTTON | WS_CHILD |
                                 WS_VISIBLE | WS_GROUP |
                                 WS_TABSTOP, 64, 32, 32, 14
}

Listing Five
/*resize.rh*/

#define IDC_STATICTEXT  101
#define IDC_TEXTAREA    102
#define IDC_TEXTFIELD   103
#define IDC_ABOUT       104
#define IDC_QUIT        105



3


