Skip to content

标签:Delphi

Delphi下实现全屏快速找图找色

 前言

 

  最近有好几个朋友都在问我找图找色的问题,奇怪?于是乎写了一个专门用于找图找色的单元文件“BitmapData.pas”。在这个单元文件中我实现了从文件中导入位图、屏幕截图、鼠标指针截图、在图片上查找子图、在图片上查找颜色等功能。在查找过程中可以设定颜色变化范围、可以从左到右从上到下查找、也可以从指定点向四周查找。关于这个文件的下载和使用,可以参考本文的第四节。下面详细说说这些功能的实现。

一、数据提取

  位图其实可以看成是一个由象素组成的矩阵,找图找色可以看成是象素值的比对。很多新手在设计这类的程序时喜欢使用TBitmap.Canvas.Pixels属性,这个属性其实是对API函数GetPixel的封装,这个函数执行速度是很慢的,主要用来对位图象素进行偶尔的访问。而比对过程中需要对象素进行频繁的访问,造成程序运行缓慢。另外一种方法是使用TBitmap.ScanLine属性,利用它可以直接访问位图的数据。但是这些数据和当前位图的格式有关,主要是色深方面的问题,不同的色深会有不同格式的数据。另外比对过程中也需要对该属性进行频繁的调用。由于比对过程完全是数据的比较,不需要进行绘制操作。所以可以一次性将位图的数据提取出来放置到一个缓冲区中再进行比对,这样程序的性能会更高,也便于查找算法的实现。这时可以调用API函数GetDIBits获得设备无关位图的RGB数据,其实ScanLine属性也是调用这个函数实现的。GetDIBits函数格式声明如下:

function GetDIBits(

    DC: HDC;         //设备上下文句柄;

    Bitmap: HBitmap; //位图句柄,注意不是TBitmap对象;

    StartScan,       //开始检索的第一条扫描线;

    NumScans: UINT;  //共检索的扫描线数;

    Bits: Pointer;   //数据缓冲区指针;

    var BitInfo: TBitmapInfo; //位图信息结构,此结构确定了设备无关位图的数据格式;

    Usage: UINT      //指定TBitmapInfo结构的bmiColors成员的格式。

    ): Integer; stdcall;

  其中TBitmapInfo结构的格式如下:

Leave a Comment

TADOQuery控件报“在所要求名称或序数的相关集合物件中,找不到此项目。”

    一个问题搞了我两三天,今天终于把问题解决。问题在于TQDOQuery.Filtered属性。

    问题是这样的:一个小项目,读取XML文件保存到数据库里。在读取几个XML没有问题,有两个出问题。报“在所要求名称或序数的相关集合物件中,找不到此项目。”错误。

    代码循环读取XML节点,保存每个节点下的Table和Field内容。其中使用了Filter和Filtered属性。基于VCL控件的惯性,在Close时会把相关属性还原为初始值,所以我在Close以后也没有把Filtered设为False。

Leave a Comment

如何在CnWizards中使用脚本专家控制代码编辑器的最大化

    把下面的代码保存成SwitchSourceEditorMaximize.pas,然后在脚本专家里添加进去。

给个热键,我使用的是Ctrl+Alt+-。

把“手动执行脚本时需要确认”去掉,对于这个脚本非常的烦。

最后试试你刚设的热键,爽吧?再也不需要去双击标题了。

代码非常简单,所以就没有写注释了。

[pascal]

{*******************************************************}

{ }

{ Pascal Script Source File }

{ Run by RemObjects Pascal Script in CnWizards }

{ }

{ Generated by CnPack IDE Wizards }

{ }

{ Using maximize or restore source editor window }

{ Writed by Kend }

{ }

{*******************************************************}

program SwitchSourceEditorMaximize;

uses

CnWizUtils;

var

srcEditor: IOTASourceEditor;

editView: IOTAEditView;

editWindow: TCustomForm;

begin

