mercredi 5 août 2015

How to set the class name for a form while creating?


EDIT:

I created an application using a windows forms. The application is having two text boxes to accept Username and password. Here my requirement is to have my own class name to a form instead of default which will be generated by VS. I observed these class name parameter using winspy++ tool. I observed my application is having class name as " WindowsForms10.Window.8.app.0.bf7d44_r29_ad1" . I want to have my own class name for a form. How can I achieve this.

I am adding snapshot here:enter image description here

In above figure I marked my present class name (default ) with red colour. My requirement is similar to the one, which is marked with green colour.

EDIT:

I tried with following code:::: Form1.cs-->

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
using System.Windows.Forms;
namespace classidtrail
{
public partial class Form1 : Form
{


    delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    [System.Runtime.InteropServices.StructLayout(
        System.Runtime.InteropServices.LayoutKind.Sequential,
       CharSet = System.Runtime.InteropServices.CharSet.Unicode
    )]
    struct WNDCLASS
    {
        public uint style;
        public IntPtr lpfnWndProc;
        public int cbClsExtra;
        public int cbWndExtra;
        public IntPtr hInstance;
        public IntPtr hIcon;
        public IntPtr hCursor;
        public IntPtr hbrBackground;
        [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
        public string lpszMenuName;
        [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
        public string lpszClassName;
    }

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern System.UInt16 RegisterClassW(
        [System.Runtime.InteropServices.In] ref WNDCLASS lpWndClass
    );

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr CreateWindowExW(
       UInt32 dwExStyle,
       [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
   string lpClassName,
       [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
   string lpWindowName,
       UInt32 dwStyle,
       Int32 x,
       Int32 y,
       Int32 nWidth,
       Int32 nHeight,
       IntPtr hWndParent,
       IntPtr hMenu,
       IntPtr hInstance,
       IntPtr lpParam
    );

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern System.IntPtr DefWindowProcW(
        IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam
    );

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern bool DestroyWindow(
        IntPtr hWnd
    );

    private const int ERROR_CLASS_ALREADY_EXISTS = 1410;

    private bool m_disposed;
    private IntPtr m_hwnd;public UInt16 class_atom;
    public Form1()
    {
        // Create WNDCLASS
        m_wnd_proc_delegate = CustomWndProc;
        WNDCLASS wind_class = new WNDCLASS();
        wind_class.lpszClassName = "hyd";
        wind_class.lpfnWndProc = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(m_wnd_proc_delegate);

        class_atom= RegisterClassW(ref wind_class);

        int last_error = System.Runtime.InteropServices.Marshal.GetLastWin32Error();

        if (class_atom == 0 && last_error != ERROR_CLASS_ALREADY_EXISTS)
        {
            throw new System.Exception("Could not register window class");
        }

        InitializeComponent();
    }
     protected override CreateParams CreateParams
    {
        get
        {
            CreateParams c = base.CreateParams;
            c.ClassName = class_atom.ToString();
            return c;
        }

    }
       private WndProc m_wnd_proc_delegate;
 }

Program.cs--->

using System;
using System.Collections.Generic;
using System.Linq;
 using System.Windows.Forms;

  namespace classidtrail
   {
   static class Program
   {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
    }
  }

If I use above code am getting exception as "Window class name is not valid" at Application.Run(new Form1()) line in Program.cs. How can resolve this?

Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire