1. VBA 在 Excel 中的常用操作

    文件操作 引用打开的工作簿 使用索引号(从 1 开始) Workbooks(1) 使用工作簿名称 Workbooks("1.xlsx") 创建一个 EXCEL 工作簿对象 Dim wd As Excel.Application Dim wb As Workbook Set wd = CreateObject("excel.application") wd.Visible = True Set wb = wd.Workbooks.Open(ThisWorkbook.Path & "/test.xls") ' ... wb.Close wd.Quit 打开/保存/关闭工作簿 Dim wb As Workbook wb = Workbooks.Open(ThisWorkbook.Path & "/test.xls") ' ... wb.Save wb.Close 关闭所有工作簿 Workbooks.close 另存为(自动打开新文件关闭源文件) ThisWorkbook.SaveAs FileName:="D:\1.xls" 另存为(保留源文件不打开新文件) ThisWorkbook.SaveCopyAs FileName:="D:\1.xls" 拷贝文件 oldfile = ThisWorkBook.Path & "/old.xlsx" newfile = ThisWorkBook.Path & "/new.xlsx" FileCopy oldfile, newfile 删除文件夹下的所有文件 base = ThisWorkBook.Path & "/文件夹/" pattern = base & "*.*" file = Dir(pattern, vbReadOnly) While file <> "" Kill base & file file = Dir Wend 创建文件夹 MkDir(directory) 判断文件夹是否存在 以下为不存在即创建 If Dir(outputDir, 16) = Empty Then MkDir (outputDir) End If 判断文件是否存在 方法 1: Dim fileSystemObject As Object Set fileSystemObject = CreateObject("Scripting.FileSystemObject") If fileSystemObject.FileExists(<filepath>) = True Then MsgBox "文件存在" End If 方法 2: Dim file As String file = Dir("E:\MyPictures\Pic\logo.gif") If file <> "" Then MsgBox "文件存在" Endif 格式操作 设置边框与自动筛选 Set Rng = MyWorkSheet.UsedRange With Rng .Borders.LineStyle = xlContinuous .Borders.Weight = xlThin .AutoFilter End With 获取或者设置单元格背景色 MyWorkSheet.Cells(i, j).Interior.ColorIndex 让某表格选中的单元格变成指定颜色 在 thisworkbook 中添加如下代码段: Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) If ActiveSheet.Name = "yoursheet" Then ActiveSheet.UsedRange.Interior.ColorIndex = 0 Target.Interior.ColorIndex = 6 End If End Sub 在单元格里回车 / 换行 设置单元格 Value 里使用 Chr(10) 和 Chr(13),分别表示回车、换行。 隐藏行 MyWorkSheet.Rows(i).Hidden = True 单元格内容为纯文本 sheet.Cells(m, n).NumberFormatLocal = "@" 选择 引用单元格 / 区域 Range("A1") '表示 A1 单元格 Range("A2:D1") '表示 A2 到 D1 区域 Range("A2:D1")(3) '表示该区域里的第三个单元格 Range("D" & i) 'i 为变量 Range("D3:F4,G10") '引用多个区域 Range("2:2") '引用第二行 Range("2:12") '引用第二行到第十二行 Range("D:A") '引用第 A 到 D 列 Rows(2) '引用第二行 Rows("2:4") '引用第二到四行 Columns("B") Columns("B:D") Range(Clee1, Cell2) '左上与右下 Range(Range1, Range2) '取最大范围 选中单元格 / 区域 Range("1:1").Select '选中第一行 获取当前选中区域 MyWorkSheet.Application.Selection 数据结构 Dictionary Dim dict Set dict = CreateObject("Scripting.Dictionary") ' 新增,各种类型都可以,包括 Dictionary dict.Add "hello", "world" ' 数量 dict.Count ' 删除 dict.Remove("hello") ' 判断是否存在 dict.exists("hello") ' 取值,需要先判断存在再取 dict.Item("hello") ' 修改、新增 dict.Item("hello") = "world" ' 循环 k = dict.Keys v = dict.Items For i = 0 to dict.count - 1 key = k(i) value = v(i) Next ' 清空 dict.RemoveAll 参考:Excel vba map/dictionary 语言基础 String to Integer、Double CInt(MyWorkSheet.Cells(1,7)) CDbl(MyWorkSheet.Cells(1,7)) 字符串分割/获取数组长度 Dim arr() As String arr() = Split(ws.Cells(a, b).Value, "-") alen = UBound(arr) - LBound(arr) + 1 判断单元格是否为空 判断单元格的 value 是否为 ““。 退出 主要使用 Exit 表达式。 Exit { Do | For | Function | Property | Select | Sub | Try | While } 参见 Exit Statement (Visual Basic) 参考 VBA Converting Data Types excel vba判断文件是否存在

    2012/03/22 Excel

  2. Firefox 和内嵌 Gecko 程序的 Plugins

    昨日在 JumuFENG 同学建的 firefox 交流群 (81424441) 里与群友们说起插件相关的一些东西,自己在本地测试了一下,然后推论出一些粗浅认识总结如下:(欢迎有兴趣和正在进行 mozilla 相关的一些开发的朋友加群交流) 一般来讲,对于 firefox 和其它内嵌 Gecko 内核的应用程序来讲,其插件的注册和搜索的机制应该与如下内容有关(windows 下): (1) 程序会优先考虑可执行文件同级的 plugins 目录下的 DLL 等类型文件,判断文件的命名是否符合插件特征且通过某种机制确定其是否为插件。命名规范:如 flash 插件的 dll 名为 NPSWF32.DLL,改成 asdfjljlk.dll 则无法识别,NPSWF311112.dll 这种则可以识别,NPSWFjkljdlfkj32.dll 这种可以识别,N1PSWF32.dll 无法识别。 (2) 注册表里 HKEY_LOCAL_MACHINE\SOFTWARE\MozillaPlugins 下注册着插件信息 根据注册表项的 path 项的键值去搜索对应 DLL,如果找不到,则在 firefox 的”附加组件”或者 about:plugins 里不会显示,即使该 DLL 放在系统环境变量的 path 包含的路径下。((1) 里面的 plugins 目录的情况除外) (3)%appdata%\Mozilla\Firefox\Profiles\wgnbwzjm.default 文件夹里头的 pluginreg.dat 文件里有具体配置项,插件是否启用和其它插件相关信息将从这里读取并在”附加组件”和 about:plugins 里显示给用户。 光标处的 4 表示不启用,若为 1 或者 5 则为启用。若为 13,则为找不到。此文件删除后再运行 firefox 时会自动再生成 (根据 plugins 和注册表里)。此设置对安装的 firefox 等这种在应用程序里启用 profile 的程序有效(我自己写的内嵌 Gecko 的程序因为没有启用 profile,所以并无此文件,只能根据 plugins 和注册表来查找和显示插件)。剪切走此文件,则在 firefox 中已经禁用过的插件也被启用。 Plugins 文件夹的优先级更高。如果在 plugins 与注册表指示的路径下都能找到某插件 DLL,那么 pluginreg.dat 文件里的路径被修改为 plugins 文件夹下 DLL 路径。 其它相关小知识点: (1) DLL 插件都实现了导出这 3 个函数。 (2) 若插件注册表项删除,pluginreg.dat 文件删除,plugins 下有 DLL,则仍可显示正确 MIME 类型,描述等信息。推测是 firefox 等程序在初始化时维护的有一个数据库,里头根据 DLL 属性描述里的”原文件名”存储有一一对应的有这些信息。 在 windows xp 下查看这些 DLL 的属性,发现有 MIMEType,FileOpenName,FileExtents 与下面在 about:plugins 里看到的 MIME 类型,描述,后缀三项一一对应,应该就是直接从插件 DLL 的属性里读取的。 一点粗浅认识,纯属自行测试后的一些推论,如有谬误尽请指正。

    2012/03/15 Gecko

  3. Moziila 文件结构概览(译)

    /** * 文件名:Moziila 文件结构概览 * 来 源:https://developer.mozilla.org/en/Source_code_directories_overview * 翻 译:mzlogin#qq.com * 日 期:2012 年 2 月 22 日 * */ 源码目录概览 这个文档是一份为开发人员提供的 Mozilla 源码目录结构树的指南。它提供源码的鸟瞰以方便开发人员理解 Mozilla 里有什么,到哪里去找想要的东西。它对一个刚刚学习 Mozilla 源码的开发人员来讲是一份不错的文档。 这份文档包含 SeaMonkey,Firefox 和 Toolkit 的相关材料。 这是一份根据 Mozilla 的源码树按月更新的文档。 在 Mozilla Source Code Directory Structure 和 more detailed overview of how the parts of Gecko fit together 可以看到相似的内容。 简介:公有子目录 一套通用名称方案贯穿整个 Mozilla 源码树。最顶层的是产品名称(如 seamonkey)。在第二层是模块的名称(如 seamonkey 的 editor 目录)。第三层一般就由通用命名方案接管了。许多第三层目录包含 base,public,和 idl 目录。虽然不是必须的,但这些同名的目录一般是用作相同的用途。如果将文件按子模块分组,它们通常会被放进第三层目录并且命一个唯一的名字(如 seamonkey 的 editor 目录中的 txmgr )。在第三层目录下面,可能会有第四层的 base,public 和 idl 目录。因此,这个方案是递归的;它适用于子模块,子模块的子模块,依此类推。 base 包含模块的基本(核心)功能。base 包含所有不能被分类成子模块的源码。 build 包含所有用作组建特定模块的 makefile。 doc 包含与模块相关联的所有文档。 idl 包含 XPIDL(跨平台接口定义语言)接口文件。这些接口能极容易地广泛应用于 JS 脚本和 C 代码。XPIDL 文件拥有它们自己的迷你语言和处理工具。 public 包含将被导出到 dist/include 目录的源码。它们并非全部都需要公开,有一些只是在特定模块作特定用途。越多的代码被写成或者转换成 XPIDL 接口,public 目录的价值就越小。 src 包含大部分源码。 tests 包含运用此模块的 C,HTML 或 XUL 示例代码。 tools 包含自动生成某些源码的脚本和组建这个模块的专用工具。 有几个目录是平台相关的,包含特定平台的源码。 windows 包含 Windows 95,Windows 98 和 Windows NT 4.0 的专用源码。 mac 包含 MacOS 的专用源码(包括 PowerPC 和 68000 版本)。 gtk 包含运行在 Unix 的 X-Windows 的 GTK(又名 GIMP 工具包)的专用源码。 motif 包含运行在 Unix 的 X-Windows 的 Motif 工具包的专用源码。 os2 包含 OS/2 专用源码。 rhapsody 包含使用 Yellow Box(Cocoa)的 Mac OS X Server(Which is based on NeXTStep which is based on X-Windows on Unix)的专用源码。 beos 包含 BeOS 的专用源码。 qt 包含 QT 工具包(可以运行在 X-Windows On Unix,Windows 95,Windows 98 和 Windows NT 4.0 的 C 库)的专用源码。 photon 包含 Photon(一个使用在 QNX Software Systems Ltd. 的几个实时操作系统上的微内核窗口系统)的专用源码。 SeaMonkey SeaMonkey 是火狐浏览器套件的开发版名称。 accessible 包含为 Linux 提供对 Microsoft Active Accessibility 和 Sun’s ATK accessibility API 的支持的源码。 browser 包含 Firefox 的一些源码。这将会包含如下。 build 包含 Mozilla build team 用于组建和管理 Mozilla 基础代码的脚本(通常是 Perl )和程序。这些程序协调 makefiles 的运行以及 dist 目录的创建。 calendar 包含各种各样的 Mozilla 日历程序和扩展。 caps 包含决定基于加密和证书(如 Verisign)的内容的性能的 C 接口和代码。 chrome 包含 chrome registry 工具包。 config 包含 Mozilla 开发人员用于操纵基础代码和执行 makefiles 里的特定行为的脚本和程序。这些程序是代码层的而不是组建层的(那些在 build 目录里)。 content 是从 layout 中分裂出来的,包含那些与 DOM 相关的对象。 db 包含用于 mdb/Mork database(一种低级,通用且跨平台的文件库)的 C 代码。它用于存储邮箱数据,新闻数据和全局历史数据。它现在还不能支持 XPCOM。这些代码起源于 Mozilla 传统基础代码。 dbm 包含用于管理,读写哈希表的 C 代码。它用于 URL 编辑框(存储在传统 Mozilla 的 netscape.hst 中)的自动补全特性和缓存页面索引(存储在传统 Mozilla 的 fat.db 中)。这部分代码起源于加州大学伯克利分校。 directory 包含 LDAP(轻量级目录访问协议)SDK。 docshell 包含载入并展示单个网页(例如 scroll)的 C 接口和代码。[#seamonkey-embedding embedding] 代码包装这些代码实现更高层次浏览器功能如向前,后退和历史。 dom 包含实现和跟踪 Javascript 里的 DOM(文档对象模型)对象的 C 接口和代码。它们组成 C 根据 Javascript 脚本创建,销毁和操纵内建及用户定义的对象的子体系结构。例如,如果 Javascript 脚本添加一个自定义的属性给文档(如 document.goofy = 1),代码将创建 “goofy” 结点,将它放到 “document” 结点并根据最近的 Javascript 命令来操纵它。 editor 包含实现可以编辑纯文本和 HTML 的可嵌入编辑器组件的 C 接口,C 代码和 XUL。它被用于 HTML 编辑器(如在传统 Mozilla 中的编排器),纯文本及 HTML 组成的邮件,以及贯穿整个产品的文本字段和文本区域。这个编辑器被设计得像「带编辑特性的浏览器窗口」并且额外附带编辑文本和管理 undo/redo。 embedding 包含实现泛型高级浏览器功能(如向前,后退,历史)的 C 接口和代码。[#seamonkey-webshell webshell] 代码依据特定平台与支持的方式(如 ActiveX)包装这些接口。 extensions 包含与浏览有关的各种插件的 C 接口,C 代码,XUL 和 Javascript 代码。包括:cookies,IRC,wallet,DOM Inspector,P3P, schema validation,spellchecker,transformiix,typeaheadfind,Javascript debugger,XForms 等等。 gfx 包含平台相关的绘图与成像的 C 接口与代码。可以用于画 rectangles,lines,images 等等。本质上来讲,它是一个平台相关的设备上下文集。它不操作小部件或者特定绘制例程;它只是提供绘图的最原始操作。 intl 包含本地化支持的 C 接口和代码。包含支持各种字符集,各种格式(如不同地方的日期和时间格式)和其它本地化功能的代码。 ipc 包含?(空白) jpeg 包含读写 JPEG 图像的 C 代码。这些代码起源于对 the Independent JPEG Group 的 JPEG 规范的引用实现。 js 包含将 Javascript 脚本语汇单元化,解析,解释和执行的 C 代码。这些代码起源于 Mozilla Classic。 l10n 包含本地化组建的代码。 layout 包含实现布局引擎的 C 接口和代码。布局引擎决定如何划分窗口资源给一块块的内容。它依据 CSS1 和 CSS2(级联样式表),对齐风格和内容调整和对齐一块块的内容。它并不实际渲染内容;它只是将窗口的不同块分配给各种内容元素。它被称作「软熔」这些内容。这些代码也被称作 “NGLayout” 和 “Gecko”。 lib 包含对 Mac 编程的开发人员参考的 C 代码。它不再使用。它展示了如何将 Mozilla 浏览器嵌入到 Mac 应用程序中去,就像一些示例文件——handling code from Apple。 mailnews 包含邮件和消息组件(如 Messenger in Mozilla Classic)的 C 代码。它包含管理邮件,读取新闻组消息,导入其它邮件格式,组成新的消息等功能的所有代码。 modules 包含组建到 Mozilla 中的各种不同浏览器关联特性的 C 代码。它包括处理各种图像格式(如 PNG,GIF),允许插入 Java 虚拟机(called OJI,for “Open Java Interface”),支持插件并且读取各种压缩格式(如 JAR,ZIP,ZLIB)的代码。这些代码来源于 Mozilla 和各种公司以及独立个人。 netwerk 包含低层次访问网络(使用 sockets 和文件以及内存缓存)和高层次访问(使用各种协议如 http,ftp,gopher 和 castanet)。这部分也被称作 “netlib” 和 “Necko”。 nsprpub 包含跨平台 C 运行时库的代码。这个 C 运行时库包含基本非可视的 C 函数,有分配和释放内存的,获取时间和日期,读和写文件,处理线程的跨平台的字符串比较。这部分也被称作 “nspr” 和 “Netscape Portable Runtime”。这些代码来源于 Mozilla Classic。 other-licenses 包含没有归于 MPL 的代码,包括 branding,libical 和 stubs for 7zip。 parser 包含 HTML 解析器和 XML 解析器(expat.)。 plugin 包含 Mac 上的 MRJ plugin 的代码。 profile 包含创建新用户数据,管理已经存在的用户数据,从 Mozilla Classic 迁移数据和为流行的 ISPs(如 Earthlink 和 Concentric Networks)使用默认数据的代码。 rdf 包含访问各种数据并且根据 RDF 组织它们之间的关系的 C 接口和代码。RDF 是一个开放的标准即「Resource Description Framework」。这些代码从本地文件系统,数据库,Internet 或者其它使用类 URL 语法的资源读写数据。 security 包含安全模块包括 NSS 和 PSM。 storage 包含 sqlite3 的一个实现。 suite 将包含特定于 Mozilla suite 的文件。 sun-java 包含使 Mozilla 能与 Sun JVM 交流的 C 代码。但是它并不包括虚拟机本身的代码。 themes 包含 Mozilla 的默认主题,modern 和 classic。 toolkit 包含 Firefox,Thunderbird 和其它独立应用程序使用的工具包的代码。This will be covered below。 tools 包含 Linux-only Leaky tool 的 C 代码。Leaky 能帮忙探测内存泄漏和 XPCOM 引用计数的问题。 uriloader 包含为某一 URL 包含的内容调用正确的查看器的 C 接口和代码。例如:如果代码决定内容是一个邮件消息,它会查找匹配的监听者(可能是 Netscape Messaenger)并将邮件消息传递给它用于显示。它是通用的,可以将内容传递给内部组件(如 Messenger),插件或者辅助程序。 view 包含不同类型视图(如滚动视图)的 C 接口和代码。一个视图包括除了标题栏,边框或者其它装饰(包含在框架中)以外的内容。这些代码服务于操纵它包含的个别的内容。例如:一个滚动视图将查找它的滚动条位置并且告诉它的内容根据滚动条 thumbs 绘制在什么哪里。 webshell 包含 C 接口,C 代码,Linux Shell 脚本和其它文件用于将 Mozilla 通过不同方式内嵌到各种不同平台的其它程序中(如 plug-in,ActiveX component,XPCOM classes)。 widget 包含 C 接口和代码用于平台依赖的控件(小部件),如 scroll bars,radio buttons 和 list boxes。 xpcom 包含低层次的 C 接口,C 代码,少量汇编代码和命令行工具用于实现 XPCOM 组件(which stands for “Cross Platform Component Object Model”)的基本机制。XPCOM 是一种允许 Mozilla 导出接口并且让它们自动化且有效于 Javascript 脚本,Microsoft COM 和规则的 Mozilla C 代码的机制。一些低层次的 XPCOM classes 和 interfaces 也在这里定义(如所有平台的事件循环)。XPCOM 能兼容并且与 Microsoft COM 很像(虽然 XPCOM 是跨平台的)。 xpfe 包含 C 接口,C 代码和 XUL 用于实现 “Cross Platform Front End”。本质上讲,这是 Mozilla 程序开始和管理其它组件完成任务的地方。这些代码中包括极少量平台相关的代码;它依赖于其它组件来提供平台相关的接口来提供平台特定的功能。 xpinstall 包含 C 接口和代码用于实现从 Mozilla Classic 的智能升级特性。XPInstall 提供下载文件,解压,并且安装它们的代码,这些与平台相关。 xulrunner 包含代码用于 XUL Runner。 FireFox Firefox 被包含在 browser 目录内。 app 包含用于组建 firefox 可执行文件的代码。 base 包含 XUL 和 branding code。 components 包含 Firefox 指定组件如历史,书签,首选项,migration,sidebar.. config 包含 Firefox 的组建脚本。 extensions 包含用于进行某一扩展工作的代码。 installer 包含 Firefox installer 代码。 locales 包含为 Firefox 翻译的文件。 themes 包含 Firefox themes。 Toolkit Toolkit 被用于单独的产品。 components 包含警告,自动补全,命令行接口,控制台,cookies,下载管理器,文件选择器,历史,密码管理,前置类型查找,查看源码,等等。 content 包含为 toolkit 指定的 XUL。 library 包含 libXUL work。 locales 包含翻译过的文件。 mozapps 包含共享的应用程序材料。 obsolete 包含过时的材料。 profile 包含属性管理器。 themes 包含与 toolkit 有关的主题。 xre 是 XUL 的运行时引擎。这个目录包含初始化 toolkit 的代码。 Original Document Information •Author(s): Daniel Howard •Other Contributors: Heikki Toivonen (11-Nov-1999), Hervé Renault (for the French translation) (16-Nov-1999) •Last Updated Date: Michael Kaply 2-June-2005 •Copyright Information: Portions of this content are © 1998-2007 by individual mozilla.org contributors; content available under a Creative Commons license Details. •Further History: Document History.

    2012/03/01 Gecko

  4. 将 Mozilla 源码里的 winEmbed 工程移植到 VC

    最近在学习怎么将 Gecko 嵌入到自己的应用程序中,下载了一份比较早一点的源码在对照官方文档痛苦地推进——网上相关资料确实相当缺乏,难道大家都各种 webkit 去了?我的计划是先弄清怎么用,让程序跑起来,然后再根据官方文档结构说明去定制,削减掉不需要的部分,折腾这个移植就花了我不少时间,果断觉得应该跟大家分享之。废话不说,直接上过程。 下载xulrunner源码并编译。 我这里用的 1.9.2rc1 版本,对应 firefox 3.6.X。 源码下载地址:ftp://ftp.mozilla.org/pub/mozilla.org/xulrunner/releases/1.9.2rc1/source/ 编译环境 mozilla-build 下载地址:http://ftp.mozilla.org/pub/mozilla.org/mozilla/libraries/win32/MozillaBuildSetup-1.3.exe 在解压的源码根目录内新建一个 .mozconfig 文件,我使用的内容是(其中有用的就是组建目标是 xulrunner,启动 tests 以生成示例程序): mk_add_options MOZ_CO_PROJECT=xulrunner mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/obj-xulrunner ac_add_options --enable-application=xulrunner #Uncomment the following line if you don't want to build JavaXPCOM: ac_add_options --disable-javaxpcom ac_add_options --disable-optimize ac_add_options --enable-debug ac_add_options --disable-vista-sdk-requirements ac_add_options --with-windows-version=600 ac_add_options --enable-tests 运行 mozilla-build 1.3 中的 start-msvc9.bat(因为我使用的是 Visual Studio 2008),切换到源码根目录下,运行 ./configure,然后 make。等待几个小时(我的是用了四个小时左右)就 OK 了。 注册GRE(Gecko运行时环境)。 在经过上面第一步的编译后,会在源码根目录下生成名为 dist 的文件夹。源码根/dist/bin/ 目录下现在有 xulrunner.exe 等程序,在 cmd 下运行 xulrunner.exe --register-global 注册 GRE。这时候实际上就可以跑 源码根/embedding/tests/winEmbed/winEmbed.exe 程序了,但是我们的目的是在 VC 下自己的工程里嵌入 Gecko,所以需要尝试将这个示例工程 winEmbed 移植到 VC 中。 重点来了,将winEmbed移植到VC中。 (1)新建工程 MozillaDemo,将 winEmbed 文件夹下的 resource.h、SMALL.ICO、WebBrowserChrome.cpp、WebBrowserChrome.h、WindowCreator.cpp、WindowCreator.h、winEmbed.cpp、winEmbed.h、winEmbed.ICO、winEmbed.rc 引入工程。编译之,你会发现 N 多错误…… (2)在 VC++ 目录中,include 里加入 源码根/dist/include,Library 里加入 源码根/dist/lib,bin 里加入 源码根/dist/bin。 (3)修改一些编译选项。如在预编译选项里添加 XPCOM_GLUE,XP_WIN,_CRT_SECURE_NO_WARNINGS。将 Project->Propeties->Configuration Properties->C/C++->Language 下的 Treat wchar_t as Built-in Type 设为 No (/Zc:wchar_t-),在引入库里添加 xpcomglue.lib。总之让编译命令行看起来像下面这样(具体为何后面解释): 从项目属性的 C/C++ 里看到的编译命令行: /O2 /Oi /GL /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "XPCOM_GLUE" /D "XP_WIN" /D "_CRT_SECURE_NO_WARNINGS" /D "_MBCS" /FD /EHsc /MD /Gy /Zc:wchar_t- /Fo"Release\\" /Fd"Release\vc90.pdb" /W3 /nologo /c /Zi /TP /errorReport:prompt 从项目属性的 Linker 里看到的链接命令行: /OUT:"E:\MZ_Test_Code\MozillaDemo\Release\MozillaDemo.exe" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:"Release\MozillaDemo.exe.intermediate.manifest" /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"e:\MZ_Test_Code\MozillaDemo\Release\MozillaDemo.pdb" /OPT:REF /OPT:ICF /LTCG /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:PROMPT xpcomglue.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib (4)在 winEmbed.cpp 文件里添加一句 #pragma comment(lib, "D:/1.9.2rc1/xulrunner-1.9.2rc1.source/mozilla-1.9.2/profile/dirserviceprovider/standalone/profdirserviceprovidersa_s.lib") 大功告成,编译成功 0 warning, 0 error。此时的程序也可以在别的机子上运行,但是需要将 xulrunner.exe 及其依赖文件拷到别的机子上并注册 GRE。 程序运行示意图: 在自己编译的 xulrunner 环境下跑会产生下面两类错误,先忽略之让程序跑起来。(用官方提供的 xulrunner-sdk 里的程序来注册 GRE 并运行程序无报错) 至于为什么要做(3)和(4),且听我慢慢道来。 为什么要添加预编译选项 XPCOM_GLUE 在原版的 winEmbed 目录下,有 makefile 文件,里面有DEFINES += -DXPCOM_GLUE这么一句。 为什么要添加预编译选项 XP_WIN 在 winEmbed/makefile 文件里,有 include $(DEPTH)/config/autoconf.mk 这么一句,而在这个 autoconf.mk 里可以看到一大串的预编译选项,我试了多番才得出这个非加不可的结论……(试!!!冏!) 为什么要添加引入库 xpcomglue.lib,为什么要有(4)步骤 在 winEmbed/makefile 文件里,有 LIBS = \ $(DEPTH)/profile/dirserviceprovider/standalone/$(LIB_PREFIX)profdirserviceprovidersa_s.$(LIB_SUFFIX) \ $(XPCOM_STANDALONE_GLUE_LDOPTS) \ $(NULL) 这么一段,很显然提示我们引入库 profdirserviceprovidersa_s,然后在 autoconf.mk 文件里可以看到 XPCOM_STANDALONE_GLUE_LDOPTS = $(LIBXUL_DIST)/lib/$(LIB_PREFIX)xpcomglue.$(LIB_SUFFIX) 这么一个定义,所以也需要引入库 xpcomglue。 为什么要将 Project->Propeties->Configuration Properties->C/C++->Language 下的 Treat wchar_t as Built-in Type 设为 No (/Zc:wchar_t-) 很简单,因为编译报错提示呗…… 不执行这些操作将产生的错误: 不将 Project->Propeties->Configuration Properties->C/C++->Language 下的 Treat wchar_t as Built-in Type 设为 No (/Zc:wchar_t-) 将报错 WebBrowserChrome.obj : error LNK2001: unresolved external symbol "public: wchar_t const * __thiscall nsAString::BeginReading(void)const " (?BeginReading@nsAString@@QBEPB_WXZ) 拿掉预编译选项 XP_WIN 会产生错误 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "public: void __thiscall nsCOMPtr_base::assign_from_helper(class nsCOMPtr_helper const &,struct nsID const &)" (?assign_from_helper@nsCOMPtr_base@@QAEXABVnsCOMPtr_helper@@ABUnsID@@@Z) 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "public: void __thiscall nsCOMPtr_base::assign_from_qi(class nsQueryInterface,struct nsID const &)" (?assign_from_qi@nsCOMPtr_base@@QAEXVnsQueryInterface@@ABUnsID@@@Z) 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "public: void __thiscall nsCOMPtr_base::assign_with_AddRef(class nsISupports *)" (?assign_with_AddRef@nsCOMPtr_base@@QAEXPAVnsISupports@@@Z) 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "public: virtual unsigned int __thiscall nsCreateInstanceByContractID::operator()(struct nsID const &,void * *)const " (??RnsCreateInstanceByContractID@@UBEIABUnsID@@PAPAX@Z) 1>WindowCreator.obj : error LNK2001: unresolved external symbol "unsigned int __cdecl NS_TableDrivenQI(void *,struct QITableEntry const *,struct nsID const &,void * *)" (?NS_TableDrivenQI@@YAIPAXPBUQITableEntry@@ABUnsID@@PAPAX@Z) 1>winEmbed.obj : error LNK2001: unresolved external symbol "public: void __thiscall nsCOMPtr_base::assign_from_gs_contractid_with_error(class nsGetServiceByContractIDWithError const &,struct nsID const &)" (?assign_from_gs_contractid_with_error@nsCOMPtr_base@@QAEXABVnsGetServiceByContractIDWithError@@ABUnsID@@@Z) 1>winEmbed.obj : error LNK2001: unresolved external symbol "public: void __thiscall nsCOMPtr_base::assign_from_gs_contractid(class nsGetServiceByContractID,struct nsID const &)" (?assign_from_gs_contractid@nsCOMPtr_base@@QAEXVnsGetServiceByContractID@@ABUnsID@@@Z) 1>winEmbed.obj : error LNK2001: unresolved external symbol "public: virtual unsigned int __thiscall nsGetInterface::operator()(struct nsID const &,void * *)const " (??RnsGetInterface@@UBEIABUnsID@@PAPAX@Z) 拿掉预编译选项 XPCOM_GLUE 会产生错误 1>.\winEmbed.cpp(48) : error C2146: syntax error : missing ';' before identifier 'XRE_InitEmbedding' 1>.\winEmbed.cpp(48) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>.\winEmbed.cpp(48) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>.\winEmbed.cpp(48) : error C2365: 'XRE_InitEmbedding' : redefinition; previous definition was 'function' 1> D:\1.9.2rc1\xulrunner-1.9.2rc1.source\mozilla-1.9.2\dist\include\nsXULAppAPI.h(355) : see declaration of 'XRE_InitEmbedding' 1>.\winEmbed.cpp(49) : error C2146: syntax error : missing ';' before identifier 'XRE_TermEmbedding' 1>.\winEmbed.cpp(49) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>.\winEmbed.cpp(49) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>.\winEmbed.cpp(49) : error C2365: 'XRE_TermEmbedding' : redefinition; previous definition was 'function' 1> D:\1.9.2rc1\xulrunner-1.9.2rc1.source\mozilla-1.9.2\dist\include\nsXULAppAPI.h(390) : see declaration of 'XRE_TermEmbedding' 1>.\winEmbed.cpp(191) : error C3861: 'XPCOMGlueStartup': identifier not found 1>.\winEmbed.cpp(209) : error C2659: '=' : function as left operand 1>.\winEmbed.cpp(209) : error C2146: syntax error : missing ';' before identifier 'GetProcAddress' 1>.\winEmbed.cpp(210) : warning C4551: function call missing argument list 1>.\winEmbed.cpp(216) : error C2659: '=' : function as left operand 1>.\winEmbed.cpp(216) : error C2146: syntax error : missing ';' before identifier 'GetProcAddress' 拿掉引入库 xpcomglue.lib 会产生错误 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol _NS_StringContainerFinish 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol _NS_StringContainerFinish 1>profdirserviceprovidersa_s.lib(nsProfileLock.obj) : error LNK2001: unresolved external symbol _NS_StringContainerFinish 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol _NS_CStringCopy 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "public: unsigned short const * __thiscall nsAString::BeginReading(void)const " (?BeginReading@nsAString@@QBEPBGXZ) 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol "public: unsigned short const * __thiscall nsAString::BeginReading(void)const " (?BeginReading@nsAString@@QBEPBGXZ) 1>profdirserviceprovidersa_s.lib(nsProfileLock.obj) : error LNK2001: unresolved external symbol "public: unsigned short const * __thiscall nsAString::BeginReading(void)const " (?BeginReading@nsAString@@QBEPBGXZ) 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol _NS_StringContainerInit 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol _NS_StringContainerInit 1>profdirserviceprovidersa_s.lib(nsProfileLock.obj) : error LNK2001: unresolved external symbol _NS_StringContainerInit 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol _NS_CStringSetDataRange 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol _NS_CStringToUTF16 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol _NS_CStringSetData 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "public: void __fastcall nsCOMPtr_base::assign_from_helper(class nsCOMPtr_helper const &,struct nsID const &)" (?assign_from_helper@nsCOMPtr_base@@QAIXABVnsCOMPtr_helper@@ABUnsID@@@Z) 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "public: void __fastcall nsCOMPtr_base::assign_from_qi(class nsQueryInterface,struct nsID const &)" (?assign_from_qi@nsCOMPtr_base@@QAIXVnsQueryInterface@@ABUnsID@@@Z) 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "public: void __fastcall nsCOMPtr_base::assign_with_AddRef(class nsISupports *)" (?assign_with_AddRef@nsCOMPtr_base@@QAIXPAVnsISupports@@@Z) 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "public: __thiscall nsCOMPtr_base::~nsCOMPtr_base(void)" (??1nsCOMPtr_base@@QAE@XZ) 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol _NS_CStringContainerInit 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol _NS_CStringContainerInit 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol _NS_CStringContainerFinish 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol _NS_CStringContainerFinish 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "public: virtual unsigned int __fastcall nsCreateInstanceByContractID::operator()(struct nsID const &,void * *)const " (??RnsCreateInstanceByContractID@@UBIIABUnsID@@PAPAX@Z) 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol _NS_CStringGetData 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "protected: void __thiscall nsSupportsWeakReference::ClearWeakReferences(void)" (?ClearWeakReferences@nsSupportsWeakReference@@IAEXXZ) 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "public: virtual unsigned int __stdcall nsSupportsWeakReference::GetWeakReference(class nsIWeakReference * *)" (?GetWeakReference@nsSupportsWeakReference@@UAGIPAPAVnsIWeakReference@@@Z) 1>WebBrowserChrome.obj : error LNK2001: unresolved external symbol "class nsIWeakReference * __cdecl NS_GetWeakReference(class nsISupports *,unsigned int *)" (?NS_GetWeakReference@@YAPAVnsIWeakReference@@PAVnsISupports@@PAI@Z) 1>WindowCreator.obj : error LNK2001: unresolved external symbol "unsigned int __fastcall NS_TableDrivenQI(void *,struct QITableEntry const *,struct nsID const &,void * *)" (?NS_TableDrivenQI@@YIIPAXPBUQITableEntry@@ABUnsID@@PAPAX@Z) 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol "unsigned int __fastcall NS_TableDrivenQI(void *,struct QITableEntry const *,struct nsID const &,void * *)" (?NS_TableDrivenQI@@YIIPAXPBUQITableEntry@@ABUnsID@@PAPAX@Z) 1>winEmbed.obj : error LNK2001: unresolved external symbol "public: char const * __thiscall nsACString::BeginReading(void)const " (?BeginReading@nsACString@@QBEPBDXZ) 1>winEmbed.obj : error LNK2001: unresolved external symbol _GRE_GetGREPathWithProperties 1>winEmbed.obj : error LNK2001: unresolved external symbol _NS_NewNativeLocalFile 1>winEmbed.obj : error LNK2001: unresolved external symbol "public: void __fastcall nsCOMPtr_base::assign_from_gs_contractid_with_error(class nsGetServiceByContractIDWithError const &,struct nsID const &)" (?assign_from_gs_contractid_with_error@nsCOMPtr_base@@QAIXABVnsGetServiceByContractIDWithError@@ABUnsID@@@Z) 1>winEmbed.obj : error LNK2001: unresolved external symbol "public: void __fastcall nsCOMPtr_base::assign_from_gs_contractid(class nsGetServiceByContractID,struct nsID const &)" (?assign_from_gs_contractid@nsCOMPtr_base@@QAIXVnsGetServiceByContractID@@ABUnsID@@@Z) 1>winEmbed.obj : error LNK2001: unresolved external symbol _NS_CStringContainerInit2 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol _NS_CStringContainerInit2 1>winEmbed.obj : error LNK2001: unresolved external symbol _XPCOMGlueStartup 1>winEmbed.obj : error LNK2001: unresolved external symbol "public: virtual unsigned int __fastcall nsGetInterface::operator()(struct nsID const &,void * *)const " (??RnsGetInterface@@UBIIABUnsID@@PAPAX@Z) 1>winEmbed.obj : error LNK2001: unresolved external symbol _NS_StringContainerInit2 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol _NS_StringContainerInit2 1>profdirserviceprovidersa_s.lib(nsProfileLock.obj) : error LNK2001: unresolved external symbol _NS_StringContainerInit2 1>winEmbed.obj : error LNK2001: unresolved external symbol _NS_UTF16ToCString 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol _NS_DebugBreak 1>profdirserviceprovidersa_s.lib(nsProfileLock.obj) : error LNK2001: unresolved external symbol _NS_DebugBreak 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol _NS_LogAddRef 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol _NS_LogRelease 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol _NS_StringGetData 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol _NS_LogCOMPtrRelease 1>profdirserviceprovidersa_s.lib(nsProfileLock.obj) : error LNK2001: unresolved external symbol _NS_LogCOMPtrRelease 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol _NS_LogCOMPtrAddRef 1>profdirserviceprovidersa_s.lib(nsProfileLock.obj) : error LNK2001: unresolved external symbol _NS_LogCOMPtrAddRef 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol "public: unsigned int __fastcall nsGetServiceByContractIDWithError::operator()(struct nsID const &,void * *)const " (??RnsGetServiceByContractIDWithError@@QBIIABUnsID@@PAPAX@Z) 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol "public: unsigned int __fastcall nsQueryInterface::operator()(struct nsID const &,void * *)const " (??RnsQueryInterface@@QBIIABUnsID@@PAPAX@Z) 1>profdirserviceprovidersa_s.lib(nsProfileLock.obj) : error LNK2001: unresolved external symbol "public: unsigned int __fastcall nsQueryInterface::operator()(struct nsID const &,void * *)const " (??RnsQueryInterface@@QBIIABUnsID@@PAPAX@Z) 1>profdirserviceprovidersa_s.lib(nsProfileDirServiceProvider.obj) : error LNK2001: unresolved external symbol "public: unsigned int __fastcall nsGetServiceByContractID::operator()(struct nsID const &,void * *)const " (??RnsGetServiceByContractID@@QBIIABUnsID@@PAPAX@Z) 添加预编译选项 _CRT_SECURE_NO_WARNINGS 用来消除警告 1>e:\mz_test_code\mozillademo\mozillademo\webbrowserchrome.cpp(375) : warning C4996: '_snprintf': This function or variable may be unsafe. Consider using _snprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> d:\program files\microsoft visual studio 9.0\vc\include\stdio.h(358) : see declaration of '_snprintf' 1>WindowCreator.cpp 1>winEmbed.cpp 1>e:\mz_test_code\mozillademo\mozillademo\winembed.cpp(163) : warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> d:\program files\microsoft visual studio 9.0\vc\include\string.h(157) : see declaration of 'strncpy' 1>e:\mz_test_code\mozillademo\mozillademo\winembed.cpp(198) : warning C4996: '_snprintf': This function or variable may be unsafe. Consider using _snprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> d:\program files\microsoft visual studio 9.0\vc\include\stdio.h(358) : see declaration of '_snprintf' 1>e:\mz_test_code\mozillademo\mozillademo\winembed.cpp(1086) : warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> d:\program files\microsoft visual studio 9.0\vc\include\string.h(157) : see declaration of 'strncpy' winEmbed.cpp文件里添加#pragma comment(lib, “D:/1.9.2rc1/xulrunner-1.9.2rc1.source/mozilla-1.9.2/profile/dirserviceprovider/standalone/profdirserviceprovidersa_s.lib”) 否则将产生错误 1>winEmbed.obj : error LNK2019: unresolved external symbol "unsigned int __cdecl NS_NewProfileDirServiceProvider(int,class nsProfileDirServiceProvider * *)" (?NS_NewProfileDirServiceProvider@@YAIHPAPAVnsProfileDirServiceProvider@@@Z) referenced in function "unsigned int __cdecl StartupProfile(void)" (<a href="mailto:?StartupProfile@@YAIXZ">?StartupProfile@@YAIXZ</a>)

    2012/02/24 Gecko

  5. HGE 引擎学习笔记 1——如何模拟物理碰撞

    模拟物理碰撞要解决的几个问题: 怎样模拟速度的变化? 设置一个摩擦系数 friction(0<friction<1.0) 和响应用户按键之后的一个 X 坐标单时间片增量 dx,一个 Y 坐标单时间片增量 dy,每隔一个时间片 dx *= friction; dy *= friction; 只要参数设置得当,看起来就会觉得速度自然地减慢。因为我们所用的浮点数的数据类型的精度限制,物体在经过一定数量时间片后就会停下来。 怎样模拟碰撞? 每个时间片处理过程中,判断物体的边缘坐标加上 dx 与 dy 后有没有超过屏幕边缘,如果超过,则采取一定的策略重新设置物体坐标让其在正常范围内,如 X 轴超过,则对 dx 取反;如 Y 轴超过,则对 dy 取反。计算好坐标之后再进行绘图。 碰撞过程中的声音处理 这里涉及到音量,左右声道,播放速度。对音量和播放速度可以按照场景来设置,可以考虑根据 X,Y 坐标作为其中一个参数,左右声道比较合理的处理方案是根据窗口宽度和物体 X 坐标来决定左右声道的混合比例。 WINDOWS SDK 窗口对此过程的模拟(仅摹仿了速度和碰撞等,对声音的相关处理貌似比较复杂,还没搞清楚怎么写。由于是做个简单 DEMO,并没有加入多线程等技术,所以程序里的坐标等数据的同步并不精准,上、下、左、右键最好是短暂地点一下即松开,连着按的话会出现速度的突兀变化): /** * FILE : collision.cpp * 功能 : 模拟一个小球在一个封闭区域内的碰撞等活动 * 作者 : Zhuang Ma( http://www.mazhuang.org ) * 声明 : 版权没有 盗版不究 */ #include <windows.h> float x = 100.0f; // 球的中心点 X 坐标 float y = 100.0f; // 球的中心点 Y 坐标 float speed = 10.0f; // 球响应按钮后的初始速度 float friction = 0.99f; // 球与地面的摩擦系数 float dx = 0.0f; // X 轴增量 float dy = 0.0f; // Y 轴增量 LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int nCmdShow) { WNDCLASS wc; MSG msg; HWND hWnd; if( !hPrevInstance ) { wc.lpszClassName = "GenericAppClass"; wc.lpfnWndProc = MainWndProc; wc.style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW; wc.hInstance = hInstance; wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = (HBRUSH)( COLOR_WINDOW+1 ); wc.lpszMenuName = "GenericAppMenu"; wc.cbClsExtra = 0; wc.cbWndExtra = 0; RegisterClass( &wc ); } hWnd = CreateWindow( "GenericAppClass", "Happy Ball", WS_OVERLAPPEDWINDOW & (~WS_MAXIMIZEBOX) & (~WS_THICKFRAME), 100, 100, 800, 600, NULL, NULL, hInstance, NULL ); ShowWindow( hWnd, nCmdShow ); while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } return msg.wParam; } LRESULT CALLBACK MainWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static PAINTSTRUCT ps; static HDC hDC; static HBRUSH hBrush; static RECT rect; GetClientRect(hwnd, &rect); const int nRadius = 16; switch (message) { case WM_CREATE: SetTimer(hwnd, 1, 5, NULL); hBrush = CreateSolidBrush(RGB(0,0,0)); return 0; case WM_PAINT: hDC = BeginPaint(hwnd, &ps); SelectObject(hDC, hBrush); Ellipse(hDC, x - nRadius, y - nRadius, x + nRadius, y + nRadius); EndPaint(hwnd, &ps); break; case WM_KEYDOWN: switch (wParam) { case VK_UP: dy -= speed; break; case VK_DOWN: dy += speed; break; case VK_LEFT: dx -= speed; break; case VK_RIGHT: dx += speed; break; } break; case WM_TIMER: dx *= friction; dy *= friction; x += dx; y += dy; if (x > rect.right - nRadius) { x = (rect.right - nRadius) - (x - (rect.right - nRadius)); dx = -dx; } if (x < nRadius) { x = nRadius + nRadius - x; dx = -dx; } if (y > rect.bottom - nRadius) { y = rect.bottom - nRadius - (y - (rect.bottom - nRadius)); dy = -dy; } if (y < nRadius) {y = nRadius + nRadius - y; dy = -dy; } InvalidateRect(hwnd, &rect, TRUE); break; case WM_DESTROY: KillTimer(hwnd, 1); DeleteObject(hBrush); PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } HGE 示例源码及 Win32 SDK 源码及可运行程序下载地址:http://download.csdn.net/detail/mzlogin/4059869

    2012/02/12 HGE

  6. 简单的全排列算法实现

    问题描述 实现一个简单的全排列算法,以整形数组{1,2,3,4,5}为例,假设元素无重复。 问题分析 如果用多层循环来实现,那么……有多少个元素将需要有多少层循环,这样作为实现一个算法的角度来看显然是不可取的。 以 a[] = {1,2,3,4,5}为例,它的全排列是 1 {2,3,4,5}的全排列 2 {1,3,4,5}的全排列 3 {1,2,4,5}的全排列 4 {1,2,3,5}的全排列 5 {1,2,3,4}的全排列 由子数组的全排列得到母数组的全排列结果,可以考虑用递归实现,具体可以设计为将 a 依次变换为 12345 21345 31245 41235 51234 然后分别求它们后四个元素的全排列,依此类推。 简单的 C++ 实现 #include <iostream> using namespace std; static int n = 0; void swapint(int *p, int *q) { int tmp = *p; *p = *q; *q = tmp; } void fullarray(int a[], int iLen, // 数组长度 int iStart) // 开始下标 { if (iLen == iStart) { for (int i = 0; i < iLen; ++i) { cout << a[i] << " "; } cout << "\n"; n++; } else { for(int j = iStart; j < iLen; ++j) { swapint(&a[iStart], &a[j]); fullarray(a, iLen, iStart + 1); swapint(&a[iStart], &a[j]); } } } int main() { int a[] = {1,2,3,4,5}; fullarray(a, sizeof(a)/sizeof(int), 0); cout << "总共" << n << "种" << endl; return 0; } 参考:http://www.cnblogs.com/nokiaguy/archive/2008/05/11/1191914.html

    2011/11/20 Algorithm

  7. 操作系统和数据库基础

    进程与线程的差别 进程是程序的一次执行。线程可以理解为进程中执行的一段程序片段。在一个多任务环境下中下面的概念可以帮助我们理解两者的区别。 进程间是独立的,这表现在内存空间,上下文环境上;线程运行在进程空间里。一般来讲(不使用特殊技术),进程无法突破进程边界存取其它进程内的存储空间;而线程由于处于线程空间内,所以同一进程所产生的线程共享内一内存空间。同一进程中的两段代码不能够同时执行,除非引入线程。 线程是属于进程的,当进程退出时该进程所产生的线程都会被强制退出并清除。线程占用的资源要少于进程所占用的资料。 进程和线程都可以有优先级。 进程间通信的方式 现在最常用进程间通信的方式有信号,信号量,消息队列,共享内存。 线程同步的方法 临界区,互斥量,信号量,事件。 DLL 文件的作用 (1)拓展应用程序 (2)便于程序员合作 (3)节省内存 (4)共享程序资源 (5)解决应用程序本地化问题 什么是存储过程? 存储过程是一组为了完成特定功能的 SQL 语句集。 什么是触发器? 触发器是一个特殊的存储过程,它的执行不是由程序调动,也不由手工启动,而是由事件来触发。 什么是事务? 事务是访问并可能更新数据库中各种数据项的一个程序执行单元。 (在关系数据库中,一个事务可以是一条SQL语句,一组 SQL 语句或整个程序) 事务的几个特性 (1)一致性:将事务中所做的操作捆绑成一个原子单元,即对于事务所进行的数据修改等操作,要么全部执行,要么全部不执行。 (2)原子性:事务在完成时,必须使所有的数据都保持一致状态,而且在相关数据中,所有规则都必须应用于事务的修改,以保持所有数据的完整性。事务结束时,所有的内部数据结构都应该是正确的。 (3)隔离性:由并发事务所做的修改必须与任何其他事务所做的修改相隔离。事务查看数据时数据所处的状态,要么是被另一并发事务修改之前的状态,要么是被另一并发事务修改之后的状态。即事务不会查看由另一个并发事务正在修改的数据。这种隔离方式也叫可串行性。 (4)持久性:事务完成之后,它对系统的影响是永久的,即使出现系统故障也是如此。 什么是 ADO?它与 ODBC 比起来哪种方式快? ADO(ActiveX Data Objects)是一个用于存取数据源的 COM 组件。它提供了编程语言和统一数据访问方式的 OLE DB 的一个中间层。允许开发人员编写访问数据的代码而不用关心数据库是如何实现的,而只用关心到数据库的连接。 ADO 更快。(这个网上争议很多)

    2011/11/06 Basis

  8. C++ 语言基础

    面试中遇到的 C++ 语言基础试题。 new 与 malloc 的区别 (1)new 是 C++ 操作符,malloc 是 C 库函数。 (2)对于非内部数据结构的对象而言,malloc 无法满足动态对象的要求。对象在创建的同时要自动执行构造函数,对象在消亡之前要自动执行析构函数,由于 malloc 是库函数而不是运算符,不在编译器的控制权限之内,不能够把执行构造函数和析构函数的任务强加于 malloc/free。 (3)new 可以认为是 malloc 加构造函数的执行。 (4)new 出来的是对象,而 malloc 出来的是 (void *) 如何禁用掉拷贝构造函数与赋值操作符? 在类里声明但是不定义。禁用后可以防止”浅拷贝”。 附:浅拷贝指当一个类里有指针成员指向 new 出来的数据时,当用一个对象来初始化另一个对象的时候,若将数据重新 new 一个,则为深拷贝,否则为浅拷贝。浅拷贝容易造成的问题是当一个对象析构后另一个对象访问该指针会出问题。 调用拷贝构造函数的情况:(1)一个对象以值传递的方式传入函数体(2)一个对象以值传递的方式从函数返回(3)一个对象定义时需要通过另一个对象进行初始化。 一个空类,编译器会默认为它加上什么东西? (1)默认构造函数 (2)拷贝构造函数 (3)析构函数 (4)赋值运算符 C++ 中向函数传递参数的方式 (1)值传递 (2)指针传递 (3)引用传递 666 个苹果,10 个箱子,如何放能让客人买 1-666 个苹果都直接给若干箱子就行了。 1,2,4,8,16,32,64,128,256,155.(用程序如何实现?) 二叉树的结点定义和求叶子数 结点{数据,左右孩子指针} 求叶子数:递归解决。 template<typename T> class BinTreeNode { public: T m_data; BinTreeNode* m_pLeftChild; BinTreeNode* m_pRightChild; BinTreeNode(T data): m_data(data), m_pLeftChild(NULL), m_pRightChild(NULL) {} ~BinTreeNode() {} }; template<typename T> int GetLeafCount(BinTreeNode<T>* pRoot) { if (NULL == pRoot) { return 0; } else if (NULL == pRoot->m_pLeftChild && NULL == pRoot->m_pRightChild) { return 1; } else { return GetLeafCount(pRoot->m_pLeftChild)+GetLeafCount(pRoot->m_pRightChild); } } 什么时候调用拷贝构造函数?什么时候调用赋值运算符? 如果在进行赋值操作时目标对象已经取得内存,则调用赋值运算符;如果赋值操作与目标对象的内存分配是一起进行的,则调用拷贝构造函数。见如下代码: #include <iostream> using namespace std; class demo { public: demo() { cout << "default constructor" << endl; } demo(const demo& d) { cout << "copy constructor" << endl; } demo& operator=(const demo& d) { cout << "= operator" << endl; } ~demo() {} }; int main() { demo A; demo B; B = A; demo C = A; return 0; } 输出结果: default constructor default constructor = operator copy constructor

    2011/11/04 Basis

  9. 青蛙跳台阶的问题——Fibonacci

    这几天正在复习算法,今天在看一篇文章时偶然看到这个题目,想了一下居然没什么思路……(抱歉,实在太菜。),文章中提示了一个关键词:Fibonacci 数列。然后我又小百度了一下,找了一个具体分析实例,结合两处,这才理清了思路。(汗啊……基础全忘光了,这以后咋办啊……深感担忧……) 问题描述 一只青蛙一次可以跳上 1 级台阶,也可以跳上 2 级台阶,请问这只青蛙跳上 n 级的台阶总共有多少种跳法? 问题分析 设青蛙跳上 n 级台阶的跳法为 f(n) 种。 设 Fibonacci 数列的第 x 项值为 fibo(x)。 当 n=1 时,f(n)=1=fibo(2) 当 n=2 时,f(n)=2=fibo(3) 当 n>2 时,分析可知,在跳上第 n 级台阶前一步,必然是在第 (n-1) 或 (n-2) 级台阶,故有 f(n) = f(n-1) + f(n-2); 依此类推…… 则有: f(n) = f(n-1) + f(n-2) = 2f(n-2) + f(n-3) = 3f(n-3) + 2f(n-4) = 5 f(n-4) + 3f(n-5) = 8f(n-5) + 5f(n-6) = … = fibo(x+1)f(n-x)+fibo(x)f(n-(x+1)) =… = fibo(n-1)f(n-(n-2)) + fibo(n-2)f(n-(n-1)) = fibo(n-1)f(2) + fibo(n-2)f(1) f(n) 的规律符合 Fibonacci 数列的规律,它与 Fibonacci 的区别是 Fibonacci 的前两个元素是 1,1,而 f(n) 的规律是 1,2,即可知有 f(n)=fibo(n+1)。 简单的 C++ 实现 #include <iostream> using namespace std; // 非递归写法 int fibo(int n) // 获取 Fibonacci 数列的第 N 项值 { if(n == 1 || n == 2) return 1; else { int a = 1; int b = 1; int tmp; for(int i = 3; i <= n; ++ i) { tmp = a; a = b; b += tmp; } return b; } } //// 递归写法 //int fibo(int n) //{ // if(n == 1 || n == 2) // return 1; // else // return fibo(n-1) + fibo(n-2); //} int main() { cout << "请输入楼梯的级数:"; int n; cin >> n; int sum; //if(1==n) // sum = 1; //else if (2==n) // sum = 2; //else //{ // sum = 2 * fibo(n-1) + fibo(n-2); //} sum = fibo(n+1); cout << "共有 " << sum << " 种跳法。" << endl; return 0; }

    2011/10/11 Algorithm