Hi expert:
i come from China,I would very appreciate for your help!
I am trying to learn of c# and windows api,so i implement api functions in c#.
now,in my first step,i want to create a window by CreateWindow,which must deal with struct
WNDCLASS and function RegisterClass;the error occurs at which i call function RegisterClass, is about parameter can not match to the function. the following is my code:
//const define area
public const long CS_VREDRAW = 0x1;
public const long CS_HREDRAW = 0x2;
public const long IDC_ARROW = 32512;
private const String myWindowClassName="Hello";
//WNDCLASS struct define
public struct WNDCLASS
{
public Int32 style;
public long lpfnwndproc;
public long cbClsextra;
public long cbWndExtra2;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public String lpszMenuName;
public String lpszClassName;
}
//api function RegisterClass
[DllImport("user32.dll",EntryPoint="RegisterClassA",CharSet=CharSet.Unicode)]
public static extern long RegisterClass(WNDCLASS Class);
[DllImport("user32.dll",EntryPoint="DefWindowProcA",CharSet=CharSet.Unicode)]
public static extern long DefWindowProc(IntPtr hWnd,long wMsg, long wParam, long lParam);
//main function
private bool MyRegisterClass()
{
WNDCLASS wndcls;
try
{
wndcls.style=(Int32)CS_HREDRAW+(Int32)CS_VREDRAW;
Type t=this.GetType();
MethodInfo m=t.GetMethod("MyWndProc");
wndcls.lpfnwndproc=(long)m.MethodHandle.Value;
wndcls.cbClsextra=0;
wndcls.cbWndExtra2=0;
wndcls.hInstance=(IntPtr)Marshal.GetHINSTANCE(System.Reflection.
Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32();
wndcls.hIcon=(IntPtr)0;
wndcls.hCursor=LoadCursor((IntPtr)0,IDC_ARROW);
wndcls.hbrBackground=(IntPtr)COLOR_WINDOW;
wndcls.lpszMenuName=null;
wndcls.lpszClassName=myWindowClassName;
return(RegisterClass(wndcls)!=0); //error occur here
}
catch(Exception e)
{
return false;
}
}
//windows message process procedure
public long MyWndProc(IntPtr hWnd,long message,long wParam,long lParam)
{
return DefWindowProc(hWnd,message,wParam,lParam);
}
and thank you for your help!