yidabu 2007-5-10 14:21
D语言GUI入门3 CreateWindowEx函数创建一个窗口
D语言GUI入门3 CreateWindowEx函数创建一个窗口知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070509点击下面网址查看原文:http://www.d-programming-language-china.org by: 孙鑫 D语言论坛 http://www.d-programming-language-china.org 增加D语言相关说明tag:CreateWindowExex失败,D语言学习,CreateWindowEx返回值CreateWindowEx函数参数详解 设计好窗口类并且将其成功注册之后,就可以用CreateWindowEx函数产生这种类型的窗口了。CreateWindowEx函数的原型声明如下: D语言论坛 http://www.d-programming-language-china.org 查了msdn,在这里: http://msdn2.microsoft.com/en-us/library/ms632680.aspx QUOTE: HWND CreateWindowEx( DWORD dwExStyle, LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam ); 1 dwExStyle dwExStyle是DWORD类型,bbs.d-programming-language-china.org 查了msdn: http://msdn2.microsoft.com/en-us/library/aa383751.aspx QUOTE: 32-bit unsigned integer. This type is declared in WinDef.h as follows: typedef unsigned long DWORD; 在D语言里32位无符号整数就是uint。 dwExStyle指定了扩展样式。 WS_EX_ACCEPTFILES 接受文件拖放 WS_EX_APPWINDOW Forces a top-level window onto the taskbar when the window is visible. WS_EX_CLIENTEDGE Specifies that a window has a border with a sunken edge. WS_EX_COMPOSITED Windows XP: Paints all descendants of a window in bottom-to-top painting order using double-buffering. For more information, see Remarks. This cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC. WS_EX_CONTEXTHELP Includes a question mark in the title bar of the window. When the user clicks the question mark, the cursor changes to a question mark with a pointer. If the user then clicks a child window, the child receives a WM_HELP message. The child window should pass the message to the parent window procedure, which should call the WinHelp function using the HELP_WM_HELP command. The Help application displays a pop-up window that typically contains help for the child window. WS_EX_CONTEXTHELP cannot be used with the WS_MAXIMIZEBOX or WS_MINIMIZEBOX styles.( 本文出处: http://www.d-programming-language-china.org ) WS_EX_CONTROLPARENT The window itself contains child windows that should take part in dialog box navigation. If this style is specified, the dialog manager recurses into children of this window when performing navigation operations such as handling the TAB key, an arrow key, or a keyboard mnemonic. WS_EX_DLGMODALFRAME Creates a window that has a double border; the window can, optionally, be created with a title bar by specifying the WS_CAPTION style in the dwStyle parameter. WS_EX_LAYERED Windows 2000/XP: Creates a layered window. Note that this cannot be used for child windows. Also, this cannot be used if the window has a class style of either CS_OWNDC or CS_CLASSDC. WS_EX_LAYOUTRTL Arabic and Hebrew versions of Windows 98/Me, Windows 2000/XP: Creates a window whose horizontal origin is on the right edge. Increasing horizontal values advance to the left. WS_EX_LEFT Creates a window that has generic left-aligned properties. This is the default. WS_EX_LEFTSCROLLBAR If the shell language is Hebrew, Arabic, or another language that supports reading order alignment, the vertical scroll bar (if present) is to the left of the client area. For other languages, the style is ignored. WS_EX_LTRREADING The window text is displayed using left-to-right reading-order properties. This is the default. WS_EX_MDICHILD Creates a multiple-document interface (MDI) child window. WS_EX_NOACTIVATE Windows 2000/XP: A top-level window created with this style does not become the foreground window when the user clicks it. The system does not bring this window to the foreground when the user minimizes or closes the foreground window. To activate the window, use the SetActiveWindow or SetForegroundWindow function. The window does not appear on the taskbar by default. To force the window to appear on the taskbar, use the WS_EX_APPWINDOW style. WS_EX_NOINHERITLAYOUT Windows 2000/XP: A window created with this style does not pass its window layout to its child windows. WS_EX_NOPARENTNOTIFY Specifies that a child window created with this style does not send the WM_PARENTNOTIFY message to its parent window when it is created or destroyed. WS_EX_OVERLAPPEDWINDOW Combines the WS_EX_CLIENTEDGE and WS_EX_WINDOWEDGE styles. WS_EX_PALETTEWINDOW Combines the WS_EX_WINDOWEDGE, WS_EX_TOOLWINDOW, and WS_EX_TOPMOST styles. WS_EX_RIGHT The window has generic "right-aligned" properties. This depends on the window class. This style has an effect only if the shell language is Hebrew, Arabic, or another language that supports reading-order alignment; otherwise, the style is ignored. Using the WS_EX_RIGHT style for static or edit controls has the same effect as using the SS_RIGHT or ES_RIGHT style, respectively. Using this style with button controls has the same effect as using BS_RIGHT and BS_RIGHTBUTTON styles. WS_EX_RIGHTSCROLLBAR Vertical scroll bar (if present) is to the right of the client area. This is the default. WS_EX_RTLREADING If the shell language is Hebrew, Arabic, or another language that supports reading-order alignment, the window text is displayed using right-to-left reading-order properties. For other languages, the style is ignored. WS_EX_STATICEDGE Creates a window with a three-dimensional border style intended to be used for items that do not accept user input. WS_EX_TOOLWINDOW Creates a tool window; that is, a window intended to be used as a floating toolbar. A tool window has a title bar that is shorter than a normal title bar, and the window title is drawn using a smaller font. A tool window does not appear in the taskbar or in the dialog that appears when the user presses ALT+TAB. If a tool window has a system menu, its icon is not displayed on the title bar. However, you can display the system menu by right-clicking or by typing ALT+SPACE. WS_EX_TOPMOST Specifies that a window created with this style should be placed above all non-topmost windows and should stay above them, even when the window is deactivated. To add or remove this style, use the SetWindowPos function. WS_EX_TRANSPARENT Specifies that a window created with this style should not be painted until siblings beneath the window (that were created by the same thread) have been painted. The window appears transparent because the bits of underlying sibling windows have already been painted. To achieve transparency without these restrictions, use the SetWindowRgn function.( 本文出处: http://www.d-programming-language-china.org ) WS_EX_WINDOWEDGE Specifies that a window has a border with a raised edge. 2 lpClassName 参数lpClassName指定窗口类的名称,即我们在设计一个窗口类中为WNDCLASS的lpszClassName成员指定的名称,在这里应该设置为“sunxin2006”,表示要产生“sunxin2006”这一类型的窗口。产生窗口的过程是由操作系统完成的,如果在调用CreateWindowEx函数之前,没有用RegisterClass函数注册过名称为“sunxin2006”的窗口类型,操作系统将无法得知这一类型窗口的相关信息,从而导致创建窗口失败。 3 lpWindowName 参数lpWindowName指定窗口的名字。如果窗口样式指定了标题栏,那么这里指定的窗口名字将显示在标题栏上。 4 dwStyle 参数dwStyle指定创建的窗口的样式。就好像同一型号的汽车可以有不同的颜色一样,同一型号的窗口也可以有不同的外观样式。要注意区分WNDCLASS中的style成员与CreateWindowEx函数的dwStyle参数,前者是指定窗口类的样式,基于该窗口类创建的窗口都具有这些样式,后者是指定某个具体的窗口的样式。 在这里,我们可以给创建的窗口指定WS_OVERLAPPEDWINDOW这一类型,该类型的定义为:( 本文出处: http://www.d-programming-language-china.org ) QUOTE: #define WS_OVERLAPPEDWINDOW (WS_OVERLAPPED | \ WS_CAPTION | \ WS_SYSMENU | \ WS_THICKFRAME | \ WS_MINIMIZEBOX | \ WS_MAXIMIZEBOX) 可以看到,WS_OVERLAPPEDWINDOW是多种窗口类型的组合,其原理和前面知识点所讲的内容是一致的。下面是这几种常用窗口类型的说明。 WS_OVERLAPPED:产生一个层叠的窗口,一个层叠的窗口有一个标题栏和一个边框。 WS_CAPTION:创建一个有标题栏的窗口。 WS_SYSMENU:创建一个在标题栏上带有系统菜单的窗口,要和WS_CAPTION类型一起使用。 WS_THICKFRAME:创建一个具有可调边框的窗口。( 本文出处: http://www.d-programming-language-china.org ) WS_MINIMIZEBOX:创建一个具有最小化按钮的窗口,必须同时设定WS_ SYSMENU类型。( 本文出处: http://www.d-programming-language-china.org ) WS_MAXIMIZEBOX:创建一个具有最大化按钮的窗口,必须同时设定WS_ SYSMENU类型。 WS_VISIBLE: 创建之后就立即显示窗口 5 x,y,nWidth,nHeight CreateWindowEx函数的参数x,y,nWidth,nHeight分别指定窗口左上角的x,y坐标,窗口的宽度,高度。如果参数x被设为CW_USEDEFAULT,那么系统为窗口选择默认的左上角坐标并忽略y参数。如果参数nWidth被设为CW_USEDEFAULT,那么系统为窗口选择默认的宽度和高度,参数nHeight被忽略。 5 hWndParent 参数hWndParent指定被创建窗口的父窗口句柄。已经介绍了,窗口之间可以有父子关系,子窗口必须具有WS_CHILD样式。对父窗口的操作同时也会影响到子窗口,表1.1列出了对父窗口的操作如何影响子窗口。( 本文出处: http://www.d-programming-language-china.org ) 表1.1 对父窗口的操作对子窗口的影响 父 窗 口 子 窗 口 销毁 在父窗口被销毁之前销毁 隐藏 在父窗口被隐藏之前隐藏,子窗口只有在父窗口可见时可见 移动 跟随父窗口客户区一起移动 显示 在父窗口显示之后显示 在父窗口显示之后显示 6 hMenu 参数hMenu指定窗口菜单的句柄。 7 hInstance 参数hInstance指定窗口所属的应用程序实例的句柄。 8 lpParam 参数lpParam:作为WM_CREATE消息的附加参数lParam传入的数据指针。在创建多文档界面的客户窗口时,lpParam必须指向CLIENTCREATESTRUCT结构体。多数窗口将这个参数设置为NULL。( 本文出处: http://www.d-programming-language-china.org ) 如果窗口创建成功,CreateWindowEx函数将返回系统为该窗口分配的句柄,否则,返回NULL。注意,在创建窗口之前应先定义一个窗口句柄变量来接收创建窗口之后返回的句柄值。用ShowWindow,UpdateWindow函数显示及更新创建的窗口 1 显示窗口 窗口创建之后,我们要让它显示出来,这就跟汽车生产出来后要推向市场一样。调用函数ShowWindow来显示窗口,该函数的原型声明如下所示: QUOTE: BOOL ShowWindow( HWND hWnd, // handle to window int nCmdShow // show state ); ShowWindow函数有两个参数,第一个参数hWnd就是在上一步骤中成功创建窗口后返回的那个窗口句柄;第二个参数nCmdShow指定了窗口显示的状态,常用的有以下几种。 SW_HIDE:隐藏窗口并激活其他窗口。 SW_SHOW:在窗口原来的位置以原来的尺寸激活和显示窗口。( 本文出处: http://www.d-programming-language-china.org ) SW_SHOWMAXIMIZED:激活窗口并将其最大化显示。 SW_SHOWMINIMIZED:激活窗口并将其最小化显示。 SW_SHOWNORMAL:激活并显示窗口。如果窗口是最小化或最大化的状态,系统将其恢复到原来的尺寸和大小。应用程序在第一次显示窗口的时候应该指定此标志。 SW_RESTORE 同SW_SHOWNORMAL SW_NORMAL SW_MAXINIZE 最大化应用程序窗口 SW_MINIMIZE 最不化应用程序窗口 SW_SHOWNOACTIVATE 按最近大小和位置显示窗口,但不改变激活特性 SW_SHOWMINNOACTIVE 最小化窗口,但不改变其激活特性 SW_SHOWNA 按当前大小各位置显示窗口,但不改变其激活特性 2 更新窗口 在调用ShowWindow函数之后,我们紧接着调用UpdateWindow来刷新窗口,就好像我们买了新房子,需要装修一下。UpdateWindow函数的原型声明如下: QUOTE: BOOL UpdateWindow( HWND hWnd // handle to window ); 其参数hWnd指的是创建成功后的窗口的句柄。UpdateWindow函数通过发送一个WM_PAINT消息来刷新窗口,UpdateWindow将WM_PAINT消息直接发送给了窗口过程函数进行处理,而没有放到我们前面所说的消息队列里,请读者注意这一点。关于WM_PAINT消息的作用和窗口过程函数,后面我们将会详细讲解。( 本文出处: http://www.d-programming-language-china.org ) 到此,一个窗口就算创建完成了。 现在,全部D语言的代码是这样: [Copy to clipboard] [ - ]CODE: import std.c.windows.windows; import std.utf; import win32.api; extern(C) void gc_init(); extern(C) void gc_term(); extern(C) void _minit(); extern(C) void _moduleCtor(); extern(C) void _moduleDtor(); extern(C) void _moduleUnitTests(); char[] ClassName = "http://www.d-programming-language-china.org"; extern(Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) { int result; gc_init(); _minit(); try { _moduleCtor(); _moduleUnitTests(); result = myWinMain(hInstance,hPrevInstance,lpCmdLine,nCmdShow); _moduleDtor(); } catch(Exception e) { printf("catch %.*s\n",e.msg); result = 0; } finally { } gc_term(); return result; } int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) { HINSTANCE hInst = cast(HINSTANCE) GetModuleHandle(null); WNDCLASSEX wc; wc.cbSize = WNDCLASSEX.sizeof; wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = &WindowProc; wc.cbClsExtra = wc.cbWndExtra = 0; wc.hInstance = hInst; //bbs.d-programming-language-china.org注: D语言标准库的windows.d主要服务于ascii字符集的windows api,IDI_APPLICATION被定义为CHAR*,所以这里要转换成wchar* //如果IDI_APPLICATION来定义是CHAR,就可以用toUTF16z来转换 wc.hIcon = LoadIcon (cast(HINSTANCE)null,cast(wchar*)IDI_APPLICATION); wc.hCursor = LoadCursor(cast(HINSTANCE)null,cast(wchar*)IDC_CROSS); wc.hbrBackground = cast(HBRUSH)(COLOR_WINDOW+1); wc.lpszMenuName = null; //转换类名到以0结尾的宽字符集 wc.lpszClassName = toUTF16z(ClassName); wc.hIconSm = LoadIcon(hInst,std.utf.toUTF16z("IDI_MYICON")); auto a = RegisterClassEx(&wc); assert(a); HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW | WS_EX_WINDOWEDGE | WS_EX_TOPMOST, toUTF16z(ClassName), toUTF16z("这是我的第一个gui程序"), WS_THICKFRAME | WS_VISIBLE | WS_VSCROLL | WS_MAXIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU, CW_USEDEFAULT,CW_USEDEFAULT,800,600, HWND_DESKTOP, cast(HMENU) null, hInst, null); if (hWnd == null) { MessageBoxEx(null,toUTF16z("欢迎来到 http://bbs.d-programming-language-china.org "),toUTF16z("error"),MB_ICONERROR | MB_HELP,0); return -2; } //显示和更新窗体 ShowWindow(hWnd, SW_SHOWMAXIMIZED); UpdateWindow(hWnd); return 0; } extern(Windows) LRESULT WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_SIZE: { return(0); } case WM_CREATE: { return(0); } case WM_DESTROY: { PostQuitMessage(0); return(TRUE); } default: return(DefWindowProcW(hwnd, uMsg, wParam, lParam)); } } 到现在为止,我们只是创建了一个闪而过的windows窗口。( lastupdate:20070510 最新文章请访问http://www.d-programming-language-china.org )关于一大步成功社区:yidabu提倡在交流中学习,在分享中提高收集感兴趣的知识,写下心得,通过网络与别人一起分享理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台