自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

  • 博客(0)
  • 资源 (56)
  • 收藏
  • 关注

空空如也

精通Oracle10编程.pdf

Oracle公司成立以来,从最初的数据库版本到Oracle7、Oracle8i、Oracle9i,Oracle10g到Oracle11g,虽然每一个版本之间的操作都存在一定的差别,但是Oracle对数据的操作基本上都遵循SQL标准。因此对Oracle开发来说版本之间的差别不大。 很多人没有学习Oracle就开始发怵,因为人们在误解Oracle,认为Oracle太难学了,认为Oracle不是一般人用的数据库,其实任何数据库对应用程序研发人员来说,都是大同小异,因为目前多数数据库都支持标准的SQL。在Oracle这本书中,我们能学习到:  Oracle的安装  Oracle数据管理  常用子查询及常用函数  PL/SQL编程  Oracle基本管理

2012-10-17

oracle 10g 教程.pdf

Oracle数据库是Oracle(中文名称叫甲骨文)公司的核心产品,Oracle数据库是一个适合于大中型企业的数据库管理系统。在所有的数据库管理系统中(比如:微软的SQL Server,IBM的DB2等),Oracle的主要用户涉及面非常广,包括:银行、电信、移动通信、航空、保险、金融、电子商务和跨国公司等。Oracle产品是免费的,可以在Oracle官方网站上下载到安装包,另一方面Oracle服务是收费的。

2012-10-17

Oracle经典教程.doc

在第一学期我们已经接触过关系型数据库SQL Server,对数据库、表、记录、表的增删改查操作等这些基本的概念已经了解。Oracle是基于对象的关系型数据库,Oracle也是用表的形式对数据存储和管理,并且在Oracle的操作中添加了一些面向对象的思想。

2012-10-17

JSP大文件上传控件-access-utf8

