如何修改窗口或控件的样式

一 窗口的样式

自己以前一直没弄懂一个问题。在CreateWindowEx中,到底什么是窗口样式?今天终于弄清了,所谓窗口样式是由两部分组成的,一部分是通用的窗口样式,如下所示:

WS_BORDER
Creates a window that has a thin-line border.
WS_CAPTION
Creates a window that has a title bar (includes the WS_BORDER style).
WS_CHILD
Creates a child window. A window with this style cannot have a menu bar. This style cannot be used with the WS_POPUP style.
WS_CHILDWINDOW
Same as the WS_CHILD style.
WS_CLIPCHILDREN
Excludes the area occupied by child windows when drawing occurs within the parent window. This style is used when creating the parent window.
WS_CLIPSIBLINGS
Clips child windows relative to each other; that is, when a particular child window receives a WM_PAINT message, the WS_CLIPSIBLINGS style clips all other overlapping child windows out of the region of the child window to be updated. If WS_CLIPSIBLINGS is not specified and child windows overlap, it is possible, when drawing within the client area of a child window, to draw within the client area of a neighboring child window.
WS_DISABLED
Creates a window that is initially disabled. A disabled window cannot receive input from the user. To change this after a window has been created, use EnableWindow.
WS_DLGFRAME
Creates a window that has a border of a style typically used with dialog boxes. A window with this style cannot have a title bar.
WS_GROUP
Specifies the first control of a group of controls. The group consists of this first control and all controls defined after it, up to the next control with the WS_GROUP style. The first control in each group usually has the WS_TABSTOP style so that the user can move from group to group. The user can subsequently change the keyboard focus from one control in the group to the next control in the group by using the direction keys.
You can turn this style on and off to change dialog box navigation. To change this style after a window has been created, use SetWindowLong.
WS_HSCROLL
Creates a window that has a horizontal scroll bar.
WS_ICONIC
Creates a window that is initially minimized. Same as the WS_MINIMIZE style.
WS_MAXIMIZE
Creates a window that is initially maximized.
WS_MAXIMIZEBOX
Creates a window that has a maximize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified.
WS_MINIMIZE
Creates a window that is initially minimized. Same as the WS_ICONIC style.
WS_MINIMIZEBOX
Creates a window that has a minimize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified.
WS_OVERLAPPED
Creates an overlapped window. An overlapped window has a title bar and a border. Same as the WS_TILED style.
WS_OVERLAPPEDWINDOW
Creates an overlapped window with the WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU, WS_THICKFRAME, WS_MINIMIZEBOX, and WS_MAXIMIZEBOX styles. Same as the WS_TILEDWINDOW style.
WS_POPUP
Creates a pop-up window. This style cannot be used with the WS_CHILD style.
WS_POPUPWINDOW
Creates a pop-up window with WS_BORDER, WS_POPUP, and WS_SYSMENU styles. The WS_CAPTION and WS_POPUPWINDOW styles must be combined to make the window menu visible.
WS_SIZEBOX
Creates a window that has a sizing border. Same as the WS_THICKFRAME style.
WS_SYSMENU
Creates a window that has a window menu on its title bar. The WS_CAPTION style must also be specified.
WS_TABSTOP
Specifies a control that can receive the keyboard focus when the user presses the TAB key. Pressing the TAB key changes the keyboard focus to the next control with the WS_TABSTOP style.
You can turn this style on and off to change dialog box navigation. To change this style after a window has been created, use SetWindowLong.
WS_THICKFRAME
Creates a window that has a sizing border. Same as the WS_SIZEBOX style.
WS_TILED
Creates an overlapped window. An overlapped window has a title bar and a border. Same as the WS_OVERLAPPED style.
WS_TILEDWINDOW
Creates an overlapped window with the WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU, WS_THICKFRAME, WS_MINIMIZEBOX, and WS_MAXIMIZEBOX styles. Same as the WS_OVERLAPPEDWINDOW style.
WS_VISIBLE
Creates a window that is initially visible.
This style can be turned on and off by using ShowWindow or SetWindowPos.
WS_VSCROLL
Creates a window that has a vertical scroll bar.

另外一部分窗口样式,则是各个控件独有的样式,例如

