dialog class for automatically changing the position and size of controls in VC (GO)

Source: Internet
Author: User

Original address: http://www.cnblogs.com/jcss2008/archive/2008/10/15/1312161.html

A dialog class that can automatically change the position and size of a control. The dialog class inherits from this class, as long as a few lines of simple code are written in OnInitDialog (), the control on the dialog will change its position and size as the size of the dialog box changes.

When developing an application with VC, it is often necessary to do some dialog boxes that can be resized, and this time the controls on the dialog will change their position and size as the size of the dialog box changes. If you have fewer controls, you can add code to the OnSize () Event of the dialog box to adjust the position and size of each control by calculating it, but it would be very painful if the controls on the dialog box were more than one. If there are many dialog boxes in the program that can be resized, then one of the OnSize () is written down, which will crash the programmer.
To solve this problem, I wrote a dialog box class Clxdialog that automatically changed the position and size of the control. The dialog class inherited from this class, as long as the control in the OnInitDialog () to make some simple settings, the dialog box control will change with the size of the dialog box changes its position and size.
To save the control information, I defined a structure:

typedef struct _DLGCONTROLTAG
{
int iId; Control ID
int Iflag; Flag indicating how to change the position or size of the control
int ipercent; Change value as a percentage of the dialog box change value
} dlgctlinfo, *pdlgctlinfo;

Here are some explanations for the Iflag and ipercent in the structure. Where Iflag is the following enumeration value:

Enum
{
MOVEX = 0,//control moves in x direction (left or right)
Movey,//control moves in the Y direction (up and down)
Movexy,//control moves simultaneously in X and y directions
ELASTICX,//control changes size in x direction (width)
Elasticy,//control changes in y direction (height) Change size
ELASTICXY//control changes size in both X and y directions
};

Ipercent indicates that the change value is the percentage of the dialog box change value. For example, a control with a ipercent value of 100,iflag is Movex, and when the width of the dialog box changes by 100 units, the control moves 100 units in the x direction, or a control with a ipercent value of 100. The Iflag value is Elasticxy, so when the width and height of the dialog box change by 100 units, the height and width of the control are changed by 100 units accordingly.
Here is the function for setting control information:

BOOL Setcontrolproperty (pdlgctlinfo LP, int nelements);

It's very simple to use, just add the following code to the OnInitDialog () function of the dialog box:

Array of control information
Static Dlgctlinfo dcmenugroup[] =
{
{IDOK, MOVEX, 100},
{IDCANCEL, MOVEX, 100},
{idc_button1, MOVEX, 50},
{idc_button1, Movey, 100},
{idc_edit1, elasticx, 100},
{idc_edit2, elasticx, 50},
{idc_edit3, elasticx, 50},
{idc_edit3, MOVEX, 50},
{IDC_EDIT4, elasticy, 100},
{idc_edit5, elasticx, 100},
{idc_edit5, Elasticy, 50},
{idc_edit6, elasticx, 100},
{IDC_EDIT6, Elasticy, 50},
{IDC_EDIT6, Movey, 50},
};

Setting Control information
Setcontrolproperty (Dcmenugroup, sizeof (Dcmenugroup)/sizeof (dlgctlinfo));

The following is a dialog box that uses the above code to change the effect before and after the size:

We can easily understand the above code by comparing the two screenshots.
I also provide a function:

void Showsizeicon (BOOL bShow = TRUE);

To set whether a dialog box appears in the lower-right corner to indicate an icon that can change size. This icon is read from the system, my screenshot above is Windows2000, in the WindowsXP will automatically become XP style.
Well, gossip not much to say, the following paste the dialog box class Clxdialog source code, there are detailed comments:
LxDialog.h file:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////
dialog class for automatically changing the position and size of controls
File name: lxDialog.h
Author: Starlee (coolstarlee@sohu.com)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////

Class Clxdialog:public CDialog
{
Public
Clxdialog (UINT nID, cwnd* pparent = NULL);

typedef struct _DLGCONTROLTAG
{
int iId;
int Iflag;
int ipercent;
} dlgctlinfo, *pdlgctlinfo;

Enum
{
MOVEX = 0,
Movey,
Movexy,
Elasticx,
Elasticy,
Elasticxy
};

Setting Control information
BOOL Setcontrolproperty (pdlgctlinfo LP, int nelements);

Whether to display an icon that indicates a resizable size in the lower-right corner of the dialog box
void Showsizeicon (BOOL bShow = TRUE);

Protected
Virtual BOOL OnInitDialog ();
afx_msg void OnSize (UINT nType, int cx, int cy);
afx_msg void Onsizing (UINT nside, LPRECT LPRECT);
Declare_message_map ()

Private
int m_iclientwidth; Width of the client area of the dialog box
int m_iclientheight; Height of the client area of the dialog box
int m_iminwidth; Minimum width of dialog box
int m_iminheight; Minimum height of dialog box
Pdlgctlinfo M_pcontrolarray; Control information Array pointer
int m_icontrolnumber; Number of controls to set control information
BOOL M_bshowsizeicon; Whether to display icons that indicate the size of the change
CStatic M_wndsizeicon; static controls for placing icons
Save Icon's bitmap
CBitmap M_bmpsizeicon;
BITMAP M_bitmap;
};

LxDialog.cpp file:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////
dialog class for automatically changing the position and size of controls
File name: LxDialog.cpp
Author: Starlee (coolstarlee@sohu.com)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////

#include "stdafx.h"
#include "LxDialog.h"