HttpUploader4全面升级了文件IO组件。新的IO组件在处理磁盘中的文件时,将不必再对文件执行I/O操作,这意味着在对文件进行处理时将不必再为文件申请并分配缓存,所有的文件缓存操作均由系统直接管理,由于取消了将文件数据加载到内存、数据从内存到文件的回写以及释放内存块等步骤,使得HttpUploader4在处理TB级数据时能够拥有闪电般的速度。 新的IO组件赋予了HttpUploader4更强的大数据处理能力。现在HttpUploader4在对GB级文件进行MD5校验时速度提高了4倍。同时CPU占用率更低。 HttpUploader4更加注重对硬盘的保护,在HttpUploader4中不再直接对文件进行I/O操作,而是在内存中对文件进行操作,所以不仅极大的减少了对硬盘的读写次数,同时速度却变的更快了。 借助于HttpUploader4企业能够帮助用户更加轻松的处理工作中的文件,让用户与用户之间的沟通更加的高效。从根本上提高企业竞争力。 考虑到不同的企业使用的开发平台不同,我们已经为企业开发人员提供了完整的与数据库相结合的示例(ASP.NET,JSP,PHP)。开发人员能够非常容易的在自已的系统中实现断点续传功能。 产品特点如下: 1. 为TB级文件提供稳定传输功能。 2. 优化MD5组件,文件扫描速度提升70%。 3. 保护磁盘,上传超大文件时,磁盘IO次数降低50%。 4. 采用全新设计IO组件,上传任意文件大小时始终占用128KB内存。 5. 支持文件及文件夹拖拽上传功能。 6. 支持文件批量上传。 7. 支持文件夹上传。 8. 基于标准HTTP协议。 9. 免费提供JavaScript SDK包,方便您将插件快速集成到已有网站中。 支持语言:PHP,JSP,ASP,ASP.NET(C#),ASP.NET(VB),C++,VC,VC.NET,VB,VB.NET,C#,C#.NET,Delphi,C++Builder 支持平台:Visual Studio 6.0/2002/2003/2005/2008/2010,C++ Builder 6.0/2009/2010,Delphi 7/2009,Visual Basic 6.0/2008,MyEclipse8.x 支持脚本:JavaScript,VBScript 支持服务器:Windows NT,Windows 2003,Windows XP,Windows Vista,Windows 7,Linux,Unix 支持浏览器:IE6,IE7,IE8,360安全浏览器,QQ浏览器,搜狐浏览器,Maxthon(遨游)浏览器1.X,Maxthon(傲游)浏览器2.x 支持文件大小:2G~8EB(1EB=102PB,1PB=1024TB,1TB=1024GB) 支持文件类型:任意类型 版权所有 2009-2012 武汉命运科技有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/http-uploader3/index.aspx 在线演示:http://www.ncmem.com/products/http-uploader3/demo/index.html 产品介绍:http://www.cnblogs.com/xproer/archive/2012/05/29/2523757.html 开发文档-ASP:http://www.cnblogs.com/xproer/archive/2012/02/17/2355458.html 开发文档-PHP:http://www.cnblogs.com/xproer/archive/2012/02/17/2355467.html 开发文档-JSP:http://www.cnblogs.com/xproer/archive/2012/02/17/2355462.html 开发文档-ASP.NET:http://www.cnblogs.com/xproer/archive/2012/02/17/2355469.html 升级日志:http://www.cnblogs.com/xproer/archive/2012/02/17/2355449.html 示例下载:http://www.ncmem.com/download/HttpUploader4-demo.rar 文档下载:http://www.ncmem.com/download/HttpUploader4-doc.rar 镜像下载(DBank):cab安装包,开发文档 镜像下载(JSP):cab安装包,开发文档,ASP.NET-ACCESS示例,JSP-ACCESS示例(GB2312),JSP-ACCESS示例(UTF-8),JSP-Sql2005示例(UTF-8),JSP-MySQL示例(UTF-8) 镜像下载(PHP):MySQL示例(UTF-8) 问题反馈:http://www.ncmem.com/blog/guestbook.asp 数字证书补丁:http://www.ncmem.com/download/rootsupd.rar VC运行库:http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=29 联系信箱:1085617561@qq.com 联系QQ:1085617561 技术QQ:1269085759

2012-08-22

HTTP断点续传上传控件

现在大部分的网站使用的是标准HTML的上传方式来上传文件。一般情况下标准HTML方式在网页中只能上传4MB左右的文件,如果访问的用户比较多的时侯这种方式容易上传失败。虽然标准HTML上传方式开发起来比较简单,但是这种方式用户体验比较差,上传的文件大小受到限制,所以如果我们需要上传上百或者更大的上G的文件时,HTML标准上传方式是无法满足我们的需求的。 而另一方面,随着互联网行业的发展用户产生的新的需求也越来越多,同时对用户体验也提出了更高的要求,传统的HTML方式也越来越难已满足新的用户需求。现在大部分的用户有文件批量上传的需求,希望只通过点击一次鼠标就能够批量的上传多张图片,而不是一张张的选择文件上传,这样操作即浪费时间又非常烦琐。 近年来,由于数码和影视行业的迅猛发展刺激了用户对大文件的上传需求,现在越来越多的用户希望能够通过WEB的方式上传更大的文件,比如电影和图片。这些类型的文件通常都非常大,一般都在500MB以上,高清的影视文件至少在1G以上。这样的大文件是根本无法通过标准HTML方式来上传的。 不仅如此,由于国内网络环境比较特殊,有许多地区的网络不够稳定,在上传文件的过程中可能会发生断网的情况。如果用户正在上传一个1000MB的文件,已经上传了500MB,这时网络出现问题上传中止了。那么下一次用户需要要重新上传前面的500MB,而不是从500MB开始上传,这将浪费用户的许多时间。 新颖网络HTTP文件断点续传控件是专门用于解决HTTP大文件上传的需求而开发的产品。通过我们的HttpPartition模块用户能够非常方便的一次性选择超过200个的文件。而且我们升级了用户体验,用户现在不仅能够通过点击按钮来选择多个文件,还可以通过HttpDroper来拖拽文件甚至是文件夹。 现在我们能够轻松支持2G左右的大文件上传。为了减轻服务器的压力在HttpUploader模块中我们并不是一次上传2G的数据,而是将2G化分为小的数据块,每次向服务器上传约128KB左右的数据。同时在每次上传的数据中附带了文件大小,起始位置,文件MD5等信息。对于开发人员来说,有了这些信息,断点续传功能将会变的和普通的文件上传功能一样简单。 相信新颖网络HTTP断点续传控件能够帮助您赢利市场。 版权所有 2009-2012 北京新颖网络 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/http-uploader3/index.aspx 在线演示:http://www.ncmem.com/products/http-uploader/demo/index.html 产品介绍:http://www.cnblogs.com/xproer/archive/2012/02/17/2355440.html 开发文档-ASP:http://www.cnblogs.com/xproer/archive/2012/02/17/2355458.html 开发文档-PHP:http://www.cnblogs.com/xproer/archive/2012/02/17/2355467.html 开发文档-JSP:http://www.cnblogs.com/xproer/archive/2012/02/17/2355462.html 开发文档-ASP.NET:http://www.cnblogs.com/xproer/archive/2012/02/17/2355469.html 升级日志:http://www.cnblogs.com/xproer/archive/2012/02/17/2355449.html 示例下载:http://www.ncmem.com/download/HttpUploader3-demo.rar 文档下载:http://www.ncmem.com/download/HttpUploader3-doc.rar 问题反馈:http://www.ncmem.com/blog/guestbook.asp Windows数字证书补丁:http://www.ncmem.com/download/rootsupd.rar Microsoft Visual C++ 2008 Redistributable Package (x86):http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=29

2012-02-21

MSYS安装与使用PDF教程

介绍、下载与安装方法 安装方法 这里我们采用了7z 格式压缩了所有需要解压缩的包,如果您打不开下载的文件,或者下载 后解压缩出现问题,可能是您用的winrar 版本过老,这些压缩包是没有问题的,强烈推荐 您使用免费、开放源代码、压缩比更高的7z文件压缩工具: http://www.7-zip.org/ 选择 1 首先,请先阅读本页最后的注意事项,然后安装下列基础包: · MSYS基础系统:http://msys-cn.googlecode.com/files/MSYS.7z 以下只选其一 · GCC 4 编译器:http://msys-cn.googlecode.com/files/mingw.7z · GCC 3 编译器:http://msys-cn.googlecode.com/files/mingw3.7z · 注意:GCC 3 与GCC 4 各有好处,3 对于大多数开源软件包兼容性最好,但是 mingw官方编译的时候没有添加iconv,也就是没有unicode支持,而这里的GCC 4

2012-01-01

FCKEditor2.6.4.1

FCKeditor文本编辑程序(共享软件)为用户提供在线的文档编辑服务,其具有与微软office软件一样的功能,与之不同的是FCKeditor不需要用户安装任何形式的客户端,FCKeditor程序非常精简但功能强大,因此而受到广大应用者的青睐。在博客日益兴盛的web2.0时代FCKeditor已经开始走向普通人的视线。 FCKeditor特性: 完成工具栏定制 皮肤支持 Plugins 支持 拼写检查程序 多语言支持以及自动用户语言侦查。 轻量级和快速 自动浏览器侦查和定制 支持多种编程语言 支持开发者安装和定做特定程序 对网友它是简单和容易使用! 完成工具栏定制 皮肤支持 Plugins 支持 拼写检查程序 多语言支持以及自动用户语言侦查。 轻量级和快速 自动浏览器侦查和定制 支持多种编程语言 支持开发者安装和定做特定程序 对网友它是简单和容易使用! 同时,FCKeditor支持以下编程语言环境: ASP.Net ASP ColdFusion PHP Java Active-FoxPro Lasso Perl Python 最新版本的FCKeditor(3.5)同时兼容绝大多数主流浏览器,包括: IE 5.5及以上版本 (windows), 火狐Firefox 1.0及以上版本, 遨游Mozilla 1.3及以上版本,网景7.0及以上版本。

2011-04-10

Google C++ 编码规范

为PDF文件增加了目录,便于快速浏览。 摘录一些不知道以及没有做到的: 1、防止多重包含(头文件的形式不能完美做到) 所有头文件都应该使用#define防止头文件被多重包含(multiple inclusion),命名格式当是:___H_ 为保证唯一性,头文件的命名应基于其所在项目源代码树的全路径。例如,项目foo中的头文件foo/src/bar/baz.h按如下方式保护: #ifndef FOO_BAR_BAZ_H_ #define FOO_BAR_BAZ_H_ ... #endif // FOO_BAR_BAZ_H_ 2、能依赖声明就不要依赖定义 使用前置声明(forward declarations)尽量减少.h文件中#include的数量。 当一个头文件被包含的同时也引入了一项新的依赖(dependency),只要该头文件被修改,代码就要重新编译。如果你的头文件包含了其他头文件,这些头文件的任何改变也将导致那些包含了你的头文件的代码重新编译。因此,我们宁可尽量少包含头文件,尤其是那些包含在其他头文件中的。 使用前置声明可以显著减少需要包含的头文件数量。举例说明:头文件中用到类File,但不需要访问File的声明,则头文件中只需前置声明class File;无需#include "file/base/file.h"。 在头文件如何做到使用类Foo而无需访问类的定义? 1) 将数据成员类型声明为Foo *或Foo &; 2) 参数、返回值类型为Foo的函数只是声明(但不定义实现); 3) 静态数据成员的类型可以被声明为Foo,因为静态数据成员的定义在类定义之外。 另一方面,如果你的类是Foo的子类,或者含有类型为Foo的非静态数据成员,则必须为之包含头文件。 3、为什么要使用内联函数,又如何改进性能? 定义(Definition):当函数被声明为内联函数之后,编译器可能会将其内联展开,无需按通常的函数调用机制调用内联函数。 重要的是,虚函数和递归函数即使被声明为内联的也不一定就是内联函数。通常,递归函数不应该被声明为内联的(译者注:递归调用堆栈的展开并不像循环那么简单,比如递归层数在编译时可能是未知的,大多数编译器都不支持内联递归函数)。析构函数内联的主要原因是其定义在类的定义中,为了方便抑或是对其行为给出文档。 4. 函数参数顺序(Function Parameter Ordering) 定义函数时,参数顺序为:输入参数在前,输出参数在后。 C/C++ 函数参数分为输入参数和输出参数两种,有时输入参数也会输出(译者注:值被修改时)。输入参数一般传值或常数引用(const references),输出参数或输入/输出参数为非常数指针(non-const pointers)。对参数排序时,将所有输入参数置于输出参数之前。不要仅仅因为是新添加的参数,就将其置于最后,而应该依然置于输出参数之前。 5、头文件定义顺序 将包含次序标准化可增强可读性、避免隐藏依赖(hidden dependencies,译者注:隐藏依赖主要是指包含的文件中编译时),次序如下:对应该CPP的头文件、C库、C++库、其他库的.h、项目内的.h。 相同目录下头文件按字母序是不错的选择。 1. 命名空间(Namespaces) 在.cc文件中,提倡使用不具名的命名空间(unnamed namespaces,译者注:不具名的命名空间就像不具名的类一样,似乎被介绍的很少:-()。使用具名命名空间时,其名称可基于项目或路径名称,不要使用using指示符。 定义:命名空间将全局作用域细分为不同的、具名的作用域,可有效防止全局作用域的命名冲突。 优点:命名空间提供了(可嵌套)命名轴线(name axis,译者注:将命名分割在不同命名空间内),当然,类也提供了(可嵌套)的命名轴线(译者注:将命名分割在不同类的作用域内)。 举例来说,两个不同项目的全局作用域都有一个类Foo,这样在编译或运行时造成冲突。如果每个项目将代码置于不同命名空间中,project1::Foo和project2::Foo作为不同符号自然不会冲突。 缺点:命名空间具有迷惑性,因为它们和类一样提供了额外的(可嵌套的)命名轴线。在头文件中使用不具名的空间容易违背C++的唯一定义原则(One Definition Rule (ODR))。 1) 不具名命名空间(Unnamed Namespaces) 在.cc文件中,允许甚至提倡使用不具名命名空间,以避免运行时的命名冲突: namespace { // .cc 文件中 // 命名空间的内容无需缩进 enum { UNUSED, EOF, ERROR }; // 经常使用的符号 bool AtEof() { return pos_ == EOF; } // 使用本命名空间内的符号EOF } // namespace 然而,与特定类关联的文件作用域声明在该类中被声明为类型、静态数据成员或静态成员函数(什么意思?),而不是不具名命名空间的成员。像上文展示的那样,不具名命名空间结束时用注释// namespace标识。 2、函数重载 结论:如果你想重载一个函数,考虑让函数名包含参数信息,例如,使用AppendString()、AppendInt()而不是Append()。 3. 缺省参数(Default Arguments) 禁止使用缺省函数参数。 优点:经常用到一个函数带有大量缺省值,偶尔会重写一下这些值,缺省参数为很少涉及的例外情况提供了少定义一些函数的方便。 缺点:大家经常会通过查看现有代码确定如何使用API,缺省参数使得复制粘贴以前的代码难以呈现所有参数,当缺省参数不适用于新代码时可能导致重大问题。 结论:所有参数必须明确指定,强制程序员考虑API和传入的各参数值,避免使用可能不为程序员所知的缺省参数。 8. 类型转换(Casting) 使用static_cast()等C++的类型转换,不要使用int y = (int)x或int y = int(x);。 定义:C++引入了有别于C的不同类型的类型转换操作。 优点:C语言的类型转换问题在于操作比较含糊:有时是在做强制转换(如(int)3.5),有时是在做类型转换(如(int)"hello")。另外,C++的类型转换查找更容易、更醒目。 缺点:语法比较恶心(nasty)。 结论:使用C++风格而不要使用C风格类型转换。 1) static_cast:和C风格转换相似可做值的强制转换,或指针的父类到子类的明确的向上转换; 2) const_cast:移除const属性; 3) reinterpret_cast:指针类型和整型或其他指针间不安全的相互转换,仅在你对所做一切了然于心时使用; 4) dynamic_cast:除测试外不要使用,除单元测试外,如果你需要在运行时确定类型信息,说明设计有缺陷(参考RTTI)。 10. 前置自增和自减(Preincrement and Predecrement) 对于迭代器和其他模板对象使用前缀形式(++i)的自增、自减运算符。 定义:对于变量在自增(++i或i++)或自减(--i或i--)后表达式的值又没有没用到的情况下,需要确定到底是使用前置还是后置的自增自减。 优点:不考虑返回值的话,前置自增(++i)通常要比后置自增(i++)效率更高,因为后置的自增自减需要对表达式的值i进行一次拷贝,如果i是迭代器或其他非数值类型,拷贝的代价是比较大的。既然两种自增方式动作一样(译者注,不考虑表达式的值,相信你知道我在说什么),为什么不直接使用前置自增呢? 缺点:C语言中,当表达式的值没有使用时,传统的做法是使用后置自增,特别是在for循环中,有些人觉得后置自增更加易懂,因为这很像自然语言,主语(i)在谓语动词(++)前。 结论:对简单数值(非对象)来说,两种都无所谓,对迭代器和模板类型来说,要使用前置自增(自减)。 16. sizeof(sizeof) 尽可能用sizeof(varname)代替sizeof(type)。 使用sizeof(varname)是因为当变量类型改变时代码自动同步,有些情况下sizeof(type)或许有意义,还是要尽量避免,如果变量类型改变的话不能同步。 8. TODO注释(TODO Comments) 对那些临时的、短期的解决方案,或已经够好但并不完美的代码使用TODO注释。 这样的注释要使用全大写的字符串TODO,后面括号(parentheses)里加上你的大名、邮件地址等,还可以加上冒号(colon):目的是可以根据统一的TODO格式进行查找: // TODO(kl@gmail.com): Use a "*" here for concatenation operator. // TODO(Zeke) change this to use relations. 如果加上是为了在“将来某一天做某事”,可以加上一个特定的时间("Fix by November 2005")或事件("Remove this code when all clients can handle XML responses.")。 TODO很不错,有时候,注释确实是为了标记一些未完成的或完成的不尽如人意的地方,这样一搜索,就知道还有哪些活要干,日志都省了。

2010-09-17

XproerIM For VS2008

Visual Studio 2008 平台下人XproerIM。 没有做太多的改变。 目前正在做一些新的升级计划,预计未来三个月中将会有大的更新。 项目论坛:http://www.5gzl.com/bbs/

2010-09-08

新颖网络文件上传插件For Visual Studio 6.0示例

新颖网络上传插件(StorageWebPlug)是一个支持超大文件(100GB)上传的COM控件, 具备断点续传,文件MD5验证,大大提高上传效率、节省带宽,有详细的上传进度显示,支持多种脚本语言,免费提供JavaScript SDK包,C++ SDK包。 官方网站:http://www.ncmem.com 官方博客:http://www.cnblogs.com/xproer 产品论坛:http://www.5gzl.com/bbs/ 联系邮箱:1085617561@qq.com

2010-09-05

仿QQ的VC6版XproerIM

交流论坛:http://ncmem.vip11.adminftp.org/index.php 相信开发人员对即时通迅软件(也称IM)已经非常熟悉了,有许多朋友更是梦想着开发一款属于自已的通迅软件,但是由于即时通迅软件涉及的领域比较广,比如UI,数据库,网络通迅等,所以工程量比较大,一个人开发难已顾及这么多,很可能最终因为维护的困难而放弃。 不仅通迅软件涉及的范围广,其它的软件也一样,影响项目的最大问题在于系统的构架设计不好,这个问题在项目的开始阶段还不会体现出来,往往随着项目功能的增加,代码量的暴涨那么系统就变得难已管理了,也许仅仅只是增加一个小的功能也会使整个系统伤筋动骨。 不过现在我非常高兴的告诉大家一个好消息,如果你现在仍然没有放弃打造自已的即时通迅软件那么你可以试试开源的即时通迅项目XproerIM。XproerIM是一款模仿QQ的即时通迅开源项目,目的在于打造国内最大的且代码质量最高的开源项目。最新版本的XproerIM是使用VC6.0编写的,暂时还没有服务端,只是一个客户端的程序。虽然没有服务端但是这并不影响XproerIM的开源进程,因为XproerIM提供了丰富和方便的扩展接口来供开发人员自定义开发,其源码完全开放,所以你可以完全根据自身的网络环境情况来定制开发服务器,服务器可以基于Linux或者Windows。 在客户端界面方面可以说XproerIM是一款集大成者,他集成了许多网上优秀的开源项目,例如菜单的项目使用CMenuXP(http://www.codeproject.com/KB/menus/menuxp2.aspx),XML文件处理使用了TinyXML,WebService方面使用gSOAP,当然也少不了一些大牛的代码。大牛的代码是从CSDN和其它网站中收集并整理的,XproerIM客户端的开源也是受这些大牛奉献精神的影响。 为了使一些组件或模块尽可能的通用我们花了一些时间来设计一个纯面向对象的类库(XproerIM Framework)。一方面是为了更好的管理整个系统和促进各模块之前的协作能力,另一方面是为XproerIM团队开发带来更多的便利。XproerIM Framework不仅仅只是提供更丰富的功能,更是在代码质量,风格,命名规范上面狠下功夫,这种全方位立体式的Framework会让XproerIM的开发人员感觉到用C++编写代码也是一种享受,毫不夸张的说XproerIM Framework的代码质量最终将会达到商用级别! 由XproerIM开发团队倾心打造的XproerIM Framework 类库是一个由 XproerIM Framework SDK 中包含的类、接口和值类型组成的库。该库提供对文件,网络,数据库,系统功能的访问,是建立 Windows,WebService应用程序、组件和控件的基础。 XproerIM Framework 是XproerIM的核心组件。XproerIM Framework 旨在实现下列目标:  从底层上简化C++编码,让开发人员更多的关注设计和业务逻辑。同时由设计和开发小组协作来保证代码质量和性能。  提供一个一致的面向对象的编程环境,这种环境规定了命名规范,编码风格以及代码注释等要求。就算是一个新加盟的成员也能轻松胜任。  使所有成员的经验在面对类型大不相同的应用程序(如基于 Windows 的应用程序和基于 Web 的应用程序)时保持一致。即便是一个对WebService不熟悉的成员也能通过类库轻松访问WebService并编写业务逻辑方面的代码。同是为第三方WebService系统提供强劲的支持,使第三方WebService能够与XproerIM无缝的集成。 由于XproerIM客户端极其优化的构架所以也是众多新手学习的宝典。同时XproerIM客户端社区也正在极积的建设中,我们不仅仅提供源代码,而且还会提供详细的开发文档,以及各种技术文档来帮助开发人员了解整套系统。 最后虽然XproerIM有这么多的优点,但是我仍然需要坦白的说明一点目前XproerIM还并不完善,里面的一些功能,控件设计的并不完善。对于开发人员来讲我觉得即然XproerIM提供了这么优秀的构架那么我们可以将完善XproerIM客户端的过程看作是对自已能力提高的一种练习。同时XproerIM开发团队会尽最大的努力使这一过程变成一种乐趣!

2010-06-30

ActiveX多文件上传插件

新颖网络上传插件(StorageWebPlug)是一个支持超大文件(2GB,可扩展)上传的COM控件, 具备断点续传,文件MD5验证,大大提高上传效率、节省带宽,有详细的上传进度显示,支持多种脚本语言,欢迎下载体验。免费提供JavaScript SDK包。 产品特点: 1、文件上传使用增强的FTP协议,用户使用浏览器就可以上传超大文件到服务器(支持上传超过1G的文件)。 2、支持断点续传,系统智能续传未上传的文件,续传操作更简单,更方便,更快捷。 3、支持文件批量上传, 一次可以上传多个文件. 上传时有详细的状态显示(包括单个文件进度,整体进度,传输速率,剩余时间等)。 4、新颖网络免费提供JavaScript SDK包。通过新颖网络提供的封装好的JavaScript类库用户可以快速的与现有系统整合。 5、StorageWebPlug提供完善的接口和帮助文档,开发文档。开发人员可以动态设置上传保存路径, 设置允许扩展名, 允许最大大小等,可自定义强。 6、支持各种代理(HTTP, Socket4, Socket5等)。 7、组件采用多线程机制来保证上传效率。 8、支持批量文件上传, 用户可以一次性上传批量文件. 客户端可以绑定HTML表单变量, 服务端并可以接收表单变量 9、服务端文件保存路径可以随意指定,服务端文件保存路径可以灵活变化。保存路径支持网络路径。 10、为提高安全性,服务端组件可以指定用户权限 11、可以限制上传单个文件大小, 控制上传带宽上限, 允许文件扩展名, 拒绝文件扩展名等 12、上传数据时会根据网络状况来控制数据包大小, 避免网络堵塞 13、控件采用ATL编写, cab包只有59KB, 用56k的modem下载不会超过12秒 14、服务端支持Windows 2000 Server/Windows 2003 Server/Windows NT/Windows XP/Unix/Linux等操作系统 15、通过新颖网络业界领先的设计水平打造的操作界面可以帮助您的系统和产品获得更高的品质。 产品介绍:http://www.ncmem.com/service_storagewebplug.aspx 下载地址:http://www.ncmem.com/download.aspx

2010-04-12

Microsoft MSXML SDK

Microsoft XML Core Services (MSXML) 4.0 MSXML 4.0 SDK What is MSXML? Microsoft® XML Core Services (MSXML) 4.0 allows customers to build high-performance XML-based applications that provide a high degree of interoperability with other applications that adhere to the XML 1.0 standard. Among the core services MSXML 4.0 provides is developer support for the following: The Document Object Model (DOM), a standard library of application programming interfaces (APIs) for accessing XML documents. The XML Schema definition language (XSD), a current W3C standard for using XML to create XML Schemas. XML Schemas can be used to validate other XML documents. The Schema Object Model (SOM), an additional set of APIs for accessing XML Schema documents programmatically. Extensible Stylesheet Language Transformations (XSLT) 1.0, a current W3C XML style sheet language standard. XSLT is recommended for transforming XML documents. The XML Path Language (XPath) 1.0, a current W3C XML standard used by XSLT and other XML programming vocabularies to query and filter data stored in XML documents. The Simple API for XML (SAX), a programmatic alternative to DOM-based processing. What's the MSXML SDK? The MSXML 4.0 Software Development Kit (SDK) provides conceptual and reference information for developers using MSXML. The SDK consists of Developer's Guides and Reference sections for each of MSXML's core technologies. For more information and links to each of these sections, see Roadmap to the MSXML 4.0 SDK. In addition to the sections for each core technology, the SDK contains the current overview section. This section contains the following topics that provide general information about MSXML and this SDK. Roadmap to the MSXML 4.0 SDK What's New Installing and Registering the MSXML 4.0 SDK Redistributing MSXML MSXML API History GUID and ProgID Information Dependencies in MSXML 4.0 Tips for Converting Samples to VBScript Using Language Filtering Sample XML File (books.xml) Copyright and Legal Information What's New? This release of the MSXML SDK provides the following new sections. Program with DOM in C/C++ Program with DOM in C/C++ Using Smart Pointer Class Wrappers Program with DOM in Visual Basic Program with DOM in JScript XDR Schema Developer's Guide DTD Developer's Guide DTD Reference For information about new product features in MSXML 4.0, see What's New. Tips and FAQ The following sections, located throughout the SDK, provide tips, frequently asked questions, and best practice information. Tips for Converting Samples to VBScript FAQ About Schemas FAQ About the SOM FAQ About XSLT FAQ About SAX2 Best Practices for Securing MSXML Code Support For free help with MSXML issues, try posting to the MSXML public newsgroup. This newsgroup is monitored by Microsoft Product Support Services (PSS) engineers who cover MSXML, and by other experienced MSXML developers. Further information about support options can be found on the Microsoft Help and Support Web site. Other Resources Microsoft Data Access Components (MDAC) | Web Services, part of the Platform SDK documentation

2010-03-04

分布式系统设计.pdf

第1章概论 显然,未来对计算速度、系统可靠性和成本实效性的要求必将促使发展另外的计算机模型 来取代传统的冯·诺依曼型结构的计算机。随着计算机网络的出现,一个新的梦想成为可能 —分布式计算。当用户需要完成任何任务时,分布式计算提供对尽可能多的计算机能力和数 据的透明访问,同时实现高性能与高可靠性的目标。在过去的1 0年里,人们对分布式计算系统 的兴趣迅猛发展。有关分布式计算的主题是多种多样的,许多研究人员正在研究关于分布式硬 件结构和分布式软件设计的各方面问题以开发利用其潜在的并行性和容错性。在这一章里,我 们将考虑一些基本概念以及与分布式计算相关的一些问题,并列出了本书所覆盖的主题。 1.1 推动因素 计算机技术的发展可以通过使用计算机的不同方式来描述。在5 0年代,计算机是串行处理 机,一次运行一个作业直至完成。这些处理机通过一个操作员从控制台操纵,而对于普通用户 则是不可访问的。在6 0年代,需求相似的作业作为一个组以批处理的方式通过计算机运行以减 少计算机的空闲时间。同一时期还提出了其他一些技术,如利用缓冲、假脱机和多道程序等的 脱机处理。7 0年代产生了分时系统,不仅作为提高计算机利用率的手段,也使用户离计算机更 近了。分时是迈向分布式系统的第一步:用户可以在不同的地点共享并访问资源。8 0年代是个 人计算的1 0年:人们有了他们自己专用的机器。由于基于微处理器的系统所提供的出色的性能/ 价格比和网络技术的稳步提高, 9 0年代是分布式系统的1 0年。 分布式系统可以有不同的物理组成:一组通过通信网络互连的个人计算机,一系列不仅共 享文件系统和数据库系统而且共享C P U周期的工作站(而且在大部分情况下本地进程比远程进 程有更高的优先级,其中一个进程就是一个运行中的程序),一个处理机池(其中终端不隶属于 任何一个处理机,而且不论本地进程还是远程进程,所有资源得以真正的共享)。 分布式系统是无缝的,也就是说网络功能单元间的接口很大程度上对用户不可见。分布式计 算的思想还被应用在数据库系统[16、38、49],文件系统[4、24、33、43、54],操作系统[2、39、46]和通用环境[19、32、35]。 另一种表示同样思想的说法是用户把系统看成一个虚拟的单处理机而不是不

2010-02-02

人工智能.PDF(chinapub)

我认为,理解智能包括理解:知识如何获取、表达和存储;智能行为如何产生和学习;动 机、情感和优先权如何发展和运用;传感器信号如何转换成各种符号;怎样利用各种符号执行 逻辑运算、对过去进行推理及对未来进行规划;智能机制如何产生幻觉、信念、希望、畏惧、 梦幻甚至善良和爱情等现象。我相信,对上述内容有一个根本的理解将会成为与拥有原子物理、 相对论和分子遗传学等级相当的科学成就。 — James Albus“答复H e n r y He x m o o r”,摘自U R L: h t t p : / / t o m m y. j s c . n a s a . g o v / e r / e r 6 / m r l / p a p e r s / s y m p o s i u m / a l b u s . t x t 1 9 9 5年2月1 3日

2010-02-02

SHTML教程TXT格式

什么是 SHTML 使用SSI(Server Side Include)的html文件扩展名,SSI(Server Side Include),通常称为“服务器端嵌入”或者叫“服务器端包含”,是一种类似于ASP的基于服务器的网页制作技术。 SSI工作原理: 将内容发送到浏览器之前,可以使用“服务器端包含 (SSI)”指令将文本、图形或应用程序信息包含到网页中。例如,可以使用 SSI 包含时间/日期戳、版权声明或供客户填写并返回的表单。对于在多个文件中重复出现的文本或图形,使用包含文件是一种简便的方法。将内容存入一个包含文件中即可,而不必将内容输入所有文件。通过一个非常简单的语句即可调用包含文件,此语句指示 Web 服务器将内容插入适当网页。而且,使用包含文件时,对内容的所有更改只需在一个地方就能完成。 因为包含 SSI 指令的文件要求特殊处理,所以必须为所有 SSI 文件赋予 SSI 文件扩展名。默认扩展名是 .stm、.shtm 和 .shtml。 Web 服务器在处理网页的同时处理 SSI 指令。当 Web 服务器遇到 SSI 指令时,直接将包含文件的内容插入 HTML 网页。如果“包含文件”中包含 SSI 指令,则同时插入此文件。除了用于包含文件的基本指令之外,还可以使用 SSI 指令插入文件的相关信息(如文件的大小)或者运行应用程序或 shell 命令。 网站维护常常碰到的一个问题是,网站的结构已经固定,却为了更新一点内容而不得不重做一大批网页。SSI提供了一种简单、有效的方法来解决这一问题,它将一个网站的基本结构放在几个简单的HTML文件中(模板),以后我们要做的只是将文本传到服务器,让程序按照模板自动生成网页,从而使管理大型网站变得容易。 所以,利用SHTML格式的页面目的和 ASP 差不多,但是因为是 API 所以运转速度更快,效率更高,比ASP快,比HTML慢,但由于可以使用服务器端包含,因此使页面更新容易(特别是批量更新banner,版权等),想象一下吧,你有一段 HTML,要在中间穿插一些特殊的服务端脚本,比如插入其他 HTML 段落,你选择 ASP 来完成这个任务,但是如果任务更繁重,需要更多的时间,比如 5 s,这个时候你不用 ASP 而用 SHTML,或许处理时间就只用 4s 了。 SSI有什么用? 之所以要扯到 SSI,是因爲 Shtml - Server-Parsed HTML 的首字母缩略词。包含有嵌入式服务器方包含命令的 HTML 文本。在被传送给浏览器之前,服务器会对 SHTML 文档进行完全地读取、分析以及修改。shtml和asp 有一些相似,以shtml命名的文件里,使用了ssi的一些指令,就像asp中的指令,你可以在SHTML文件中写入SSI指令,当客户端访问这些shtml文件时,服务器端会把这些SHTML文件进行读取和解释,把SHTML文件中包含的SSI指令解释出来比如:你可以在SHTML文件中用SSI指令引用其他的html文件(#include ),服务器传送给客户端的文件,是已经解释的SHTML不会有SSI指令。它实现了HTML所没有的功能,就是可以实现了动态的SHTML,可以说是HTML的一种进化吧。像新浪的新闻系统就是这样的,新闻内容是固定的但它上面的广告和菜单等就是用#include引用进来的。 目前,主要有以下几种用用途: 1.显示服务器端环境变量<#echo> 2.将文本内容直接插入到文档中<#include> 3.显示WEB文档相关信息<#flastmod #fsize> (如文件制作日期/大小等) 4.直接执行服务器上的各种程序<#exec>(如CGI或其他可执行程序) 5.设置SSI信息显示格式<#config>(如文件制作日期/大小显示方式) 高级SSI<XSSI>可设置变量使用if条件语句。 使用SSI SSI是为WEB服务器提供的一套命令,这些命令只要直接嵌入到HTML文档的注释内容之中即可。如: <!--#include file="info.htm"--> 就是一条SSI指令,其作用是将"info.htm"的内容拷贝到当前的页面中,当访问者来浏览时,会看到其它HTML文档一样显示info.htm其中的内容。其它的SSI指令使用形式基本同刚才的举例差不多,可见SSI使用只是插入一点代码而已,使用形式非常简单。当然,如果WEB服务器不支持SSI,它就会只不过将它当作注释信息,直接跳过其中的内容;浏览器也会忽略这些信息。 如何在我的WEB服务器上配置SSI功能? 在一些WEB服务器上(如IIS 4.0/SAMBAR 4.2),包含 #include 指令的文件必须使用已被映射到 SSI 解释程序的扩展名;否则,Web 服务器将不会处理该SSI指令;默认情况下,扩展名 .stm、.shtm 和 .shtml 被映射到解释程序(Ssinc.dll)。 Apache则是根据你的设置情况而定,修改srm.conf如: AddType text/x-server-parsed-html .shtml 将只对.shtml扩展名的文件解析SSI指令 AddType text/x-server-parsed-html .html 将对所有HTML文档解析SSI指令 Netscape WEB服务器直接使用 Administration Server(管理服务器)可打开SSI功能。 Website 使用 Server Admin 程序中的 Mapping 标签,扩展名添加内容类型为:wwwserver/html-ssi Cern 服务器不支持SSI,可用SSI诈骗法,到 http://sw.cse.bris.ac.uk/WebTools/fakessi.html 上下载一个PERL脚本,即可使你的CERN服务器使用一些SSI指令。(不支持exec指令。) SSI指令基本格式 程序代码: <!-- 指令名称="指令参数"> 示例: <!--#include file="info.htm"--> 说明: 1.<!-- -->是HTML语法中表示注释,当WEB服务器不支持SSI时,会忽略这些信息。 2.#include 为SSI指令之一。 3.file 为include的参数, info.htm为参数值,在本指令中指将要包含的文档名。 注意: 1.<!--与#号间无空格,只有SSI指令与参数间存在空格。 2.上面的标点="",一个也不能少。 3.SSI指令是大小写敏感的,因此参数必须是小写才会起作用。 SSI指令使用详解 #echo 示范 作用:将环境变量插入到页面中。 语法: <!--#echo var="变量名称"--> 示例: <!--#echo var="DOCUMENT_NAME"--> 本文档名称 <!--#echo var="DATE_LOCAL"--> 现在时间 <!--#echo var="REMOTE_ADDR"--> 你的IP地址 #include 示范 作用:将文本文件的内容直接插入到文档页面中。 语法: <!--#include file="文件名称"--> <!--#include virtual="文件名称"--> file 文件名是一个相对路径,该路径相对于使用 #include 指令的文档所在的目录。被包含文件可以在同一级目录或其子目录中,但不能在上一级目录中。如表示当前目录下的的nav_head.htm文档,则为file="nav_head.htm"。 virtual 文件名是 Web 站点上的虚拟目录的完整路径。如表示相对于服务器文档根目录下hoyi目录下的nav_head.htm文件;则为file="/hoyi/nav_head.htm" 参数: file 指定包含文件相对于本文档的位置 virtual 指定相对于服务器文档根目录的位置 注意: 1.文件名称必须带有扩展名。 2.被包含的文件可以具有任何文件扩展名,我觉得直接使用htm扩展名最方便,微软公司推荐使用 .inc 扩展名(这就看你的爱好了)。 示例: <!--#include file="nav_head.htm"--> 将头文件插入到当前页面 <!--#include file="nav_foot.htm"--> 将尾文件插入到当前页面 #flastmod 和 #fsize 示范 作用: #flastmod 文件最近更新日期 #fsize 文件的长度 语法: <!--#flastmod file="文件名称"--> <!--#fsize file="文件名称"--> 参数: file 指定包含文件相对于本文档的位置 如 info.txt 表示当前目录下的的info.txt文档 virtual 指定相对于服务器文档根目录的位置 如 /hoyi/info.txt 表示 注意:文件名称必须带有扩展名。 示例: <!--#flastmod file="news.htm"--> 将当前目录下news.htm文件的最近更新日期插插入到当前页面 <!--#fsize file="news.htm"--> 将当前目录下news.htm的文件大小入到当前页面 #exec 示范 作用:将某一外部程序的输出插入到页面中。可插入CGI程序或者是常规应用程序的输入,这取决于使用的参数是cmd还是cgi。 语法: <!--#exec cmd="文件名称"--> <!--#exec cgi="文件名称"--> 参数: cmd 常规应用程序 cgi CGI脚本程序 示例: <!--#exec cmd="cat /etc/passwd"--> 将会显示密码文件 <!--#exec cmd="dir /b"--> 将会显示当前目录下文件列表 <!--#exec cgi="/cgi-bin/gb.cgi"--> 将会执行CGI程序gb.cgi。 <!--#exec cgi="/cgi-bin/access_log.cgi"--> 将会执行CGI程序access_log.cgi。 注意:从上面的示例可以看出,这个指令相当方便,但是也存在安全问题。 禁止方法: 1.Apache,将access.conf中的"Options Includes ExecCGI"这行代码删除; 2.在IIS中,要禁用 #exec 命令,可修改 SSIExecDisable 元数据库; #config 作用: 指定返回给客户端浏览器的错误信息、日期和文件大小的格式。 语法: <!--#config errmsg="自定义错误信息"--> <!--#config sizefmt="显示单位"--> <!--#config timefmt="显示格式"--> 参数: errmsg 自定义SSI执行错误信息,可以为任何你喜欢的方式。 sizefmt 文件大小显示方式,默认为字节方式("bytes")可以改为千字节方式("abbrev") timefmt 时间显示方式,最灵活的配置属性。 示例:显示一个不存在文件的大小 <!--#config errmsg="服务器执行错误,请联系管理员 yiho@126.com,谢谢!"--> <!--#fsize file="不存在的文件.htm"--> 以千字节方式显示文件大小 语法: <!--#config sizefmt="abbrev"--> <!--#fsizefile="news.htm"--> 以特定的时间格式显示时间 <!--#config timefmt="%Y年/%m月%d日 星期%W 北京时间%H:%M:%s,%Y年已过去了%j天 今天是%Y年的第%U个星期"--> <!--#echo var="DATE_LOCAL"--> 显示今天是星期几,几月,时区 <!--#config timefmt="今天%A, %B ,服务器时区是 %z,是"--> <!--#echo var="DATE_LOCAL"--> XSSI XSSI(Extended SSI)是一组高级SSI指令,内置于Apache 1.2或更高版本的mod-include模块之中。其中可利用的的指令有: #printenv #set #if #printenv 作用: 显示当前存在于WEB服务器环境中的所有环境变量。 语法: <!--#printenv--> #set 作用:可给变量赋值,以用于后面的if语句。 语法: <!--#set var="变量名" value="变量值"--> 示例: <!--#set var="color" value="红色"--> #if 作用:创建可以改变数据的页面,这些数据根据使用if语句时计算的要求予以显示。 语法: <!--#if expr="$变量名=\"变量值A\""--> 显示内容 <!--#elif expr="$变量名=\"变量值B\""--> 显示内容 <!--#else--> 显示内容 <!--#endif"--> 示例: <!--#if expr="$SERVER_NAME=\"www.baidu.com\""--> 欢迎光临 http://www.baidu.com <!--#elif expr="$SERVER_NAME=\"www.google.com\"" --> 欢迎光临 http://www.google.com <!--#else--> 欢迎光临 Afly's Blog! <!--#endif"--> 注意:用于前面指令中的反斜杠,是用来代换内部的引号,以便它们不会被解释为结束表达式。不可省略。 1、Config 命令 Config 命令主要用于修改SSI的默认设置。其中: Errmsg:设置默认错误信息。为了能够正常的返回用户设定的错误信息,在HTML文件中Errmsg参数必须被放置在其它SSI命令的前面,否则客户端只能显示默认的错误信息,而不是由用户设定的自定义信息。 <!--#config errmsg="Error! Please email webmaster@mydomain.com" --> Timefmt:定义日期和时间的使用格式。Timefmt参数必须在echo命令之前使用。 <!--#config timefmt="%A, %B %d, %Y"--> <!--#echo var="LAST_MODIFIED" --> 显示结果为: Wednesday, April 12, 2000 也许用户对上例中所使用的%A %B %d感到很陌生,下面我们就以表格的形式总结一下SSI中较为常用的一些日期和时间格式。 Sizefmt:决定文件大小是以字节、千字节还是兆字节为单位表示。如果以字节为单位,参数值为"bytes";对于千字节和兆字节可以使用缩写形式。同样,sizefmt参数必须放在fsize命令的前面才能使用。 <!--#config sizefmt="bytes" --> <!--#fsize file="index.html" --> 2、Include 命令 Include 命令可以把其它文档中的文字或图片插入到当前被解析的文档中,这是整个SSI的关键所在。通过Include命令只需要改动一个文件就可以瞬间更新整个站点! Include 命令具有两个不同的参数: Virtual:给出到服务器端某个文档的虚拟路径。 File:给出到当前目录的相对路径,其中不能使用"../",也不能使用绝对路径。 <!--#include virtual="/includes/header.html" --> <!--#include file="header.html" --> 这就要求每一个目录中都包含一个header.html文件。 3、Echo 命令 Echo 命令可以显示以下各环境变量: DOCUMENT_NAME:显示当前文档的名称。 DOCUMENT_URI:显示当前文档的虚拟路径。例如: <!--#echo var="DOCUMENT_NAME" --> <!--#echo var="DOCUMENT_URI" --> 随着网站的不断发展,那些越来越长的URL地址肯定会让人头疼。如果使用SSI,一切就会迎刃而解。因为我们可以把网站的域名和SSI命令结合在一起显示完整的URL,即: http://YourDomain<!--#echo var="DOCUMENT_URI" --> QUERY_STRING_UNESCAPED:显示未经转义处理的由客户端发送的查询字串,其中所有的特殊字符前面都有转义符"\"。例如: <!--#echo var="QUERY_STRING_UNESCAPED" --> DATE_LOCAL:显示服务器设定时区的日期和时间。用户可以结合config命令的timefmt参数,定制输出信息。例如: <!--#config timefmt="%A, the %d of %B, in the year %Y" --> <!--#echo var="DATE_LOCAL" --> 显示结果为: Saturday, the 15 of April, in the year 2000 DATE_GMT:功能与DATE_LOCAL一样,只不过返回的是以格林尼治标准时间为基准的日期。例如: <!--#echo var="DATE_GMT" --> LAST_MODIFIED:显示当前文档的最后更新时间。同样,这是SSI中非常实用的一个功能,只要在HTML文档中加入以下这行简单的文字,就可以在页面上动态的显示更新时间。 <!--#echo var="LAST_MODIFIED" --> CGI环境变量 除了SSI环境变量之外,echo命令还可以显示以下CGI环境变量: SERVER_SOFTWARE:显示服务器软件的名称和版本。例如: <!--#echo var="SERVER_SOFTWARE" --> SERVER_NAME: 显示服务器的主机名称,DNS别名或IP地址。例如: <!--#echo var="SERVER_NAME" --> SERVER_PROTOCOL:显示客户端请求所使用的协议名称和版本,如HTTP/1.0。例如: <!--#echo var="SERVER_PROTOCOL" --> SERVER_PORT:显示服务器的响应端口。例如: <!--#echo var="SERVER_PORT" --> REQUEST_METHOD:显示客户端的文档请求方法,包括GET, HEAD, 和POST。例如: <!--#echo var="REQUEST_METHOD" --> REMOTE_HOST:显示发出请求信息的客户端主机名称。 <!--#echo var="REMOTE_HOST" --> REMOTE_ADDR:显示发出请求信息的客户端IP地址。 <!--#echo var="REMOTE_ADDR" --> AUTH_TYPE:显示用户身份的验证方法。 <!--#echo var="AUTH_TYPE" --> REMOTE_USER:显示访问受保护页面的用户所使用的帐号名称。 <!--#echo var="REMOTE_USER" --> 4、Fsize:显示指定文件的大小,可以结合config命令的sizefmt参数定制输出格式。 <!--#fsize file="index_working.html" --> 5、Flastmod:显示指定文件的最后修改日期,可以结合config 命令的timefmt参数控制输出格式。 <!--#config timefmt="%A, the %d of %B, in the year %Y" --> <!--#flastmod file="file.html" --> 这里,我们可以利用flastmod参数显示出一个页面上所有链接页面的更新日期。方法如下: <!--#config timefmt=" %B %d, %Y" --> <A HREF="/directory/file.html">File</A> <!--#flastmod virtual="/directory/file.html" --> <A HREF="/another_directory/another_file.html">Another File</A> <!--#flastmod virtual="/another_directory/another_file.html" --> 显示结果为: File April 19, 2000 Another File January 08, 2000 6、Exec Exec命令可以执行CGI脚本或者shell命令。使用方法如下: Cmd:使用/bin/sh执行指定的字串。如果SSI使用了IncludesNOEXEC选项,则该命令将被屏蔽。 Cgi:可以用来执行CGI脚本。例如,下面这个例子中使用服务端cgi-bin目录下的counter.pl脚本程序在每个页面放置一个计数器: <!--#exec cgi="/cgi-bin/counter.pl" --> 关于SHTML和HTML的区别 让我们先来看看SHTML和HTML的区别,如果用一句话来解释就是:SHTML 不是HTML而是一种服务器 API,shtml是服务器动态产成的html. 虽然两者都是超文本格式,但shtml是一种用于SSI技术的文件。也就是Server Side Include--SSI 服务器端包含指令。如果Web Server有SSI功能的话,大多数(尤其是基于Unix平台)的WEB服务器,如Netscape Enterprise Server等均支持SSI命令。

2010-01-07

SQL示例大全 V1.0

更新如下: (1)增加单表高性能分页存储过程(由微软MVP编写) (2)增加多表高性能分页存储过程 (3)增加CASE示例 (4)增加验证表是否存在示例

2009-11-30

微软内部资料-SQL性能优化5

Contents Overview 1 Lesson 1: Index Concepts 3 Lesson 2: Concepts – Statistics 29 Lesson 3: Concepts – Query Optimization 37 Lesson 4: Information Collection and Analysis 61 Lesson 5: Formulating and Implementing Resolution 75 Module 6: Troubleshooting Query Performance Overview At th

2009-11-27

微软内部资料-SQL性能优化3

Contents Overview 1 Lesson 1: Concepts – Locks and Lock Manager 3 Lesson 2: Concepts – Batch and Transaction 31 Lesson 3: Concepts – Locks and Applications 51 Lesson 4: Information Collection and Analysis 63 Lesson 5: Concepts – Formulating and Implementing Resolution 81 Module 4: Troubleshooting Locking and Blocking Overview At the end of this module, you will be able to:  Discuss how lock manager uses lock mode, lock resources, and lock compatibility to achieve transaction isolation.  Describe the various transaction types and how transactions differ from batches.  Describe how to troubleshoot blocking and locking issues.  Analyze the output of blocking scripts and Microsoft® SQL Server™ Profiler to troubleshoot locking and blocking issues.  Formulate hypothesis to resolve locking and blocking issues. Lesson 1: Concepts – Locks and Lock Manager This lesson outlines some of the common causes that contribute to the perception of a slow server. What You Will Learn After completing this lesson, you will be able to:  Describe locking architecture used by SQL Server.  Identify the various lock modes used by SQL Server.  Discuss lock compatibility and concurrent access.  Identify different types of lock resources.  Discuss dynamic locking and lock escalation.  Differentiate locks, latches, and other SQL Server internal “locking” mechanism such as spinlocks and other synchronization objects. Recommended Reading  Chapter 14 “Locking”, Inside SQL Server 2000 by Kalen Delaney  SOX000821700049 – SQL 7.0 How to interpret lock resource Ids  SOX000925700237 – TITLE: Lock escalation in SQL 7.0  SOX001109700040 – INF: Queries with PREFETCH in the plan hold lock until the end of transaction Locking Concepts Delivery Tip Prior to delivering this material, test the class to see if they fully understand the different isolation levels. If the class is not confident in their understanding, review appendix A04_Locking and its accompanying PowerPoint® file. Transactions in SQL Server provide the ACID properties: Atomicity A transaction either commits or aborts. If a transaction commits, all of its effects remain. If it aborts, all of its effects are undone. It is an “all or nothing” operation. Consistency An application should maintain the consistency of a database. For example, if you defer constraint checking, it is your responsibility to ensure that the database is consistent. Isolation Concurrent transactions are isolated from the updates of other incomplete transactions. These updates do not constitute a consistent state. This property is often called serializability. For example, a second transaction traversing the doubly linked list mentioned above would see the list before or after the insert, but it will see only complete changes. Durability After a transaction commits, its effects will persist even if there are system failures. Consistency and isolation are the most important in describing SQL Server’s locking model. It is up to the application to define what consistency means, and isolation in some form is needed to achieve consistent results. SQL Server uses locking to achieve isolation. Definition of Dependency: A set of transactions can run concurrently if their outputs are disjoint from the union of one another’s input and output sets. For example, if T1 writes some object that is in T2’s input or output set, there is a dependency between T1 and T2. Bad Dependencies These include lost updates, dirty reads, non-repeatable reads, and phantoms. ANSI SQL Isolation Levels An isolation level determines the degree to which data is isolated for use by one process and guarded against interference from other processes. Prior to SQL Server 7.0, REPEATABLE READ and SERIALIZABLE isolation levels were synonymous. There was no way to prevent non-repeatable reads while not preventing phantoms. By default, SQL Server 2000 operates at an isolation level of READ COMMITTED. To make use of either more or less strict isolation levels in applications, locking can be customized for an entire session by setting the isolation level of the session with the SET TRANSACTION ISOLATION LEVEL statement. To determine the transaction isolation level currently set, use the DBCC USEROPTIONS statement, for example: USE pubs GO SET TRANSACTION ISOLATION LEVEL REPEATABLE READ GO DBCC USEROPTIONS GO Multigranular Locking Multigranular Locking In our example, if one transaction (T1) holds an exclusive lock at the table level, and another transaction (T2) holds an exclusive lock at the row level, each of the transactions believe they have exclusive access to the resource. In this scenario, since T1 believes it locks the entire table, it might inadvertently make changes to the same row that T2 thought it has locked exclusively. In a multigranular locking environment, there must be a way to effectively overcome this scenario. Intent lock is the answer to this problem. Intent Lock Intent Lock is the term used to mean placing a marker in a higher-level lock queue. The type of intent lock can also be called the multigranular lock mode. An intent lock indicates that SQL Server wants to acquire a shared (S) lock or exclusive (X) lock on some of the resources lower down in the hierarchy. For example, a shared intent lock placed at the table level means that a transaction intends on placing shared (S) locks on pages or rows within that table. Setting an intent lock at the table level prevents another transaction from subsequently acquiring an exclusive (X) lock on the table containing that page. Intent locks improve performance because SQL Server examines intent locks only at the table level to determine whether a transaction can safely acquire a lock on that table. This removes the requirement to examine every row or page lock on the table to determine whether a transaction can lock the entire table. Lock Mode The code shown in the slide represents how the lock mode is stored internally. You can see these codes by querying the master.dbo.spt_values table: SELECT * FROM master.dbo.spt_values WHERE type = N'L' However, the req_mode column of master.dbo.syslockinfo has lock mode code that is one less than the code values shown here. For example, value of req_mode = 3 represents the Shared lock mode rather than the Schema Modification lock mode. Lock Compatibility These locks can apply at any coarser level of granularity. If a row is locked, SQL Server will apply intent locks at both the page and the table level. If a page is locked, SQL Server will apply an intent lock at the table level. SIX locks imply that we have shared access to a resource and we have also placed X locks at a lower level in the hierarchy. SQL Server never asks for SIX locks directly, they are always the result of a conversion. For example, suppose a transaction scanned a page using an S lock and then subsequently decided to perform a row level update. The row would obtain an X lock, but now the page would require an IX lock. The resultant mode on the page would be SIX. Another type of table lock is a schema stability lock (Sch-S) and is compatible with all table locks except the schema modification lock (Sch-M). The schema modification lock (Sch-M) is incompatible with all table locks. Locking Resources Delivery Tip Note the differences between Key and Key Range locks. Key Range locks will be covered in a couple of slides. SQL Server can lock these resources: Item Description DB A database. File A database file Index An entire index of a table. Table An entire table, including all data and indexes. Extent A contiguous group of data pages or index pages. Page An 8-KB data page or index page. Key Row lock within an index. Key-range A key-range. Used to lock ranges between records in a table to prevent phantom insertions or deletions into a set of records. Ensures serializable transactions. RID A Row Identifier. Used to individually lock a single row within a table. Application A lock resource defined by an application. The lock manager knows nothing about the resource format. It simply compares the 'strings' representing the lock resources to determine whether it has found a match. If a match is found, it knows that resource is already locked. Some of the resources have “sub-resources.” The followings are sub-resources displayed by the sp_lock output: Database Lock Sub-Resources: Full Database Lock (default) [BULK-OP-DB] – Bulk Operation Lock for Database [BULK-OP-LOG] – Bulk Operation Lock for Log Table Lock Sub-Resources: Full Table Lock (default) [UPD-STATS] – Update statistics Lock [COMPILE] – Compile Lock Index Lock sub-Resources: Full Index Lock (default) [INDEX_ID] – Index ID Lock [INDEX_NAME] – Index Name Lock [BULK_ALLOC] – Bulk Allocation Lock [DEFRAG] – Defragmentation Lock For more information, see also… SOX000821700049 SQL 7.0 How to interpret lock resource Ids Lock Resource Block The resource type has the following resource block format: Resource Type (Code) Content DB (2) Data 1: sub-resource; Data 2: 0; Data 3: 0 File (3) Data 1: File ID; Data 2: 0; Data 3: 0 Index (4) Data 1: Object ID; Data 2: sub-resource; Data 3: Index ID Table (5) Data 1: Object ID; Data 2: sub-resource; Data 3: 0. Page (6) Data 1: Page Number; Data 3: 0. Key (7) Data 1: Object ID; Data 2: Index ID; Data 3: Hashed Key Extent (8) Data 1: Extent ID; Data 3: 0. RID (9) Data 1: RID; Data 3: 0. Application (10) Data 1: Application resource name The rsc_bin column of master..syslockinfo contains the resource block in hexadecimal format. For an example of how to decode value from this column using the information above, let us assume we have the following value: 0x000705001F83D775010002014F0BEC4E With byte swapping within each field, this can be decoded as: Byte 0: Flag – 0x00 Byte 1: Resource Type – 0x07 (Key) Byte 2-3: DBID – 0x0005 Byte 4-7: ObjectID – 0x 75D7831F (1977058079) Byte 8-9: IndexID – 0x0001 Byte 10-16: Hash Key value – 0x 02014F0BEC4E For more information about how to decode this value, see also… Inside SQL Server 2000, pages 803 and 806. Key Range Locking Key Range Locking To support SERIALIZABLE transaction semantics, SQL Server needs to lock sets of rows specified by a predicate, such as WHERE salary BETWEEN 30000 AND 50000 SQL Server needs to lock data that does not exist! If no rows satisfy the WHERE condition the first time the range is scanned, no rows should be returned on any subsequent scans. Key range locks are similar to row locks on index keys (whether clustered or not). The locks are placed on individual keys rather than at the node level. The hash value consists of all the key components and the locator. So, for a nonclustered index over a heap, where columns c1 and c2 where indexed, the hash would contain contributions from c1, c2 and the RID. A key range lock applied to a particular key means that all keys between the value locked and the next value would be locked for all data modification. Key range locks can lock a slightly larger range than that implied by the WHERE clause. Suppose the following select was executed in a transaction with isolation level SERIALIZABLE: SELECT * FROM members WHERE first_name between ‘Al’ and ‘Carl’ If 'Al', 'Bob', and 'Dave' are index keys in the table, the first two of these would acquire key range locks. Although this would prevent anyone from inserting either 'Alex' or 'Ben', it would also prevent someone from inserting 'Dan', which is not within the range of the WHERE clause. Prior to SQL Server 7.0, page locking was used to prevent phantoms by locking the entire set of pages on which the phantom would exist. This can be too conservative. Key Range locking lets SQL Server lock only a much more restrictive area of the table. Impact Key-range locking ensures that these scenarios are SERIALIZABLE:  Range scan query  Singleton fetch of nonexistent row  Delete operation  Insert operation However, the following conditions must be satisfied before key-range locking can occur:  The transaction-isolation level must be set to SERIALIZABLE.  The operation performed on the data must use an index range access. Range locking is activated only when query processing (such as the optimizer) chooses an index path to access the data. Key Range Lock Mode Again, the req_mode column of master.dbo.syslockinfo has lock mode code that is one less than the code values shown here. Dynamic Locking When modifying individual rows, SQL Server typically would take row locks to maximize concurrency (for example, OLTP, order-entry application). When scanning larger volumes of data, it would be more appropriate to take page or table locks to minimize the cost of acquiring locks (for example, DSS, data warehouse, reporting). Locking Decision The decision about which unit to lock is made dynamically, taking many factors into account, including other activity on the system. For example, if there are multiple transactions currently accessing a table, SQL Server will tend to favor row locking more so than it otherwise would. It may mean the difference between scanning the table now and paying a bit more in locking cost, or having to wait to acquire a more coarse lock. A preliminary locking decision is made during query optimization, but that decision can be adjusted when the query is actually executed. Lock Escalation When the lock count for the transaction exceeds and is a multiple of ESCALATION_THRESHOLD (1250), the Lock Manager attempts to escalate. For example, when a transaction acquired 1250 locks, lock manager will try to escalate. The number of locks held may continue to increase after the escalation attempt (for example, because new tables are accessed, or the previous lock escalation attempts failed due to incompatible locks held by another spid). If the lock count for this transaction reaches 2500 (1250 * 2), Lock Manager will attempt escalation again. The Lock Manager looks at the lock memory it is using and if it is more than 40 percent of SQL Server’s allocated buffer pool memory, it tries to find a scan (SDES) where no escalation has already been performed. It then repeats the search operation until all scans have been escalated or until the memory used drops under the MEMORY_LOAD_ESCALATION_THRESHOLD (40%) value. If lock escalation is not possible or fails to significantly reduce lock memory footprint, SQL Server can continue to acquire locks until the total lock memory reaches 60 percent of the buffer pool (MAX_LOCK_RESOURCE_MEMORY_PERCENTAGE=60). Lock escalation may be also done when a single scan (SDES) holds more than LOCK_ESCALATION_THRESHOLD (765) locks. There is no lock escalation on temporary tables or system tables. Trace Flag 1211 disables lock escalation. Important Do not relay this to the customer without careful consideration. Lock escalation is a necessary feature, not something to be avoided completely. Trace flags are global and disabling lock escalation could lead to out of memory situations, extremely poor performing queries, or other problems. Lock escalation tracing can be seen using the Profiler or with the general locking trace flag, -T1200. However, Trace Flag 1200 shows all lock activity so it should not be usable on a production system. For more information, see also… SOX000925700237 “TITLE: SQL 7.0 Lock escalation in SQL 7.0” Lock Timeout Application Lock Timeout An application can set lock timeout for a session with the SET option: SET LOCK_TIMEOUT N where N is a number of milliseconds. A value of -1 means that there will be no timeout, which is equivalent to the version 6.5 behavior. A value of 0 means that there will be no waiting; if a process finds a resource locked, it will generate error message 1222 and continue with the next statement. The current value of LOCK_TIMEOUT is stored in the global variable @@lock_timeout. Note After a lock timeout any transaction containing the statement, is rolled back or canceled by SQL Server 2000 (bug#352640 was filed). This behavior is different from that of SQL Server 7.0. With SQL Server 7.0, the application must have an error handler that can trap error 1222 and if an application does not trap the error, it can proceed unaware that an individual statement within a transaction has been canceled, and errors can occur because statements later in the transaction may depend on the statement that was never executed. Bug#352640 is fixed in hotfix build 8.00.266 whereby a lock timeout will only Internal Lock Timeout At time, internal operations within SQL Server will attempt to acquire locks via lock manager. Typically, these lock requests are issued with “no waiting.” For example, the ghost record processing might try to clean up rows on a particular page, and before it can do that, it needs to lock the page. Thus, the ghost record manager will request a page lock with no wait so that if it cannot lock the page, it will just move on to other pages; it can always come back to this page later. If you look at SQL Profiler Lock: Timeout events, internal lock timeout typically have a duration value of zero. Lock Duration Lock Mode and Transaction Isolation Level For REPEATABLE READ transaction isolation level, update locks are held until data is read and processed, unless promoted to exclusive locks. "Data is processed" means that we have decided whether the row in question matched the search criteria; if not then the update lock is released, otherwise, we get an exclusive lock and make the modification. Consider the following query: use northwind go dbcc traceon(3604, 1200, 1211) -- turn on lock tracing -- and disable escalation go set transaction isolation level repeatable read begin tran update dbo.[order details] set discount = convert (real, discount) where discount = 0.0 exec sp_lock Update locks are promoted to exclusive locks when there is a match; otherwise, the update lock is released. The sp_lock output verifies that the SPID does not hold any update locks or shared locks at the end of the query. Lock escalation is turned off so that exclusive table lock is not held at the end. Warning Do not use trace flag 1200 in a production environment because it produces a lot of output and slows down the server. Trace flag 1211 should not be used unless you have done extensive study to make sure it helps with performance. These trace flags are used here for illustration and learning purposes only. Lock Ownership Most of the locking discussion in this lesson relates to locks owned by “transactions.” In addition to transaction, cursor and session can be owners of locks and they both affect how long locks are held. For every row that is fetched, when SCROLL_LOCKS option is used, regardless of the state of a transaction, a cursor lock is held until the next row is fetched or when the cursor is closed. Locks owned by session are outside the scope of a transaction. The duration of these locks are bounded by the connection and the process will continue to hold these locks until the process disconnects. A typical lock owned by session is the database (DB) lock. Locking – Read Committed Scan Under read committed isolation level, when database pages are scanned, shared locks are held when the page is read and processed. The shared locks are released “behind” the scan and allow other transactions to update rows. It is important to note that the shared lock currently acquired will not be released until shared lock for the next page is successfully acquired (this is commonly know as “crabbing”). If the same pages are scanned again, rows may be modified or deleted by other transactions. Locking – Repeatable Read Scan Under repeatable read isolation level, when database pages are scanned, shared locks are held when the page is read and processed. SQL Server continues to hold these shared locks, thus preventing other transactions to update rows. If the same pages are scanned again, previously scanned rows will not change but new rows may be added by other transactions. Locking – Serializable Read Scan Under serializable read isolation level, when database pages are scanned, shared locks are held not only on rows but also on scanned key range. SQL Server continues to hold these shared locks until the end of transaction. Because key range locks are held, not only will this prevent other transactions from modifying the rows, no new rows can be inserted. Prefetch and Isolation Level Prefetch and Locking Behavior The prefetch feature is available for use with SQL Server 7.0 and SQL Server 2000. When searching for data using a nonclustered index, the index is searched for a particular value. When that value is found, the index points to the disk address. The traditional approach would be to immediately issue an I/O for that row, given the disk address. The result is one synchronous I/O per row and, at most, one disk at a time working to evaluate the query. This does not take advantage of striped disk sets. The prefetch feature takes a different approach. It continues looking for more record pointers in the nonclustered index. When it has collected a number of them, it provides the storage engine with prefetch hints. These hints tell the storage engine that the query processor will need these particular records soon. The storage engine can now issue several I/Os simultaneously, taking advantage of striped disk sets to execute multiple operations simultaneously. For example, if the engine is scanning a nonclustered index to determine which rows qualify but will eventually need to visit the data page as well to access columns that are not in the index, it may decide to submit asynchronous page read requests for a group of qualifying rows. The prefetch data pages are then revisited later to avoid waiting for each individual page read to complete in a serial fashion. This data access path requires that a lock be held between the prefetch request and the row lookup to stabilize the row on the page so it is not to be moved by a page split or clustered key update. For our example, the isolation level of the query is escalated to REPEATABLE READ, overriding the transaction isolation level. With SQL Server 7.0 and SQL Server 2000, portions of a transaction can execute at a different transaction isolation level than the entire transaction itself. This is implemented as lock classes. Lock classes are used to control lock lifetime when portions of a transaction need to execute at a stricter isolation level than the underlying transaction. Unfortunately, in SQL Server 7.0 and SQL Server 2000, the lock class is created at the topmost operator of the query and hence released only at the end of the query. Currently there is no support to release the lock (lock class) after the row has been discarded or fetched by the filter or join operator. This is because isolation level can be set at the query level via a lock class, but no lower. Because of this, locks acquired during the query will not be released until the query completes. If prefetch is occurring you may see a single SPID that holds hundreds of Shared KEY or PAG locks even though the connection’s isolation level is READ COMMITTED. Isolation level can be determined from DBCC PSS output. For details about this behavior see “SOX001109700040 INF: Queries with PREFETCH in the plan hold lock until the end of transaction”. Other Locking Mechanism Lock manager does not manage latches and spinlocks. Latches Latches are internal mechanisms used to protect pages while doing operations such as placing a row physically on a page, compressing space on a page, or retrieving rows from a page. Latches can roughly be divided into I/O latches and non-I/O latches. If you see a high number of non-I/O related latches, SQL Server is usually doing a large number of hash or sort operations in tempdb. You can monitor latch activities via DBCC SQLPERF(‘WAITSTATS’) command. Spinlock A spinlock is an internal data structure that is used to protect vital information that is shared within SQL Server. On a multi-processor machine, when SQL Server tries to access a particular resource protected by a spinlock, it must first acquire the spinlock. If it fails, it executes a loop that will check to see if the lock is available and if not, decrements a counter. If the counter reaches zero, it yields the processor to another thread and goes into a “sleep” (wait) state for a pre-determined amount of time. When it wakes, hopefully, the lock is free and available. If not, the loop starts again and it is terminated only when the lock is acquired. The reason for implementing a spinlock is that it is probably less costly to “spin” for a short time rather than yielding the processor. Yielding the processor will force an expensive context switch where:  The old thread’s state must be saved  The new thread’s state must be reloaded  The data stored in the L1 and L2 cache are useless to the processor On a single-processor computer, the loop is not useful because no other thread can be running and thus, no one can release the spinlock for the currently executing thread to acquire. In this situation, the thread yields the processor immediately. Lesson 2: Concepts – Batch and Transaction This lesson outlines some of the common causes that contribute to the perception of a slow server. What You Will Learn After completing this lesson, you will be able to:  Review batch processing and error checking.  Review explicit, implicit and autocommit transactions and transaction nesting level.  Discuss how commit and rollback transaction done in stored procedure and trigger affects transaction nesting level.  Discuss various transaction isolation level and their impact on locking.  Discuss the difference between aborting a statement, a transaction, and a batch.  Describe how @@error, @@transcount, and @@rowcount can be used for error checking and handling. Recommended Reading  Charter 12 “Transactions and Triggers”, Inside SQL Server 2000 by Kalen Delaney Batch Definition SQL Profiler Statements and Batches To help further your understanding of what is a batch and what is a statement, you can use SQL Profiler to study the definition of batch and statement.  Try This: Using SQL Profiler to Analyze Batch 1. Log on to a server with Query Analyzer 2. Startup the SQL Profiler against the same server 3. Start a trace using the “StandardSQLProfiler” template 4. Execute the following using Query Analyzer: SELECT @@VERSION SELECT @@SPID The ‘SQL:BatchCompleted’ event is captured by the trace. It shows both the statements as a single batch. 5. Now execute the following using Query Analyzer {call sp_who()} What shows up? The ‘RPC:Completed’ with the sp_who information. RPC is simply another entry point to the SQL Server to call stored procedures with native data types. This allows one to avoid parsing. The ‘RPC:Completed’ event should be considered the same as a batch for the purposes of this discussion. Stop the current trace and start a new trace using the “SQLProfilerTSQL_SPs” template. Issue the same command as outlines in step 5 above. Looking at the output, not only can you see the batch markers but each statement as executed within the batch. Autocommit, Explicit, and Implicit Transaction Autocommit Transaction Mode (Default) Autocommit mode is the default transaction management mode of SQL Server. Every Transact-SQL statement, whether it is a standalone statement or part of a batch, is committed or rolled back when it completes. If a statement completes successfully, it is committed; if it encounters any error, it is rolled back. A SQL Server connection operates in autocommit mode whenever this default mode has not been overridden by either explicit or implicit transactions. Autocommit mode is also the default mode for ADO, OLE DB, ODBC, and DB-Library. A SQL Server connection operates in autocommit mode until a BEGIN TRANSACTION statement starts an explicit transaction, or implicit transaction mode is set on. When the explicit transaction is committed or rolled back, or when implicit transaction mode is turned off, SQL Server returns to autocommit mode. Explicit Transaction Mode An explicit transaction is a transaction that starts with a BEGIN TRANSACTION statement. An explicit transaction can contain one or more statements and must be terminated by either a COMMIT TRANSACTION or a ROLLBACK TRANSACTION statement. Implicit Transaction Mode SQL Server can automatically or, more precisely, implicitly start a transaction for you if a SET IMPLICIT_TRANSACTIONS ON statement is run or if the implicit transaction option is turned on globally by running sp_configure ‘user options’ 2. (Actually, the bit mask 0x2 must be turned on for the user option so you might have to perform an ‘OR’ operation with the existing user option value.) See SQL Server 2000 Books Online on how to turn on implicit transaction under ODBC and OLE DB (acdata.chm::/ac_8_md_06_2g6r.htm). Transaction Nesting Explicit transactions can be nested. Committing inner transactions is ignored by SQL Server other than to decrements @@TRANCOUNT. The transaction is either committed or rolled back based on the action taken at the end of the outermost transaction. If the outer transaction is committed, the inner nested transactions are also committed. If the outer transaction is rolled back, then all inner transactions are also rolled back, regardless of whether the inner transactions were individually committed. Each call to COMMIT TRANSACTION applies to the last executed BEGIN TRANSACTION. If the BEGIN TRANSACTION statements are nested, then a COMMIT statement applies only to the last nested transaction, which is the innermost transaction. Even if a COMMIT TRANSACTION transaction_name statement within a nested transaction refers to the transaction name of the outer transaction, the commit applies only to the innermost transaction. If a ROLLBACK TRANSACTION statement without a transaction_name parameter is executed at any level of a set of nested transaction, it rolls back all the nested transactions, including the outermost transaction. The @@TRANCOUNT function records the current transaction nesting level. Each BEGIN TRANSACTION statement increments @@TRANCOUNT by one. Each COMMIT TRANSACTION statement decrements @@TRANCOUNT by one. A ROLLBACK TRANSACTION statement that does not have a transaction name rolls back all nested transactions and decrements @@TRANCOUNT to 0. A ROLLBACK TRANSACTION that uses the transaction name of the outermost transaction in a set of nested transactions rolls back all the nested transactions and decrements @@TRANCOUNT to 0. When you are unsure if you are already in a transaction, SELECT @@TRANCOUNT to determine whether it is 1 or more. If @@TRANCOUNT is 0 you are not in a transaction. You can also find the transaction nesting level by checking the sysprocess.open_tran column. See SQL Server 2000 Books Online topic “Nesting Transactions” (acdata.chm::/ac_8_md_06_66nq.htm) for more information. Statement, Transaction, and Batch Abort One batch can have many statements and one transaction can have multiple statements, also. One transaction can span multiple batches and one batch can have multiple transactions. Statement Abort Currently executing statement is aborted. This can be a bit confusing when you start talking about statements in a trigger or stored procedure. Let us look closely at the following trigger: CREATE TRIGGER TRG8134 ON TBL8134 AFTER INSERT AS BEGIN SELECT 1/0 SELECT 'Next command in trigger' END To fire the INSERT trigger, the batch could be as simple as ‘INSERT INTO TBL8134 VALUES(1)’. However, the trigger contains two statements that must be executed as part of the batch to satisfy the clients insert request. When the ‘SELECT 1/0’ causes the divide by zero error, a statement abort is issued for the ‘SELECT 1/0’ statement. Batch and Transaction Abort On SQL Server 2000 (and SQL Server 7.0) whenever a non-informational error is encountered in a trigger, the statement abort is promoted to a batch and transactional abort. Thus, in the example the statement abort for ‘select 1/0’ promotion results in an entire batch abort. No further statements in the trigger or batch will be executed and a rollback is issued. On SQL Server 6.5, the statement aborts immediately and results in a transaction abort. However, the rest of the statements within the trigger are executed. This trigger could return ‘Next command in trigger’ as a result set. Once the trigger completes the batch abort promotion takes effect. Conversely, submitting a similar set of statements in a standalone batch can result in different behavior. SELECT 1/0 SELECT 'Next command in batch' Not considering the set option possibilities, a divide by zero error generally results in a statement abort. Since it is not in a trigger, the promotion to a batch abort is avoided and subsequent SELECT statement can execute. The programmer should add an “if @@ERROR” check immediately after the ‘select 1/0’ to T-SQL execution to control the flow correctly. Aborting and Set Options ARITHABORT If SET ARITHABORT is ON, these error conditions cause the query or batch to terminate. If the errors occur in a transaction, the transaction is rolled back. If SET ARITHABORT is OFF and one of these errors occurs, a warning message is displayed, and NULL is assigned to the result of the arithmetic operation. When an INSERT, DELETE, or UPDATE statement encounters an arithmetic error (overflow, divide-by-zero, or a domain error) during expression evaluation when SET ARITHABORT is OFF, SQL Server inserts or updates a NULL value. If the target column is not nullable, the insert or update action fails and the user receives an error. XACT_ABORT When SET XACT_ABORT is ON, if a Transact-SQL statement raises a run-time error, the entire transaction is terminated and rolled back. When OFF, only the Transact-SQL statement that raised the error is rolled back and the transaction continues processing. Compile errors, such as syntax errors, are not affected by SET XACT_ABORT. For example: CREATE TABLE t1 (a int PRIMARY KEY) CREATE TABLE t2 (a int REFERENCES t1(a)) GO INSERT INTO t1 VALUES (1) INSERT INTO t1 VALUES (3) INSERT INTO t1 VALUES (4) INSERT INTO t1 VALUES (6) GO SET XACT_ABORT OFF GO BEGIN TRAN INSERT INTO t2 VALUES (1) INSERT INTO t2 VALUES (2) /* Foreign key error */ INSERT INTO t2 VALUES (3) COMMIT TRAN SELECT 'Continue running batch 1...' GO SET XACT_ABORT ON GO BEGIN TRAN INSERT INTO t2 VALUES (4) INSERT INTO t2 VALUES (5) /* Foreign key error */ INSERT INTO t2 VALUES (6) COMMIT TRAN SELECT 'Continue running batch 2...' GO /* Select shows only keys 1 and 3 added. Key 2 insert failed and was rolled back, but XACT_ABORT was OFF and rest of transaction succeeded. Key 5 insert error with XACT_ABORT ON caused all of the second transaction to roll back. Also note that 'Continue running batch 2...' is not Returned to indicate that the batch is aborted. */ SELECT * FROM t2 GO DROP TABLE t2 DROP TABLE t1 GO Compile and Run-time Errors Compile Errors Compile errors are encountered during syntax checks, security checks, and other general operations to prepare the batch for execution. These errors can prevent the optimization of the query and thus lead to immediate abort. The statement is not run and the batch is aborted. The transaction state is generally left untouched. For example, assume there are four statements in a particular batch. If the third statement has a syntax error, none of the statements in the batch is executed. Optimization Errors Optimization errors would include rare situations where the statement encounters a problem when attempting to build an optimal execution plan. Example: “too many tables referenced in the query” error is reported because a “work table” was added to the plan. Runtime Errors Runtime errors are those that are encountered during the execution of the query. Consider the following batch: SELECT * FROM pubs.dbo.titles UPDATE pubs.dbo.authors SET au_lname = au_lname SELECT * FROM foo UPDATE pubs.dbo.authors SET au_lname = au_lname If you run the above statements in a batch, the first two statements will be executed, the third statement will fail because table foo does not exist, and the batch will terminate. Deferred Name Resolution is the feature that allows this batch to start executing before resolving the object foo. This feature allows SQL Server to delay object resolution and place a “placeholder” in the query’s execution. The object referenced by the placeholder is resolved until the query is executed. In our example, the execution of the statement “SELECT * FROM foo” will trigger another compile process to resolve the name again. This time, error message 208 is returned. Error: 208, Level 16, State 1, Line 1 Invalid object name 'foo'. Message 208 can be encountered as a runtime or compile error depending on whether the Deferred Name Resolution feature is available. In SQL Server 6.5 this would be considered a compile error and on SQL Server 2000 (and SQL Server7.0) as a runtime error due to Deferred Name Resolution. In the following example, if a trigger referenced authors2, the error is detected as SQL Server attempts to execute the trigger. However, under SQL Server 6.5 the create trigger statement fails because authors2 does not exist at compile time. When errors are encountered in a trigger, generally, the statement, batch, and transaction are aborted. You should be able to observe this by running the following script in pubs database: Create table tblTest(iID int) go create trigger trgInsert on tblTest for INSERT as begin select * from authors select * from authors2 select * from titles end go begin tran select 'Before' insert into tblTest values(1) select 'After' go select @@TRANCOUNT go When run in a batch, the statement and the batch are aborted but the transaction remains active. The follow script illustrates this: begin tran select 'Before' select * from authors2 select 'After' go select @@TRANCOUNT go One other factor in a compile versus runtime error is implicit data type conversions. If you were to run the following statements on SQL Server 6.5 and SQL Server 2000 (and SQL Server 7.0): create table tblData(dtData datetime) go select 1 insert into tblData values(12/13/99) go On SQL Server 6.5, you get an error before execution of the batch begins so no statements are executed and the batch is aborted. Error: 206, Level 16, State 2, Line 2 Operand type clash: int is incompatible with datetime On SQL Server 2000, you get the default value (1900-01-01 00:00:00.000) inserted into the table. SQL Server 2000 implicit data type conversion treats this as integer division. The integer division of 12/13/99 is 0, so the default date and time value is inserted, no error returned. To correct the problem on either version is to wrap the date string with quotes. See Bug #56118 (sqlbug_70) for more details about this situation. Another example of a runtime error is a 605 message. Error: 605 Attempt to fetch logical page %S_PGID in database '%.*ls' belongs to object '%.*ls', not to object '%.*ls'. A 605 error is always a runtime error. However, depending on the transaction isolation level, (e.g. using the NOLOCK lock hint), established by the SPID the handling of the error can vary. Specifically, a 605 error is considered an ACCESS error. Errors associated with buffer and page access are found in the 600 series of errors. When the error is encountered, the isolation level of the SPID is examined to determine proper handling based on information or fatal error level. Transaction Error Checking Not all errors cause transactions to automatically rollback. Although it is difficult to determine exactly which errors will rollback transactions and which errors will not, the main idea here is that programmers must perform error checking and handle errors appropriately. Error Handling Raiserror Details Raiserror seems to be a source of confusion but is really rather simple. Raiserror with severity levels of 20 or higher will terminate the connection. Of course, when the connection is terminated a full rollback of any open transaction will immediately be instantiated by the SQL Server (except distributed transaction with DTC involved). Severity levels lower than 20 will simply result in the error message being returned to the client. They do not affect the transaction scope of the connection. Consider the following batch: use pubs begin tran update authors set au_lname = 'smith' raiserror ('This is bad', 19, 1) with log select @@trancount With severity set at 19, the 'select @@trancount' will be executed after the raiserror statement and will return a value of 1. If severity is changed to 20, then the select statement will not run and the connection is broken. Important Error handling must occur not only in T-SQL batches and stored procedures, but also in application program code. Transactions and Triggers (1 of 2) Basic behavior assumes the implicit transactions setting is set to OFF. This behavior makes it possible to identify business logic errors in a trigger, raise an error, rollback the action, and add an audit table entry. Logically, the insert to the audit table cannot take place before the ROLLBACK action and you would not want to build in the audit table insert into every applications error handler that violated the business rule of the trigger. For more information, see also… SQL Server 2000 Books Online topic “Rollbacks in stored procedure and triggers“ (acdata.chm::/ac_8_md_06_4qcz.htm) IMPLICIT_TRANSACTIONS ON Behavior The behavior of firing other triggers on the same table can be tricky. Say you added a trigger that checks the CODE field. Read only versions of the rows contain the code ‘RO’ and read/write versions use ‘RW.’ Whenever someone tries to delete a row with a code ‘RO’ the trigger issues the rollback and logs an audit table entry. However, you also have a second trigger that is responsible for cascading delete operations. One client could issue the delete without implicit transactions on and only the current trigger would execute and then terminate the batch. However, a second client with implicit transactions on could issue the same delete and the secondary trigger would fire. You end up with a situation in which the cascading delete operations can take place (are committed) but the initial row remains in the table because of the rollback operation. None of the delete operations should be allowed but because the transaction scope was restarted because of the implicit transactions setting, they did. Transactions and Triggers (2 of 2) It is extremely difficult to determine the execution state of a trigger when using explicit rollback statements in combination with implicit transactions. The RETURN statement is not allowed to return a value. The only way I have found to set the @@ERROR is using a ‘raiserror’ as the last execution statement in the last trigger to execute. If you modify the example, this following RAISERROR statement will set @@ERROR to 50000: CREATE TRIGGER trgTest on tblTest for INSERT AS BEGIN ROLLBACK INSERT INTO tblAudit VALUES (1) RAISERROR('This is bad', 14,1) END However, this value does not carry over to a secondary trigger for the same table. If you raise an error at the end of the first trigger and then look at @@ERROR in the secondary trigger the @@ERROR remains 0. Carrying Forward an Active/Open Transaction It is possible to exit from a trigger and carry forward an open transaction by issuing a BEGIN TRAN or by setting implicit transaction on and doing INSERT, UPDATE, or DELETE. Warning It is never recommended that a trigger call BEGIN TRANSACTION. By doing this you increment the transaction count. Invalid code logic, not calling commit transaction, can lead to a situation where the transaction count remains elevated upon exit of the trigger. Transaction Count The behavior is better explained by understanding how the server works. It does not matter whether you are in a transaction, when a modification takes place the transaction count is incremented. So, in the simplest form, during the processing of an insert the transaction count is 1. On completion of the insert, the server will commit (and thus decrement the transaction count). If the commit identifies the transaction count has returned to 0, the actual commit processing is completed. Issuing a commit when the transaction count is greater than 1 simply decrements the nested transaction counter. Thus, when we enter a trigger, the transaction count is 1. At the completion of the trigger, the transaction count will be 0 due to the commit issued at the end of the modification statement (insert). In our example, if the connection was already in a transaction and called the second INSERT, since implicit transaction is ON, the transaction count in the trigger will be 2 as long as the ROLLBACK is not executed. At the end of the insert, the commit is again issued to decrement the transaction reference count to 1. However, the value does not return to 0 so the transaction remains open/active. Subsequent triggers are only fired if the transaction count at the end of the trigger remains greater than or equal to 1. The key to continuation of secondary triggers and the batch is the transaction count at the end of a trigger execution. If the trigger that performs a rollback has done an explicit begin transaction or uses implicit transactions, subsequent triggers and the batch will continue. If the transaction count is not 1 or greater, subsequent triggers and the batch will not execute. Warning Forcing the transaction count after issuing a rollback is dangerous because you can easily loose track of your transaction nesting level. When performing an explicit rollback in a trigger, you should immediately issue a return statement to maintain consistent behavior between a connection with and without implicit transaction settings. This will force the trigger(s) and batch to terminate immediately. One of the methods of dealing with this issue is to run ‘SET IMPLICIT_TRANSACTIONS OFF’ as the first statement of any trigger. Other methods may entails checking @@TRANCOUNT at the end of the trigger and continue to COMMIT the transaction as long as @@TRANCOUNT is greater than 1. Examples The following examples are based on this table: create table tbl50000Insert (iID int NOT NULL) go Note If more than one trigger is used, to guarantee the trigger firing sequence, the sp_settriggerorder command should be used. This command is omitted in these examples to simplify the complexity of the statements. First Example In the first example, the second trigger was never fired and the batch, starting with the insert statement, was aborted. Thus, the print statement was never issued. print('Trigger issues rollback - cancels batch') go create trigger trg50000Insert on tbl50000Insert for INSERT as begin select 'Inserted', * from inserted rollback tran select 'End of trigger', @@TRANCOUNT as 'TRANCOUNT' end go create trigger trg50000Insert2 on tbl50000Insert for INSERT as begin select 'In Trigger2' select 'Trigger 2 Inserted', * from inserted end go insert into tbl50000Insert values(1) print('---------------------- In same batch') select * from tbl50000Insert go -- Cleanup drop trigger trg50000Insert drop trigger trg50000Insert2 go delete from tbl50000Insert Second Example The next example shows that since a new transaction is started, the second trigger will be fired and the print statement in the batch will be executed. Note that the insert is rolled back. print('Trigger issues rollback - increases tran count to continue batch') go create trigger trg50000Insert on tbl50000Insert for INSERT as begin select 'Inserted', * from inserted rollback tran begin tran end go create trigger trg50000Insert2 on tbl50000Insert for INSERT as begin select 'In Trigger2' select 'Trigger 2 Inserted', * from inserted end go insert into tbl50000Insert values(2) print('---------------------- In same batch') select * from tbl50000Insert go -- Cleanup drop trigger trg50000Insert drop trigger trg50000Insert2 go delete from tbl50000Insert Third Example In the third example, the raiserror statement is used to set the @@ERROR value and the BEGIN TRAN statement is used in the trigger to allow the batch to continue to run. print('Trigger issues rollback - uses raiserror to set @@ERROR') go create trigger trg50000Insert on tbl50000Insert for INSERT as begin select 'Inserted', * from inserted rollback tran begin tran -- Increase @@trancount to allow -- batch to continue select @@trancount as ‘Trancount’ raiserror('This is from the trigger', 14,1) end go insert into tbl50000Insert values(3) select @@ERROR as 'ERROR', @@TRANCOUNT as 'Trancount' go -- Cleanup drop trigger trg50000Insert go delete from tbl50000Insert Fourth Example For the fourth example, a second trigger is added to illustrate the fact that @@ERROR value set in the first trigger will not be seen in the second trigger nor will it show up in the batch after the second trigger is fired. print('Trigger issues rollback - uses raiserror to set @@ERROR, not seen in second trigger and cleared in batch') go create trigger trg50000Insert on tbl50000Insert for INSERT as begin select 'Inserted', * from inserted rollback begin tran -- Increase @@trancount to -- allow batch to continue select @@TRANCOUNT as 'Trancount' raiserror('This is from the trigger', 14,1) end go create trigger trg50000Insert2 on tbl50000Insert for INSERT as begin select @@ERROR as 'ERROR', @@TRANCOUNT as 'Trancount' end go insert into tbl50000Insert values(4) select @@ERROR as 'ERROR', @@TRANCOUNT as 'Trancount' go -- Cleanup drop trigger trg50000Insert drop trigger trg50000Insert2 go delete from tbl50000Insert Lesson 3: Concepts – Locks and Applications This lesson outlines some of the common causes that contribute to the perception of a slow server. What You Will Learn After completing this lesson, you will be able to:  Explain how lock hints are used and their impact.  Discuss the effect on locking when an application uses Microsoft Transaction Server.  Identify the different kinds of deadlocks including distributed deadlock. Recommended Reading  Charter 14 “Locking”, Inside SQL Server 2000 by Kalen Delaney  Charter 16 “Query Tuning”, Inside SQL Server 2000 by Kalen Delaney Q239753 – Deadlock Situation Not Detected by SQL Server Q288752 – Blocked SPID Not Participating in Deadlock May Incorrectly be Chosen as victim Locking Hints UPDLOCK If update locks are used instead of shared locks while reading a table, the locks are held until the end of the statement or transaction. UPDLOCK has the advantage of allowing you to read data (without blocking other readers) and update it later with the assurance that the data has not changed since you last read it. READPAST READPAST is an optimizer hint for use with SELECT statements. When this hint is used, SQL Server will read past locked rows. For example, assume table T1 contains a single integer column with the values of 1, 2, 3, 4, and 5. If transaction A changes the value of 3 to 8 but has not yet committed, a SELECT * FROM T1 (READPAST) yields values 1, 2, 4, 5. Tip READPAST only applies to transactions operating at READ COMMITTED isolation and only reads past row-level locks. This lock hint can be used to implement a work queue on a SQL Server table. For example, assume there are many external work requests being thrown into a table and they should be serviced in approximate insertion order but they do not have to be completely FIFO. If you have 4 worker threads consuming work items from the queue they could each pick up a record using read past locking and then delete the entry from the queue and commit when they're done. If they fail, they could rollback, leaving the entry on the queue for the next worker thread to pick up. Caution The READPAST hint is not compatible with HOLDLOCK.  Try This: Using Locking Hints 1. Open a Query Window and connect to the pubs database. 2. Execute the following statements (--Conn 1 is optional to help you keep track of each connection): BEGIN TRANSACTION -- Conn 1 UPDATE titles SET price = price * 0.9 WHERE title_id = 'BU1032' 3. Open a second connection and execute the following statements: SELECT @@lock_timeout -- Conn 2 GO SELECT * FROM titles SELECT * FROM authors 4. Open a third connection and execute the following statements: SET LOCK_TIMEOUT 0 -- Conn 3 SELECT * FROM titles SELECT * FROM authors 5. Open a fourth connection and execute the following statement: SELECT * FROM titles (READPAST) -- Conn 4 WHERE title_ID < 'C' SELECT * FROM authors How many records were returned? 3 6. Open a fifth connection and execute the following statement: SELECT * FROM titles (NOLOCK) -- Conn 5 WHERE title_ID 0 the lock manager also checks for deadlocks every time a SPID gets blocked. So a single deadlock will trigger 20 seconds of more immediate deadlock detection, but if no additional deadlocks occur in that 20 seconds, the lock manager no longer checks for deadlocks at each block and detection again only happens every 5 seconds. Although normally not needed, you may use trace flag -T1205 to trace the deadlock detection process. Note Please note the distinction between application lock and other locks’ deadlock detection. For application lock, we do not rollback the transaction of the deadlock victim but simply return a -3 to sp_getapplock, which the application needs to handle itself. Deadlock Resolution How is a deadlock resolved? SQL Server picks one of the connections as a deadlock victim. The victim is chosen based on either which is the least expensive transaction (calculated using the number and size of the log records) to roll back or in which process “SET DEADLOCK_PRIORITY LOW” is specified. The victim’s transaction is rolled back, held locks are released, and SQL Server sends error 1205 to the victim’s client application to notify it that it was chosen as a victim. The other process can then obtain access to the resource it was waiting on and continue. Error 1205: Your transaction (process ID #%d) was deadlocked with another process and has been chosen as the deadlock victim. Rerun your transaction. Symptoms of deadlocking Error 1205 usually is not written to the SQL Server errorlog. Unfortunately, you cannot use sp_altermessage to cause 1205 to be written to the errorlog. If the client application does not capture and display error 1205, some of the symptoms of deadlock occurring are:  Clients complain of mysteriously canceled queries when using certain features of an application.  May be accompanied by excessive blocking. Lock contention increases the chances that a deadlock will occur. Triggers and Deadlock Triggers promote the deadlock priority of the SPID for the life of the trigger execution when the DEADLOCK PRIORITY is not set to low. When a statement in a trigger causes a deadlock to occur, the SPID executing the trigger is given preferential treatment and will not become the victim. Warning Bug 235794 is filed against SQL Server 2000 where a blocked SPID that is not a participant of a deadlock may incorrectly be chosen as a deadlock victim if the SPID is blocked by one of the deadlock participants and the SPID has the least amount of transaction logging. See KB article Q288752: “Blocked Spid Not Participating in Deadlock May Incorrectly be Chosen as victim” for more information. Distributed Deadlock – Scenario 1 Distributed Deadlocks The term distributed deadlock is ambiguous. There are many types of distributed deadlocks. Scenario 1 Client application opens connection A, begins a transaction, acquires some locks, opens connection B, connection B gets blocked by A but the application is designed to not commit A’s transaction until B completes. Note SQL Server has no way of knowing that connection A is somehow dependent on B – they are two distinct connections with two distinct transactions. This situation is discussed in scenario #4 in “Q224453 INF: Understanding and Resolving SQL Server 7.0 Blocking Problems”. Distributed Deadlock – Scenario 2 Scenario 2 Distributed deadlock involving bound connections. Two connections can be bound into a single transaction context with sp_getbindtoken/sp_bindsession or via DTC. Spid 60 enlists in a transaction with spid 61. A third spid 62 is blocked by spid 60, but spid 61 is blocked by spid 62. Because they are doing work in the same transaction, spid 60 cannot commit until spid 61 finishes his work, but spid 61 is blocked by 62 who is blocked by 60. This scenario is described in article “Q239753 - Deadlock Situation Not Detected by SQL Server.” Note SQL Server 6.5 and 7.0 do not detect this deadlock. The SQL Server 2000 deadlock detection algorithm has been enhanced to detect this type of distributed deadlock. The diagram in the slide illustrates this situation. Resources locked by a spid are below that spid (in a box). Arrows indicate blocking and are drawn from the blocked spid to the resource that the spid requires. A circle represents a transaction; spids in the same transaction are shown in the same circle. Distributed Deadlock – Scenario 3 Scenario 3 Distributed deadlock involving linked servers or server-to-server RPC. Spid 60 on Server 1 executes a stored procedure on Server 2 via linked server. This stored procedure does a loopback linked server query against a table on Server 1, and this connection is blocked by a lock held by Spid 60. Note No version of SQL Server is currently designed to detect this distributed deadlock. Lesson 4: Information Collection and Analysis This lesson outlines some of the common causes that contribute to the perception of a slow server. What You Will Learn After completing this lesson, you will be able to:  Identify specific information needed for troubleshooting issues.  Locate and collect information needed for troubleshooting issues.  Analyze output of DBCC Inputbuffer, DBCC PSS, and DBCC Page commands.  Review information collected from master.dbo.sysprocesses table.  Review information collected from master.dbo.syslockinfo table.  Review output of sp_who, sp_who2, sp_lock.  Analyze Profiler log for query usage pattern.  Review output of trace flags to help troubleshoot deadlocks. Recommended Reading Q244455 - INF: Definition of Sysprocesses Waittype and Lastwaittype Fields Q244456 - INF: Description of DBCC PSS Command for SQL Server 7.0 Q271509 - INF: How to Monitor SQL Server 2000 Blocking Q251004 - How to Monitor SQL Server 7.0 Blocking Q224453 - Understanding and Resolving SQL Server 7.0 Blocking Problem Q282749 – BUG: Deadlock information reported with SQL Server 2000 Profiler Locking and Blocking  Try This: Examine Blocked Processes 1. Open a Query Window and connect to the pubs database. Execute the following statements: BEGIN TRAN -- connection 1 UPDATE titles SET price = price + 1 2. Open another connection and execute the following statement: SELECT * FROM titles-- connection 2 3. Open a third connection and execute sp_who; note the process id (spid) of the blocked process. (Connection 3) 4. In the same connection, execute the following: SELECT spid, cmd, waittype FROM master..sysprocesses WHERE waittype 0 -- connection 3 5. Do not close any of the connections! What was the wait type of the blocked process?  Try This: Look at locks held Assumes all your connections are still open from the previous exercise. • Execute sp_lock -- Connection 3 What locks is the process from the previous example holding? Make sure you run ROLLBACK TRAN in Connection 1 to clean up your transaction. Collecting Information See Module 2 for more about how to gather this information using various tools. Recognizing Blocking Problems How to Recognize Blocking Problems  Users complain about poor performance at a certain time of day, or after a certain number of users connect.  SELECT * FROM sysprocesses or sp_who2 shows non-zero values in the blocked or BlkBy column.  More severe blocking incidents will have long blocking chains or large sysprocesses.waittime values for blocked spids.  Possibl

2009-11-27

微软内部资料-SQL性能优化2

Contents Module Overview 1 Lesson 1: Memory 3 Lesson 2: I/O 73 Lesson 3: CPU 111 Module 3: Troubleshooting Server Performance Module Overview Troubleshooting server performance-based support calls requires product knowledge, good communication skills, and a proven troubleshooting methodology. In this module we will discuss Microsoft® SQL Server™ interaction with the operating system and methodology of troubleshooting server-based problems. At the end of this module, you will be able to:  Define the common terms associated the memory, I/O, and CPU subsystems.  Describe how SQL Server leverages the Microsoft Windows® operating system facilities including memory, I/O, and threading.  Define common SQL Server memory, I/O, and processor terms.  Generate a hypothesis based on performance counters captured by System Monitor.  For each hypothesis generated, identify at least two other non-System Monitor pieces of information that would help to confirm or reject your hypothesis.  Identify at least five counters for each subsystem that are key to understanding the performance of that subsystem.  Identify three common myths associated with the memory, I/O, or CPU subsystems. Lesson 1: Memory What You Will Learn After completing this lesson, you will be able to:  Define common terms used when describing memory.  Give examples of each memory concept and how it applies to SQL Server.  Describe how SQL Server user and manages its memory.  List the primary configuration options that affect memory.  Describe how configuration options affect memory usage.  Describe the effect on the I/O subsystem when memory runs low.  List at least two memory myths and why they are not true. Recommended Reading  SQL Server 7.0 Performance Tuning Technical Reference, Microsoft Press  Windows 2000 Resource Kit companion CD-ROM documentation. Chapter 15: Overview of Performance Monitoring  Inside Microsoft Windows 2000, Third Edition, David A. Solomon and Mar

2009-11-27

微软内部资料-SQL性能优化1

Contents Overview 1 Lesson 1: Gathering and Evaluating Core Information 3 Lesson 2: Using Performance Monitor 19 Lesson 3: Using SQL Profiler 35 Lesson 4: Using Index Tuning Wizard 57 Lesson 5: Using Other System Tools 67 Module 2: Tools – Exploring the Conditions Overview At the end of this module, you will be able to:  List the basic set of information to collect to help narrow down and identify the problem.  Explore and confirm conditions or messages presented or already known.  Describe Performance Monitor and how to use it to troubleshoot performance issues.  Analyze a Performance Monitor log to identify potential performance bottlenecks.  List DBCC commands relevant to performance troubleshooting and describe how they are used.  Analyze DBCC command output to identify potential performance bottlenecks.  List Trace Flags relevant to performance troubleshooting and describe how they are used.  Analyze Trace Flags output, if any, to identify potential performance bottlenecks.  List Profiler Events and their respective data columns relevant to performance troubleshooting and describe how they are used.  Choose and log the necessary events to troubleshoot performance issues.  Analyze Profiler Log to identify potential performance bottlenecks.  Describe the Index Tuning Wizard’s architecture.  List the command arguments for the ITWiz executable.  Discuss considerations when using the Index Tuning Wizard.  List other tools and commands relevant to performance troubleshooting  Describe what information can be collected using TList, Pviewer, Pstat, and Vmstat.  Analyze information collected using TList, Pviewer, Pstat, and Vmstat.  Explain why scripts are used to collect information.  Discuss examples of how scripts can be used.  Describe the use of Microsoft® SQL Server™ Profiler to replay trace and simulate stress.  Describe the use of OStress tool to simulate stress.  List external load simulation tools. Lesson 1: Gathering a

2009-11-27

PowerDesigner逆向工程详细教程

1 PowerDesigner逆向工程 传说中,程序员们喜欢用powerDesign进行数据库建模。通常都是先设计出物理模型图,在转换出数据库需要的SQL语句,从而生成数据库。 但,江湖中流传着“powerDesign逆向工程”的传说。好,我们今天就来利用PowerDesign来建立逆向工程。 基于MySql 5.0 的数据库,PowerDesigner12.主要分为这几个步骤。 1> 通过windows数据源管理,建立ODBC数据源。 • 首先,安装ODBC的补丁。 • 这里是mySql 3.5.1 和 mySql5.1.5的补丁文件。使用他们进行安装。 • 打开Windows的控制面板 • 打开管理工具 • 打开数据源(ODBC) • 选择你要操作的数据库类型 • 输入数据库参数,并测试连接 2> 通过powerDesigner使用ODBC数据源,建立PowerDesigner的数据源。 • 新建物理模型. • 选择数据库(DataBase)---连接(Connect)。 • 选择已经配置好的ODBC数据源。 • 输入登录数据库的【用户名】和【密码】。 2> 使用PowerDesigner进行逆向工程。 • 选择 DataBase----Reverse Engineer Database。 在 15.0.0.2613版本中逆向菜单已换成 • 使用已经配置好的数据源。 • 选择你感兴趣的内容。 • 生成物理模型。 • 我们稍做等待,我出现了我们需要的数据模型。如下图: • 放大后 • 到这里,我们的逆向工程,就完成了。

2009-11-24

SQL示例大全.pdf

精心收集整理的各种SQL语句示例,帮助您更加容易的熟悉和使SQL。 1 DECLARE @local_variable 1.1 使用 DECLARE 以下示例将使用名为 @find 的局部变量检索所有姓氏以“Man”开头的联系人信息。 USE AdventureWorks; GO DECLARE @find varchar(30); SET @find = 'Man%'; SELECT LastName, FirstName, Phone FROM Person.Contact WHERE LastName LIKE @find; 2 EXECUTE 2.1 使用 EXECUTE 传递单个参数 uspGetEmployeeManagers 存储过程需要一个参数 (@EmployeeID)。 以下示例执行 uspGetEmployeeManagers 存储过程,以 Employee ID 6 作为参数值。 USE AdventureWorks; GO EXEC dbo.uspGetEmployeeManagers 6; GO 2.2 在执行过程中变量可以显式命名: EXEC dbo.uspGetEmployeeManagers @EmployeeID = 6; GO 2.3 使用多个参数 以下示例执行 spGetWhereUsedProductID 存储过程。 该存储过程将传递两个参数:第一个参数为产品 ID (819),第二个参数 @CheckDate, 是 datetime 值。 USE AdventureWorks; GO DECLARE @CheckDate datetime; SET @CheckDate = GETDATE(); EXEC dbo.uspGetWhereUsedProductID 819, @CheckDate; GO 3 sp_executesql 执行可以多次重复使用或动态生成的 Transact-SQL 语句或批处理。Transact-SQL 语句或批处理可以包含嵌入参数。 sp_executesql 支持与 Transact-SQL 字符串分开的参数值的设置,如以下示例所示。 DECLARE @IntVariable int; DECLARE @SQLString nvarchar(500); DECLARE @ParmDefinition nvarchar(500); /* Build the SQL string one time.*/ SET @SQLString = N'SELECT EmployeeID, NationalIDNumber, Title, ManagerID FROM AdventureWorks.HumanResources.Employee WHERE ManagerID = @ManagerID'; SET @ParmDefinition = N'@ManagerID tinyint'; /* Execute the string with the first parameter value. */ SET @IntVariable = 197; EXECUTE sp_executesql @SQLString, @ParmDefinition, @ManagerID = @IntVariable; /* Execute the same string with the second parameter value. */ SET @IntVariable = 109; EXECUTE sp_executesql @SQLString, @ParmDefinition, @ManagerID = @IntVariable; 输出参数也可用于 sp_executesql。以下示例从 AdventureWorks.HumanResources.Employee 表中检索职务,并在输出参数 @max_title 中返回它。 DECLARE @IntVariable int; DECLARE @SQLString nvarchar(500); DECLARE @ParmDefinition nvarchar(500); DECLARE @max_title varchar(30); SET @IntVariable = 197; SET @SQLString = N'SELECT @max_titleOUT = max(Title) FROM AdventureWorks.HumanResources.Employee WHERE ManagerID = @level'; SET @ParmDefinition = N'@level tinyint, @max_titleOUT varchar(30) OUTPUT'; EXECUTE sp_executesql @SQLString, @ParmDefinition, @level = @IntVariable, @max_titleOUT=@max_title OUTPUT; SELECT @max_title; 3.1 执行简单的 SELECT 语句 以下示例将创建并执行一个简单的 SELECT 语句,其中包含名为 @level 的嵌入参数。 EXECUTE sp_executesql N'SELECT * FROM AdventureWorks.HumanResources.Employee WHERE ManagerID = @level', N'@level tinyint', @level = 109; 3.2 执行动态生成的字符串 以下示例显示使用 sp_executesql 执行动态生成的字符串。该示例中的存储过程用于向一组表中插入数据,这些表用于划分一年的销售数据。一年中的每个月均有一个表,格式如下: CREATE TABLE May1998Sales (OrderID int PRIMARY KEY, CustomerID int NOT NULL, OrderDate datetime NULL CHECK (DATEPART(yy, OrderDate) = 1998), OrderMonth int CHECK (OrderMonth = 5), DeliveryDate datetime NULL, CHECK (DATEPART(mm, OrderDate) = OrderMonth) ) 3.3 使用 OUTPUT 参数 以下示例使用 OUTPUT 参数将由 SELECT 语句生成的结果集存储于 @SQLString 参数中。然后将执行两个使用 OUTPUT 参数值的 SELECT 语句。 USE AdventureWorks; GO DECLARE @SQLString nvarchar(500); DECLARE @ParmDefinition nvarchar(500); DECLARE @SalesOrderNumber nvarchar(25); DECLARE @IntVariable int; SET @SQLString = N'SELECT @SalesOrderOUT = MAX(SalesOrderNumber) FROM Sales.SalesOrderHeader WHERE CustomerID = @CustomerID'; SET @ParmDefinition = N'@CustomerID int, @SalesOrderOUT nvarchar(25) OUTPUT'; SET @IntVariable = 22276; EXECUTE sp_executesql @SQLString, @ParmDefinition, @CustomerID = @IntVariable, @SalesOrderOUT = @SalesOrderNumber OUTPUT; -- This SELECT statement returns the value of the OUTPUT parameter. SELECT @SalesOrderNumber; -- This SELECT statement uses the value of the OUTPUT parameter in -- the WHERE clause. SELECT OrderDate, TotalDue FROM Sales.SalesOrderHeader WHERE SalesOrderNumber = @SalesOrderNumber; 4 CAST 和 CONVERT 将一种数据类型的表达式显式转换为另一种数据类型的表达式。CAST 和 CONVERT 提供相似的功能。 DECLARE @myval decimal (5, 2) SET @myval = 193.57 SELECT CAST(CAST(@myval AS varbinary(20)) AS decimal(10,5)) -- Or, using CONVERT SELECT CONVERT(decimal(10,5), CONVERT(varbinary(20), @myval)) 4.1 同时使用 CAST 和 CONVERT -- Use CAST USE AdventureWorks; GO SELECT SUBSTRING(Name, 1, 30) AS ProductName, ListPrice FROM Production.Product WHERE CAST(ListPrice AS int) LIKE '3%'; GO -- Use CONVERT. USE AdventureWorks; GO SELECT SUBSTRING(Name, 1, 30) AS ProductName, ListPrice FROM Production.Product WHERE CONVERT(int, ListPrice) LIKE '3%'; GO 4.2 使用包含算术运算符的 CAST USE AdventureWorks; GO SELECT CAST(ROUND(SalesYTD/CommissionPCT, 0) AS int) AS 'Computed' FROM Sales.SalesPerson WHERE CommissionPCT != 0; GO 4.3 使用 CAST 进行连接 USE AdventureWorks; GO SELECT 'The list price is ' + CAST(ListPrice AS varchar(12)) AS ListPrice FROM Production.Product WHERE ListPrice BETWEEN 350.00 AND 400.00; GO 4.4 使用 CAST 生成可读性更高的文本 USE AdventureWorks; GO SELECT DISTINCT CAST(p.Name AS char(10)) AS Name, s.UnitPrice FROM Sales.SalesOrderDetail s JOIN Production.Product p on s.ProductID = p.ProductID WHERE Name LIKE 'Long-Sleeve Logo Jersey, M'; GO 4.5 对日期时间数据使用 CAST 和 CONVERT SELECT GETDATE() AS UnconvertedDateTime, CAST(GETDATE() AS nvarchar(30)) AS UsingCast, CONVERT(nvarchar(30), GETDATE(), 126) AS UsingConvertTo_ISO8601 ; GO 5 SELECT 5.1 将 DISTINCT 与 SELECT 一起使用 以下示例使用 DISTINCT 以避免检索重复标题。 USE AdventureWorks ; GO SELECT DISTINCT Title FROM HumanResources.Employee ORDER BY Title ; GO 5.2 使用 SELECT INTO 创建表 以下第一个示例将在 tempdb 中创建一个名为 #Bicycles 的临时表。若要使用该表,则必须使用与下面显示的名称完全相同的名称进行引用。这包括数字符号 (#)。 USE tempdb ; IF OBJECT_ID (N'#Bicycles',N'U') IS NOT NULL DROP TABLE #Bicycles ; GO USE AdventureWorks; GO SET NOCOUNT ON SELECT * INTO #Bicycles FROM Production.Product WHERE ProductNumber LIKE 'BK%' SET NOCOUNT OFF SELECT name FROM tempdb..sysobjects WHERE name LIKE '#Bicycles%' ; GO 5.3 使用相关子查询 以下示例显示了语义等价的查询并说明了使用 EXISTS 关键字和 IN 关键字的区别。两个都是有效子查询示例,用于检索产品型号为长袖标志运动衫且 ProductModelID 编号在 Product 和 ProductModel 两个表中相匹配的每种产品名称的实例。 USE AdventureWorks ; GO SELECT DISTINCT Name FROM Production.Product p WHERE EXISTS (SELECT * FROM Production.ProductModel pm WHERE p.ProductModelID = pm.ProductModelID AND pm.Name = 'Long-sleeve logo jersey') ; GO -- OR USE AdventureWorks ; GO SELECT DISTINCT Name FROM Production.Product WHERE ProductModelID IN (SELECT ProductModelID FROM Production.ProductModel WHERE Name = 'Long-sleeve logo jersey') ; GO 5.4 使用 GROUP BY 以下示例查找数据库中各销售订单的总额。 USE AdventureWorks ; GO SELECT SalesOrderID, SUM(LineTotal) AS SubTotal FROM Sales.SalesOrderDetail sod GROUP BY SalesOrderID ORDER BY SalesOrderID ; GO 由于使用了 GROUP BY 子句,因此针对每个销售订单只返回一行销售总额。 5.5 对多个组使用 GROUP BY 以下示例查找按产品 ID 和特价产品 ID 分组的平均价格和迄今为止的年销售总额。 Use AdventureWorks SELECT ProductID, SpecialOfferID, AVG(UnitPrice) AS 'Average Price', SUM(LineTotal) AS SubTotal FROM Sales.SalesOrderDetail GROUP BY ProductID, SpecialOfferID ORDER BY ProductID GO 5.6 使用 GROUP BY 和 WHERE 以下示例在只检索标价大于 $1000 的行后,将结果进行分组。 USE AdventureWorks; GO SELECT ProductModelID, AVG(ListPrice) AS 'Average List Price' FROM Production.Product WHERE ListPrice > $1000 GROUP BY ProductModelID ORDER BY ProductModelID ; GO 5.7 将 GROUP BY 与表达式一起使用 以下示例按表达式进行分组。如果表达式不包含聚合函数,则可以按表达式进行分组。 USE AdventureWorks ; GO SELECT AVG(OrderQty) AS 'Average Quantity', NonDiscountSales = (OrderQty * UnitPrice) FROM Sales.SalesOrderDetail sod GROUP BY (OrderQty * UnitPrice) ORDER BY (OrderQty * UnitPrice) DESC ; GO 6 日期和时间 Syntax for CAST: CAST ( expression AS data_type [ (length ) ]) Syntax for CONVERT: CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) 不带世纪数位 (yy) (1) 带世纪数位 (yyyy) 标准 输入/输出 (3) - 0 或 100 (1, 2) 默认设置 mon dd yyyy hh:miAM(或 PM) 1 101 美国 mm/dd/yyyy 2 102 ANSI yy.mm.dd 3 103 英国/法国 dd/mm/yy 4 104 德国 dd.mm.yy 5 105 意大利 dd-mm-yy 6 106 (1) - dd mon yy 7 107 (1) - mon dd, yy 8 108 - hh:mi:ss - 9 或 109 (1, 2) 默认设置 + 毫秒 mon dd yyyy hh:mi:ss:mmmAM(或 PM) 10 110 美国 mm-dd-yy 11 111 日本 yy/mm/dd 12 112 ISO yymmdd - 13 或 113 (1, 2) 欧洲默认设置 + 毫秒 dd mon yyyy hh:mi:ss:mmm(24h) 14 114 - hh:mi:ss:mmm(24h) - 20 或 120 (2) ODBC 规范 yyyy-mm-dd hh:mi:ss(24h) - 21 或 121 (2) ODBC 规范(带毫秒) yyyy-mm-dd hh:mi:ss.mmm(24h) - 126 (4) ISO8601 yyyy-mm-ddThh:mi:ss.mmm(无空格) 127(6, 7) 带时区 Z 的 ISO8601。 yyyy-mm-ddThh:mi:ss.mmmZ (无空格) - 130 (1, 2) 回历 (5) dd mon yyyy hh:mi:ss:mmmAM - 131 (2) 回历 (5) dd/mm/yy hh:mi:ss:mmmAM Select CONVERT(varchar(100), GETDATE(), 0): 05 16 2006 10:57AM Select CONVERT(varchar(100), GETDATE(), 1): 05/16/06 Select CONVERT(varchar(100), GETDATE(), 2): 06.05.16 Select CONVERT(varchar(100), GETDATE(), 3): 16/05/06 Select CONVERT(varchar(100), GETDATE(), 4): 16.05.06 Select CONVERT(varchar(100), GETDATE(), 5): 16-05-06 Select CONVERT(varchar(100), GETDATE(), 6): 16 05 06 Select CONVERT(varchar(100), GETDATE(), 7): 05 16, 06 Select CONVERT(varchar(100), GETDATE(), 8): 10:57:46 Select CONVERT(varchar(100), GETDATE(), 9): 05 16 2006 10:57:46:827AM Select CONVERT(varchar(100), GETDATE(), 10): 05-16-06 Select CONVERT(varchar(100), GETDATE(), 11): 06/05/16 Select CONVERT(varchar(100), GETDATE(), 12): 060516 Select CONVERT(varchar(100), GETDATE(), 13): 16 05 2006 10:57:46:937 Select CONVERT(varchar(100), GETDATE(), 14): 10:57:46:967 Select CONVERT(varchar(100), GETDATE(), 20): 2006-05-16 10:57:47 Select CONVERT(varchar(100), GETDATE(), 21): 2006-05-16 10:57:47.157 Select CONVERT(varchar(100), GETDATE(), 22): 05/16/06 10:57:47 AM Select CONVERT(varchar(100), GETDATE(), 23): 2006-05-16 Select CONVERT(varchar(100), GETDATE(), 24): 10:57:47 Select CONVERT(varchar(100), GETDATE(), 25): 2006-05-16 10:57:47.250 Select CONVERT(varchar(100), GETDATE(), 100): 05 16 2006 10:57AM Select CONVERT(varchar(100), GETDATE(), 101): 05/16/2006 Select CONVERT(varchar(100), GETDATE(), 102): 2006.05.16 Select CONVERT(varchar(100), GETDATE(), 103): 16/05/2006 Select CONVERT(varchar(100), GETDATE(), 104): 16.05.2006 Select CONVERT(varchar(100), GETDATE(), 105): 16-05-2006 Select CONVERT(varchar(100), GETDATE(), 106): 16 05 2006 Select CONVERT(varchar(100), GETDATE(), 107): 05 16, 2006 Select CONVERT(varchar(100), GETDATE(), 108): 10:57:49 Select CONVERT(varchar(100), GETDATE(), 109): 05 16 2006 10:57:49:437AM Select CONVERT(varchar(100), GETDATE(), 110): 05-16-2006 Select CONVERT(varchar(100), GETDATE(), 111): 2006/05/16 Select CONVERT(varchar(100), GETDATE(), 112): 20060516 Select CONVERT(varchar(100), GETDATE(), 113): 16 05 2006 10:57:49:513 Select CONVERT(varchar(100), GETDATE(), 114): 10:57:49:547 Select CONVERT(varchar(100), GETDATE(), 120): 2006-05-16 10:57:49 Select CONVERT(varchar(100), GETDATE(), 121): 2006-05-16 10:57:49.700 Select CONVERT(varchar(100), GETDATE(), 126): 2006-05-16T10:57:49.827 Select CONVERT(varchar(100), GETDATE(), 130): 18 ???? ?????? 1427 10:57:49:907AM Select CONVERT(varchar(100), GETDATE(), 131): 18/04/1427 10:57:49:920AM

2009-11-20

Graphics Programming with GDI+

Graphics Programming with GDI+    GDI+是新一代的图形接口。如果要设计.NET Framework图形应用程序,就必须使用GDI+。本书是一本为.NET开发人员讲授如何编写Windows和Web图形应用程序的专著,书中全面介绍了GDI+和Windows图形程序设计的基本知识和GDI+图形程序设计的各个方面。 本书适合于开发GDI+图形应用程序的初、中级程序员阅读,书中给出了大量用C#语言编写的可重用示例代码,可以使读者更快地掌握书中所介绍的各种知识和概念。本书也可以作为大专院校相关课程的重要辅导教材。 《GDI+图形程序设计)是为.NET开发人员介绍如何编写Windows和Web图形应用程序的指南用书。通过大量详尽的实例,本书使有经验的程序员可以更深入地理解在.NET Framework类库中定义的整个GDI+API。 本书从介绍GDI+和Windows图形程序设计的基本知识开始,其核心是对一些实际问题的指导,包括如何使用WindowsForms及如何优化GDI+的性能。本书通过一些例子来说明如何开发真实世界的工具,如GDI+Painter,GDI+Editor、ImageViewer和lnmgeAninmtor等。另外,作者还给出了大量使用C#语言编写的可重用示例代码,读者可从阔上下载完整的C#和VisualBasic.NET源代码,并可遣过这些源代码查看书中各图的彩色效果。 本书主要内容包括: ●比较GDI+与GDI ●GDI+在.NETFramework中的定义和使用 ●绘制和填充图形对象 ●查看和操作图像 ●图形对象,图像和颜色等的变形 ●.NET中的打印 ●开发GDI+Web应用程序 ●优化绘图质量和性能 ●交互式颜色混合和透明颜色 ●GDI瓦操作性 ●回答一些常见的GDI+问题

2009-09-17

界面技术-对话框的内容滚动显示.doc

Selecting the "Horizontal Scroll" and "Verticla Scroll" styles among the properties of your dialog box in the resource editor, you can add scroll bars to the dialog box. Remember also to select the 'resizing' border style. However for adding functionality to the scroll bars, you need to override the WM_VSCROLL and WM_HSCROLL message handlers. Also,override the WM_SIZE handler to set the scroll bar range if the size is reduced than the original. So you get the original size of the dialog in your OninitDialog(). The code would look something like this. Modify to your needs. 1. To OnInitDialog(),add the following line. GetWindowRect(m_rect); m_nScrollPos = 0; to get the original window size. Make m_rect a member variable of your dialog. Add another variable m_nScrollPos and initialize its value to zero. It stores the current vertical scroll position. 2. Here is the WM_SIZE handler for setting the scroll bar range.Set range 0 if size is increased more than original. void CCharlesDlg::OnSize(UINT nType, int cx, int cy) { CDialog::OnSize(nType, cx, cy); // TODO: Add your message handler code here m_nCurHeight = cy; int nScrollMax; if (cy < m_rect.Height()) { nScrollMax = m_rect.Height() - cy; } else nScrollMax = 0; SCROLLINFO si; si.cbSize = sizeof(SCROLLINFO); si.fMask = SIF_ALL;// SIF_ALL = SIF_PAGE | SIF_RANGE | SIF_POS; si.nMin = 0; si.nMax = nScrollMax; si.nPage = si.nMax/10; si.nPos = 0; SetScrollInfo(SB_VERT, &si, TRUE); } You need m_nCurHeight to store the current height of the dialog and use it to handle the scrolling in OnVScroll. m_ncurHeight is also a member variable of the dialog. 3. Here is the handler for WM_VSCROLL. void CCharlesDlg::OnVScroll(UINT nSBCode,UINT nPos,CScrollBar* pScrollBar) { //TODO:Add your message handler code here and/or call default int nDelta; int nMaxPos = m_rect.Height() - m_nCurHeight; switch(nSBCode) { case SB_LINEDOWN: if (m_nScrollPos >= nMaxPos) return; nDelta = min(nMaxPos/100,nMaxPos-m_nScrollPos); break; case SB_LINEUP: if (m_nScrollPos <= 0) return; nDelta = -min(nMaxPos/100,m_nScrollPos); break; case SB_PAGEDOWN: if (m_nScrollPos >= nMaxPos) return; nDelta = min(nMaxPos/10,nMaxPos-m_nScrollPos); break; case SB_THUMBPOSITION: nDelta = (int)nPos - m_nScrollPos; break; case SB_PAGEUP: if (m_nScrollPos <= 0) return; nDelta = -min(nMaxPos/10,m_nScrollPos); break; default: return; } m_nScrollPos += nDelta; SetScrollPos(SB_VERT,m_nScrollPos,TRUE); ScrollWindow(0,-nDelta); CDialog::OnVScroll(nSBCode, nPos, pScrollBar); } The above code handles the vertical scrolling. For horizontal scrolling add the WM_HSCROLL similarly and add the necessary code to OnSize and OnInitDialog. Information provided in this document and any software that may accompany this document is provided "as is" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The user assumes the entire risk as to the accuracy and the use of this information.

2009-08-27

Visual Assistant 10 设置图文教程

1 编辑快捷键 2 下划线设置 3 重构 4 显示上下文列表 5 导出设置 6 取消每日提示 7 高亮当前行

2009-07-11

Windows窗口样式-PDF

本文全面的概括了基本Windows控件的窗口样式,并以中文注释这些样式,方便开发时快速查看。 1 窗口样式 WS_POPUP 弹出式窗口(不能与WS_CHILDWINDOW样式同时使用) WS_CHILDWINDOW 子窗口(不能与WS_POPUP合用) WS_MINIMIZE 创建窗口拥有最小化按钮 WS_MINIMIZEBOX 创建窗口拥有最小化按钮,须同时指定WS_SYSTEM样式 WS_VISIBLE 可见状态 WS_DISABLED 不可用状态 WS_CLIPSIBLINGS 使窗口排除子窗口之间的相对区域 WS_CLIPCHILDREN 当在父窗口内绘图时,排除子窗口区域 WS_MAXIMIZE 具有最大化按钮 WS_MAXIMIZEBOX 创建窗口拥有最大化按钮,须同时指定WS_SYSTEM样式 WS_CAPTION 有标题框和边框(和WS_TILED样式相同) WS_BORDER 有单边框 WS_DLGFRAME 带对话框边框样式,不带标题框 WS_VSCROLL 有垂直滚动条 WS_HSCROLL 有水平滚动条 WS_SYSMENU 标题框上带有窗口菜单(须指定WS_CAPTION样式) WS_THICKFRAME 有可调边框(与WS_SIZEBOX样式相同) WS_TILED 与WS_OVERLAPPED风格相同 WS_TILEDWINDOW 与WWS_OVERLAPPEDWINDOW风格相同 WS_GROUP 组样式,每个组的第一个控件具有WS_TABSTOP样式 WS_TABSTOP 可接受TAB键 WS_OVERLAPPED 创建一个重叠式窗口,拥有标题栏和边框 WS_OVERLAPPEDWINDOW WS_OVERLAPPED风格 WS_CAPTION风格 WS_SYSMENU风格 WS_THICKFRAME风格 WS_MINIMIZEBOX风格 WS_MAXIMIZEBOX风格 2 窗口扩展样式参考列表

2009-07-02

Visio2000技术大全-PDF

本部分包含下列内容: • 第1章理解Visio 2000 • 第2章漫游Visio 2000工程环境 • 第3章熟悉Vi s i o工程 第一部分简单介绍Visio 2000。本部分介绍该软件的功能和最新特征,还介绍专业人员使 用Vi s i o进行的一些商业绘图任务,同时简要介绍了公司的历史和软件系列,以及漫游Vi s i o 2 0 0 0启动窗口功能。 第1章理解Visio 2000 本章包含下列内容: • 了解Vi s i o。 • 了解自己适合使用哪种Vi s i o程序。 • 理解Visio 2000的功能。 Vi s i o是一种绘图程序,特别适合于业务人员使用。然而,因为使用该软件创建图表和可 视化演示文稿非常方便,以至于各种人士都可用来进行自己的工作。毕竟,在许多场合中都 需要使用可视化处理,无论是打算美化住所环境还是在办公室中制作数据库结构模型,使用 Vi s i o绘图软件会让人们感觉既精确又容易。

2009-07-02

JSP高级编程05-PDF

JSP是一种如日中天的新型Internet/Intranet开发语言,可以在多种操作系统平台和多种Web服务器下使用。本书从最基础的JSP开发开始,循序渐进地介绍了JSP 开发技术,并涵盖了许多高级主题,如需要在企业级Web应用中使用的特性—Enterprise JavaBeans、JDBC 2.0、数据库连接池和自定义标签库。本书既适合初学者阅读,也适合具有一定JSP基础的开发人员深入研究使用。 前 言 JSP是SUN公司推出的一种新型的Internet/Intranet开发语言,和前一代Internet/Intranet开发语言(ASP、PHP)相比,JSP在以下几个方面有了重大的突破: 1) 通过JSP的扩展标签库和JavaBeans功能,网站逻辑和网站界面可以完美地分离。 2) 使用Enterprise JavaBeans,可以轻松地在JSP开发的Web中实现事务、安全、会话等等企业级应用所需要的功能。 3) JDBC2.0提供了不同的数据库产品无关的数据库连接方式,更重要的是,数据库连接池提供了一种比普通的数据库连接方式效率高得多的连接方式。 JSP的语法基本上和Java是相同的,有Java基础的读者可以很快学会如何使用JSP,而没有Java语言基础的读者,只要循序渐进地阅读本书,一样可以成为JSP编程的高手。本书主要分为两个部分:第一部分为JSP基础部分。通过这一部分的学习,读者可以掌握JSP的基本使用方法,学会如何使虽JSP来开发一般的中、小型Web应用。这一部分使用常见的Apache Group的Tomcat作为JSP引擎的例子。第二部分为JSP高级应用部分。这一部分主要讲述如何使用JSP进行大型Web应用的开发,为了方便读者学习,本书还专门讲述了SUN公司的J2SDKEE和B趴公司的Webloglc应用服务器的基本使用方法。 JSP可以在各种操作系统和各种Web服务器下使用,其代码基本上不需要任何改动就可以使用。本书为了适应大多数读者的情况,使用了Windows操作系统作为例子,具体的试验平台如下: Windows 2000Advanced Server Apachel.3.14 Intemetln允rmation Server 5.0 Tomcat 3.1 J2SDKEE l.2 BEA Wiblogic 5.1 除了上述平台,书中的代码还在如下平台进行了测试: RedhatUnux 6.1 Apache l.3.12 TOmcat 3.1 BEA Weblogic 4.51 数据库系统主要使用了Microsoft SQL Server 7.0,部分代码使用了MySQL。作者 2000.11

2009-07-01

JSP高级编程04-PDF

JSP是一种如日中天的新型Internet/Intranet开发语言,可以在多种操作系统平台和多种Web服务器下使用。本书从最基础的JSP开发开始,循序渐进地介绍了JSP 开发技术,并涵盖了许多高级主题,如需要在企业级Web应用中使用的特性—Enterprise JavaBeans、JDBC 2.0、数据库连接池和自定义标签库。本书既适合初学者阅读,也适合具有一定JSP基础的开发人员深入研究使用。 前 言 JSP是SUN公司推出的一种新型的Internet/Intranet开发语言,和前一代Internet/Intranet开发语言(ASP、PHP)相比,JSP在以下几个方面有了重大的突破: 1) 通过JSP的扩展标签库和JavaBeans功能,网站逻辑和网站界面可以完美地分离。 2) 使用Enterprise JavaBeans,可以轻松地在JSP开发的Web中实现事务、安全、会话等等企业级应用所需要的功能。 3) JDBC2.0提供了不同的数据库产品无关的数据库连接方式,更重要的是,数据库连接池提供了一种比普通的数据库连接方式效率高得多的连接方式。 JSP的语法基本上和Java是相同的,有Java基础的读者可以很快学会如何使用JSP,而没有Java语言基础的读者,只要循序渐进地阅读本书,一样可以成为JSP编程的高手。本书主要分为两个部分:第一部分为JSP基础部分。通过这一部分的学习,读者可以掌握JSP的基本使用方法,学会如何使虽JSP来开发一般的中、小型Web应用。这一部分使用常见的Apache Group的Tomcat作为JSP引擎的例子。第二部分为JSP高级应用部分。这一部分主要讲述如何使用JSP进行大型Web应用的开发,为了方便读者学习,本书还专门讲述了SUN公司的J2SDKEE和B趴公司的Webloglc应用服务器的基本使用方法。 JSP可以在各种操作系统和各种Web服务器下使用,其代码基本上不需要任何改动就可以使用。本书为了适应大多数读者的情况,使用了Windows操作系统作为例子,具体的试验平台如下: Windows 2000Advanced Server Apachel.3.14 Intemetln允rmation Server 5.0 Tomcat 3.1 J2SDKEE l.2 BEA Wiblogic 5.1 除了上述平台,书中的代码还在如下平台进行了测试: RedhatUnux 6.1 Apache l.3.12 TOmcat 3.1 BEA Weblogic 4.51 数据库系统主要使用了Microsoft SQL Server 7.0,部分代码使用了MySQL。作者 2000.11

