这叫什么事儿?

这几天忙了个昏头涨脑。视频不是咱能干的。没事交流一下,发现还是黑白片最爱啊。可惜控制力度不够,不知道能不能搞定客户。

Delphi2010启动两次报错,退出了也再进不去了。查来查去,真查技术,还是得google和stackoverflow。
Delphi 2006-2010 error: “Cannot create file C:\Users\Admin\AppData\Local\Temp\EditorLineEnds.ttr”
大牛还写了段程序:dzEditorLineEndsFix

下载这个补丁吧。

微软这两个补丁:KB2982791, KB2970228这两个补丁搞毛啊。

How to scroll to desired element in WebBrowser with Delphi

在Delphi里控制浏览器成为一个需求的原因是有很多文档的html文件都特别巨大,比如有100个章节,想要控制浏览器滚动到指定的章节就成为一件特别有意思的事。
要做这样一件事,很简单,只需要两步,即:
1. 找到它/Found the element
[code language=”pascal”]
uses
MSHTML, SysUtils, Variants;

function GetElementById(const Doc: IDispatch; const Id: string): IDispatch;
var
Document: IHTMLDocument2; // IHTMLDocument2 interface of Doc
Body: IHTMLElement2; // document body element
Tags: IHTMLElementCollection; // all tags in document body
Tag: IHTMLElement; // a tag in document body
I: Integer; // loops thru tags in document body
begin
Result := nil;
// Check for valid document: require IHTMLDocument2 interface to it
if not Supports(Doc, IHTMLDocument2, Document) then
raise Exception.Create(‘Invalid HTML document’);
// Check for valid body element: require IHTMLElement2 interface to it
if not Supports(Document.body, IHTMLElement2, Body) then
raise Exception.Create(‘Can”t find element’);
// Get all tags in body element (‘*’ => any tag name)
Tags := Body.getElementsByTagName(‘*’);
// Scan through all tags in body
for I := 0 to Pred(Tags.length) do
begin
// Get reference to a tag
Tag := Tags.item(I, EmptyParam) as IHTMLElement;
// Check tag’s id and return it if id matches
if AnsiSameText(Tag.id, Id) then
begin
Result := Tag;
Break;
end;
end;
end;
[/code]
2. 滚到它/do scroll
[code language=”pascal”]
function ScrollToElement(wb: TEmbeddedWB; id: String): Boolean;
var
Elem: IHTMLElement;
begin
Elem := GetElementById(wb.Document, id) as IHTMLElement;
if Assigned(Elem) then
wb.ScrollToPosition(0, Elem.offsetTop);
end;
[/code]
references:
1. Finding HTML elements by ID in a TWebBrowser document
2. 源码已经发到我在code.google.com上的小工具项目了,直接看吧/You could down the full source code at my pumpkin project hosted on code.google.com
3. Scroll view to Anchor position

限制程式只能起動單一實例

[code language=”pascal”]
//1. CreateMutex

unit Unit1;

uses
Windows, Dialogs, Sysutils;

{….}
implementation

{….}

var
mHandle: THandle; // Mutexhandle

initialization
mHandle := CreateMutex(nil, True, ‘XYZ’);
if GetLastError = ERROR_ALREADY_EXISTS then
begin
ShowMessage(‘Program is already running!’);
halt;
end;

finalization
if mHandle <> 0 then CloseHandle(mHandle)
end.

{
HANDLE CreateMutex(LPSECURITY_ATTRIBUTES lpMutexAttributes,
BOOL bInitialOwner,
LPCTSTR lpName);

lpMutexAttributes:
Ignored. Must be NULL.

bInitialOwner:
Boolean that specifies the initial owner of the mutex object.
If this value is TRUE and the caller created the mutex,
the calling thread obtains ownership of the mutex object.
Otherwise, the calling thread does not obtain ownership of the mutex.

lpName:
Long pointer to a null-terminated string specifying the name of the mutex object.
The name is limited to MAX_PATH characters and can contain any character except the
backslash path-separator character (\). Name comparison is case sensitive.

Return Values:
A handle to the mutex object indicates success.
If the named mutex object existed before the function call,
the function returns a handle to the existing object and GetLastError
returns ERROR_ALREADY_EXISTS.

}

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

// 2. CreateSemaphore
// (Alternative Funtion, Alternative Funktion)

procedure TForm1.FormCreate(Sender: TObject);
var
Sem: THandle;
begin
Sem := CreateSemaphore(nil, 0, 1, ‘PROGRAM_NAME’);
if ((Sem <> 0) and (GetLastError = ERROR_ALREADY_EXISTS)) then
begin
CloseHandle(Sem);
ShowMessage(‘This program is already running.’);
Halt;
end;
end;

