---
title: "VC++ 深入详解 学习笔记(4) -- 修改窗口样式"
author: "zzysh"
date: "2008-12-25T15:25:37Z"
tags: []
slug: "vc-4"
draft: false
lastmod: "2008-12-25T15:32:26Z"
id: 301466
url: "https://elton.iteye.com/blog/301466"
---

在MFC里面，更改窗口大小和样式是在创建窗口的时候做的，而窗口的图标，背景，光标样式是通过设计窗口类的时候实现的（下篇文章会详细说明）。 更改窗口大小和样式有两种方式：

1\. 在窗口创建之前，在BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)函数中，修改其中的CREATESTRUCT结构的属性来改变窗口的样式。

```
typedef struct tagCREATESTRUCT {
    LPVOID lpCreateParams;
    HINSTANCE hInstance;
    HMENU hMenu;
    HWND hwndParent;
    int cy;
    int cx;
    int y;
    int x;
    LONG style;
    LPCTSTR lpszName;
    LPCTSTR lpszClass;
    DWORD dwExStyle;
} CREATESTRUCT, *LPCREATESTRUCT;
```

如：

```
cs.cx = 300;
cs.cy = 200;
```

 

2\. 在窗口创建后， 在int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)函数中设置SetWindowsLongPtr来改变窗口样式

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

 其中， nIndex表示一个类型， 而dwNewLong是对应的值

nIndex包括：

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(m_hWnd, GWL_STYLE, GetWindowLongPtr(m_hWnd, GWL_STYLE) & ~WS_MAXIMIZEBOX);
```