2009-07-01

JSP高级编程03-PDF

JSP是一种如日中天的新型Internet/Intranet开发语言,可以在多种操作系统平台和多种Web服务器下使用。本书从最基础的JSP开发开始,循序渐进地介绍了JSP 开发技术,并涵盖了许多高级主题,如需要在企业级Web应用中使用的特性—Enterprise JavaBeans、JDBC 2.0、数据库连接池和自定义标签库。本书既适合初学者阅读,也适合具有一定JSP基础的开发人员深入研究使用。 前 言 JSP是SUN公司推出的一种新型的Internet/Intranet开发语言,和前一代Internet/Intranet开发语言(ASP、PHP)相比,JSP在以下几个方面有了重大的突破: 1) 通过JSP的扩展标签库和JavaBeans功能,网站逻辑和网站界面可以完美地分离。 2) 使用Enterprise JavaBeans,可以轻松地在JSP开发的Web中实现事务、安全、会话等等企业级应用所需要的功能。 3) JDBC2.0提供了不同的数据库产品无关的数据库连接方式,更重要的是,数据库连接池提供了一种比普通的数据库连接方式效率高得多的连接方式。 JSP的语法基本上和Java是相同的,有Java基础的读者可以很快学会如何使用JSP,而没有Java语言基础的读者,只要循序渐进地阅读本书,一样可以成为JSP编程的高手。本书主要分为两个部分:第一部分为JSP基础部分。通过这一部分的学习,读者可以掌握JSP的基本使用方法,学会如何使虽JSP来开发一般的中、小型Web应用。这一部分使用常见的Apache Group的Tomcat作为JSP引擎的例子。第二部分为JSP高级应用部分。这一部分主要讲述如何使用JSP进行大型Web应用的开发,为了方便读者学习,本书还专门讲述了SUN公司的J2SDKEE和B趴公司的Webloglc应用服务器的基本使用方法。 JSP可以在各种操作系统和各种Web服务器下使用,其代码基本上不需要任何改动就可以使用。本书为了适应大多数读者的情况,使用了Windows操作系统作为例子,具体的试验平台如下: Windows 2000Advanced Server Apachel.3.14 Intemetln允rmation Server 5.0 Tomcat 3.1 J2SDKEE l.2 BEA Wiblogic 5.1 除了上述平台,书中的代码还在如下平台进行了测试: RedhatUnux 6.1 Apache l.3.12 TOmcat 3.1 BEA Weblogic 4.51 数据库系统主要使用了Microsoft SQL Server 7.0,部分代码使用了MySQL。作者 2000.11