{
HANDLE CreateSemaphore(
LPSECURITY_ATTRIBUTES lpSemaphoreAttributes, // SD
LONG lInitialCount, // initial count
LONG lMaximumCount, // maximum count
LPCTSTR lpName // object name
);

lpSemaphoreAttributes:
[in] Pointer to a structure that determines whether the returned handle
can be inherited by child processes.
If lpSemaphoreAttributes is NULL, the handle cannot be inherited.

lInitialCount:
[in] Specifies an initial count for the semaphore object.
This value must be greater than or equal to zero and less
than or equal to lMaximumCount.

lMaximumCount:
[in] Specifies the maximum count for the semaphore object.
This value must be greater than zero.

lpName
[in] Pointer to a null-terminated string specifying the name
of the semaphore object. The name is limited to MAX_PATH characters.
Name comparison is case sensitive.
}

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

// 3: GlobalFindAtom, GlobalAddAtom

{
This sample shows how to determine if your program was
running in a current session of Windows
It can be usefull if you want ie. to limit your program (a demo)
to run only once per session. It can be stimulating for a registering. :
}

// Place in FormShow event:

procedure TForm1.FormCreate(Sender: TObject);
var
atom: Integer;
CRLF: string;
begin
if GlobalFindAtom(‘A Text used to be stored in memory’) = 0 then
atom := GlobalAddAtom(‘A Text used to be stored in memory’)
else
begin
CRLF := #10 + #13;
ShowMessage(‘This version may only be run once for every Windows Session.’ +
CRLF +
‘To run this program again, you need to restart Windows, or better yet:’ +
CRLF +
‘REGISTER !!’);
Close;
end;
end;
[/code]

參見:
1. .load only one instance of a program?
2.
Creating a real singleton class in Delphi 5
— 注:本篇參照GoF的設計模式
3. Single Instance Applications

JSON for Delphi

按說今天是骑车上下班第28天的吉祥日子,怎麼也需要寫一大篇兒才算合情又合理。但是今天路上沒心又沒肺,除了偶爾街邊走過一兩個黑絲大粗腿也沒什麼記憶了。是敬告某些人,潮流如果趕了,跟沒趕上也差不多。人只是穿出自我以得了,何必看別人下菜碟呢?

今晚是紀念的除了騎行就是Delphi。我從到軟體公司,第一個接觸的雖然是PB,但是之前的最愛以是Delphi了。原因很簡單,VCL是開源的,你能找到的大多數控件都提供了源碼。這叫一個愛好偷龔別我程式的我情以何堪呐。

新的時代來臨了,網路上各種協議此起彼伏。原來研究過的WebService臃腫不堪。現在又出現了JSON、YAML等新秀。說起來以是為了傳的少、用起來簡單。正好手頭要做件事,以測試一下JSON吧。

1. lkJSON

2. JSON – SuperObject
– Fastest JSON parser for Delphi.
– XML to JSON parser.
– Easy to use.
– JSON Validator.
– JSON-RPC.
– Can write JSON format to be human readable.

在Delphi7下似乎SuperObject中的例子無法正常編譯。

關於JSON的資料,請看它的主站http://www.json.org/

明天東北路橋應該通車了吧。祝願一下吧。

新RAD Studio2010版现在发布!

今早收到Embarcadero公司的邮件,新版的RAD Studio 2010发布了。

令人兴奋的全新RAD Studio 2010包括全新的Delphi ®、C + +Builder®、和Delphi Prism ™。 Embarcadero公司(原CodeGear的)RAD Studio在提供一切您所需要的,能够闪电般的建立快速、紧凑、生动丰富的原生Windows、.NET、Web和数据库应用程序的功能。

RAD Studio 2010 的新功能主要在于:增强的 IDE和可视化组件库 (VCL) 框架和基础的编译器以及其他各项工具,这使得你构建那些跨越所有的数据源和层次的可视化应用变得更加容易。 RAD Studio 2010 中的重要新功能包括:

  • 新增功能! 支持最尖端的触摸和手势-使您的应用程序能够支持包括Windows 7在内的任何 Windows 操作系统
  • 新增功能! 通过增强的DataSnap™框架和DBExpress数据访问驱动,支持最新的数据库平台,包括Firebird数据库的支持!
  • 新增功能! IDE Insight,随时访问任何功能,设置,或组件
  • 新增功能! 包括tabbed toolbar的Delphi 7 经典模式布局选项
  • 新增功能! 超过 120项针对开发的IDE 功能增强
  • 功能增强! 在2009版的基础上,又有超过一百项的功能增强,包括 Unicode、 泛型、 匿名方法等等

原文