Different views on dynamically changing the widget size)

Source: Internet
Author: User

Original article: http://www.huarw.com/program/vc/vc01/200804/1538412.html

 

About Dynamic ChangesWidgetDifferent views on Size

 

The programming Forum (hur.cn) was published:2008-04-14 12:14:05 Landlord

 

I read a lot of posts on the Forum about dynamically changing the size of the Edit Control, which is basically very simple. Reload wm_size and use getdlgitem (idc_edit)-> movewindow (crect );
I can use it but I can't. I wrote a dialog box program with an edit box in the dialog box. I used movewindow IN THE onsize function of the dialog box, and there was no compilation error, however, errors occur during each operation. No one on many posts said that errors would occur. I had to change the usage. The Code is as follows:
Void ctelecomdlg: onsize (uint ntype, int CX, int CY)
{
Cdialog: onsize (ntype, CX, CY );

// Todo: add your message handler code here
Try
{
Getdlgitem (idc_richedit)-> movewindow (0, 0, CX, CY );
}
Catch (...)
{
Afxmessagebox ("onsize fail ");
Return;
}
}
An exception occurs every time you run the program. The message "onsize fail" is displayed. If you change the form size in the future, the exception does not occur. The edit box also changes dynamically. Further tracking is found, the ntype value is 0 at each run, 1 at minimum, 2 at maximum, and 0 at restoration. If an IF (ntype! = 0) Judgment statement. When the ntype is not 0, movewindow is executed. It is normal to run the statement. No exception will occur. The problem may occur again after the restoration is minimized, the edit box "no" is displayed because movewindow is not executed.
Summary:
When you start running the program, you should not reload wm_size and execute movewindow. (You do not know if it is correct.) there should be other better solutions, it is not a problem for my method to throw an exception for the first time. Knowing that there is an exception without solving it is not an exception. It should be called an error.
Please give me some advice. Thank you.
(You can also make it clearer in the future and mislead people. Is it because of this small problem? I am a newbie)

The programming Forum (hur.cn) was published:2008-04-14 12:18:311Floor score:6

 

When onsize is run for the first time, the handle of the dialog box has not yet been generated (which can be determined through debugging). Therefore, you need to first determine whether m_hwnd is null, and if it is null, it will not be processed.

The programming Forum (hur.cn) was published:12:25:352Floor score:6

 

Such as ls

I usually write it like this.

If (m_edit)
{
Movewindow (rect );
}

The programming Forum (hur.cn) was published:13:06:153Floor score:2

Lxdialog. h file:

//////////////////////////////////////// //////////////////////////////////////// /////
// Dialog box class for automatically changing the widget position and size
// File name: lxdialog. h
// By 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
};

// Set control information
Bool setcontrolproperty (pdlgctlinfo LP, int nelements );

// Whether to display an icon in the lower-right corner of the dialog box that can change the size
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 );
Declare_message_map ()

PRIVATE:
Int m_iclientwidth; // The width of the client area in the dialog box.
Int m_iclientheight; // height of the client area in the dialog box
Int m_iminwidth; // The minimum width of the dialog box.
Int m_iminheight; // The minimum height of the dialog box.
Pdlgctlinfo m_pcontrolarray; // control information array pointer
Int m_icontrolnumber; // sets the number of controls in the control information.
Bool m_bshowsizeicon; // whether to display an icon that can change the size
Cstatic m_wndsizeicon; // The static control of the widget.
// Save the bitmap of the icon
Cbitmap m_bmp sizeicon;
Bitmap m_bitmap;
};

Lxdialog. cpp file:

//////////////////////////////////////// //////////////////////////////
// Automatically change

Dialog Box class for widget location and size
// File name: lxdialog. cpp
// By starlee (coolstarlee@sohu.com)
//////////////////////////////////////// //////////////////////////////

# Include "stdafx. H"
# Include "lxdialog. H"

// Indicates the icon ID that can be changed
# 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 ();

// Set the dialog box to variable-size
Modifystyle (0, ws_sizebox );

// Use the initial size of the dialog box as the minimum value of the width and height of the dialog box.
Crect rectdlg;
Getwindowrect (rectdlg );
M_iminwidth = rectdlg. Width ();
M_iminheight = rectdlg. Height ();