2009-07-01

JSP高级编程02-PDF

JSP是一种如日中天的新型Internet/Intranet开发语言,可以在多种操作系统平台和多种Web服务器下使用。本书从最基础的JSP开发开始,循序渐进地介绍了JSP 开发技术,并涵盖了许多高级主题,如需要在企业级Web应用中使用的特性—Enterprise JavaBeans、JDBC 2.0、数据库连接池和自定义标签库。本书既适合初学者阅读,也适合具有一定JSP基础的开发人员深入研究使用。 前 言 JSP是SUN公司推出的一种新型的Internet/Intranet开发语言,和前一代Internet/Intranet开发语言(ASP、PHP)相比,JSP在以下几个方面有了重大的突破: 1) 通过JSP的扩展标签库和JavaBeans功能,网站逻辑和网站界面可以完美地分离。 2) 使用Enterprise JavaBeans,可以轻松地在JSP开发的Web中实现事务、安全、会话等等企业级应用所需要的功能。 3) JDBC2.0提供了不同的数据库产品无关的数据库连接方式,更重要的是,数据库连接池提供了一种比普通的数据库连接方式效率高得多的连接方式。 JSP的语法基本上和Java是相同的,有Java基础的读者可以很快学会如何使用JSP,而没有Java语言基础的读者,只要循序渐进地阅读本书,一样可以成为JSP编程的高手。本书主要分为两个部分:第一部分为JSP基础部分。通过这一部分的学习,读者可以掌握JSP的基本使用方法,学会如何使虽JSP来开发一般的中、小型Web应用。这一部分使用常见的Apache Group的Tomcat作为JSP引擎的例子。第二部分为JSP高级应用部分。这一部分主要讲述如何使用JSP进行大型Web应用的开发,为了方便读者学习,本书还专门讲述了SUN公司的J2SDKEE和B趴公司的Webloglc应用服务器的基本使用方法。 JSP可以在各种操作系统和各种Web服务器下使用,其代码基本上不需要任何改动就可以使用。本书为了适应大多数读者的情况,使用了Windows操作系统作为例子,具体的试验平台如下: Windows 2000Advanced Server Apachel.3.14 Intemetln允rmation Server 5.0 Tomcat 3.1 J2SDKEE l.2 BEA Wiblogic 5.1 除了上述平台,书中的代码还在如下平台进行了测试: RedhatUnux 6.1 Apache l.3.12 TOmcat 3.1 BEA Weblogic 4.51 数据库系统主要使用了Microsoft SQL Server 7.0,部分代码使用了MySQL。作者 2000.11

