D语言学习笔记
![]() 1,配置环境(2008-7-15) 需要如下的软件(只是基于我的学习路线,其他的还没有空闲时间研究): D语言编译器下载 http://ftp.digitalmars.com/dmd.2.014.zip D语言连接器及工具下载 http://ftp.digitalmars.com/dmc.zip D语言IDE下载 http://svn.dsource.org/projects/dwin/downloads/scite4d/scite4dsetup.exe D语言编译工具下载 http://svn.dsource.org/projects/dsss/downloads/0.75/dsss-0.75-dmd-win.zip D语言基本库下载 http://downloads.dsource.org/projects/tango/0.99.6/tango-0.99.6-bin-win32-dmd.1.029.zip D语言Windows下GUI库下载 http://www.dprogramming.com/dfl09701.zip 有了以上的软件,我们开始配置D语言开发环境。 首先,需要配置编译器环境,假定我们的工作环境在D:/D-Language文件夹下,将dmd,dmc解压后放在该 目录,其中dmd会产生两个子目录dm和dmd,而dmc只产生一个dm目录,将它们放在D:/D-Language目录下 。 接着设置环境变量,将D:/D-Language/dm/bin和D:/D-Language/dmd/bin加入Path路径中。 这样编译器环境已经设置好了,打开一个命令行窗口,输入dmd,如果出现一堆参数提示,表明成功。 再次,设置编译工具。将dsss解压,进入其bin所在的上层目录(也就是能看到bin的目录),将所有的 包含bin目录的子目录覆盖到D:/D-Language/dmd目录下(会提示是否覆盖bin目录,选择是,我们仅仅是 想将dsss的路径和dmd的路径放在一起而已)。 在命令行窗口,输入dsss,如果出现一堆参数提示,表明成功。 然后,设置IDE环境。双击scite4dsetup.exe安装sci软件,安装完后打开sci安装路径下的 SciTEGlobal.properties文件(一般来说在C:/Program Files/ SciTE4D目录下),修改第一行,将它改 成具体的路径(D:/D-Language/dmd)。 tango安装: 一,同dsss安装,解压后将bin目录以及其兄弟目录全部拷贝到D:/D-Language/dmd目录下。 二,打开D:/D-Language/dmd/bin/sc.ini文件用下面的覆盖: [Version] version=7.51 Build 020 [Environment] LIB="[email=%@P%/../lib;%@P%/../../dm/lib] %@P%/../lib;%@P%/../../dm/lib[/email]" DFLAGS="[email=-I%@P%/../import] -I%@P%/../import[/email]" -version=Tango -defaultlib=tango-base-dmd.lib -debuglib=tango- base-dmd.lib -L+tango-user-dmd.lib [email=LINKCMD=%@P%/link.exe] LINKCMD=%@P%/link.exe[/email] 安装完毕。 DFL安装(使用dsss安装该库): 一,解压后将bin目录以及其兄弟目录全部拷贝到D:/D-Language/dmd目录下。 二,打开sci,并到D:/D-Language/dmd/import/dfl下打开all.d文件。 三,在D:/D-Language/dmd/import/目录下创建dsss.conf文件,内容如下: [dfl] target=dfl type=library postbuild=copy /y dfl.lib ../../lib/dfl.lib 四,在sci中F7 Build。 五,安装成功。 2,使用学习: tango网络编程实例: 打开sci, 将以下代码拷贝到文档中,并保存成server.d: private import tango.core.Thread; private import tango.io.Console; private import tango.net.ServerSocket; void main() { const int port = 8080; // thread body for socket-listener void run() { auto server = new ServerSocket (new InternetAddress(port)); while(true) { // wait for requests auto request = server.accept; // write a response request.output.write ("server replies 'hello'"); Cout("Socket accept!/n"); } } // start server in a separate thread, and wait for it to start (new Thread (&run)).start; } 在保存的server.d同目录下创建dsss.conf文件,文件内容如下: [server.d] 在sci中F7 Build,F5 run。 可以看到如今一个网络server端已经写好。 我们现在需要写一个client端连接服务端,并打印出服务端反馈的信息。 client.d如下: private import tango.io.Console; private import tango.net.SocketConduit, tango.net.InternetAddress; void main() { // make a connection request to the server auto request = new SocketConduit; request.connect (new InternetAddress ("localhost", 8080)); request.output.write ("hello/n"); // wait for response (there is an optional timeout supported) char[64] response; auto size = request.input.read (response); // close socket request.close; // display server response Cout (response[0..size]).newline; } 如果不想再繁琐的使用dsss.conf进行编译,直接在sci中Ctrl+F5直接就可以运行,看结果可以看到,本程序已经连接服务器,并打印了服务器的反馈字符串。 ------------------------------ DFL界面编程: 简单的界面直接如下代码(form.d): import dfl.all; int main() { Form myForm; Label myLabel; myForm = new Form; myForm.text = "hello, D Language China"; myForm.width = 600; myForm.height = 300; myLabel = new Label; myLabel.font = new Font("Verdana", 14f); char[] str = " http://www.d-programming-language-china.org/ /n" " http://bbs.d-programming-language-china.org/ /n" " http://scite4d.d-programming-language-china.org/ "; myLabel.text = str; myLabel.location = Point(15, 15); myLabel.autoSize = true; myLabel.parent = myForm; Application.run(myForm); return 0; } 如果需要命令行编译的话:dmd form.d dfl.lib即可。这里需要连接dfl.lib。 如果直接使用dfl.exe编译的话可以如此(记住如果找不到dfl.exe,到dfl的bin目录下将dfl.exe拷贝到dmd/bin目录下):dfl -release form.d -gui即可(-gui选项去除exe执行背后的命令行窗口)。 如果有resource文件(比如.ico,.rc,.res),则使用 dfl -release form.d form.res -gui就可以将这些资源文件link到程序中。 --end of 2008-7-15 --start of 2008-7-17 今天,学习一下DFL的GDI画图功能,我们使用DFL开启一个MDI工程,在其子窗口中,增加一个按钮,当点击该按钮时,将会在该窗口下画出一个图形,多次点击依序画出图形,可以根据这个demo程序开发一个画图程序(比如UML类图...)。 private import dfl.all; class DrawForm: Form { private int count; this() { text = "draw ellipse!"; count = 0; } public void add() { count++; invalidate(false); } protected override void onPaint(PaintEventArgs ea) { super.onPaint(ea); auto Pen p = new Pen(Color(0xFF, 0, 0), 4); for(int i = 0;i < count;i++) { ea.graphics.drawEllipse(p, (i % 6) * 50, (i / 6) * 50, 50, 50); } } } class MainForm: Form { uint docnum = 0; this() { text = "Draw Studio"; startPosition = FormStartPosition.WINDOWS_DEFAULT_BOUNDS; MenuItem mi; menu = new MainMenu; with(mi = new MenuItem) { text = "New"; click ~= &menubar_click; } menu.menuItems.add(mi); isMdiContainer = true; addChild(); } private void mdiChildHiClick(Object sender, EventArgs ea) { Button btn; btn = cast(Button)sender; assert(btn); DrawForm f; f = cast(DrawForm)btn.parent; assert(f); f.add(); } final void addChild() { Form f; with(f = new DrawForm) { text = "Studio:"; mdiParent = this; with(new Button) { text = "&Add"; location = Point(42, 42); parent = f; click ~= &mdiChildHiClick; } show(); } } private void menubar_click(Object sender, EventArgs ea) { addChild(); } } int main() { int result = 0; try { Application.run(new MainForm); } catch(Object o) { msgBox(o.toString(), "Fatal Error", MsgBoxButtons.OK, MsgBoxIcon.ERROR); result = 1; } return result; } --end of 2008-7-17 --start of 2008-7-18 D语言游戏开发包Derelict安装使用: 首先到 http://www.dsource.org/projects/derelict/changeset/head/trunk?old_path=%2F&format=zip 下载Derelict开发包。 再次下载SDL的动态库 http://www.libsdl.org/release/SDL-1.2.13-win32.zip文件。 SDL动态库安装: 解压后将sdl.dll放在windows/system32目录下(其它系统目录也可)。 Derelict安装: 解压后,使用命令行进入Derelict目录(该目录下有dsss.conf文件),输入dsss build,也可以进入子目 录build 相应的模块(比如DerelictSDL)。 产生的lib文件都在Derelict目录下的lib目录中,将它们都拷贝到dmd/lib目录下,并且将每个 DerelictXXX(比如DerelictSDL)目录下的derelict目录整个拷贝到dmd/import目录下(记住该目录在 以前的编译器配置中,已经在dmd/bin/sc.ini中指定为第三方lib的指定目录)。这些DerelictXXX下的 子模块是可以互相独立的(除了DerelictUtil中的Util模块,这是所有的其它模块都依赖的模块,必须 拷贝过去)。 既然这些模块依赖于DerelictUtil,那么如果我们现在进行SDL游戏编程,除了依赖DerelictSDL模块外 ,还需依赖DerelictUtil模块。所以以下的demo程序编译时需要使用如下的命令行指令: dmd sdl_ex1.d DerelictUtil.lib DerelictSDL.lib 以下是demo程序,需要SDL库支持。 文件名: sdl_ex1.d 文件内容: module sdl_ex1; import derelict.sdl.sdl; version(Tango) { import tango.stdc.stdio; } SDL_Surface* screen; // pointer to the screen surface object void render() { if(SDL_MUSTLOCK(screen)) if(SDL_LockSurface(screen) < 0) return; int tick = SDL_GetTicks(); // track the current yoffset when looping throught the pixels int yoff = 0; uint* buff = cast(uint*)screen.pixels; for(int i=0; i<600; ++i) { for(int j=0, off = yoff; j<800; ++j, ++off) { buff[off] = i * i*4 + j * j*4 ;//you can add + tick for flash window } // add the width of the screen (the surface pitch / bytes per pixel) // to the yoffset in order to move to the next row yoff += screen.pitch/4; } // unlock the screen if we had to lock it if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); // tell SDL to update the whole screen SDL_UpdateRect(screen, 0, 0, 800, 600); } int main() { // load the SDL shared library DerelictSDL.load(); // Initialize SDL if(SDL_Init(SDL_INIT_VIDEO) < 0) { printf("Unable to init SDL: %s/n", SDL_GetError()); SDL_Quit(); return 1; } // Create the screen surface (window) screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE); if(screen is null) { printf("Unable to set 800x600 video: %s/n", SDL_GetError()); SDL_Quit(); return 1; } // main loop flag bool running = true; // main loop while(running) { // draw to the screen render(); // process events SDL_Event event; while(SDL_PollEvent(&event)) { switch(event.type) { // exit if SDLK or the window close button are pressed case SDL_KEYUP: if(event.key.keysym.sym == SDLK_ESCAPE) running = false; break; case SDL_QUIT: running = false; break; default: break; } } // yield the rest of the timeslice SDL_Delay(0); } // clean up SDL SDL_Quit(); return 0; } 运行后的快照如下: DWin安装使用: 下载安装包: http://svn.dsource.org/projects/dwin/downloads/dwin/dwin.exe 双击执行。 将安装目录下的lib目录下的所有lib拷贝到dmd/lib目录下。 将该目录下的其它子文件夹拷贝到dmd/import/dwin目录下(dwin目录不存在请创建,而上层import父目 录则在以前的安装过程中已经指明是第三方的D语言库源文件存放目录)。 安装完毕。 记住使用dwin.lib时,根据我的经验,pcre.lib和dfl.lib需要包含,否则不能运行成功。 以下的代码示例显示如何打开一个IE窗口,并自动导航到google页面,并在其中输入"D language",自 动点击搜索按钮,并显示查询结果。希望以此作为指导,显示一个自动化IE测试如何进行。 程序如果一下子没有显示相应的自动化,请耐心等待,因为程序中使用了Sleep函数。 文件名:test.d 执行命令行:dmd test.d(注意没有添加一起link的lib文件是因为程序中已经pragma需要的lib文件了 ) 程序: module test; import tango.io.Stdout; import dwin.sys.win32.ie.IE; pragma(lib, "tango-base-dmd.lib"); pragma(lib, "tango-user-dmd.lib"); pragma(lib, "dwin.lib"); pragma(lib, "dfl.lib"); pragma(lib, "pcre.lib"); void main(char[][] args) { auto ie = ieCreate(null, "Width",800, "Height",600, "ToolBar",false, "MenuBar",false, "Navigate", " http://www.google.cn"); Sleep(8000); //waitId(ie, "postform"); //wait postform node load auto text = getElementsByName(ie, "q"); putInnerText(text[0],"D Language"); Sleep(1000); auto result = getElementsByName(ie, "btnG"); result[0].click(); Stdout( getInnerText(result[0]) ).newline; Sleep(8000); ie.Quit(); } | |
|
D语言学习笔记
|
|
|