srcEditor := CnOtaGetCurrentSourceEditor;

editView := CnOtaGetTopMostEditView(srcEditor);

editWindow := editView.GetEditWindow.GetForm;

if IsIdeEditorForm(editWindow) then

if editWindow.WindowState <> wsMaximized then

editWindow.WindowState := wsMaximized

else

editWindow.WindowState := wsNormal;

end.[/pascal]

 

Leave a Comment

Delphi IDE 缺省快捷键

    从Borland网站上找到的快捷键列表,这里列出的快捷键应该是比较全的,另外还有一些有关Delphi的技巧,到这个网站去看:http://bdn1.borland.com/article/borcon/files/3010/slides/3010_files/frame.htm

Ctrl+F 进行搜索

快捷键 描述 IDE 版本
Escape Selects parent control in design mode. Left mouse click + Escape drags the parent control Delphi 2 and up
Shift + Click After selecting control in form designer, click on it with Shift again to select top most parent Form Delphi 5 and up
Tab In Object Inspector activates incremental search for properties. Press again Tab to move focus to property value Delphi 5 and up
Ctrl + J Invoke code templates also called code snippets Delphi 5 and up
Ctrl + T Delete text from cursor position until end of word Delphi 5 and up
Ctrl + E Incremental search Delphi 5 and up
Ctrl + Y Delete current line Delphi 5 and up
Ctrl + B Invoke buffer window when focus is on editor. The buffer window shows a list of all different tabs. Delphi 2007 and up
Ctrl + Shift + G Insert a new GUID at the cursor position Delphi 5 and up
Ctrl + Shift + I Indent the current selected block Delphi 5 and up
Ctrl + Shift + U Unindent the current selected block Delphi 5 and up
Ctrl + K + I Indent the current selected block or current line in case of no selection Delphi 5 and up
Ctrl + K + U Unindent the current selected block or current line in case of no selection Delphi 5 and up
Ctrl + Shift + J Invoke SyncEdit Borland Developer Studio 2006
Ctrl + Shift + V Automatically declare variable at cursor position Borland Developer Studio 2006
Ctrl + Shift + Y Delete to the end of line Delphi 5 and up
Alt + [ Match pair forward Delphi 5 and up
Alt + ] Match pair backward Delphi 5 and up
Alt + RightArrow Browse forward (hotlink history) Delphi 7 and up
Alt + LeftArrow Browse backward (hotlink history) Delphi 7 and up
Alt + UpArrow Browse to symbol under editor cursor (invoke a hotlink and add it to the hotlink history) Borland Developer Studio 2006
F4 Run program to current cursor position Delphi 2 and up
F5 Toggle breakpoint Delphi 2 and up
F7 Debugger step into Delphi 2 and up
Shift – F7 Trace into next source line Delphi 5 and up
F8 Debugger step over Delphi 2 and up
Shift – F8 Run until return Delphi 5 and up
F9 Run program under debugger Delphi 2 and up
Ctrl + F2 Program Reset Delphi 5 and up
Ctrl + F7 Evaluate/modify Delphi 2005 and up
Ctrl + F5 Add Watch Delphi 5 and up
Ctrl + F12 View units Delphi 2 and up
Alt + G Goto line number in editor Delphi 2 and up
Ctrl + O + G Goto line number in editor Delphi 5 and up
Ctrl + Shift + R Start/stop recording editor macro Delphi 2 and up
Ctrl + Shift + P Play editor macro Delphi 2 and up
Ctrl + Spacebar Invoke code completion Delphi 5 and up
Ctrl + Shift + Spacebar Invoke code parameter hints Delphi 5 and up
Ctrl + Enter Open file at cursor Delphi 2 and up
Ctrl + Shift + Enter Find all references Delphi 2005 and up
Ctrl + Shift + C Invoke class completion for the class that currently contains the cursor Delphi 5 and up
Ctrl + Shift + UpArrow Navigate to method implementation/declaration Delphi 5 and up
Ctrl + Shift + DownArrow Navigate to method implementation/declaration Delphi 5 and up
Ctrl + Alt + F12 Opens the drop-down on the top-right corner of the code editor, listing all open files Borland Developer Studio 2006
Ctrl + Alt + Mouse Wheel Navigate to previous or next method implementation in lexical order in this source unit, or within the class to which scope has been limited using Ctrl+Q+L Borland Developer Studio 2006
Ctrl + Alt + UpArrow Navigate to previous method implementation in lexical order in this source unit, or within the class to which scope has been limited using Ctrl+Q+L Borland Developer Studio 2006
Ctrl + Alt + DownArrow Navigate to next method implementation in lexical order in this source unit, or within the class to which scope has been limited using Ctrl+Q+L Borland Developer Studio 2006
Ctrl + Alt + Home Navigate to the lexically first method implementation in this source unit, or within the class to which scope has been limited using Ctrl+Q+L Borland Developer Studio 2006
Ctrl + Alt + End Navigate to the lexically last method implementation in this source unit, or within the class to which scope has been limited using Ctrl+Q+L Borland Developer Studio 2006
Ctrl + Alt + P Activate the Tool Palette in filtering mode (start typing, press Enter to drop component) Delphi 2005 and up
Ctrl + / Toggle comment per line or selected block, Ctrl + ‘ on nordic (and perhaps other international ) keyboards Delphi 2005 and up
Ctrl + Shift + T Add todo list item Delphi 2005 and up
Alt + F12 Toggle between Form as text mode of design mode Delphi 5 and up
Alt + F11 Invoke Use unit dialog Delphi 5 and up
Ctrl + F11 Invoke Open Project Dialog Delphi 2005 and up
Ctrl + Alt + F11 Invoke Project manager window Delphi 2005 and up
F11 Invoke Object Inspector window Delphi 5 and up
F12 Toggle between code view and design view Delphi 5 and up
Alt + 0 Invoke Window list Delphi 2005 and up
Shift + Alt + F11 Invoke Structure window Delphi 2005 and up
Ctrl + Shift + B Invoke Browser window Delphi 5
Ctrl + Alt + B Invoke Breakpoint window Delphi 5 and up
Ctrl + Alt + S Invoke Call Stack window Delphi 5 and up
Ctrl + Alt + W Invoke Watches window Delphi 5 and up
Ctrl + Alt + L Invoke Local Variables window Delphi 5 and up
Ctrl + Alt + T Invoke Threads window Delphi 7 and up
Ctrl + Alt + F Invoke FPU window Delphi 2005(removed from Delphi 2006)
Ctrl + Alt + C Invoke CPU window Delphi 5 and up
Ctrl + Alt + V Invoke Event window Delphi 5 and up
Ctrl + Alt + M Invoke Modules window Delphi 2005 and up
Shift + F12 Invoke Forms window Delphi 5 and up
Ctrl + Shift + A Invoke Add to uses clause(Find unit in Delphi 2006) window (Refactoring) Delphi 2005 and up
Ctrl + Shift + F11 Invoke Project Options window Delphi 2005 and up
Ctrl + F9 Compile Project Delphi 5 and up
Shift + F9 Build Project Delphi 5 and up
Shift + F11 Invoke Add to project window Delphi 5 and up
Shift + Ctrl + F9 Run without debugging Delphi 2005 and up
Ctrl + F Find Delphi 3 and up
Shift + Ctrl + F Find in Files Delphi 3 and up
Alt + F7/F8 Previous/next result (for Find in Files and Messages) Delphi 7 and up
Ctrl+K+E Changes a word to lowercase and deselects selection Delphi 5 and up
Ctrl+K+F Changes a word to uppercase and deselects selection Delphi 5 and up
Ctrl+K+O Changes a word to lowercase Delphi 5 and up
Ctrl+K+N Changes a word to uppercase Delphi 5 and up
Ctrl+O+U Toggles case Delphi 5 and up
Ctrl+Shift+K+O Toggles Code folding between On and Off Delphi 2006 and up
Ctrl+Shift+K+A Code folding: Expands All blocks of code Delphi 2006 and up
Ctrl+Shift+K+E Code folding: Collapse current block of code Delphi 2006 and up
Ctrl+Shift+K+U Code folding: Expand current block of code Delphi 2006 and up
Ctrl+Shift+K+T Code folding: Toggle Current block (expand & collapse)  
Ctrl+Shift+K+R Code folding: Collapses all regions {$region ‘comment’}..{$endregion} Delphi 2006 and up
Ctrl+Shift+K+P Code folding: Collapse nested procedures  
Ctrl+Shift+K+M Code folding: Collapse all methods  
Ctrl+Shift+K+C Code folding: Collapse all classes  
Ctrl+Shift+K+G Code folding: Collapses down to primary Groups [Interface/Implementation] Delphi 2006 and up
Ctrl+Shift+K+N Code folding: Collapses Namespace/Unit Delphi 2006 and up
Ctrl+Tab Switch to the next open file in the editor Delphi 2006 and up
Ctrl+Shift+Tab Switch to the previous open file in the editor Delphi 2006 and up
Ctrl + K + P Invokes print selection dialog Delphi 5 and up
Ctrl + K + W Invokes Write block to file(selected text) Delphi 5 and up
Ctrl + K + R Invokes Read block from file(inserts text at cursor) Delphi 5 and up
Ctrl + K + C Copies selected text at the end of the selection Delphi 5 and up
Ctrl + K + T Select word at cursor position Delphi 5 and up
Ctrl + O + L Selects current line Delphi 5 and up
Ctrl + Q + L Toggles between limiting the Ctrl+Alt navigation shortcuts to the current class, or limiting it to the current unit Borland Developer Studio 2006
Ctrl + Q + W Go to next item of Messages window Delphi 5 and up

 

