C++ 怎么用代码更改按钮控件宽和高,或者用代码来更改文本框的宽和高,刚接触C++请大虾门多多指教。

我是想改变控件的大小,比如说按钮的,我以前是搞.NET的对C++不是很了解刚接触,在.NET中直接ID.Width或ID.Height就可以知道空间的宽和高了,我想知道在C++中怎么获取到,回答真确的追加50。哪位大侠多多帮忙,非常感谢。

c++中如果你的按钮是从资源管理器中的控件框创建的,那可以用鼠标拖动来改变大小,通过继承自CWND类的GetWindowRect()函数可以获得一个关于这个控件的大小和位置参数。
如果是在代码中创建的,创建时调用CBUTTON类的
Create( LPCTSTR lpszCaption,DWORD dwStyle,const RECT& rect,CWnd* pParentWnd,UINT nID )函数,const RECT& rect这个参数可以指定大小和位置。在创建之后,也可以调用SetWindowRgn( HRGN hRgn, BOOL bRedraw )来改变大小。
文本框和按钮基本一样。以上函数的具体使用如果不明白,可查看MSDN.追问

GetWindowRect()用这个方法获得了控件的位子,再怎么改变控件的大小呢????或者说SetWindowRgn()这个方法怎么用,里面还想没有改变控件大小的参数,只有HRGN和bRedraw和怎么设置大小啊???

追答

你要先创建一个CRgn类,CRgn类是MFC的区域类,可以表示一片特定形状的区域。调用CRgn类的CreateRectRgn(int x1,int y1,int x2,int y2 )或者CreateRectRgnIndirect(LPCRECT lpRect )就可以创建一个代表一个矩形区域的CRgn类,CRgn类重载了HRGN操作,也就是说可以把CRgn类的对象当HRGN用。调用SetWindowRgn后就会把你的控件放置到CRgn所代表的矩形区域。

追问

我有先头创建的CRect rc;放进CRgn.CreateRectRgnIndirect(rc);里面,然后直接写SetWindowRgn(rgn,true);页面直接是空白的。

追答

不好意思,通过试验,我发现SetWindowRgn和SetWindowPos对于按钮控件是没有用的,我查了下资料,发现如果按钮有BS_OWNERDRAW属性,那么可以重载该按钮的DrawItem函数,在这个函数中可以改变大小,这貌似有点复杂。别怪我才疏学浅啊!!!你要是找到怎么改变的方法,记得写出来,我也想学下!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-03-11
lpRect 包含,x,y,宽,高
BOOL GetWindowRect(HWND hWnd,LPRECT lpRect); 包含标题兰

BOOL GetClientRect(
HWND hWnd, // 窗口句柄
LPRECT lpRect // 客户区坐标
); //客户去的大小

SetWindowPos(hWnd,HWND_TOP,
0,0,宽,高,SWP_NOMOVE | SWP_NOZORDER)

BOOL SetWindowPos( HWND hWnd,
HWND hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
UINT uFlags
);
Parameters

hWnd
[in] Handle to the window.
hWndInsertAfter
[in] Handle to the window to precede the positioned window in the Z order. This parameter must be a window handle or one of the following values.
HWND_BOTTOM
Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost status and is placed at the bottom of all other windows.
HWND_NOTOPMOST
Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a non-topmost window.
HWND_TOP
Places the window at the top of the Z order.
HWND_TOPMOST
Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated.
For more information about how this parameter is used, see the following Remarks section.
X
[in] Specifies the new position of the left side of the window, in client coordinates.
Y
[in] Specifies the new position of the top of the window, in client coordinates.
cx
[in] Specifies the new width of the window, in pixels.
cy
[in] Specifies the new height of the window, in pixels.
uFlags
[in] Specifies the window sizing and positioning flags. This parameter can be a combination of the following values.
SWP_ASYNCWINDOWPOS
If the calling thread and the thread that owns the window are attached to different input queues, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request.
SWP_DEFERERASE
Prevents generation of the WM_SYNCPAINT message.
SWP_DRAWFRAME
Draws a frame (defined in the window's class description) around the window.
SWP_FRAMECHANGED
Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window's size is being changed.
SWP_HIDEWINDOW
Hides the window.
SWP_NOACTIVATE
Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter parameter).
SWP_NOCOPYBITS
Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client area are saved and copied back into the client area after the window is sized or repositioned.
SWP_NOMOVE
Retains the current position (ignores X and Y parameters).
SWP_NOOWNERZORDER
Does not change the owner window's position in the Z order.
SWP_NOREDRAW
Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of the window being moved. When this flag is set, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing.
SWP_NOREPOSITION
Same as the SWP_NOOWNERZORDER flag.
SWP_NOSENDCHANGING
Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
SWP_NOSIZE
Retains the current size (ignores the cx and cy parameters).
SWP_NOZORDER
Retains the current Z order (ignores the hWndInsertAfter parameter).
SWP_SHOWWINDOW
Displays the window.
Return Value
第2个回答  2011-03-08
每个控件都是一个窗口
是窗口就有HWND
如果是mfc的话每个控件都对应一个CWnd*对象m_hWnd就是他的HWND
使用系统api SetWindowPos来修改窗口大小
就可以达到你的要求