An icon ID that represents the size of the change
#ifndef obm_size
#define Obm_size 32766
#endif

Clxdialog::clxdialog (UINT NID, cwnd* pparent/*=null*/)
: CDialog (NID, pparent)
, M_iclientwidth (0)
, M_iclientheight (0)
, M_iminwidth (0)
, M_iminheight (0)
, M_pcontrolarray (NULL)
, M_icontrolnumber (0)
, M_bshowsizeicon (TRUE)
{}

Begin_message_map (Clxdialog, CDialog)
On_wm_size ()
On_wm_sizing ()
End_message_map ()

BOOL Clxdialog::oninitdialog ()
{
CDialog::OnInitDialog ();

The Settings dialog box is a variable-sized
ModifyStyle (0, Ws_sizebox);

The minimum value of the dialog box's width and height as the initial size of the dialog box
CRect Rectdlg;
GetWindowRect (RECTDLG);
M_iminwidth = Rectdlg.width ();
M_iminheight = Rectdlg.height ();

Get the size of the dialog client area
CRect rectclient;
GetClientRect (rectclient);
M_iclientwidth = Rectclient.width ();
M_iclientheight = Rectclient.height ();

Load icon
M_bmpsizeicon.loadoembitmap (obm_size);
M_bmpsizeicon.getbitmap (&M_BITMAP);

Create a static control that displays the icon and place it in the lower right corner of the dialog box
M_wndsizeicon.create (NULL, Ws_child | ws_visible | Ss_bitmap, CRect (0, 0, M_bitmap.bmwidth, m_bitmap.bmheight), this, 0);
M_wndsizeicon.setbitmap (M_bmpsizeicon);
M_wndsizeicon.movewindow (M_iclientwidth-m_bitmap.bmwidth, M_iclientheight-m_bitmap.bmheight, M_bitmap.bmWidth, m_ Bitmap.bmheight);

Display icon
M_wndsizeicon.showwindow (M_bshowsizeicon);

return TRUE;
}

void Clxdialog::onsize (UINT nType, int cx, int cy)
{
Cdialog::onsize (NType, CX, CY);

Increment of dialog box width and height
int iincrementx = Cx-m_iclientwidth;
int iincrementy = Cy-m_iclientheight;

0 increments at the time of minimization
if (NType = = size_minimized)
{
Iincrementx = Iincrementy = 0;
}

for (int i = 0; i < M_icontrolnumber; i++)
{
CWnd *pwndctrl = NULL;

int iId = M_PCONTROLARRAY[I].IID;
int iflag = M_pcontrolarray[i].iflag;
int ipercent = m_pcontrolarray[i].ipercent;

Invalid value
if ((Ipercent < 0) | | (Ipercent > 100))
Continue

Get the control pointer
Pwndctrl = GetDlgItem (iId);
if ((NULL! = Pwndctrl) && IsWindow (Pwndctrl->getsafehwnd ()))
{
CRect Rectctrl;
Pwndctrl->getwindowrect (Rectctrl);

ScreenToClient (Rectctrl);

int ileft = Rectctrl.left;
int iTop = Rectctrl.top;
int iwidth = Rectctrl.width ();
int iheight = Rectctrl.height ();

Switch (Iflag)
{
Case MOVEX://X-Direction movement
ILeft + = (Iincrementx * ipercent/100);
Break

Case Movey://y-direction move
ITop + = (Iincrementy * ipercent/100);
Break

Case Movexy://x direction and Y direction simultaneous movement
ILeft + = (Iincrementx * ipercent/100);
ITop + = (Iincrementy * ipercent/100);
Break

Case ELASTICX://change size in x direction
Iwidth + = (Iincrementx * ipercent/100);
Break

Case Elasticy://change size in y direction
Iheight + = (Iincrementy * ipercent/100);
Break

Case ELASTICXY://x direction and y direction change size simultaneously
Iwidth + = (Iincrementx * ipercent/100);
Iheight + = (Iincrementy * ipercent/100);
Break

Default
;
}

Move the control to a new location
Pwndctrl->movewindow (ILeft, ITop, iwidth, iheight);
}
}

Move the icon to the bottom right corner of the dialog box
if (IsWindow (M_wndsizeicon.getsafehwnd ()))
M_wndsizeicon.movewindow (Cx-m_bitmap.bmwidth, Cy-m_bitmap.bmheight, M_bitmap.bmwidth, m_bitmap.bmHeight);

Log the size of the client area of the dialog box
if (nType! = size_minimized)
{
M_iclientwidth = CX;
M_iclientheight = cy;
}
}

void Clxdialog::onsizing (UINT nside, LPRECT LPRECT)
{
Cdialog::onsizing (Nside, lpRect);

dialog box cannot be less than the initial size

int iwidth = lprect->right-lprect->left;
int iheight = lprect->bottom-lprect->top;

if (iwidth <= m_iminwidth)
Lprect->right = Lprect->left + m_iminwidth;

if (iheight <= m_iminheight)
Lprect->bottom = Lprect->top + m_iminheight;
}

BOOL Clxdialog::setcontrolproperty (pdlgctlinfo LP, int nelements)
{
Setting control array Information

if (NULL = = LP)
return FALSE;

if (nelements <= 0)
return FALSE;

M_pcontrolarray = LP;
M_icontrolnumber = nelements;

return TRUE;
}

void Clxdialog::showsizeicon (BOOL bShow/*=null*/)
{
M_bshowsizeicon = BShow;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.