Leave a Comment

数组与字符串转换

其实规则很简单:

  • Move时访问的是string或string[n],要用string[1],因为string[0]是字符串的长度,所以所有string都以string[1]开始。
  • Move时访问的是array,静态array(array[0..n] of Char)时直接使用,动态array(array of Char)时使用array[0]。因为动态array时变量名其实只是一个地址。
  • Move时访问的是PChar,要使用^取指向的内容。

在Move前,如果目标是数组的话:

  1. 静态数组array[0..n] of Char,取两者最小长度直接Move
  2. 动态数组array of Char,取string长度并把数组SetLength,Move

 如果目标是string,无论是string或string[n]都要先SetLength。

如果目标是PChar,要用GetMemory申请内存,使用完后可别忘了FreeMemory。

Leave a Comment

宣传一下CnPack ;-)

COPY自http://www.CnPack.org
论坛地址:http://bbs.CnPack.org

CnPack 开发组启用新QQ群III,号码 32712412,欢迎 CnPack 开发组成员和 CnPack 用户加入讨论。

 CnPack 简介

  CnPack 是由互联网上一群中国程序员开发的开放源码的自由软件项目,当前主要的工作成果包括 CnPack 组件包、CnWizards 专家包以及 CVSTracNT 错误跟踪系统等。
  CnPack 组件包是一个涵盖不可视工具组件、界面控件、网络通讯组件、多语言处理等多个方面的 Delphi/C++ Builder 组件包。
  CnPack IDE 专家包(CnWizards)是一组集成在 Delphi/C++ Builder 的 IDE 中,用于增强 IDE 功能、提高 IDE 的可用性及开发效率的工具,见 界面截图
  CVSTrac 是由 D. Richard Hipp 为 CVS 开发的基于 WEB 的任务管理及错误跟踪系统,CVSTracNT 为其 Windows 下的移植版,由 CnPack 开发组开发及维护。

Leave a Comment