Exportar (0) Imprimir
Expandir Tudo
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
Tradução
Original

NativeWindow.CreateHandle Método

Cria uma janela e seu identificador com os parâmetros de criação especificado.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (em System.Windows.Forms. dll)

public virtual void CreateHandle(
	CreateParams cp
)

Parâmetros

cp
Tipo: System.Windows.Forms.CreateParams
A CreateParams that specifies the creation parameters for this window.

ExceçãoCondição
OutOfMemoryException

O sistema operacional ficou sem recursos ao tentar criar a janela nativa.

Win32Exception

A API do Win32 nativo não pôde criar a janela especificada.

InvalidOperationException

The handle of the current native window is already assigned; in explanation, the Handle property is not equal to Zero.

The cp parameter specifies the values that are passed to the native Win32 CreateWindowEx method to create a window and its handle.

When the ClassName field is not null, the newly created window handle inherits from the specified class.For example, if ClassName is set to BUTTON, the newly created window is based on the Win32 BUTTON window class.The Param property of the ClassName object must either be null or reference an instance of a class that was declared as a structure.

This code is an excerpt from the example shown in the NativeWindow class overview.Algum código não é mostrado com a finalidade de concisão.See NativeWindow for the whole code listing.

ObservaçãoObservação:

The Nome de Classe provided is Registered with the sistema operacional.

O exemplo de código a seguir demonstra criando uma janela com um nome de classe janela específica do sistema operacional.The example creates a class that inherits from NativeWindow to accomplish this.

The MyNativeWindow class creates a new window with the ClassName set to BUTTON.Isso cria uma janela botão Win32.O local e o tamanho do botão for definido, juntamente com especificar estilos de janela adicionais.The class demonstrates how to use the CreateHandle method and override the WndProc method to intercept window messages that are received.Embora o exemplo procurará a mensagem WM_ACTIVATEAPP, isso pode ser substituído em um programa real com mensagens janela específicas para o tipo criado.

ObservaçãoObservação:

Alguns tipos de Controlarar enviam suas mensagens janela a janela pai em vez da janela.Consulte o SDK da plataforma do Windows para obter mais informações.

// MyNativeWindow class to create a window given a class name.
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
internal class MyNativeWindow : NativeWindow
{

    // Constant values were found in the "windows.h" header file.
    private const int WS_CHILD = 0x40000000,
                      WS_VISIBLE = 0x10000000,
                      WM_ACTIVATEAPP = 0x001C;

    private int windowHandle;

    public MyNativeWindow(Form parent)
    {

        CreateParams cp = new CreateParams();

        // Fill in the CreateParams details.
        cp.Caption = "Click here";
        cp.ClassName = "Button";

        // Set the position on the form
        cp.X = 100;
        cp.Y = 100;
        cp.Height = 100;
        cp.Width = 100;

        // Specify the form as the parent.
        cp.Parent = parent.Handle;

        // Create as a child of the specified parent
        cp.Style = WS_CHILD | WS_VISIBLE;

        // Create the actual window
        this.CreateHandle(cp);
    }

    // Listen to when the handle changes to keep the variable in sync
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    protected override void OnHandleChange()
    {
        windowHandle = (int)this.Handle;
    }

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    protected override void WndProc(ref Message m)
    {
        // Listen for messages that are sent to the button window. Some messages are sent
        // to the parent window instead of the button's window.

        switch (m.Msg)
        {
            case WM_ACTIVATEAPP:
                // Do something here in response to messages
                break;
        }
        base.WndProc(ref m);
    }
}


// MyNativeWindow class to create a window given a class name.
/** @attribute SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)
 */
public class MyNativeWindow extends NativeWindow
{
    // Constant values were found in the "windows.h" header file.
    private int WS_CHILD = 0x40000000;
    private int WS_VISIBLE = 0x10000000;
    private int WM_ACTIVATEAPP = 0x1C;
    private int windowHandle;

    public MyNativeWindow(Form parent)
    {
        CreateParams cp = new CreateParams();

        // Fill in the CreateParams details.
        cp.set_Caption("Click here");
        cp.set_ClassName("Button");

        // Set the position on the form
        cp.set_X(100);
        cp.set_Y(100);
        cp.set_Height(100);
        cp.set_Width(100);

        // Specify the form as the parent.
        cp.set_Parent(parent.get_Handle());

        // Create as a child of the specified parent
        cp.set_Style(WS_CHILD | WS_VISIBLE);

        // Create the actual window
        this.CreateHandle(cp);
    } //MyNativeWindow

    // Listen to when the handle changes to keep the variable in sync
    protected void OnHandleChange()
    {
        windowHandle = this.get_Handle().ToInt32();
    } //OnHandleChange

    protected void WndProc(Message m)
    {
        // Listen for messages that are sent to the button window. 
        // Some messages are sent to the parent window 
        // instead of the button's window.
        if (m.get_Msg() == WM_ACTIVATEAPP) {
            // Do something here in response to messages
        }
        super.WndProc(m);
    } //WndProc
} //MyNativeWindow


Contribuições da comunidade

ADICIONAR
Mostrar:
© 2015 Microsoft