Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
createwindowex (user32)
 
.
Summary
Creates a window with an extended window style.

C# Signature:

/// <summary>
/// The CreateWindowEx function creates an overlapped, pop-up, or child window with an extended window style; otherwise, this function is identical to the CreateWindow function.
/// </summary>
/// <param name="dwExStyle">Specifies the extended window style of the window being created.</param>
/// <param name="lpClassName">Pointer to a null-terminated string or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero. If lpClassName is a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, provided that the module that registers the class is also the module that creates the window. The class name can also be any of the predefined system class names.</param>
/// <param name="lpWindowName">Pointer to a null-terminated string that specifies the window name. If the window style specifies a title bar, the window title pointed to by lpWindowName is displayed in the title bar. When using CreateWindow to create controls, such as buttons, check boxes, and static controls, use lpWindowName to specify the text of the control. When creating a static control with the SS_ICON style, use lpWindowName to specify the icon name or identifier. To specify an identifier, use the syntax "#num". </param>
/// <param name="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.</param>
/// <param name="x">Specifies the initial horizontal position of the window. For an overlapped or pop-up window, the x parameter is the initial x-coordinate of the window's upper-left corner, in screen coordinates. For a child window, x is the x-coordinate of the upper-left corner of the window relative to the upper-left corner of the parent window's client area. If x is set to CW_USEDEFAULT, the system selects the default position for the window's upper-left corner and ignores the y parameter. CW_USEDEFAULT is valid only for overlapped windows; if it is specified for a pop-up or child window, the x and y parameters are set to zero.</param>
/// <param name="y">Specifies the initial vertical position of the window. For an overlapped or pop-up window, the y parameter is the initial y-coordinate of the window's upper-left corner, in screen coordinates. For a child window, y is the initial y-coordinate of the upper-left corner of the child window relative to the upper-left corner of the parent window's client area. For a list box y is the initial y-coordinate of the upper-left corner of the list box's client area relative to the upper-left corner of the parent window's client area.
/// <para>If an overlapped window is created with the WS_VISIBLE style bit set and the x parameter is set to CW_USEDEFAULT, then the y parameter determines how the window is shown. If the y parameter is CW_USEDEFAULT, then the window manager calls ShowWindow with the SW_SHOW flag after the window has been created. If the y parameter is some other value, then the window manager calls ShowWindow with that value as the nCmdShow parameter.</para></param>
/// <param name="nWidth">Specifies the width, in device units, of the window. For overlapped windows, nWidth is the window's width, in screen coordinates, or CW_USEDEFAULT. If nWidth is CW_USEDEFAULT, the system selects a default width and height for the window; the default width extends from the initial x-coordinates to the right edge of the screen; the default height extends from the initial y-coordinate to the top of the icon area. CW_USEDEFAULT is valid only for overlapped windows; if CW_USEDEFAULT is specified for a pop-up or child window, the nWidth and nHeight parameter are set to zero.</param>
/// <param name="nHeight">Specifies the height, in device units, of the window. For overlapped windows, nHeight is the window's height, in screen coordinates. If the nWidth parameter is set to CW_USEDEFAULT, the system ignores nHeight.</param> <param name="hWndParent">Handle to the parent or owner window of the window being created. To create a child window or an owned window, supply a valid window handle. This parameter is optional for pop-up windows.
/// <para>Windows 2000/XP: To create a message-only window, supply HWND_MESSAGE or a handle to an existing message-only window.</para></param>
/// <param name="hMenu">Handle to a menu, or specifies a child-window identifier, depending on the window style. For an overlapped or pop-up window, hMenu identifies the menu to be used with the window; it can be NULL if the class menu is to be used. For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.</param>
/// <param name="hInstance">Handle to the instance of the module to be associated with the window.</param> <param name="lpParam">Pointer to a value to be passed to the window through the CREATESTRUCT structure (lpCreateParams member) pointed to by the lParam param of the WM_CREATE message. This message is sent to the created window by this function before it returns.
/// <para>If an application calls CreateWindow to create a MDI client window, lpParam should point to a CLIENTCREATESTRUCT structure. If an MDI client window calls CreateWindow to create an MDI child window, lpParam should point to a MDICREATESTRUCT structure. lpParam may be NULL if no additional data is needed.</para></param>
/// <returns>If the function succeeds, the return value is a handle to the new window.
/// <para>If the function fails, the return value is NULL. To get extended error information, call GetLastError.</para>
/// <para>This function typically fails for one of the following reasons:</para>
/// <list type="">
/// <item>an invalid parameter value</item>
/// <item>the system class was registered by a different module</item>
/// <item>The WH_CBT hook is installed and returns a failure code</item>
/// <item>if one of the controls in the dialog template is not registered, or its window window procedure fails WM_CREATE or WM_NCCREATE</item>
/// </list></returns>

[DllImport("user32.dll", SetLastError=true)]
public static extern IntPtr CreateWindowEx(
   WindowStylesEx dwExStyle,
   string lpClassName,
   string lpWindowName,
   WindowStyles dwStyle,
   int x,
   int y,
   int nWidth,
   int nHeight,
   IntPtr hWndParent,
   IntPtr hMenu,
   IntPtr hInstance,
   IntPtr lpParam);