2009-07-01

JSP高级编程01-PDF

JSP是一种如日中天的新型Internet/Intranet开发语言,可以在多种操作系统平台和多种Web服务器下使用。本书从最基础的JSP开发开始,循序渐进地介绍了JSP 开发技术,并涵盖了许多高级主题,如需要在企业级Web应用中使用的特性—Enterprise JavaBeans、JDBC 2.0、数据库连接池和自定义标签库。本书既适合初学者阅读,也适合具有一定JSP基础的开发人员深入研究使用。 前 言 JSP是SUN公司推出的一种新型的Internet/Intranet开发语言,和前一代Internet/Intranet开发语言(ASP、PHP)相比,JSP在以下几个方面有了重大的突破: 1) 通过JSP的扩展标签库和JavaBeans功能,网站逻辑和网站界面可以完美地分离。 2) 使用Enterprise JavaBeans,可以轻松地在JSP开发的Web中实现事务、安全、会话等等企业级应用所需要的功能。 3) JDBC2.0提供了不同的数据库产品无关的数据库连接方式,更重要的是,数据库连接池提供了一种比普通的数据库连接方式效率高得多的连接方式。 JSP的语法基本上和Java是相同的,有Java基础的读者可以很快学会如何使用JSP,而没有Java语言基础的读者,只要循序渐进地阅读本书,一样可以成为JSP编程的高手。本书主要分为两个部分:第一部分为JSP基础部分。通过这一部分的学习,读者可以掌握JSP的基本使用方法,学会如何使虽JSP来开发一般的中、小型Web应用。这一部分使用常见的Apache Group的Tomcat作为JSP引擎的例子。第二部分为JSP高级应用部分。这一部分主要讲述如何使用JSP进行大型Web应用的开发,为了方便读者学习,本书还专门讲述了SUN公司的J2SDKEE和B趴公司的Webloglc应用服务器的基本使用方法。 JSP可以在各种操作系统和各种Web服务器下使用,其代码基本上不需要任何改动就可以使用。本书为了适应大多数读者的情况,使用了Windows操作系统作为例子,具体的试验平台如下: Windows 2000Advanced Server Apachel.3.14 Intemetln允rmation Server 5.0 Tomcat 3.1 J2SDKEE l.2 BEA Wiblogic 5.1 除了上述平台,书中的代码还在如下平台进行了测试: RedhatUnux 6.1 Apache l.3.12 TOmcat 3.1 BEA Weblogic 4.51 数据库系统主要使用了Microsoft SQL Server 7.0,部分代码使用了MySQL。作者 2000.11

