excel函数使用技巧:WindowsAPI函数使用技巧



得到WINDOWSSYSTEM路径:
思路方法:
var
MySysPath : PCHAR ;
begin
GetMem(MySysPath,255);
GetDirectory(MySysPath,255);
end;
注:MySysPath为SYSTEM路径
得到路径
ExtractFileDir(Application.Exename);



察看文件是否存在
FileExists(FileName:String):Boolean;



改变文件扩展名
ChangeFileExt(FileName:String)



得到文件扩展名
ExtractFileExt(FileName:String):String;



如何取得Windows临时文件目录?
适合版本:Delphi 3,2.0,1.0

Windows 95 & NT都指定了放置临时文件目录然而用户能改变临时目录位置而不使用缺省目录这篇文章是告诉你如何得到Windows 95 & NT当前临时目录位置这个Windows API GetTempPath就是解决这个问题原形为:

DWORD GetTempPath(DWORD nBufferLength, LPTSTR lpBuffer);

下面例子示范如何使用:

function GetTempDirectory: String;

var

TempDir: .gif' />[0..255] of Char;

begin

GetTempPath(255, @TempDir);

Result := StrPas(TempDir);

end;


备注:临时目录确定原则:

1,如果有TMP环境变量则临时目录为TMP指定目录

2,如果没有TMP环境变量而有TEMP环境变量则为TEMP变量指定目录

3,如果TMP和TEMP都没有定义则取当前目录为临时目录



不出现在任务栏

  般Windows 95运行时都会在任务栏上出现按钮如果你个监视那么出现按钮就不是明智的举了要实现该功能就要在OnCreate事件里利用到APISetWindowLong
procedure TForm1.FormCreate(sender:TObject);
begin
SetWindowLong(Application,Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
end;



改计算机名

改变计算机在网络中名字重新启动后才生效
SetComputerName(\'Hello World\');


控制热启动

要使系统热启动键(Ctrl+Alt+Del)失效使用以下语句
ParametersInfo(SPI_SCREENSAVERRUNNING, 1, 0, 0);
要恢复系统热启动键(Ctrl+Alt+Del)使用以下语句
ParametersInfo(SPI_SCREENSAVERRUNNING, 0, 0, 0);



临时路径

有时需要Windows临时路径来做备份等工作那么就要知道路径在哪下面帮你忙:
var aa:pchar;
begin
GetTempPath(20,aa); file://返回路径名
edit1.text:=aa;
end;



返回执行参数

  有关 Delphi 传入应用程式命令列参数, 请参考以下介绍说明:
用ParamCount取得命令参数个数:
呼叫 ParamStr(0), 传回执行档档名(含路径)
呼叫 ParamStr(n), 传回第n个参数内容
procedure TForm1.FormCreate(Sender: TObject);
var
sFileName: ;
begin
ParamCount > 0 then begin (* 有执行参数传入 *)
sFileName := ParamStr(1); (* 取得参数内容 *)
FileExists(sFileName) then
Memo1.Lines.LoadFromFile(sFileName)

Application.MessageBox(\'找不到指定档案\', \'讯息\', 48);
end;
end;



关闭Windows

控制WINDOWS开关:如关闭WINDOWS重新启动WINDOWS等, ExitWindowsEx(UINT uFlags,DWORD dwReserved);是实现这功能API
首先定义常数
const
EWX_FORCE=4; file://关闭所有并以其他用户身份登录
EWX_LOGOFF=0; file://重新启动计算机并切换到MS-DOS方式
EWX_REBOOT=2; file://重新启动计算机
EWX_SHUTDOWN=1;//关闭计算机
运行时给How赋值让他等于EWX_SHUTDOWN或其他以下语句
ExitWindowsEx(How,0);



关闭外部应用

如何在 Delphi 应用中, 去关闭外部已开启应用
下面给出段在 Delphi 中关闭“计算器”为例:
var
HWndCalculator : HWnd;
begin
// find the exist calculator window
HWndCalculator := Winprocs.FindWindow(nil, \'计算器\'); // close the exist Calculator
HWndCalculator <> 0 then
SendMessage(HWndCalculator, WM_CLOSE, 0, 0);
end;



得到执行目录

  SysUtils 单元中有 ExtractFileDir 和 ExtractFilePath两个类似, 用哪个?没有太大关系
  不过有以下差别: ExtractFilePath 传回值最後个字元是反斜杠“/”
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(ExtractFileDir(Application.Exename));
// ie: c:\\temp
ShowMessage(ExtractFilePath(Application.Exename));
// ie: c:\\temp\\
end;
相同点: 如果执行文件在根目录下(如:C:\\SAMPLE.EXE)话, 两者传回值相同 且最后都是“/”



使用GetFileVersionInfo 得到版本信息例子
Samples Using GetFileVersionInfo?

回答1:
procedure GetBuildInfo(var V1, V2, V3, V4: Word);
var
VerInfoSize: DWORD;


VerInfo: Poer;
VerValueSize: DWORD;
VerValue: PVSFixedFileInfo;
Dummy: DWORD;
begin
VerInfoSize := GetFileVersionInfoSize(PChar(ParamStr(0)), Dummy);
GetMem(VerInfo, VerInfoSize);
GetFileVersionInfo(PChar(ParamStr(0)), 0, VerInfoSize, VerInfo);
VerQueryValue(VerInfo, \'\\\', Poer(VerValue), VerValueSize);
with VerValue^ do
begin
V1 := dwFileVersionMS shr 16;
V2 := dwFileVersionMS and $FFFF;
V3 := dwFileVersionLS shr 16;
V4 := dwFileVersionLS and $FFFF;
end;
FreeMem(VerInfo, VerInfoSize);
end;
------------------------------------------
回答2
If you want a component, check out TVersionInfoResource at
delphi/\">http://www.pobox.com/~bstowers/delphi/ in the My Stuff section. D1/D2/D3/CB
compatible, freeware with full source code and a small demo.
And you can see the delphi/\">http://www.aye.net/~bstowers/delphi/
个component VersionInfo.zip



防止运行多个例程?
More than _disibledevent=>// register a message to use later _disibledevent=>handle
WaitResult := WaitForSingleObject(hMutex,10); // wait to see
we can have exclusive use of the mutex
( waitResult = WAIT_TIMEOUT ) then // we can\'t then broadcast
the message to make the owner of the mutex respond
{ request that the running application takes focus }
begin
BroadcastList := BSM_APPLICATIONS;
BroadcastMessage(
BSF_POSTMESSAGE,@BroadcastList,MessageID,0,0); file://32 bit - broadcast the
message to all apps - _disibledevent=> Application.CreateForm(TMainForm, MainForm);
Application.Run;
ReleaseMutex(hMutex); // release the mutex as a politeness
end;
CloseHandle(hMutex); // close the mutex handle
end.

This goes in the MainForm

procedure Tform.OnAppMessage(var Msg : TMsg ; Var Handled : Boolean);
begin
{ If it\'s the special message then focus _disibledevent=>another instance of this app that is trying to start up
begin
show;
WindowState := wsMaximized;
BringToFront;
SetFocus;
Handled := true;
end;
end;

file://And this goes in the TMainForm.FormCreate ;-

Application.OnMessage:= _disibledevent=> fl_loaded := GetComputerName(CNameBuffer,CLen^);

fl_loaded then
ComputerName := StrPas(CNameBuffer)

ComputerName := \'Unkown\';

FreeMem(CNameBuffer,255);
Dispose(CLen);

end;



7. 停止个线程?
问 Stop A Thread?
回答
You can Terminate your thread in two ways:
1) Assign ThreadDone to _disibledevent=>

type
Txyz = (TThread)
published
procedure Execute; override;
end;

var
XYZThread: Txyz;

implementation

procedure Txyz.Execute;
begin
while True do Application.ProcessMessages;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
XYZThread := Txyz.Create(False);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
XYZThread2.Suspend;
XYZThread2.Free;
end;

end.



如何在WINDOWS中象在UCDOS下控制打印字体长宽而不受限于SIZE 限制

首先为了达到这个功能可以采用Windows逻辑字体(LogFont)
可以使用 CreateFont 或 CreateFontIndirect 这两个Windows API
来定义任何想要字体由于 CreateFont 所需参数甚多通常
我们使用 CreateFontIndirect 来建立所需逻辑字体这个API
在Delphi中声明为
function CreateFontIndirect(const p1: TLogFont): HFONT; stdcall;
其中只有个参数 p1: TLogfont, 所有有关字体参数完全通过这个
TLogfont结构来传送Windows将根据结构中内容创建出相应逻辑
字体在DelphiWindows.pas中TLogFont是这样定义

TLogFontA = packed record
lfHeight: Long;
lfWidth: Long;
lfEscapement: Long;
lfOrientation: Long;
lfWeight: Long;
lfItalic: Byte;
lfUnderline: Byte;
lfStrikeOut: Byte;
lfCharSet: Byte;
lfOutPrecision: Byte;
lfClipPrecision: Byte;
lfQuality: Byte;
lfPitchAndFamily: Byte;
lfFaceName: .gif' />[0..LF_FACESIZE - 1] of AnsiChar;
end;
TLogFontW = packed record
lfHeight: Long;
lfWidth: Long;
lfEscapement: Long;
lfOrientation: Long;
lfWeight: Long;
lfItalic: Byte;
lfUnderline: Byte;
lfStrikeOut: Byte;
lfCharSet: Byte;
lfOutPrecision: Byte;
lfClipPrecision: Byte;
lfQuality: Byte;
lfPitchAndFamily: Byte;
lfFaceName: .gif' />[0..LF_FACESIZE - 1] of WideChar;
end;
TLogFont = TLogFontA;

其中涉及到很多参数其中

lfHeight: Long;
指定以逻辑单位标定字体高度取值可为正负或零对于需要随意
定义字体高度情况下通常取负值以保证获得实际尺寸字体

lfWidth: Long;
用于指定字体平均宽度由于Windows系统下大多数字体都是比例
字体因而采用平均宽度这个表示思路方法若指定为0则系统会自动根据
适当比例自动处理宽度

lfEscapement: Long;
指定输出方向和当前坐标系X轴的间以十分的度为单位角度

lfOrientation: Long;
指定每个和当前坐标系X轴的间以十分的度为单位角度
Windows95中这个值等同于lfEscpement

lfWeight: Long;
指定范围为从0至1000字体加重程度400是标准字体700为加重字体
0表示采用默认值

lfItalic: Byte;
不为0表示采用斜体字

lfUnderline: Byte;
不为0表示带下划线

lfStrikeOut: Byte;
不为0表示带穿透线

lfCharSet: Byte;
指定字体集

lfOutPrecision: Byte;
指定输出精度用于确定对前面些设定值精确程度

lfClipPrecision: Byte;
指定裁剪精度裁剪是Windows图形环境下种特殊处理简单说就是
去掉图形中落在视图以外部分有助于提高图形处理速度

lfQuality: Byte;
指定输出质量

lfPitchAndFamily: Byte;
指定字体Pitch和Family

lfFaceName: .gif' />[0..LF_FACESIZE - 1] of AnsiChar;
指定采用字体名称

在建立逻辑字体时我们通常使用

lfHeight和lfWidth来确定字体尺寸使用lfEscapement和lfOrientation
来确定字体输出方向使用lfWeight, lfItalic, lfUnderline,
lfStrikeOut, 来确定字体加重斜体下划线和穿透线使用lfCharSet
来确定字体通常采用系统默认
对于lfOutPrecision, lfClipPrecision, lfQuality般应用于对屏幕的外
输出设备通常采用默认值采用lfPitchAndFamily来确定采用定宽或可
变字体和字体家族以lfFaceName来通过名称选择采用字体
另外应当注意在Windows环境下每种字体具体输出为何种形式取决于很多
原因需要对以上这些参数进行有效组合才能达到所要效果

Tags:  excel函数技巧 windowsapi大全 windowsapi excel函数使用技巧

延伸阅读

最新评论

发表评论