微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

delphi7编程技巧

加入一句{$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' Nowrap; list-style:none; font-family:Consolas, [I])
;
==================================

{$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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