2009-07-01

CPPToolTip VC汽泡提示控件

CPPToolTip控件 链接:http://www.codeproject.com/KB/miscctrl/pptooltip.aspx 截图: 1 CPPToolTip控件介绍 Files Description PPTooltip.h PPTooltip.cpp CPPTooltip class PPHtmlDrawer.h PPHtmlDrawer.cpp CPPHtmlDrawer class. It's need to drawing HTML string in tooltip body PPDrawManager.h PPDrawManager.cpp CPPDrawManager class is a set of methods to work with graphics. CeXDib.h CeXDib.cpp CCeXDib class thanks to Davide Pizzolato and Davide Calabro. This class use for extend background's effect. Extend background effects by Davide Pizzolato and Davide Calabro become available if defined USE_SHADE: in PPDrawManager.h #define USE_SHADE 2 在普通窗体控件中使用 2.1 创建CPPToolTip对象 CPPToolTip m_tooltip; 2.2 在窗口初始化函数OnInitDialog中: // Create the CPPToolTip object m_tooltip.Create(this); 2.3 添加提示控件 m_tooltip.AddTool(GetDlgItem(IDC_BUTTON1), _T("Tooltip to the control IDC_BUTTON1")); 或者: m_tooltip.AddTool(this, _T("Tooltip for rectangle area"),CRect (100, 100, 200, 200)); 2.4 拦截处理鼠标消息 BOOL ... ::PreTranslateMessage(MSG* pMsg) { m_tooltip.RelayEvent(pMsg); } 3 在工具栏中使用 3.1 在CMainFrame中定义CPPToolTip对象 CPPToolTip m_tooltip; 3.2 在CMainFrame的OnCreate函数中创建CPPToolTip对象,添加工具栏提示 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { ... m_tooltip.Create(this); //Adds tooltip for toolbar m_tooltip.AddToolBar(&m_wndToolBar); return 0; } 3.3 截取和处理鼠标消息 BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) { m_tooltip.RelayEvent(pMsg); } 4 在菜单中使用 4.1 在CMainFrame中定义CPPToolTip变量 CPPToolTip m_tooltip; 4.2 在CMainFrame的OnCreate()函数中创建CPPToolTip对象 m_tooltip.Create(this); 4.3 Uncomments a line to enable a work with menus. in PPTooltip.h #define PPTOOLTIP_USE_MENU 4.4 为CMainFrame添加两个事件处理对象 //选中菜单事件 void CMainFrame::OnMenuSelect(UINT nItemID, UINT nFlags, HMENU hSubMenu) { m_tooltip.OnMenuSelect(nItemID, nFlags, hSubMenu); CFrameWnd::OnMenuSelect(nItemID, nFlags, hSubMenu); } //闲置状态事件 void CMainFrame::OnEnterIdle(UINT nWhy, CWnd* pWho) { m_tooltip.OnEnterIdle(nWhy, pWho); } 4.5 截取和处理鼠标消息 BOOL CMainFrame::PreTra

2009-06-19

VC之美化界面篇本文专题讨论VC中的界面美化,适用于具有中等VC水平的读者。读者最好具有以下VC基础:

