protected virtual void CreateHandle() { WinApi.User32.WNDCLASSEX windowClassEx = GetWindowClassEx(); if (WinApi.User32.RegisterClassEx(ref windowClassEx) == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } _hWnd = WinApi.User32.CreateWindowEx( StyleEx, windowClassEx.lpszClassName, Text, Style, _rect.X, _rect.Y, _rect.Width, _rect.Height, IntPtr.Zero, IntPtr.Zero, windowClassEx.hInstance, IntPtr.Zero); if (_hWnd == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } }
protected virtual WinApi.User32.WNDCLASSEX GetWindowClassEx() { WinApi.User32.WNDCLASSEX wndClassEx = new WinApi.User32.WNDCLASSEX(); wndClassEx.cbSize = WinApi.User32.WNDCLASSEX.SizeOf; wndClassEx.style = ClassStyle; wndClassEx.lpfnWndProc = WndProcInternal; wndClassEx.cbClsExtra = 0; wndClassEx.cbWndExtra = 0; wndClassEx.hInstance = _moduleHandle; wndClassEx.hIcon = Icon; wndClassEx.hIconSm = SmallIcon; wndClassEx.hCursor = Cursor; wndClassEx.hbrBackground = BackgroundBrush; wndClassEx.lpszClassName = "LPWindowClass_" + GetHashCode(); return(wndClassEx); }
static void Main(string[] args) { IntPtr handle = Process.GetCurrentProcess().MainWindowHandle; SetConsoleMode(handle, ENABLE_EXTENDED_FLAGS); const string appName = "Test App"; const string wndName = "Test App Window"; ApplicationContext applicationContext = new ApplicationContext(); Form form1 = new Form(applicationContext); form1.Text = "ololo"; form1.Text += "GG"; WinApi.RECT rect = form1.Rect; rect.Width = 300; rect.Height = 200; form1.Rect = rect; form1.Show(); form1.Text += "GG"; //Form form2 = new Form(applicationContext); //form2.Text = "ololo"; //form2.Text += "GG2"; //rect = form2.Rect; //rect.Width = 500; //rect.Height = 400; //form2.Rect = rect; //form2.Show(); applicationContext.Run(); return; IntPtr hThisInst = WinApi.Kernel32.GetModuleHandle(null); _wndProc = WndProc; WinApi.User32.WNDCLASSEX wndClassEx = new WinApi.User32.WNDCLASSEX(); wndClassEx.cbSize = WinApi.User32.WNDCLASSEX.SizeOf; wndClassEx.style = WinApi.User32.WindowClassStyles.CS_HREDRAW | WinApi.User32.WindowClassStyles.CS_VREDRAW; wndClassEx.lpfnWndProc = _wndProc; wndClassEx.cbClsExtra = 0; wndClassEx.cbWndExtra = 0; wndClassEx.hInstance = IntPtr.Zero; wndClassEx.hIcon = WinApi.User32.LoadIcon(WinApi.User32.IconId.IDI_SHIELD); wndClassEx.hIconSm = WinApi.User32.LoadIcon(WinApi.User32.IconId.IDI_SHIELD); wndClassEx.hCursor = WinApi.User32.LoadCursor(WinApi.User32.CursorId.IDC_ARROW); wndClassEx.hbrBackground = new IntPtr((int)WinApi.User32.SystemColorId.COLOR_WINDOW); wndClassEx.lpszClassName = "test app"; if (WinApi.User32.RegisterClassEx(ref wndClassEx) == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } _hWnd = WinApi.User32.CreateWindowEx( WinApi.User32.WindowStylesEx.WS_EX_TOPMOST, "test app", wndName, WinApi.User32.WindowStyles.WS_VISIBLE | WinApi.User32.WindowStyles.WS_OVERLAPPEDWINDOW, 200, 150, 300, 200, IntPtr.Zero, IntPtr.Zero, hThisInst, IntPtr.Zero); if (_hWnd == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } WinApi.MSG message; bool isExit = false; //while (!isExit) { //while (WinApi.User32.PeekMessage(out message, IntPtr.Zero, 0, 0, WinApi.User32.PeekMessageOptions.PM_NOREMOVE)) { while (WinApi.User32.GetMessage(out message, _hWnd, 0, 0) > 0) { WinApi.User32.TranslateMessage(ref message); WinApi.User32.DispatchMessage(ref message); } //} Console.WriteLine("end"); Console.ReadLine(); }