加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi7编程技巧

发布时间:2020-12-15 09:57:43 所属栏目:大数据 来源:网络整理
导读:加入一句{$R?WindowsXP},控件具有系统主题风格。 implementation {$R *.dfm} {$R windowsXP} { TMyThread } ============================== procedure TForm1.Button1Click(Sender: TObject); const ? {$I str.inc} begin ? ?showmessage(MSG); end; 文件

加入一句{$R?WindowsXP},控件具有系统主题风格。

implementation


{$R *.dfm}
{$R windowsXP}


{ TMyThread }

==============================


procedure TForm1.Button1Click(Sender: TObject);
const
? {$I str.inc}
begin
? ?showmessage(MSG);
end;

文件 str.inc

?MSG = 'abc';

=============================

设置标题可以用[color=#FF0000]SetTextBuf('abc')Text:='abc'[/color].

Delphi/Pascal code
?
1
2
3
4
5
6
7
procedure? TForm1 . FormCreate(Sender:?TObject);
begin
?? SetTextBuf( 'abc' );
ShowMessage(Text);
Text?:=? 'def' ;
ShowMessage(Caption)
end ;

新建一个普通工程,然后在工程源文件里,在program一句的下一行加上{$APPTYPE?CONSOLE},这样就可以在程序中使用Write之类的语句在控制台输出调试信息了。
====================================
7
8
9
10
11
12
13
14
15
16
program? Project2;
?
{ $APPTYPE? CONSOLE}
?
uses
Forms,
Unit1? in? 'Unit1.pas'? {Form1} ;
?
$R? *.res}
?
begin
?? Write ( 'start!' );
Application Initialize;
CreateForm(TForm1,?Form1);
Run;
.


=============================================

procedure也可以有返回值:

16
17
Add(a,b: Integer );
asm //eax存储a,edx存储b,相加的结果又保存到eax
add?eax,edx
;
?
FormCreate(Sender:?TObject);
type
TAdd= function (a,monospace!important; font-size:1em!important; padding:0px!important; margin:0px!important; border:0px!important; outline:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:inherit!important">):? ;
var
P:? Pointer ;
I:? ;
begin???
P?:=?@Add;
I?:=?TAdd(P)( 1 , 2 );
?
ShowMessageFmt( '%d' ;
==================================

{$R?'zz.res'?'zz.rc'}
这样就能在工程里包含zz.rc脚本,并自动build出zz.res!

==================================


===========================================



局部 常量字符串?or? 全局 常量字符串?

12
a;
const
STR?=? ;
begin
Form1 Tag?:=? ( PChar (STR));
;
?
FormCreate(Sender:?TObject);
begin
a;
ShowMessage( (Form1 Tag))
;


●●还以为看错了呢!发表于:?2014-02-06?13:23:59?新帖啊
楼主还在使用加入一句{$R?WindowsXP},控件具有系统主题风格???都2014年了,还在用什么版本?
{$R?WindowsXP}根本就已经淘汰了!就这样谁敢给你救命稻草?
如果是多年前的老版本比如D7,放几个按钮、单选、复选框,编译后按一下Alt试试?......
新版本直接工程--选项--应用程序--选中启用运行时主题,即可!

======================

父进程与子进程通信:
父进程调用SetEnvironmentVariable设置进程的环境变量(比如一个窗口A的句柄什么的),然后调用CreateProcess(其参数lpEnvironment指定为nil)来启动子进程。
子进程启动之后,就可以使用GetEnvironmentVariable来取得环境变量的值(得到句柄,就可以对窗口A发消息了)。


=========

在implementation下头的函数声明:

Delphi/Pascal code
?
15
implementation
?
*.dfm}
?
a;?forward; //注意关键词forward
?
FormCreate(Sender:?TObject);
begin
a //虽然在此之前未实现,但事先已经声明,所以可以调用
;
?
a; //实现
begin
ShowMessage( )
;

=======================

在某些情况下用[color=#FF0000]Clipboard.AsText来输出调试信息很好用。
比如写一个dll,将他注入到第三方的程序,此时通过在dll里写剪贴板就方便调试了。[/color]

OutputDebugString,用这个输出调试信息更好一点吧。


用GUID作为?内核对象的名字?或?窗口的类名?等,可做唯一性判断。
[code=delphi]procedure?TForm1.FormCreate(Sender:?TObject);
begin
??CreateMutex(nil,?True,?'{B44CCB6D-293C-4A5F-AEF2-05B3B100BBDF}');
??if?GetLastError?=?ERROR_ALREADY_EXISTS?then
??begin
????ShowMessage('软件已经运行!');
????ExitProcess(1)
??end;
end;[code]


=======================

循环语句配合Break,可以减少代码的缩进
例如:

Delphi/Pascal code
?
17
18
19
20
21
22
23
var
I:= + if? I= 3? then
begin
???? 3 ;
???? 4? then
begin
?????? 4 ;
?????? 5? then
???????? 5 ;
???????? 6? then
begin
end
end
;
;
?
'' )
改写后:
20
b;
repeat
;
I<> then? Break;
?
;
Break;
?
;
Break;
?
;
Break
until? False ;
?
)
;
===================================

好贴勿沉,继续接力。如何使程序在Win7下自动请求以管理员身份运行。
1、编辑文件UAC.manifest,内容如下:
<?xml?version="1.0"?encoding="UTF-8"?standalone="yes"?>?
<assembly?xmlns="urn:schemas-microsoft-com:asm.v1"?manifestVersion="1.0">?????
<trustInfo?xmlns="urn:schemas-microsoft-com:asm.v3">?
???????<security>?
???????????<requestedPrivileges>?
???????????????<requestedExecutionLevel?level="requireAdministrator"/>?
???????????</requestedPrivileges>?
???????</security>?
</trustInfo>?
</assembly>?
2、编辑文件uac.rc,内容如下:
1?24?UAC.manifest
3、工程文件参见以下:
program?DFKCpatch;
uses
??renyuanFunction,
??Messages,245)">??AutoRegOleUnit?in?'AutoRegOleUnit.pas'?{Form1};
{$R?*.res}
{$R?uac.res}
{$R?library.res}
var
??Res:?TResourceStream;
function?RunAsAdmin(hWnd:?hWnd;?filename:?string;?Parameters:?string):?Boolean;
{
????See?Step?3:?Redesign?for?UAC?Compatibility?(UAC)
????http://msdn.microsoft.com/en-us/library/bb756922.aspx
}
??sei:?TShellExecuteInfo;
??ZeroMemory(@sei,?SizeOf(sei));
??sei.cbSize?:=?SizeOf(TShellExecuteInfo);
??sei.Wnd?:=?hWnd;
??sei.fMask?:=?SEE_MASK_FLAG_DDEWAIT?or?SEE_MASK_FLAG_NO_UI;
??sei.lpVerb?:=?PChar('runas');
??sei.lpFile?:=?PChar(filename);?//?PAnsiChar;
??if?Parameters?<>?''?then
????sei.lpParameters?:=?PChar(Parameters);?//?PAnsiChar;
??sei.nShow?:=?SW_SHOWNORMAL;?//Integer;
??Result?:=?ShellExecuteEx(@sei);
end;
??Application.Initialize;
??Application.Title?:=?'DFKC?Patch';
??Application.CreateForm(TForm1,?Form1);
??Application.Run;
end.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

相关内容
推荐文章
站长推荐
热点阅读