BS_3STATE
Creates a button that is the same as a check box, except that the box can be grayed as well as checked or cleared. Use the grayed state to show that the state of the check box is not determined.
BS_AUTO3STATE
Creates a button that is the same as a three-state check box, except that the box changes its state when the user selects it. The state cycles through checked, indeterminate, and cleared.
BS_AUTOCHECKBOX
Creates a button that is the same as a check box, except that the check state automatically toggles between checked and cleared each time the user selects the check box.
BS_AUTORADIOBUTTON
Creates a button that is the same as a radio button, except that when the user selects it, the system automatically sets the button’s check state to checked and automatically sets the check state for all other buttons in the same group to cleared.
BS_CHECKBOX
Creates a small, empty check box with text. By default, the text is displayed to the right of the check box. To display the text to the left of the check box, combine this flag with the BS_LEFTTEXT style (or with the equivalent BS_RIGHTBUTTON style).
BS_DEFPUSHBUTTON
Creates a push button that behaves like a BS_PUSHBUTTON style button, but has a distinct appearance. If the button is in a dialog box, the user can select the button by pressing the ENTER key, even when the button does not have the input focus. This style is useful for enabling the user to quickly select the most likely (default) option.
BS_GROUPBOX
Creates a rectangle in which other controls can be grouped. Any text associated with this style is displayed in the rectangle’s upper left corner.
BS_LEFTTEXT
Places text on the left side of the radio button or check box when combined with a radio button or check box style. Same as the BS_RIGHTBUTTON style.
BS_OWNERDRAW
Creates an owner-drawn button. The owner window receives a WM_DRAWITEM message when a visual aspect of the button has changed. Do not combine the BS_OWNERDRAW style with any other button styles.
BS_PUSHBUTTON
Creates a push button that posts a WM_COMMAND message to the owner window when the user selects the button.
BS_RADIOBUTTON
Creates a small circle with text. By default, the text is displayed to the right of the circle. To display the text to the left of the circle, combine this flag with the BS_LEFTTEXT style (or with the equivalent BS_RIGHTBUTTON style). Use radio buttons for groups of related, but mutually exclusive choices.
BS_USERBUTTON
Obsolete, but provided for compatibility with 16-bit versions of Windows. Applications should use BS_OWNERDRAW instead.
BS_BITMAP
Specifies that the button displays a bitmap. See the Remarks section for its interaction with BS_ICON.
BS_BOTTOM
Places text at the bottom of the button rectangle.
BS_CENTER
Centers text horizontally in the button rectangle.
BS_ICON
Specifies that the button displays an icon. See the Remarks
section for its interaction with BS_BITMAP.
BS_FLAT
Specifies that the button is two-dimensional; it does not use the default shading to create a 3-D image.
BS_LEFT
Left-justifies the text in the button rectangle. However, if the button is a check box or radio button that does not have the BS_RIGHTBUTTON style, the text is left justified on the right side of the check box or radio button.
BS_MULTILINE
Wraps the button text to multiple lines if the text string is too long to fit on a single line in the button rectangle.
BS_NOTIFY
Enables a button to send BN_KILLFOCUS and BN_SETFOCUS notification messages to its parent window.
Note that buttons send the BN_CLICKED notification message regardless of whether it has this style. To get BN_DBLCLK notification messages, the button must have the BS_RADIOBUTTON or BS_OWNERDRAW style.

这两部分样式——包括window style 还有各个控件自身的——是可以组合起来的。
因此,在下面函数中

HWND CreateWindowEx(
DWORD dwExStyle,
LPCTSTR lpClassName,
LPCTSTR lpWindowName,
DWORD dwStyle,
int x,
int y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam );

dwStyle实际是指两者的位组合(Specifies the style of the window being created. This parameter can be a combination of window styles, plus the control styles indicated in the Remarks section. )
另外,dwExStyle实际是指窗口的的扩展样式,为什么会有扩展样式,猜测最早指定的窗口通用样式太少,后来又想增加一些,这个时候没有办法了,就做了一个CreateWindowEx函数,把这部分扩展样式加了进去。扩展样式的部分宏如下所述:

WS_EX_ACCEPTFILES
Specifies that a window created with this style accepts drag-drop files.
WS_EX_APPWINDOW
Forces a top-level window onto the taskbar when the window is visible.
WS_EX_CLIENTEDGE
Specifies that a window has a border with a sunken edge.
WS_EX_COMPOSITED
Windows XP: Paints all descendants of a window in bottom-to-top painting order using double-buffering. For more information, see Remarks. This cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC.
WS_EX_CONTEXTHELP
Includes a question mark in the title bar of the window. When the user clicks the question mark, the cursor changes to a question mark with a pointer. If the user then clicks a child window, the child receives a WM_HELP message. The child window should pass the message to the parent window procedure, which should call the WinHelp function using the HELP_WM_HELP command. The Help application displays a pop-up window that typically contains help for the child window.

二 如何改变窗口的样式

1 通过 SetWindowLongPtr 改变

SetWindowLongPtr 函数可以改变窗口的属性(The SetWindowLong function changes an attribute of the specified window. ),当然包括窗口的过程函数以及窗口的样式等等属性。

LONG_PTR SetWindowLongPtr( HWND hWnd, int nIndex, LONG_PTR dwNewLong );

其中第2个参数指示了要修改的属性名称(如下列表),特别注意修改样式(GWL_STYLE)和扩展样式(GWL_EXSTYLE)两种。

GWL_EXSTYLE
Sets a new extended window style. For more information,see CreateWindowEx.
GWL_STYLE
Sets a new window style.
GWLP_WNDPROC
Sets a new address for the window procedure.
GWLP_HINSTANCE
Sets a new application instance handle.
GWLP_ID
Sets a new identifier of the window.
GWLP_USERDATA
Sets the user data associated with the window. This data is intended for use by the application that created the> window. Its value is initially zero.
The following values are also available when the hWnd parameter identifies a dialog box.
DWLP_DLGPROC
Sets the new pointer to the dialog box procedure.
DWLP_MSGRESULT
Sets the return value of a message processed in the dialog box procedure.
DWLP_USER
Sets new extra information that is private to the application, such as handles or pointers.

在采用SetWindowLongPtr 修改样式以前,可以用GetWindowLongPtr取得窗口当前的样式,例如修改一个树控件的样式

	DWORD dwStyles = GetWindowLongPtr(m_wndTree.m_hWnd, GWL_STYLE);//获取树控制原风格
	dwStyles |= TVS_EDITLABELS | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT;
	SetWindowLongPtr(m_wndTree.m_hWnd, GWL_STYLE, dwStyles);//设置风格

2 通过 CWnd::ModifyStyle 进行修改

调用这个函数取修改一个窗口的样式(Call this member function to modify a window’s style. )。这里所谓的样式就是指上面所说的窗口的标准样式以及各个控件的样式;如果想修改扩展样式,可以采用CWnd::ModifyStyleEx 这个接口。

BOOL ModifyStyle(DWORD dwRemove, DWORD dwAdd, UINT nFlags = 0 );

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Santiago

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值