VC之美化界面篇 作者:白乔 链接:http://vcer.net/1046595482643.html 本文专题讨论VC中的界面美化,适用于具有中等VC水平的读者。读者最好具有以下VC基础: 1. 大致了解MFC框架的基本运作原理; 2. 熟悉Windows消息机制,熟悉MFC的消息映射和反射机制; 3. 熟悉OOP理论和技术; 本文根据笔者多年的开发经验,并结合简单的例子一一展开,希望对读者有所帮助。 1 美化界面之开题篇 相信使用过《金山毒霸》、《瑞星杀毒》软件的读者应该还记得它们的精美界面: 图1 瑞星杀毒软件的精美界面 程序的功能如何如何强大是一回事,它的用户界面则是另一回事。千万不要忽视程序的用户界面,因为它是给用户最初最直接的印象,丑陋的界面、不友好的风格肯定会影响用户对软件程序的使用。 “受之以鱼,不若授之以渔”,本教程并不会向你推荐《瑞星杀毒软件》精美界面的具体实现,而只是向你推荐一些常用的美化方法。 2 美化界面之基础篇 美化界面需要先熟悉Windows下的绘图操作,并明白Windows的幕后绘图操作,才能有的放矢,知道哪些可以使用,知道哪些可以避免…… 2.1 Windows下的绘图操作 熟悉DOS的读者可能就知道:DOS下面的图形操作很方便,进入图形模式,整个屏幕就是你的了,你希望在哪画个点,那个地方就会出现一个点,红的、或者黄的,随你的便。你也可以花点时间画个按钮,画个你自己的菜单,等等…… Windows本身就是图形界面,所以Windows下面的绘图操作功能更丰富、简单。要了解Windows下的绘图操作,要实现Windows界面的美化,就必须了解MFC封装的设备环境类和图形对象类。 2.1.1 设备环境类 Windows下的绘图操作说到底就是DC操作。DC(Device Context设备环境)对象是一个抽象的作图环境,可能是对应屏幕,也可能是对应打印机或其它。这个环境是设备无关的,所以你在对不同的设备输出时只需要使用不同的设备环境就行了,而作图方式可以完全不变。这也就是Windows的设备无关性。 MFC的CDC类封装了Windows API 中大部分的画图函数。CDC的常见操作函数包括: Drawing-Attribute Functions:绘图属性操作,如:设置透明模式 Mapping Functions:映射操作 Coordinate Functions:坐标操作 Clipping Functions:剪切操作 Line-Output Functions:画线操作 Simple Drawing Functions:简单绘图操作,如:绘制矩形框 Ellipse and Polygon Functions:椭圆/多边形操作 Text Functions:文字输出操作 Printer Escape Functions:打印操作 Scrolling Functions:滚动操作 *Bitmap Functions:位图操作 *Region Functions:区域操作 *Font Functions:字体操作 *Color and Color Palette Functions:颜色/调色板操作 其中,标注*项会用到相应的图形对象类,参见2.1.2内容。 2.1.2 图形对象类 设备环境不足以包含绘图功能所需的所有绘图特征,除了设备环境外, Windows还有其他一些图形对象用来储存绘图特征。这些附加的功能包括从画线的宽度和颜色到画文本时所用的字体。图形对象类封装了所有六个图形对象。 下面的表格列出了MFC的图形对象类: MFC类 图形对象句柄 图形对象目的 CBitmap HBITMAP 内存中的位图 CBrush HBRUSH 画刷特性—填充某个图形时所使用的颜色和模式 CFont HFONT 字体特性—写文本时所使用的字体 CPalette HPALETTE 调色板颜色 CPen HPEN 画笔特性—画轮廓时所使用的线的粗细 CRgn HRGN 区域特性—包括定义它的点 表1 图形对象类和它们封装的句柄 使用CDC和图形对象类,在Windows里绘图还算是很简单的。观察以下的画面: 图2 使用CDC绘制出的按钮 该画面通过以下代码自行绘制的假按钮: BOOL CUi1View::PreCreateWindow(CREATESTRUCT& cs) { //设置背景色 //CBrush CUi1View::m_Back m_Back.CreateSolidBrush(::GetSysColor(COLOR_3DFACE)); cs.lpszClass = AfxRegisterWndClass(0, 0, m_Back, NULL); return CView::PreCreateWindow(cs); } int CUi1View::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; //创建字体 //CFont CUi1View::m_Font m_Font.CreatePointFont(120, "Impact"); return 0; } void CUi1View::OnDraw(CDC* pDC) { //绘制按钮框架 pDC->DrawFrameControl(CRect(100, 100, 220, 160), DFC_BUTTON, DFCS_BUTTONPUSH); //输出文字 pDC->SetBkMode(TRANSPARENT); pDC->TextOut(120, 120, "Hello, CFan!"); } 呵呵,不好意思,这并不是真的Windows按钮,它只是一个假的空框子,当用户在按钮上点击鼠标时,放心,什么事情都不会发生。 2.2 Windows的幕后绘图操作 在Window中,如果所有的界面操作都由用户代码来实现,那将是一个很浩大的工程。笔者曾经在DOS设计过窗口图形界面,代码上千行,但实现的界面还是很古板、难看,除了我那个对编程一窍不通的女友,没有一个人欣赏它L;而且,更要命的是,操作系统,包括别的应用程序并不认识你的界面元素,这才是真正悲哀的。认识这些界面的只有你的程序,图2中的按钮永远只是一个无用的框子。 有了Windows,一切都好办了,Windows将诸如按钮、菜单、工具栏等等这些通用界面的绘制及动作都交给了系统,程序员就不用花心思再画那些按钮了,可以将更多的精力放在程序的功能实现方面。 所有的标准界面元素都被Windows封装好了。Windows知道怎么画你的菜单以及你的标注着“Hello, Cfan!”的按钮。当CFan某个快乐的小编(譬如:小飞)点击这个按钮的时候,Windows也明白按钮按下去的时候该有的模样,甚至,当这个友好的按钮获取焦点时,Windows也会不失时机地为它准备一个虚框…… 有利必有弊。你的不满这时候产生了:你既想使用Windows的True Button,可也嫌它的界面不够好看,譬如,你喜欢用蓝色的粗体表达你对CFan的无限情怀(正如图2那样)——人心不足,有办法吗?有的。 3 美化界面之实现篇 Windows还是给程序员留下了很多后门,通过一些途径还是可以美化界面的。本章节我们系统学习一下Windows界面美化的实现。 3.1 美化界面的途径 如何以合法的手段来达到美化界面的效果?一般美化界面的方法包括: 1. 使用MFC类的既有函数,设定界面属性; 2. 利用Windows的消息机制,截获有用的Windows的消息。通过MFC的消息映射(Message Mapping)和反射(Message Reflecting)机制,在Windows准备或者正在绘制该元素时,偷偷修改它的状态和行为,譬如:让按钮的边框为红色; 3. 利用MFC类的虚函数机制,重载有用的虚函数。在MFC框架调用该函数的时候,重新定义它的状态和行为; 一般来说,应用程序可以通过以下两种途径来实现以上的方法: 1. 在父窗口里,截获自身的或者由子元素(包括控件和菜单等元素)传递的关于界面绘制的消息; 2. 子类化子元素,或者为子元素准备一个新的类(一般来说该类必须继承于MFC封装的某个标准类,如:CButton)。在该子元素里,截获自身的或者从父窗口反射过来的关于界面绘制的消息。譬如:用户可以创建一个CXPButton类来实现具有XP风格的按钮,CXPButton继承于CButton。 对于应用程序,使用CXPButton类的途径相对于对话框窗口和普通窗口分成两种: ① 对话框窗口中,直接将原先绑定按钮的CButton类替换成CXPButton类,或者在绑定变量时直接指定Control类型为CXPButton,如图3所示: 图3 为按钮指定CXPButton类型 ②在普通窗口中,直接创建一个CXPButton类对象,然后在OnCreate()中调用CXPButton的Create方法; 以下的章节将综合地使用以上的方法,请读者朋友留心观察。 3.2 使用MFC类的既有函数 在界面美化的专题中,MFC也并非一无是处。MFC类对于界面美化也做了部分的努力,以下是一些可以使用的,参数说明略去。 CWinApp::SetDialogBkColor void SetDialogBkColor( COLORREF clrCtlBk = RGB(192, 192, 192), COLORREF clrCtlText = RGB(0, 0, 0) ); 指定对话框的背景色和文本颜色。 CListCtrl::SetBkColor CReBarCtrl::SetBkColor CStatusBarCtrl::SetBkColor CTreeCtrl::SetBkColor COLORREF SetBkColor( COLORREF clr ); 设定背景色。 CListCtrl::SetTextColor CReBarCtrl::SetTextColor CTreeCtrl::SetTextColor COLORREF SetTextColor( COLORREF clr ); 设定文本颜色。 CListCtrl::SetBkImage BOOL SetBkImage( LVBKIMAGE* plvbkImage ); BOOL SetBkImage( HBITMAP hbm, BOOL fTile = TRUE, int xOffsetPercent = 0, int yOffsetPercent = 0); BOOL SetBkImage( LPTSTR pszUrl, BOOL fTile = TRUE, int xOffsetPercent = 0, int yOffsetPercent = 0 ); 设定列表控件的背景图片。 CComboBoxEx::SetExtendedStyle CListCtrl::SetExtendedStyle CTabCtrl::SetExtendedStyle CToolBarCtrl::SetExtendedStyle DWORD SetExtendedStyle( DWORD dwExMask, DWORD dwExStyles ); 设置控件的扩展属性,例如:设置列表控件属性带有表格线。 图4是个简单应用MFC类的既有函数来改善Windows界面的例子: 图4 使用MFC类的既有函数美化界面 相关实现代码如下: BOOL CUi2App::InitInstance() { //… //设置对话框背景色和字体颜色 SetDialogBkColor(RGB(128, 192, 255), RGB(0, 0, 255)); //… } BOOL CUi2Dlg::OnInitDialog() { //… //设置列表控件属性带有表格线 DWORD NewStyle = m_List.GetExtendedStyle(); NewStyle |= LVS_EX_GRIDLINES; m_List.SetExtendedStyle(NewStyle); //设置列表控件字体颜色为红色 m_List.SetTextColor(RGB(255, 0, 0)); //填充数据 m_List.InsertColumn(0, "QQ", LVCFMT_LEFT, 100); m_List.InsertColumn(1, "昵称", LVCFMT_LEFT, 100); m_List.InsertItem(0, "5854165"); m_List.SetItemText(0, 1, "白乔"); m_List.InsertItem(1, "6823864"); m_List.SetItemText(1, 1, "Satan"); //… } 嗯,这样的界面还算不错吧? 3.3 使用Windows的消息机制 使用MFC类的既有函数来美化界面,其功能是有限的。既然Windows是通过消息机制进行通讯的,那么我们就可以通过截获一些有用的消息来美化我们的界面,以下是一些有用的Windows消息: WM_PAINT WM_ERASEBKGND WM_CTLCOLOR* WM_DRAWITEM* WM_MEASUREITEM* NM_CUSTOMDRAW* 注意,标注*的消息是子元素发送给父窗口的通知消息,其它的为窗口或者子元素自身的消息。 3.3.1 WM_PAINT WM_PAINT消息相信大家都很熟悉,一个窗口要重绘了,就会有一个WM_PAINT消息发送给窗口。 可以响应窗口的WM_PAINT,以更改它们的模样。WM_PAINT的映射函数原型如下: afx_msg void OnPaint(); 控件也是窗口,所以控件也有WM_PAINT消息,通过消息映射我们完全可以定义控件的界面。如图5所示: 图5 利用WM_ PAINT消息美化界面 实现代码也很简单: void CLazyStatic::OnPaint() { CPaintDC dc(this); // device context for painting //什么都不输出,仅仅画一个矩形框 CRect rc; GetClientRect(&rc); dc.Rectangle(rc); } 哈哈,简单吧?不过WM_PAINT确实绝了点,它要求应用程序完成元素界面的所有绘制过程,想象一下如何画出一个完整的列表控件?太烦了吧。一般来说,很少有人喜欢使用WM_PAINT,还有其它更细致的消息。 3.3.2 WM_ERASEBKGND Windows在向窗口发送WM_PAINT消息之前,总会发送一个WM_ERASEBKGND消息通知该窗口擦除背景,默认情况下,Windows将以窗口的背景色清除该窗口。 可以响应窗口(包括子元素)的WM_ERASEBKGND,以更改它们的背景。WM_ERASEBKGND的映射函数原型如下: afx_msg BOOL OnEraseBkgnd( CDC* pDC ); 返回值: 指定背景是否已清除,如果为FALSE,系统将自动清除 参数: pDC指定了绘制操作所使用的设备环境。 图6是个简单的例子,通过OnEraseBkgnd为对话框加载了一副位图背景: 图6 利用WM_ ERASEBKGND消息美化界面 实现代码也很简单: BOOL CUi4Dlg::OnInitDialog() { //… //加载位图 //CBitmap m_Back; m_Back.LoadBitmap(IDB_BACK); //… } BOOL CUi4Dlg::OnEraseBkgnd(CDC* pDC) { CDC dc; dc.CreateCompatibleDC(pDC); dc.SelectObject(&m_Back); //获取BITMAP对象 BITMAP hb; m_Back.GetBitmap(&hb); //获取窗口大小 CRect rt; GetClientRect(&rt); //显示位图 pDC->StretchBlt(0, 0, rt.Width(), rt.Height(), &dc, 0, 0, hb.bmWidth, hb.bmHeight, SRCCOPY); return TRUE; } HBRUSH CUi4Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { //设置透明背景模式 pDC->SetBkMode(TRANSPARENT); //设置背景刷子为空 return (HBRUSH)::GetStockObject(HOLLOW_BRUSH); } 同时别忘了响应OnCtlColor,否则窗口里面的控件就不透明了。OnCtlColor的内容。 3.3.3 WM_CTLCOLOR 在控件显示之前,每一个控件都会向父对话框发送一个WM_CTLCOLOR消息要求获取绘制所需要的颜色。WM_CTLCOLOR消息缺省处理函数CWnd::OnCtlColor返回一个HBRUSH类型的句柄,这样,就可以设置前景和背景文本颜色,并为控件或者对话框的非文本区域选定一个刷子。 WM_CTLCOLOR的映射函数原型如下: afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor ); 返回值: 用以指定背景的刷子 参数: pDC指定了绘制操作所使用的设备环境。 pWnd 控件指针 nCtlColor 指定控件类型,其取值如表2所示: 类型值 含义 CTLCOLOR_BTN 按钮控件 CTLCOLOR_DLG 对话框 CTLCOLOR_EDIT 编辑控件 CTLCOLOR_LISTBOX 列表框 CTLCOLOR_MSGBOX 消息框 CTLCOLOR_SCROLLBAR 滚动条 CTLCOLOR_STATIC 静态控件 表2 nCtlColor的类型值与含义 作为一个简单的例子,观察以下的代码: BOOL CUi5Dlg::OnInitDialog() { //… //创建字体 //CFont CUi1View::m_Font1, CUi1View::m_Font2 m_Font1.CreatePointFont(120, "Impact"); m_Font3.CreatePointFont(120, "Arial"); return TRUE; // return TRUE unless you set the focus to a control } HBRUSH CUi5Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); if(nCtlColor == CTLCOLOR_STATIC) { //区分静态控件 switch(pWnd->GetDlgCtrlID()) { case IDC_STATIC1: { pDC->SelectObject(&m_Font1); pDC->SetTextColor(RGB(0, 0, 255)); break; } case IDC_STATIC2: { pDC->SelectObject(&m_Font2); pDC->SetTextColor(RGB(255, 0, 0)); break; } } } return hbr; } 生成的界面如下: 图7 利用WM_CTLCOLOR消息美化界面 3.3.4 WM_DRAWITEM OnCtlColor只能修改元素的颜色,但不能修改元素的界面框架,WM_DRAWITEM则可以。 当一个具有Owner draw风格的元素(包括按钮、组合框、列表框和菜单等)需要显示外观时,该元素会发送一条WM_DRAWITEM消息至它的隶属窗口(Owner)。 WM_DRAWITEM的映射函数原型如下: afx_msg void OnDrawItem( int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct ); 参数: nIDCtl 该控件的ID,如果该元素为菜单,则nIDCtl为0 lpDrawItemStruct 指向DRAWITEMSTRUCT结构对象的指针,DRAWITEMSTRUCT的结构定义如下: typedef struct tagDRAWITEMSTRUCT { UINT CtlType; UINT CtlID; UINT itemID; UINT itemAction; UINT itemState; HWND hwndItem; HDC hDC; RECT rcItem; DWORD itemData; }DRAWITEMSTRUCT; CtlType指定了控件的类型,其取值如表3所示: 类型值 含义 ODT_BUTTON 按钮控件 ODT_COMBOBOX 组合框控件 ODT_LISTBOX 列表框控件 ODT_LISTVIEW 列表视图 ODT_MENU 菜单项 ODT_STATIC 静态文本控件 ODT_TAB Tab控件 表3 CtlType的类型值与含义 CtlID 指定自绘控件的ID值,该成员不适用于菜单项 itemID表示菜单项ID,也可以表示列表框或者组合框中某项的索引值。对于一个空的列表框或组合框,该成员的值为?C1。这时应用程序只绘制焦点矩形(该矩形的坐标由rcItem 成员给出)虽然此时控件中没有需要显示的项,但是绘制焦点矩形还是很有必要的,因为这样做能够提示用户该控件是否具有输入焦点。当然也可以设置itemAction 成员为合适值,使得无需绘制焦点。 itemAction 指定绘制行为,其取值为表4中所示值的一个或者多个的联合: 类型值 含义 ODA_DRAWENTIRE 当整个控件都需要被绘制时,设置该值。 ODA_FOCUS 如果控件需要在获得或失去焦点时被绘制,则设置该值。此时应该检查itemState成员,以确定控件是否具有输入焦点。 ODA_SELECT 如果控件需要在选中状态改变时被绘制,则设置该值。此时应该检查itemState 成员,以确定控件是否处于选中状态。 表4 itemAction的类型值与含义 itemState 指定了当前绘制项的状态。例如,如果菜单项应该被灰色显示,则可以指定ODS_GRAYED状态标志。其取值为表5中所示值的一个或者多个的联合: 类型值 含义 ODS_CHECKED 标记状态,仅适用于菜单项。 ODS_DEFAULT 默认状态。 ODS_DISABLED 禁止状态。 ODS_FOCUS 焦点状态。 ODS_GRAYED 灰化状态,仅适用于菜单项。 ODS_SELECTED 选中状态。 ODS_HOTLIGHT 仅适用于Windows 98/Me/Windows 2000/XP,热点状态:如果鼠标指针位于控件之上,则设置该值,这时控件会显示高亮颜色。 ODS_INACTIVE 仅适用于Windows 98/Me/Windows 2000/XP,非激活状态。 ODS_NOACCEL 仅适用于Windows 2000/XP,控件是否有快速键。 ODS_COMBOBOXEDIT 在自绘组合框控件中只绘制选择区域。 ODS_NOFOCUSRECT 仅适用于Windows 2000/XP,不绘制捕获焦点的效果。 表5 itemState的类型值与含义 hwndItem 指定了组合框、列表框和按钮等自绘控件的窗口句柄;如果自绘的对象为菜单项,则表示包含该菜单项的菜单句柄。 hDC 指定了绘制操作所使用的设备环境。 rcItem 指定了将被绘制的矩形区域。这个矩形区域就是上面hDC的作用范围。系统会自动裁剪组合框、列表框或按钮等控件的自绘制区域以外的部分。也就是说rcItem中的坐标点(0,0)指的就是控件的左上角。但是系统不裁剪菜单项,所以在绘制菜单项的时候,必须先通过一定的换算得到该菜单项的位置,以保证绘制操作在我们希望的区域中进行。 itemData 对于菜单项,该成员的取值为由CMenu::AppendMenu、CMenu::InsertMenu、CMenu::ModifyMenu等函数传递给菜单的值。 对于列表框或这组合框,该成员的取值为由ComboBox::AddString、CComboBox::InsertString、CListBox::AddString或者CListBox::InsertString等函数传递给控件的值。 如果ctlType 的取值是ODT_BUTTON或者ODT_STATIC,itemData的取值为0。 图5是个相应的例子,它修改了按钮的界面: 图8 利用WM_DRAWITEM消息美化界面 实现代码如下: BOOL CUi6Dlg::OnInitDialog() { //… //创建字体 //CFont CUi1View::m_Font m_Font.CreatePointFont(120, "Impact"); //… } void CUi6Dlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) { if(nIDCtl == IDC_HELLO_CFAN) { //绘制按钮框架 UINT uStyle = DFCS_BUTTONPUSH; //是否按下去了? if (lpDrawItemStruct->itemState & ODS_SELECTED) uStyle |= DFCS_PUSHED; CDC dc; dc.Attach(lpDrawItemStruct->hDC); dc.DrawFrameControl(&lpDrawItemStruct->rcItem, DFC_BUTTON, uStyle); //输出文字 dc.SelectObject(&m_Font); dc.SetTextColor(RGB(0, 0, 255)); dc.SetBkMode(TRANSPARENT); CString sText; m_HelloCFan.GetWindowText(sText); dc.TextOut(lpDrawItemStruct->rcItem.left + 20, lpDrawItemStruct->rcItem.top + 20, sText); //是否得到焦点 if(lpDrawItemStruct->itemState & ODS_FOCUS) { //画虚框 CRect rtFocus = lpDrawItemStruct->rcItem; rtFocus.DeflateRect(3, 3); dc.DrawFocusRect(&rtFocus); } return; } CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct); } 别忘了标记Owner draw属性: 图9 指定按钮的Owner draw属性 值得一提的是,CWnd内部截获了WM_DRAWITEM、WM_MEASUREITEM等消息,并映射成子元素的相应虚函数的调用,如CButton::DrawItem()。所以,以上例子也可以通过派生出一个CButton的派生类,并重载该类的DrawItem()函数来实现。使用虚函数机制实现界面美化参见3.4章节。 3.3.5 WM_MEASUREITEM 仅仅WM_DRAWITEM还是不够的,对于一些特殊的控件,如ListBox,系统在发送WM_DRAWITEM消息前,还发送WM_MEASUREITEM消息,需要你设置ListBox中每个项目的高度。 WM_DRAWITEM的映射函数原型如下: afx_msg void OnMeasureItem( int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct ); nIDCtl 该控件的ID,如果该元素为菜单,则nIDCtl为0 lpMeasureItemStruct指向MEASUREITEMSTRUCT结构对象的指针,MEASUREITEMSTRUCT的结构定义如下: typedef struct tagMEASUREITEMSTRUCT { UINT CtlType; UINT CtlID; UINT itemID; UINT itemWidth; UINT itemHeight; DWORD itemData } MEASUREITEMSTRUCT; CtlType指定了控件的类型,其取值如表6所示: 类型值 含义 ODT_COMBOBOX 组合框控件 ODT_LISTBOX 列表框控件 ODT_MENU 菜单项 表6 CtlType的类型值与含义 CtlID 指定自绘控件的ID值,该成员不适用于菜单项 itemID表示菜单项ID,也可以表示可变高度的列表框或组合框中某项的索引值。该成员不适用于固定高度的列表框或组合框。 itemWidth 指定菜单项的宽度 itemHeight指定菜单项或者列表框中某项的的高度,最大值为255 itemData 对于菜单项,该成员的取值为由CMenu::AppendMenu、CMenu::InsertMenu、CMenu::ModifyMenu等函数传递给菜单的值。 对于列表框或这组合框,该成员的取值为由ComboBox::AddString、CComboBox::InsertString、CListBox::AddString或者CListBox::InsertString等函数传递给控件的值。 图示出了OnMeasureItem的效果: 图10 利用WM_MEASUREITEM消息美化界面 相应的OnMeasureItem()实现如下: void CUi7Dlg::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct) { if(nIDCtl == IDC_COLOR_PICKER) { //设定高度为 lpMeasureItemStruct->itemHeight = 30; return; } CDialog::OnMeasureItem(nIDCtl, lpMeasureItemStruct); } 同样别忘了指定列表框的Owner draw属性: 图11 指定下拉框的Owner draw属性 3.3.6 NM_CUSTOMDRAW 大家也许熟悉WM_NOTIFY,控件通过WM_NOTIFY向父窗口发送消息。在WM_NOTIFY消息体中,部分控件会发送NM_CUSTOMDRAW告诉父窗口自己需要绘图。 可以反射NM_CUSTOMDRAW消息,如: ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw) afx_msg void OnCustomDraw(NMHDR *pNMHDR, LRESULT *pResult); 参数: pNMHDR 说到底只是一个指针,大多数情况下它指向一个NMHDR结构对象,NMHDR结构如下: typedef struct tagNMHDR { HWND hwndFrom; UINT idFrom; UINT code; } NMHDR; 其中: hwndFrom 发送方控件的窗口句柄 idFrom 发送方控件的ID code 通知代码 对于某些控件来说,pNMHDR则会解释成其它内容更丰富的结构对象的指针,如:对于列表控件来说,pNMHDR常常指向一个NMCUSTOMDRAW对象,NMCUSTOMDRAW结构如下: typedef struct tagNMCUSTOMDRAWINFO { NMHDR hdr; DWORD dwDrawStage; HDC hdc; RECT rc; DWORD dwItemSpec; UINT uItemState; LPARAM lItemlParam; } NMCUSTOMDRAW, FAR * LPNMCUSTOMDRAW; hdr NMHDR对象 dwDrawStage 当前绘制状态,其取值如表7所示: 类型值 含义 CDDS_POSTERASE 擦除循环结束 CDDS_POSTPAINT 绘制循环结束 CDDS_PREERASE 准备开始擦除循环 CDDS_PREPAINT 准备开始绘制循环 CDDS_ITEM 指定dwItemSpec, uItemState, lItemlParam参数有效 CDDS_ITEMPOSTERASE 列表项擦除结束 CDDS_ITEMPOSTPAINT 列表项绘制结束 CDDS_ITEMPREERASE 准备开始列表项擦除 CDDS_ITEMPREPAINT 准备开始列表项绘制 CDDS_SUBITEM 指定列表子项 表7 dwDrawStage的类型值与含义 hdc指定了绘制操作所使用的设备环境。 rc指定了将被绘制的矩形区域。 dwItemSpec 列表项的索引 uItemState 当前列表项的状态,其取值如表8所示: 类型值 含义 CDIS_CHECKED 标记状态。 CDIS_DEFAULT 默认状态。 CDIS_DISABLED 禁止状态。 CDIS_FOCUS 焦点状态。 CDIS_GRAYED 灰化状态。 CDIS_SELECTED 选中状态。 CDIS_HOTLIGHT 热点状态。 CDIS_INDETERMINATE 不定状态。 CDIS_MARKED 标注状态。 表8 uItemState的类型值与含义 lItemlParam 当前列表项的绑定数据 pResult 指向状态值的指针,指定系统后续操作,依赖于dwDrawStage: 当dwDrawStage为CDDS_PREPAINT,pResult含义如表9所示: 类型值 含义 CDRF_DODEFAULT 默认操作,即系统在列表项绘制循环过程不再发送NM_CUSTOMDRAW。 CDRF_NOTIFYITEMDRAW 指定列表项绘制前后发送消息。 CDRF_NOTIFYPOSTERASE 列表项擦除结束时发送消息。 CDRF_NOTIFYPOSTPAINT 列表项绘制结束时发送消息。 表9 pResult的类型值与含义(一) 当dwDrawStage为CDDS_ITEMPREPAINT,pResult含义如表10所示: 类型值 含义 CDRF_NEWFONT 指定后续操作采用应用中指定的新字体。 CDRF_NOTIFYSUBITEMDRAW 列表子项绘制时发送消息。 CDRF_SKIPDEFAULT 系统不必再绘制该子项。 表10 pResult的类型值与含义(二) 以下是一个利用NM_CUSTOMDRAW消息绘制出的多色列表框的例子: 图12 利用NM_CUSTOMDRAW消息美化界面 对应代码如下: void CCoolList::OnCustomDraw(NMHDR *pNMHDR, LRESULT *pResult) { //类型安全转换 NMLVCUSTOMDRAW* pLVCD = reinterpret_cast(pNMHDR); *pResult = 0; //指定列表项绘制前后发送消息 if(CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage) { *pResult = CDRF_NOTIFYITEMDRAW; } else if(CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage) { //奇数行 if(pLVCD->nmcd.dwItemSpec % 2) pLVCD->clrTextBk = RGB(255, 255, 128); //偶数行 else pLVCD->clrTextBk = RGB(128, 255, 255); //继续 *pResult = CDRF_DODEFAULT; } } 注意到上例采取了3.1所推荐的第2种实现方法,派生了一个新类CCoolList。 3.4 使用MFC类的虚函数机制 修改Windows界面,除了从Windows消息机制下功夫,也可以从MFC类下功夫,这应该得益于类的虚函数机制。为了防止诸如“面向对象技术”等术语在此泛滥,以下仅举一段代码作为例子: void CView::OnPaint() { // standard paint routine CPaintDC dc(this); OnPrepareDC(&dc); OnDraw(&dc); } 这是MFC中viewcore.cpp中的源代码,很多读者总不明白OnDraw()和OnPaint()之间的关系,从以上的代码中很容易看出,CView的WM_PAINT消息响应函数OnPaint()会自动调用CView::OnDraw()。而作为开发者的用户,可以通过简单的OnDraw()的重载实现对WM_PAINT的处理。所以说,对MFC类的虚函数的重载是对消息机制的扩展。 以下列出了与界面美化相关的虚函数,参数说明略去: CButton::DrawItem CCheckListBox::DrawItem CComboBox::DrawItem CHeaderCtrl::DrawItem CListBox::DrawItem CMenu::DrawItem CStatusBar::DrawItem CStatusBarCtrl::DrawItem CTabCtrl::DrawItem virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct ); Owner draw元素自绘函数 很显然,位图菜单都是通过这个DrawItem画出来的。限于篇幅,在此不再附以例程。

