---
title: "VB 窗口处理技巧大全 "
author: "yeuego"
date: "2011-03-09T13:17:18Z"
tags: ["VB"]
categories: ["研发管理"]
slug: "vb"
draft: false
lastmod: "2011-03-09T13:17:18Z"
id: 948232
url: "https://yeuego.iteye.com/blog/948232"
---

VB提供了API函数SetWindowLong和GetWindowLong，可以让我们很容易取得对窗口的操作；通过对窗口属性的操作，可以更改窗口的显示风格。有些看来是正常情况下无法实现的窗口，现在你可以很容易的实现。只要你想到，更多希奇古怪的你也能做到。快试试下面的例子吧。

> '以下例子中可能用到的API声明和常量、变量声明  
> Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long  
> Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long  
> Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long  
> Private Const SWP\_NOSIZE = &H1  
> Private Const SWP\_NOZORDER = &H4  
> Private Const SWP\_NOMOVE = &H2  
> Private Const SWP\_DRAWFRAME = &H20  
> Private Const GWL\_STYLE = (-16)  
> Private Const WS\_THICKFRAME = &H40000  
> Private Const WS\_DLGFRAME = &H400000  
> Private Const WS\_POPUP = &H80000000  
> Private Const WS\_CAPTION = &HC00000  
> Private Const WS\_SYSMENU = &H80000  
> Private Const WS\_MINIMIZEBOX = &H20000  
> Private Const WS\_MAXIMIZEBOX = &H10000  
> Private Const WS\_MINIMIZE = &H20000000  
> Private Const WS\_MAXIMIZE = &H1000000
> 
> --------------------------------------------------------------------------------
> 
> '例子一：任何一个控件（只要有窗口，这是我们的前提，下同），你可以在运行时随便更改它的大小。  
> Private Sub ControlSize(ControlName As Control, SetTrue As Boolean)  
> Dim dwStyle As Long  
> dwStyle = GetWindowLong(ControlName.hwnd, GWL\_STYLE)  
> If SetTrue Then  
> dwStyle = dwStyle Or WS\_THICKFRAME  
> Else  
> dwStyle = dwStyle - WS\_THICKFRAME  
> End If  
> dwStyle = SetWindowLong(ControlName.hwnd, GWL\_STYLE, dwStyle)  
> SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd, 0, 0, 0, 0, SWP\_NOZORDER Or SWP\_NOSIZE Or SWP\_NOMOVE Or SWP\_DRAWFRAME  
> End Sub  
> '用法：ControlSize picture1,true；设置第二个参数为False取消这种设置，下同
> 
> --------------------------------------------------------------------------------
> 
> '例子二：任何一个控件，我们都可以控制其显示风格为对话框的风格。  
> Private Sub ControlDialog(ControlName As Control, SetTrue As Boolean)  
> Dim dwStyle As Long  
> dwStyle = GetWindowLong(ControlName.hwnd, GWL\_STYLE)  
> If SetTrue Then  
> dwStyle = dwStyle Or WS\_DLGFRAME  
> Else  
> dwStyle = dwStyle - WS\_DLGFRAME  
> End If  
> dwStyle = SetWindowLong(ControlName.hwnd, GWL\_STYLE, dwStyle)  
> SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd, 0, 0, 0, 0, SWP\_NOZORDER Or SWP\_NOSIZE Or SWP\_NOMOVE Or SWP\_DRAWFRAME  
> End Sub  
> '用法：ControlSize picture1,true
> 
> --------------------------------------------------------------------------------
> 
> '例子三：任何一个控件，我们都可以控制其显示风格为模式对话框的风格  
> Private Sub ControlModal(ControlName As Control, SetTrue As Boolean)  
> Dim dwStyle As Long  
> dwStyle = GetWindowLong(ControlName.hwnd, GWL\_STYLE)  
> If SetTrue Then  
> dwStyle = dwStyle Or WS\_POPUP  
> Else  
> dwStyle = dwStyle - WS\_POPUP  
> End If  
> dwStyle = SetWindowLong(ControlName.hwnd, GWL\_STYLE, dwStyle)  
> SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd, 0, 0, 0, 0, SWP\_NOZORDER Or SWP\_NOSIZE Or SWP\_NOMOVE Or SWP\_DRAWFRAME  
> End Sub  
> '用法：ControlModal Picture1,true
> 
> --------------------------------------------------------------------------------
> 
> '例子四：任何一个控件，我们都可以给它加上标题栏，通过拖动标题栏，可以实现控件的运行时移动。  
> Private Sub ControlCaption(ControlName As Control, SetTrue As Boolean) Dim dwStyle As Long  
> dwStyle = GetWindowLong(ControlName.hwnd, GWL\_STYLE)  
> If SetTrue Then  
> dwStyle = dwStyle Or WS\_CAPTION  
> Else  
> dwStyle = dwStyle - WS\_CAPTION  
> End If  
> dwStyle = SetWindowLong(ControlName.hwnd, GWL\_STYLE, dwStyle)  
> SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd, 0, 0, 0, 0, SWP\_NOZORDER Or SWP\_NOSIZE Or SWP\_NOMOVE Or SWP\_DRAWFRAME  
> End Sub  
> '用法：ControlCaption picture1,true
> 
> --------------------------------------------------------------------------------
> 
> '例子五：任何一个控件，我们都可以给它加上ControlBox（所谓ControlBox，就是窗体的图标＋最小化＋最大化＋关闭按钮）。  
> Private Sub ControlSysMenu(ControlName As Control, SetTrue As Boolean)  
> Dim dwStyle As Long  
> dwStyle = GetWindowLong(ControlName.hwnd, GWL\_STYLE)  
> If SetTrue Then  
> dwStyle = dwStyle Or WS\_SYSMENU  
> Else  
> dwStyle = dwStyle - WS\_SYSMENU  
> End If  
> dwStyle = SetWindowLong(ControlName.hwnd, GWL\_STYLE, dwStyle)  
> SetWindowPos ControlName.hwnd, ControlName.Parent.hwnd, 0, 0, 0, 0, SWP\_NOZORDER Or SWP\_NOSIZE Or SWP\_NOMOVE Or SWP\_DRAWFRAME  
> End Sub  
> '用法：ControlCaption picture1,true:ControlSysmenu picture1,true