Tech Support > Microsoft Windows > Development Resources > Unable to create window after registering a window.
Unable to create window after registering a window.
Posted by vidishasharma@gmail.com on May 9th, 2008


Hi I am trying to register a window as follows(I am doing this so that
I get empty window and I can add controls into it as I desire)

wc = new WNDCLASSEX();
//wc.hInstance =
Marshal.GetHINSTANCE(this.GetType().Module);
wc.lpszClassName = "TestWindow";
wc.lpfnWndProc = wndProcDelegate1;
wc.cbSize = Marshal.SizeOf(typeof(WNDCLASSEX));
wc.hbrBackground = GetStockObject(BLACK_BRUSH);
atom = RegisterClassEx(ref wc);

This is how I am registering I get soem value of atom

Then I register the window using

IntPtr FloatingWindow = CreateWindowEx(WS_EX_TOPMOST, atom,
wc.lpszClassName, WS_CHILD | WS_OVERLAPPEDWINDOW | WS_VISIBLE,
readingpane.X - 200, readingpane.Y, 200, 200, _windowLike,
IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

However I get my window handle as zero.


This is what I have used to createwindow

[DllImport("user32.dll")]
static extern IntPtr CreateWindowEx(
uint dwExStyle,
UInt16 lpClassName,
string lpWindowName,
uint dwStyle,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hWndParent,
IntPtr hMenu,
IntPtr hInstance,
IntPtr lpParam);


Please let mw know what wrong I am doing.

Posted by vidishasharma@gmail.com on May 9th, 2008


I Get some value in atom however if use the following api for checkin
bool check=GetClassInfoEx(IntPtr.Zero, "TestWindow", ref wc1);

I get false meaning that class is not getting created properly.

Can somebody guide what wrong I am doing.....

Posted by mark on May 9th, 2008


vidishasharma@gmail.com wrote:
is your wndProcDelegate1 callback right ?

Posted by vidishasharma@gmail.com on May 10th, 2008


private delegate IntPtr ChildWndProcDelegate(IntPtr hwnd, int Msg,
IntPtr wParam, IntPtr lParam);

This is how I define that
static private ChildWndProcDelegate wndProcDelegate1=new
ChildWndProcDelegate(this.ScreenSaverProc);

This is the defination for ScreenSaverProc


private IntPtr ScreenSaverProc(IntPtr hWnd, int msg, IntPtr wParam,
IntPtr lParam)
{
System.Windows.Forms.Message m =
System.Windows.Forms.Message.Create(hWnd, msg, wParam, lParam);
// MessageBox.Show("win 32 api " + msg);
ScreenSaverProc(ref m);
if (m.Msg == WM_MOUSEMOVE)
{
if (count == 0)
{

CreateFloatingWindow();
count++;
}
}
if (m.Msg == WM_MOUSEHOVER)
{
MessageBox.Show("Mouse Hover");
}

if (m.Msg == BN_CLICKED)
{
MessageBox.Show("button clicked");
}

if (m.Msg == WM_MOUSELEAVE)
{
MessageBox.Show("Mouse Leave");
}
if (m.Msg == WM_LBUTTONDOWN)
{
//MessageBox.Show("Mouse Left button down");
}

if (m.Msg == WM_LBUTTONUP)
{
MessageBox.Show("Mouse Left button up");
}
return m.Result;
}


protected virtual void ScreenSaverProc(ref
System.Windows.Forms.Message msg)
{
///MessageBox.Show("win 32 api " + msg.Msg);


DefScreenSaverProc(ref msg);
}


public void DefScreenSaverProc(ref
System.Windows.Forms.Message m)
{
m.Result = CallWindowProc(oldWndFunc, m.HWnd, m.Msg,
m.WParam, m.LParam);
}



Is there something which I am doing wrong....

Posted by vidishasharma@gmail.com on May 10th, 2008



Hi my class is not getting registered properly

With Marshal.GetLastWin32Error(); I am getting error as "Class does
not exist"

I am using following definations

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct WNDCLASS
{
[MarshalAs(UnmanagedType.U4)]
public int style;
public WndProc lpfnwndproc;
public int cbClsextra;
public int cbWndExtra2;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public string lpszMenuName;
public string lpszClassName;
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.U2)]
static extern UInt16 RegisterClassEx([In] ref WNDCLASSEX lpwcx);

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

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

IntPtr hinstance =
Marshal.GetHINSTANCE(System.Reflection.Assembly.Ge tExecutingAssembly().GetModule("ConsoleApplication 1.exe"));
wndProc = new WndProc(ScreenSaverProc);
wc = new WNDCLASSEX();
wc.hInstance = hinstance;
wc.style = 11;
wc.lpszClassName = "HelloWin";
wc.lpfnWndProc = wndProc;
wc.cbSize = Marshal.SizeOf(typeof(WNDCLASSEX));
wc.hbrBackground = GetStockObject(BLACK_BRUSH);
atom = RegisterClassEx(ref wc);

IntPtr FloatingWindow = CreateWindowEx((uint)WS_EX_LEFT, atom,
wc.lpszClassName, WS_VISIBLE, 50, 50, 100, 100, IntPtr.Zero,
IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);


My procedures are as follows :

private static IntPtr ScreenSaverProc(IntPtr hWnd, uint msg, IntPtr
wParam, IntPtr lParam)
{
System.Windows.Forms.Message m =
System.Windows.Forms.Message.Create(hWnd, (int)msg, wParam, lParam);
ScreenSaverProc(ref m);
return m.Result;
}
protected static void ScreenSaverProc(ref System.Windows.Forms.Message
msg)
{
DefScreenSaverProc(ref msg);
}
public static void DefScreenSaverProc(ref System.Windows.Forms.Message
m)
{
m.Result = CallWindowProc(oldWndFunc, m.HWnd, m.Msg, m.WParam,
m.LParam);
}


Please guide me what wrong I am doing

Posted by vidishasharma@gmail.com on May 12th, 2008




Hi when I try to get Marshal.GetLastWin32Error() for
RegisterClassEx(ref wc); I get error code as 2 which means "The system
cannot find the file specified."

now I do not know which file is being refered here.

Please help.


Similar Posts