CreateWindowEx Function

Declare Function CreateWindowEx Lib "user32.dll" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long

Platforms

Description & Usage

CreateWindowEx creates a new window. The window can be a "regular" (overlapped) window, a control on another window, or a popup window.

Return Value

If successful, the function returns a handle to the newly created window. If an error occured, the function returns zero (use GetLastError to get the error code).

Visual Basic-Specific Issues

None.

Parameters

dwExStyle
A combination of extended styles to give the window.
lpClassName
The name of the class to create the window using. This class must have previously been registered using RegisterClassEx, InitCommonControlsEx, or some other function that registers window classes.
lpWindowName
The name to give the newly created window. For controls, this text will be the initial content of the control. For regular windows, this text appears in the window's title bar.
dwStyle
A combination of the window styles to give the window. This is normally a combination of base window styles and styles that are specific to the window's class.
x
The initial x-coordinate of the upper-left corner of the window to create. If this window is a child of another window, this coordinate is relative to its parent window, otherwise it is relative to the screen. If this is CW_USEDEFAULT (for an overlapped window only), y will be ignored and the window will be placed in a default position.
y
The initial y-coordinate of the upper-left corner of the window to create. It is interpreted in the same way as x. This parameter is ignored if x is set to CW_USEDEFAULT.
nWidth
The initial width of the window. If this is CW_USEDEFAULT (for overlapped windows only), nHeight will be ignored and the window will be given a default size.
nHeight
The initial height of the window. If x is set to CW_USEDEFAULT, this parameter will be ignored.
hWndParent
A handle to the parent of the window to create. If the window does not have a parent, this should be zero. Windows 2000: Set this parameter to HWND_MESSAGE to create a message-only window.
hMenu
A handle to a menu to assign to the window. For child windows, this is the child-window identifier, used to notify its parent about events. To not give the window a menu, set this parameter to zero.
hInstance
A handle to the instance of the module or program that owns the window.
lParam
An additional value to associate with the window, used in the window creation messages.

Constant Definitions

Const CW_USEDEFAULT = &H80000000
Const HWND_BROADCAST = &HFFFF

Example

Create an IP Address control and use it to prompt the user for an IP address. When the user clicks button cmdGetDomain, the program looks up the first domain name assigned to that address.

To use this example, place a command button named cmdGetDomain on a form window. The IP Address control is created and destroyed by invoking API functions directly and does not need to be placed on the form beforehand.

' This code is licensed according to the terms and conditions listed here.

' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type WSADATA
	wVersion As Integer
	wHighVersion As Integer
	szDescription As String * 257
	szSystemStatus As String * 129
	iMaxSockets As Long
	iMaxUdpDg As Long
	lpVendorInfo As Long
End Type 
Public Declare Function WSAStartup Lib "wsock32.dll" (ByVal wVersionRequested As Integer, lpWSAData _
	As WSADATA) As Long
Public Declare Function WSACleanup Lib "wsock32.dll" () As Long
Public Type HOSTENT
	h_name As Long
	h_aliases As Long
	h_addrtype As Integer
	h_length As Integer
	h_addr_list As Long
End Type
Public Const AF_INET = 2
Public Declare Function htonl Lib "wsock32.dll" (ByVal hostlong As Long) As Long
Public Declare Function gethostbyaddr Lib "wsock32.dll" (addr As Long, ByVal length As Long, ByVal _
	protocol As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source _
	As Any, ByVal length As Long)
Public Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Any) As Long
Public Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As Any, ByVal _
	lpString2 As Any) As Long
Public Type INITCOMMONCONTROLSEX_TYPE
	dwSize As Long
	dwICC As Long
End Type
Public Declare Function InitCommonControlsEx Lib "comctl32.dll" (lpInitCtrls As _
	INITCOMMONCONTROLSEX_TYPE) As Long
Public Const ICC_INTERNET_CLASSES = &H800
Public Declare Function CreateWindowEx Lib "user32.dll" Alias "CreateWindowExA" (ByVal dwExStyle As Long, _
	ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x _
	As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As Long, _
	ByVal hMenu As Long, ByVal hInstance As Long, lpParam As Any) As Long