2009-06-17

Install Shield安装程序制作图解

本文介绍了利用Install Shield制作应用程序安装软件的方法。在文中作者除了对常用的一些技术进行介绍外,还对安装过程位图的显示、标题和背景的定制等高级技术作了简要的阐述,本文所述方法能够满足大多数安装软件的制作需求。 InstallShield是一种非常成功的应用软件安装程序制作工具,以其功能强大、灵活性好、容易扩展和强大的网络支持而著称,并因此成为目前最为流行的安装程序专业制作工具之一。该软件不仅提供了灵活方便的向导支持,也允许用户通过其内建的脚本语言InstallScript来对整个安装过程在代码级上进行修改,可以象VC等高级语言一样对安装过程进行精确控制。InstallShield也是Visual C++附带的一个安装程序制作工具,在VC安装结束前将会询问用户是否安装Install Shiled工具,如果当时没有安装,也可以在使用时单独从VC安装盘进行安装。本文将结合一个具体的实例来对InstallShield的使用做一个较为全面的介绍,使读者能够初步掌握使用InstallShield制作专业水准的安装程序。 运行InstallShield将会出现如图1所示的程序界面,与使用其他高级语言开发程序一样,首先要建立一个工程。

2009-06-02

最新名企标准通用C++面试题,

C++面试题 参考:http://blog.csdn.net/Ghost90/archive/2009/04/22/4099672.aspx 整理:松鼠 时间:2009-5-8 1、const 有什么用途?(请至少说明两种) 答: (1)可以定义 const 常量 (2)const可以修饰函数的参数、返回值,甚至函数的定义体。被const修饰的东西都受到强制保护,可以预防意外的变动,能提高程序的健壮性。 2、在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern “C”? 答:C++语言支持函数重载,C语言不支持函数重载。函数被C++编译后在库中的名字与C语言的不同。假设某个函数的原型为: void foo(int x, int y); 该函数被C编译器编译后在库中的名字为_foo,而C++编译器则会产生像_foo_int_int之类的名字。 C++提供了C连接交换指定符号extern“C”来解决名字匹配问题。 3、请简述以下两个for循环的优缺点(5分) for (i=0; i<N; i++) { if (condition) DoSomething(); else DoOtherthing(); } if (condition) { for (i=0; i<N; i++) DoSomething(); } else { for (i=0; i<N; i++) DoOtherthing(); } 优点:程序简洁 缺点:多执行了N-1次逻辑判断,并且打断了循环“流水线”作业,使得编译器不能对循环进行优化处理,降低了效率。 优点:循环的效率高 缺点:程序不简洁 4、有关内存的思考题 void GetMemory(char *p) { p = (char *)malloc(100); } void Test(void) { char *str = NULL; GetMemory(str); strcpy(str, "hello world"); printf(str); } 请问运行Test函数会有什么样的结果? 答:程序崩溃。 因为GetMemory并不能传递动态内存, Test函数中的 str一直都是 NULL。 strcpy(str, "hello world");将使程序崩溃。 char *GetMemory(void) { char p[] = "hello world"; return p; } void Test(void) { char *str = NULL; str = GetMemory(); printf(str); } 请问运行Test函数会有什么样的结果? 答:可能是乱码。 因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是 NULL,但其原现的内容已经被清除,新内容不可知。 void GetMemory2(char **p, int num) { *p = (char *)malloc(num); } void Test(void) { char *str = NULL; GetMemory(&str, 100); strcpy(str, "hello"); printf(str); } 请问运行Test函数会有什么样的结果? 答: (1)能够输出hello (2)内存泄漏 void Test(void) { char *str = (char *) malloc(100); strcpy(str, “hello”); free(str); if(str != NULL) { strcpy(str, “world”); printf(str); } } 请问运行Test函数会有什么样的结果? 答:篡改动态内存区的内容,后果难以预料,非常危险。 因为free(str);之后,str成为野指针, if(str != NULL)语句不起作用。 5、编写strcpy函数(10分) 已知strcpy函数的原型是 char *strcpy(char *strDest, const char *strSrc); 其中strDest是目的字符串,strSrc是源字符串。 (1)不调用C++/C的字符串库函数,请编写函数 strcpy char *strcpy(char* strDest, const char* strSrc) { assert((strDest!=NULL) && (strSrc !=NULL));//2分 char *address = strDest;//2分 while( (*strDest++ = * strSrc++) != ‘\0’ )//2分 NULL; return address;// 2分 } 5.1 strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值? 答:为了实现链式表达式。 // 2分 例如int length = strlen( strcpy( strDest, "hello world") ); 6、编写类String的构造函数、析构函数和赋值函数(25分) 已知类String的原型为: class String { public: String(const char *str = NULL);//普通构造函数 String(const String &other);//拷贝构造函数 ~ String(void);// 析构函数 String & operate =(const String &other);// 赋值函数 private: char* m_data;// 用于保存字符串 }; 请编写String的上述4个函数。 标准答案: // String的析构函数 String::~String(void) // 3分 { delete [] m_data; // 由于m_data是内部数据类型,也可以写成delete m_data; } // String的普通构造函数 String::String(const char *str) // 6分 { if(str==NULL) { m_data = new char[1]; // 若能加NULL 判断则更好 *m_data = ‘\0’; } else { int length = strlen(str); m_data = new char[length+1]; // 若能加NULL 判断则更好 strcpy(m_data, str); } } // 拷贝构造函数 String::String(const String &other) // 3分 { int length = strlen(other.m_data); m_data = new char[length+1]; // 若能加NULL 判断则更好 strcpy(m_data, other.m_data); } // 赋值函数 String & String::operate =(const String &other) // 13分 { // (1) 检查自赋值 // 4分 if(this == &other) return *this; // (2) 释放原有的内存资源 // 3分 delete [] m_data; // ()分配新的内存资源,并复制内容// 3分 int length = strlen(other.m_data); m_data = new char[length+1]; // 若能加NULL 判断则更好 strcpy(m_data, other.m_data); // ()返回本对象的引用 // 3分 return *this; } 7、实现双向链表删除一个节点P,在节点P后插入一个节点,写出这两个函数。 void DeleteNode(DuNode *p) { p->prior->next=p->next; p->next->prior=p->prior; } void InsertNode(DuNode *p, DuNode *s)//Node "s" is inserted after "p" { s->next=p->next; p->next->prior=s; p->next=s; s->prior=p; } 8、Windows程序的入口是哪里?写出Windows消息机制的流程。 WINDOWS入口是WinMain函数 消息机制的流程: 系统中发生了某个事件 Windows把这个事件翻译为消息,然后把它放到消息队列中 1. 应用程序从消息队列中接收到这个消息,把它存放在TMsg记录中 2. 应用程序把消息传递给一个适当的窗口的窗口过程 3. 窗口过程响应这个消息并进行处理 9.写一个函数,将其中的\t都转换成4个空格。 #include<iostream> using namespace std; char* Convert_t(char *des,char *src) { char *temp; des=new char[100]; temp=des; while(*src!='\0') { if(*src=='\t') { src++; *des++=' '; *des++=' '; *des++=' '; *des++=' '; continue; } *des++=*src++; } *des='\0'; des=temp; return des; } int main() { char *t="asdf\tasd\tasasddas\\tdfasdf",*d; cout<<t<<endl; cout<<Convert_t(d,t); getchar(); } 10.如何定义和实现一个类的成员函数为回调函数? 如果类的成员函数是一个callback函数, 必须宣告它为"static",才能把C++ 编译器加诸于函数的一个隐藏参数this去掉。 11.C++里面是不是所有的动作都是main()引起的?如果不是,请举例。 不是的,C++里面有些动作不是引起的,比如,全局对象的实例化、全局变量的动态空间申请,等等 下面是一个例子: #include<iostream> using namespace std; char *des=new char[100]; //全局变量的动态空间申请在程序运行之后,main运行之前完成。所以不是所有的动作都是main引起的。 int main() { char *des="abc"; cout<<des<<endl; getchar(); } 12.C++里面如何声明const void f(void)函数为C程序中的库函数? extern "C" const void f(void); 这样声明之后,相当于告诉C, 函数const void f(void)是在C++语言的文件中声明或者实现的,c程序可以使用这个C++中的函数了,从而实现C++和c的混合编程。 13、编写一个函数,作用是把一个char组成的字符串循环右移n个。比如原来是“abcdefghi”如果n=2,移位后应该是“hiabcdefgh” 正确解答1: void LoopMove(char* pStr, int steps) { int n = strlen( pStr ) - steps; char tmp[MAX_LEN]; strcpy ( tmp, pStr + n ); strcpy ( tmp + steps, pStr); *( tmp + strlen ( pStr ) ) = '\0'; strcpy( pStr, tmp ); } 正确解答2: void LoopMove(char* pStr,int steps ) { int n = strlen( pStr ) - steps; char tmp[MAX_LEN]; memcpy( tmp, pStr + n, steps ); memcpy(pStr + steps, pStr, n ); memcpy(pStr, tmp, steps ); } 14、写出输出结果 void fun(char s[10]) { char a[10]; cout<<"a:"<<sizeof(a)<<endl; cout<<"s:"<<sizeof(s)<<endl; } 输出: a:10 s:4 15、内存的分配方式的分配方式有几种? 答: 1. 从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量。 2. 在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。 3. 从堆上分配,亦称动态内存分配。程序在运行的时候用malloc或new申请任意多少的内存,程序员自己负责在何时用free或delete释放内存。动态内存的生存期由我们决定,使用非常灵活,但问题也最多。 16.是不是一个父类写了一个virtual 函数,如果子类覆盖它的函数不加virtual ,也能实现多态? 答:virtual修饰符会被隐形继承的。private 也被集成,只事派生类没有访问权限而已。virtual可加可不加。子类的空间里有父类的所有变量(static除外)。同一个函数只存在一个实体(inline除外)。子类覆盖它的函数不加virtual ,也能实现多态。在子类的空间里,有父类的私有变量。私有变量不能直接访问。 17.进程间通信的方式有? 进程间通信的方式有 共享内存, 管道 ,Socket ,消息队列 , DDE等 18.C++中什么数据分配在栈或堆中,New分配数据是在近堆还是远堆中? 答:栈: 存放局部变量,函数调用参数,函数返回值,函数返回地址。由系统管理 堆: 程序运行时动态申请,new 和 malloc申请的内存就在堆上 (Google搜):DOS下程序是独占方式,堆分为近堆和远堆,近堆和栈是在数据段开辟的同一块内存地址,栈从下往上增长,堆从上向下分配,中间没有规定分界线,所以程序控制不当,如深层次的递归,大量的动态地址分配很容易造成堆栈冲突,即堆栈地址重叠,从而造成死机和程序运行异常。堆和栈连在一起说的原因就是如此。至于远堆则是指在数据段和代码段以外计算机所有没有使用的剩余基本内存。Windows采用的是虚拟地址,内存分配方式就不一样了 补充:DOS下堆栈的分配是由程序而不是操作系统自己控制的,具体分配大小、方式随编译系统、程序模式不同而异。C语言的堆栈分配代码在启动代码中. 19.全局变量和局部变量在内存中是否有区别?如果有,是什么区别? 全局变量储存在静态数据库,局部变量在堆栈。 20.TCP/IP 建立连接的过程?(3-way shake) 答:在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接。 第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认; 第二次握手:服务器收到syn包,必须确认客户的SYN(ack=j+1),同时自己也发送一个SYN包(syn=k),即SYN+ACK包,此时服务器进入SYN_RECV状态; 第三次握手:客户端收到服务器的SYN+ACK包,向服务器发送确认包ACK(ack=k+1),此包发送完毕,客户端和服务器进入ESTABLISHED状态,完成三次握手。 21.winsock建立连接的主要实现步骤? 答:服务器端:socker()建立套接字,绑定(bind)并监听(listen),用accept()等待客户端连接。 客户端:socker()建立套接字,连接(connect)服务器,连接上后使用send()和recv(),在套接字上写读数据,直至数据交换完毕,closesocket()关闭套接字。 服务器端:accept()发现有客户端连接,建立一个新的套接字,自身重新开始等待连接。该新产生的套接字使用send()和recv()写读数据,直至数据交换完毕,closesocket()关闭套接字。 22.static有什么用途?(请至少说明两种) 1) 在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变。 2) 在模块内(但在函数体外),一个被声明为静态的变量可以被模块内所用函数访问,但不能被模块外其它函数访问。它是一个本地的全局变量。 3) 在模块内,一个被声明为静态的函数只可被这一模块内的其它函数调用。那就是,这个函数被限制在声明它的模块的本地范围内使用 23.引用与指针有什么区别? 1) 引用必须被初始化,指针不必。 2) 引用初始化以后不能被改变,指针可以改变所指的对象。 3) 不存在指向空值的引用,但是存在指向空值的指针。 24、请写出下列代码的输出内容 #include<stdio.h> main() { int a,b,c,d; a=10; b=a++; c=++a; d=10*a++; printf("b,c,d:%d,%d,%d",b,c,d); return 0; } 答:10,12,120 main() { int a[5]={1,2,3,4,5}; int *ptr=(int *)(&a+1); printf("%d,%d",*(a+1),*(ptr-1)); } 输出:2,5 *(a+1)就是a[1],*(ptr-1)就是a[4],执行结果是2,5 &a+1不是首地址+1,系统会认为加一个a数组的偏移,是偏移了一个数组的大小(本例是5个int) int *ptr=(int *)(&a+1); 则ptr实际是&(a[5]),也就是a+5 原因如下: &a是数组指针,其类型为 int (*)[5]; 而指针加1要根据指针类型加上一定的值, 不同类型的指针+1之后增加的大小不同 a是长度为5的int数组指针,所以要加 5*sizeof(int) 所以ptr实际是a[5] 但是prt与(&a+1)类型是不一样的(这点很重要) 所以prt-1只会减去sizeof(int*) a,&a的地址是一样的,但意思不一样,a是数组首地址,也就是a[0]的地址,&a是对象(数组)首地址,a+1是数组下一元素的地址,即a[1],&a+1是下一个对象的地址,即a[5]

2009-05-08

如何 IObjectSafety 标记 ATL 控件安全初始化

需要用来获得所需的功能在步骤涉及到 IObjectSafetyImpl 用作您的控件派生的类之一,和重写 GetInterfaceSafetyOptions 和 SetInterfaceSafetyOptions。 这使您实现所需的功能在这种情况下意味着将标记为可安全编写脚本和初始化该控件。 若要将 IObjectSafetyImpl 需要将其添加到您的控件派生的类的列表。 是例如多边形教程中您看到以下: class ATL_NO_VTABLE CPolyCtl : ... public IObjectSafetyImpl // ATL's version of // IObjectSafety { public: BEGIN_COM_MAP(CPolyCtl) ... COM_INTERFACE_ENTRY_IMPL(IObjectSafety) // Tie IObjectSafety // to this COM map END_COM_MAP() STDMETHOD(GetInterfaceSafetyOptions)(REFIID riid, DWORD *pdwSupportedOptions, DWORD *pdwEnabledOptions) { ATLTRACE(_T("CObjectSafetyImpl::GetInterfaceSafetyOptions\n")); if (!pdwSupportedOptions || !pdwEnabledOptions) return E_FAIL; LPUNKNOWN pUnk; if (_InternalQueryInterface (riid, (void**)&pUnk) == E_NOINTERFACE) { // Our object doesn't even support this interface. return E_NOINTERFACE; }else{ // Cleanup after ourselves. pUnk->Release(); pUnk = NULL; } if (riid == IID_IDispatch) { // IDispatch is an interface used for scripting. If your // control supports other IDispatch or Dual interfaces, you // may decide to add them here as well. Client wants to know // if object is safe for scripting. Only indicate safe for // scripting when the interface is safe. *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; *pdwEnabledOptions = m_dwSafety & INTERFACESAFE_FOR_UNTRUSTED_CALLER; return S_OK; }else if ((riid == IID_IPersistStreamInit) || (riid == IID_IPersistStorage)) { // IID_IPersistStreamInit and IID_IPersistStorage are // interfaces used for Initialization. If your control // supports other Persistence interfaces, you may decide to // add them here as well. Client wants to know if object is // safe for initializing. Only indicate safe for initializing // when the interface is safe. *pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA; *pdwEnabledOptions = m_dwSafety & INTERFACESAFE_FOR_UNTRUSTED_DATA; return S_OK; }else{ // We are saying that no other interfaces in this control are // safe for initializing or scripting. *pdwSupportedOptions = 0; *pdwEnabledOptions = 0; return E_FAIL; } } STDMETHOD(SetInterfaceSafetyOptions)(REFIID riid, DWORD dwOptionSetMask, DWORD dwEnabledOptions) { ATLTRACE(_T("CObjectSafetyImpl::SetInterfaceSafetyOptions\n")); if (!dwOptionSetMask && !dwEnabledOptions) return E_FAIL; LPUNKNOWN pUnk; if (_InternalQueryInterface (riid, (void**)&pUnk) == E_NOINTERFACE) { // Our object doesn't even support this interface. return E_NOINTERFACE; }else{ // Cleanup after ourselves. pUnk->Release(); pUnk = NULL; } // Store our current safety level to return in // GetInterfaceSafetyOptions m_dwSafety |= dwEnabledOptions & dwOptionSetMask; if ((riid == IID_IDispatch) && (m_dwSafety & INTERFACESAFE_FOR_UNTRUSTED_CALLER)) { // Client wants us to disable any functionality that would // make the control unsafe for scripting. The same applies to // any other IDispatch or Dual interfaces your control may // support. Because our control is safe for scripting by // default we just return S_OK. return S_OK; }else if (((riid == IID_IPersistStreamInit) || (riid == IID_IPersistStorage)) && (m_dwSafety & INTERFACESAFE_FOR_UNTRUSTED_DATA)) { // Client wants us to make the control safe for initializing // from persistent data. For these interfaces, this control // is safe so we return S_OK. For Any interfaces that are not // safe, we would return E_FAIL. return S_OK; }else{ // This control doesn't allow Initialization or Scripting // from any other interfaces so return E_FAIL. return E_FAIL; } } ... } ATL 3.0 中, IObjectSafetyImpl 的实现已更改,使您现在可以作为模板参数提供安全选项。 例如,上述类的声明将显示为 class ATL_NO_VTABLE CPolyCtl : ... public IObjectSafetyImpl { public: BEGIN_COM_MAP(CPolyCtl) ... ,您将不必重写两个方法。 有关其他信息,单击下面,文章编号,以查看 Microsoft 知识库中相应: 192093 PRB: 编译器错误时移植到 ATL 3.0 IObjectSafetyImpl

2009-04-30

在ATL中如何执行带参数的JavaScript函数

back reading bstr.CopyTo(&dispparams.rgvarg[i].bstrVal); dispparams.rgvarg[i].vt = VT_BSTR; } dispparams.cNamedArgs = 0; EXCEPINFO excepInfo; memset(&excepInfo, 0, sizeof excepInfo); CComVariant vaResult; UINT nArgErr = (UINT)-1; // initialize to invalid arg hr = pScript->Invoke(dispid,IID_NULL,0,DISPATCH_METHOD,&dispparams,&vaResult,&excepInfo,&nArgErr);

2009-04-30

ATL中如何传递SAFEARRAY给VBSscript.pdf

{ CComVariant cv(i+1);//数组:1,2,3 ... size ::SafeArrayPutElement(psa, &i, &cv); } pvarOut->vt = VT_ARRAY | VT_VARIANT; pvarOut->parray = psa; return S_OK; } 为什么实际上是一个简单至极的int数组却要用VT_ARRAY | VT_VARIANT类型呢? 其实这都是为了支持脚本环境,最好是使用VARIANT,OK?

2009-04-30

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除