// Obtain the size of the client area in the dialog box.
Crect rectclient;
Getclientrect (rectclient );
M_iclientwidth = rectclient. Width ();
M_iclientheight = rectclient. Height ();

// Load icon
M_bmp sizeicon.loadoembitmap (obm_size );
M_bmp sizeicon.getbitmap (& m_bitmap );

// Create a static control for the display 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_bmp sizeicon );
M_wndsizeicon.movewindow (m_iclientwidth-m_bitmap.bmwidth, m_iclientheight-m_bitmap.bmheight, m_bitmap.bmwidth, m_bitmap.bmheight );

// Display the icon
M_wndsizeicon.showwindow (m_bshowsizeicon );

Return true;
}

Void clxdialog: onsize (uint ntype, int CX, int CY)
{
Cdialog: onsize (ntype, CX, CY );

// Increment the width and height of the dialog box
Int iincrementx = Cx-m_iclientwidth;
Int iincrementy = cy-m_iclientheight;

// When minimized, the increase is 0.
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;

// Obtain the control pointer
Pwndctrl = getdlgitem (IID );
If (null! = Pwndctrl) & iswindow (pwndctrl-> getsafehwnd ()))
{
Crect rectctrl;
Pwndctrl-> getwindowrect (rectctrl );

Screentoclient (rectctrl );

Int ileft = rectctrl. Left;
Int I = rectctrl. Top;
Int iwidth = rectctrl. Width ();
Int iheight = rectctrl. Height ();

Switch (iflag)
{
Case movex: // move in the direction of X
Ileft + = (iincrementx * ipercent/100 );
Break;

Case Movey: // move in Y direction
I + = (iincrementy * ipercent/100 );
Break;

Case movexy: // move both the X and Y directions.
Ileft + = (iincrementx * ipercent/100 );
I + = (iincrementy * ipercent/100 );
Break;

Case elasticx: // change the size in the X direction
Iwidth + = (iincrementx * ipercent/100 );
Break;

Case elasticy: // change the size in the Y direction
Iheight + = (iincrementy * ipercent/100 );
Break;

Case elasticxy: // both the X and Y directions change the size.
Iwidth + = (iincrementx * ipercent/100 );
Iheight + = (iincrementy * ipercent/100 );
Break;

Default:
;
}

// Move the widget to a new location
Pwndctrl-> movewindow (ileft, I, 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 );

// Record the size of the client area in the dialog box
If (ntype! = Size_minimized)
{
M_iclientwidth = Cx;
M_iclientheight = Cy;
}
}

Void clxdialog: onsizing (uint nside, lprect)
{
Cdialog: onsizing (nside, lprect );

// The dialog box cannot be smaller 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 = & nbsp; lprect-> top + m_iminheight;
}

Bool clxdialog: setcontrolproperty (pdlgctlinfo LP, int nelements)
{
// Set 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;
}

Http://blog.csdn.NET/scq2099yt/archive/2008/03/12/2174124.ASPx

 

Programming Forum (hur.cn) published on: 2008-04-14 13: 56: 484 floor score: 6

 

The first floor of the project is very clear.

Basically speaking, it is quite simple to reload wm_size and use getdlgitem (idc_edit)-> movewindow (crect );
Before movewindow, you need to add a window to determine whether to create it. Because there is a wm_size message response before the window is created, getdlgitem will return null at this time.

 

Programming Forum (hur.cn) published on: 2008-04-14 17: 26: 395 floor score: 0

 

Thank you!
Determine whether to create a window before movewindow
If all previous posts are

 

In this case, I don't need to post this post anymore.

 

 

 

 

 

Programming Forum (hur.cn) posted at: 2008-04-14 score: 0

 

If (getdlgitem (idc_richedit ))
Getdlgitem (idc_richedit)-> movewindow (10, 10, cx-20, cy-20 );

 

Programming Forum (hur.cn) published at: 2008-04-14 building score: 0

 

If (this-> m_hwnd)
Getdlgitem (idc_richedit)-> movewindow (10, 10, cx-20, cy-20 );
This method does not seem to work. This-> m_hwnd is not empty during the first running.

Welcome to reprint, but please keep the source, this article from [China soft network] original link: http://www.huarw.com/program/vc/vc01/200804/1538412_9.html

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.