下面是SetWindowPos的使用方法
BOOL SetWindowPos( HWND hWnd,
HWND hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
UINT uFlags
);
Parameters

hWnd
[in] Handle to the window.
hWndInsertAfter
[in] Handle to the window to precede the positioned window in the Z order. This parameter must be a window handle or one of the following values.
HWND_BOTTOM
Places the window at the bottom of the Z order. If the hWnd parameter identifies a topmost window, the window loses its topmost status and is placed at the bottom of all other windows.
HWND_NOTOPMOST
Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a non-topmost window.
HWND_TOP
Places the window at the top of the Z order.
HWND_TOPMOST
Places the window above all non-topmost windows. The window maintains its topmost position even when it is deactivated.
For more information about how this parameter is used, see the following Remarks section.
X
[in] Specifies the new position of the left side of the window, in client coordinates.
Y
[in] Specifies the new position of the top of the window, in client coordinates.
cx
[in] Specifies the new width of the window, in pixels.
cy
[in] Specifies the new height of the window, in pixels.
uFlags
[in] Specifies the window sizing and positioning flags. This parameter can be a combination of the following values.
SWP_ASYNCWINDOWPOS
If the calling thread and the thread that owns the window are attached to different input queues, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request.
SWP_DEFERERASE
Prevents generation of the WM_SYNCPAINT message.
SWP_DRAWFRAME
Draws a frame (defined in the window's class description) around the window.
SWP_FRAMECHANGED
Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window's size is being changed.
SWP_HIDEWINDOW
Hides the window.
SWP_NOACTIVATE
Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter parameter).
SWP_NOCOPYBITS
Discards the entire contents of the client area. If this flag is not specified, the valid contents of the client area are saved and copied back into the client area after the window is sized or repositioned.
SWP_NOMOVE
Retains the current position (ignores X and Y parameters).
SWP_NOOWNERZORDER
Does not change the owner window's position in the Z order.
SWP_NOREDRAW
Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of the window being moved. When this flag is set, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing.
SWP_NOREPOSITION
Same as the SWP_NOOWNERZORDER flag.
SWP_NOSENDCHANGING
Prevents the window from receiving the WM_WINDOWPOSCHANGING message.
SWP_NOSIZE
Retains the current size (ignores the cx and cy parameters).
SWP_NOZORDER
Retains the current Z order (ignores the hWndInsertAfter parameter).
SWP_SHOWWINDOW
Displays the window.
Return Valuegood luck
第3个回答  2011-03-11
最简单的方法是这样的,
1.你首先定义一个按钮控件的控制变量,用来直接控制按钮。假定
一个按钮控件的控制变量为:m_InfoControl
2.直接调用movewindow函数
m_InfoControl.MoveWindow(left,top,right,bottom)
3.这样设定后你的控件就会是你要求的宽度
希望对你有帮助追问

声明了一个CButton Btn1;的变量 怎么把我要改变的大小的按钮控件ID Btn2,装载到Btn1里面去啊???然后用Btn1.MoveWindow()你是这个意思吗??

追答

将btn2的指阵赋给btn1,通过btn1指针操作btn2的控件。你试试这个方法。

追问

大虾,你不要直接给我说,你帮我写写代码,我自己写又会偏离方向!!!万分感谢。

追答

// m_btn1.MoveWindow(0,0,100,100);
CButton *m_btn2;
m_btn2=&m_btn1;
m_btn2->MoveWindow(0,0,100,100);
你看看这段代码,如果有不懂的再问我。

相似回答