Public Const WC_IPADDRESS = "SysIPAddress32"
Public Const WS_CHILD = &H40000000
Public Const WS_VISIBLE = &H10000000
Public Declare Function DestroyWindow Lib "user32.dll" (ByVal hWnd As Long) As Long
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg _
	As Long, wParam As Any, lParam As Any) As Long
Public Const IPM_ISBLANK = &H469
Public Const IPM_GETADDRESS = &H466

' *** Place the following code in a form window. ***

Private hIPControl As Long  ' handle to the IP Address control

' When the form is initialized, create an IP Address control in the
' upper-left corner of the form.
Private Sub Form_Initialize()
	Dim comctls As INITCOMMONCONTROLSEX_TYPE  ' identifies the control to register
	Dim retval As Long                        ' generic return value
	
	' Register the IP Address control window class.
	With comctls
		.dwSize = Len(comctls)
		.dwICC = ICC_INTERNET_CLASSES
	End With
	retval = InitCommonControlsEx(comctls)
	
	' Create the IP Address control in the corner of the window.
	hIPControl = CreateWindowEx(0, WC_IPADDRESS, "", WS_CHILD Or WS_VISIBLE, 0, 0, 125, 20, _
		Me.hWnd, 0, App.hInstance, ByVal CLng(0))
End Sub

' Destroy the IP Address control when the form closes.
Private Sub Form_Unload(Cancel As Integer)
	Dim retval As Long  ' return value
	
	retval = DestroyWindow(hIPControl)
End Sub

' Look up the primary domain name of the host computer identified by the
' address in the IP Address control.
Private Sub cmdGetDomain_Click()
	Dim ipAddress_h As Long   ' the IP address, in host byte order
	Dim ipAddress_n As Long   ' the IP address, in network byte order
	Dim sockinfo As WSADATA   ' information about the Winsock implementation
	Dim pHostinfo As Long     ' pointer to information about the host computer
	Dim hostinfo As HOSTENT   ' information about the host computer
	Dim domainName As String  ' the primary domain name of the host computer
	Dim retval As Long        ' generic return value
	
	' Verify that an IP address was entered.
	retval = SendMessage(hIPControl, IPM_ISBLANK, ByVal CLng(0), ByVal CLng(0))
	If retval <> 0 Then
		Debug.Print "No IP address was entered!"
		Exit Sub
	End If
	
	' Get the IP address entered by the user and verify that all
	' four fields in the address were entered.
	retval = SendMessage(hIPControl, IPM_GETADDRESS, ByVal CLng(0), ipAddress_h)
	If retval < 4 Then
		Debug.Print "An incomplete IP address was entered!"
		Exit Sub
	End If
	
	' Open up a Winsock v2.2 session.
	retval = WSAStartup(&H202, sockinfo)
	If retval <> 0 Then
		Debug.Print "ERROR: Attempt to open Winsock failed: error"; retval
		Exit Sub
	End If
	
	' Convert the IP address into network byte order.
	ipAddress_n = htonl(ipAddress_h)
	' Get information about the host computer.
	pHostinfo = gethostbyaddr(ipAddress_n, 4, AF_INET)
	If pHostInfo = 0 Then
		Debug.Print "Could not find a host with the specified IP address."
	Else
		' Copy the data into the structure.
		CopyMemory hostinfo, ByVal pHostinfo, Len(hostinfo)
		' Copy the host domain name into a string.
		domainName = Space(lstrlen(hostinfo.h_name))
		retval = lstrcpy(domainName, hostinfo.h_name)
		Debug.Print "Domain name is: "; domainName
	End If
	
	' End the Winsock session.
	retval = WSACleanup()
End Sub

See Also

DestroyWindow

Category

Windows

Back to the Function list.
Back to the Reference section.


Last Modified: October 29, 2000
This page is copyright © 2000 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/c/createwindowex.html