VB.NET Signature:

   ''' <summary>
''' The CreateWindowEx function creates an overlapped, pop-up, or child window with an extended window style; otherwise, this function is identical to the CreateWindow function.
''' </summary>
''' <param name="dwExStyle">Specifies the extended window style of the window being created.</param>
''' <param name="lpClassName">Pointer to a null-terminated string or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero. If lpClassName is a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, provided that the module that registers the class is also the module that creates the window. The class name can also be any of the predefined system class names.</param>
''' <param name="lpWindowName">Pointer to a null-terminated string that specifies the window name. If the window style specifies a title bar, the window title pointed to by lpWindowName is displayed in the title bar. When using CreateWindow to create controls, such as buttons, check boxes, and static controls, use lpWindowName to specify the text of the control. When creating a static control with the SS_ICON style, use lpWindowName to specify the icon name or identifier. To specify an identifier, use the syntax "#num". </param>
''' <param name="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.</param>
''' <param name="x">Specifies the initial horizontal position of the window. For an overlapped or pop-up window, the x parameter is the initial x-coordinate of the window's upper-left corner, in screen coordinates. For a child window, x is the x-coordinate of the upper-left corner of the window relative to the upper-left corner of the parent window's client area. If x is set to CW_USEDEFAULT, the system selects the default position for the window's upper-left corner and ignores the y parameter. CW_USEDEFAULT is valid only for overlapped windows; if it is specified for a pop-up or child window, the x and y parameters are set to zero.</param>
''' <param name="y">Specifies the initial vertical position of the window. For an overlapped or pop-up window, the y parameter is the initial y-coordinate of the window's upper-left corner, in screen coordinates. For a child window, y is the initial y-coordinate of the upper-left corner of the child window relative to the upper-left corner of the parent window's client area. For a list box y is the initial y-coordinate of the upper-left corner of the list box's client area relative to the upper-left corner of the parent window's client area.
''' <para>If an overlapped window is created with the WS_VISIBLE style bit set and the x parameter is set to CW_USEDEFAULT, then the y parameter determines how the window is shown. If the y parameter is CW_USEDEFAULT, then the window manager calls ShowWindow with the SW_SHOW flag after the window has been created. If the y parameter is some other value, then the window manager calls ShowWindow with that value as the nCmdShow parameter.</para></param>
''' <param name="nWidth">Specifies the width, in device units, of the window. For overlapped windows, nWidth is the window's width, in screen coordinates, or CW_USEDEFAULT. If nWidth is CW_USEDEFAULT, the system selects a default width and height for the window; the default width extends from the initial x-coordinates to the right edge of the screen; the default height extends from the initial y-coordinate to the top of the icon area. CW_USEDEFAULT is valid only for overlapped windows; if CW_USEDEFAULT is specified for a pop-up or child window, the nWidth and nHeight parameter are set to zero.</param>
''' <param name="nHeight">Specifies the height, in device units, of the window. For overlapped windows, nHeight is the window's height, in screen coordinates. If the nWidth parameter is set to CW_USEDEFAULT, the system ignores nHeight.</param> <param name="hWndParent">Handle to the parent or owner window of the window being created. To create a child window or an owned window, supply a valid window handle. This parameter is optional for pop-up windows.
''' <para>Windows 2000/XP: To create a message-only window, supply HWND_MESSAGE or a handle to an existing message-only window.</para></param>
''' <param name="hMenu">Handle to a menu, or specifies a child-window identifier, depending on the window style. For an overlapped or pop-up window, hMenu identifies the menu to be used with the window; it can be NULL if the class menu is to be used. For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.</param>
''' <param name="hInstance">Handle to the instance of the module to be associated with the window.</param> <param name="lpParam">Pointer to a value to be passed to the window through the CREATESTRUCT structure (lpCreateParams member) pointed to by the lParam param of the WM_CREATE message. This message is sent to the created window by this function before it returns.
''' <para>If an application calls CreateWindow to create a MDI client window, lpParam should point to a CLIENTCREATESTRUCT structure. If an MDI client window calls CreateWindow to create an MDI child window, lpParam should point to a MDICREATESTRUCT structure. lpParam may be NULL if no additional data is needed.</para></param>
''' <returns>If the function succeeds, the return value is a handle to the new window.
''' <para>If the function fails, the return value is NULL. To get extended error information, call GetLastError.</para>
''' <para>This function typically fails for one of the following reasons:</para>
''' <list type="">
''' <item>an invalid parameter value</item>
''' <item>the system class was registered by a different module</item>
''' <item>The WH_CBT hook is installed and returns a failure code</item>
''' <item>if one of the controls in the dialog template is not registered, or its window window procedure fails WM_CREATE or WM_NCCREATE</item>
''' </list></returns>
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function CreateWindowEx( _
     ByVal dwExStyle As WindowStylesEx, _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String, _
     ByVal dwStyle As WindowStyles, _
     ByVal x As Integer, _
     ByVal y As Integer, _
     ByVal nWidth As Integer, _
     ByVal nHeight As Integer, _
     ByVal hWndParent As IntPtr, _
     ByVal hMenu As IntPtr, _
     ByVal hInstance As IntPtr, _
     ByVal lpParam As IntPtr) As IntPtr
End Function

User-Defined Types:

WindowStylesEx, WindowStyles

Notes:

Returns a handle for the window created.

Warning - if you are creating a window management class from scratch:

When you call this function, the WndProc function must respond to the WM_NCCREATE message by returning TRUE. If it does not, the creation process will fail. A null handle will be returned from CreateWindowEx and GetLastError will return 0. See MSDN on WM_NCCREATE (http://msdn.microsoft.com/en-us/library/ms632635.aspx) and also WM_CREATE (http://msdn.microsoft.com/en-us/library/ms632619.aspx). You can have your WndProc call DefWindowProc, which will take care of this issue.

Tips & Tricks:

This method should have at least two versions: one where lpClassName is string, and other in which is IntPtr.

I sometimes get error 1407 ("Cannot find window class.") when trying to create the window (after a successful call to RegisterClass)

Solution is to pass in the second version the class as new IntPtr((int)(uint)regResult), where regResult is the result from RegisterClass

(See sample code)

Sample Code:

This is a sample HelloWin ported from Petzolds book (chapter 3) to illustrate the IntPtr issue for classname on CreateWindow

(Win32 is a class containing Win32 APIs created with this wiki):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using WindowsInterop;

namespace HelloWin
{
    class Program
    {
        static void Main(string[] args)
        {
            IntPtr hInstance = Process.GetCurrentProcess().Handle;
            string szAppName = "HelloWin";

            WNDCLASS wndclass;

            wndclass.style = ClassStyles.HorizontalRedraw | ClassStyles.VerticalRedraw;
            wndclass.lpfnWndProc = (WndProc)((hWnd, message, wParam, lParam ) => {
                IntPtr hdc;
                PAINTSTRUCT ps;
                RECT rect;

                switch ((WM)message)
                {
                    case WM.PAINT:
                        hdc = Win32.BeginPaint (hWnd, out ps) ;
                        Win32.GetClientRect (hWnd, out rect) ;

                        Win32.DrawText (hdc, "Hello, Windows 98!", -1, ref rect,
                            Win32.DT_SINGLELINE | Win32.DT_CENTER | Win32.DT_VCENTER );

                        Win32.EndPaint(hWnd, ref ps);
                        return IntPtr.Zero;
                        break;
                    case WM.DESTROY:
                        Win32.PostQuitMessage(0);
                        return IntPtr.Zero;
                        break;
                }

                return Win32.DefWindowProc(hWnd, (WM)message, wParam, lParam);
            });

            wndclass.cbClsExtra = 0;
            wndclass.cbWndExtra = 0;
            wndclass.hInstance = hInstance;
            wndclass.hIcon = Win32.LoadIcon(IntPtr.Zero, new IntPtr((int)SystemIcons.IDI_APPLICATION));
            wndclass.hCursor = Win32.LoadCursor(IntPtr.Zero,(int)IdcStandardCursors.IDC_ARROW);
            wndclass.hbrBackground = Win32.GetStockObject(StockObjects.WHITE_BRUSH);
            wndclass.lpszMenuName = null;
            wndclass.lpszClassName = szAppName;

            ushort regResult = Win32.RegisterClass(ref wndclass);

            if (regResult == 0)
            {
                Win32.MessageBox(0, "This program requires Windows NT!", szAppName, MessageBoxOptions.IconError);
                return;
            }

            IntPtr hwnd = Win32.CreateWindowEx(
                WindowStylesEx.WS_EX_OVERLAPPEDWINDOW,
                new IntPtr((int)(uint)regResult),
                //szAppName, // window class name
                "The Hello Program", // window caption
                WindowStyles.WS_OVERLAPPEDWINDOW, // window style
                Win32.CW_USEDEFAULT, // initial x position
                Win32.CW_USEDEFAULT, // initial y position
                Win32.CW_USEDEFAULT, // initial x size
                Win32.CW_USEDEFAULT, // initial y size
                IntPtr.Zero, // parent window handle
                IntPtr.Zero, // window menu handle
                hInstance, // program instance handle
                IntPtr.Zero); // creation parameters

            if( hwnd == IntPtr.Zero )
            {
                int lastError = Marshal.GetLastWin32Error();
                string errorMessage = new Win32Exception(lastError).Message;
            }

            Win32.ShowWindow(hwnd, ShowWindowCommands.Normal );
            Win32.UpdateWindow(hwnd);

            MSG msg;
            while (Win32.GetMessage( out msg, IntPtr.Zero, 0, 0) != 0)
            {
                Win32.TranslateMessage( ref msg);
                Win32.DispatchMessage( ref msg);
            }

            return;
        }
    }
}

Alternative Managed API:

Instantiate a System.Windows.Forms.Form and set its properties to control its style.

The System.Windows.Forms.NativeWindow class can be used to encapsulate a window handle. It provides properties and methods that can be used to perform the same behavior as the CreateWindowEx method.

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions