Moodsky 发表于 2007-2-8 10:29:10

小知识,如Form淡出、捕捉Form最小化

(*)Form淡出
1)AnimateWindow(Form1.Handle,1000,AW_BLEND);//淡入
AnimateWindow(Form1.Handle,1000,AW_HIDE or AW_BLEND);//淡出
2)窗体有AlphaBlend属性和AlphaBlendValue属性,将AlphaBlend属性设为True,然后在Timer组件中随着时间改变AlphaBlendValue的值(0-255)即可
AlphaBlendValue:=1;//窗体透明

(*)捕捉Form最小化
定义一个过程
public
    procedure onminsize(var msg:twmsyscommand);message wm_syscommand;
procedure TForm1.onminsize(var msg: twmsyscommand);
begin
if msg.CmdType=SC_MINIMIZE then
begin
    form1.Width:=100;
    form1.Height:=100;
    form1.Left:=100;
    form1.Top:=300;
    exit;
end;
inherited;
end;

(*)createGuid(out newGUID)
   s:=GUIDToString(newGUID)

(*)四舍五入取正: y:=Round(x)

(*) string:=StringReplace(string1,'*','#',);

(*)把一个string用单引号括起来作为string返回
QuotedStr(const S: string): string

(*) 如何让DBGRID中的内容分页显示
ADODataSet1.Open;
ADODataSet1.Recordset.PageSize := 10;         //显示第一页   
ADODataSet1.Recordset.AbsolutePosition := 1;
ADODataSet1.Recordset.PageSize := 10;         //显示第二页
ADODataSet1.Recordset.AbsolutePosition := 11;

(*)实现解析IP地址为主机域名
uses WinSock
过程如下:
function IPAddrToName(IPAddr : String): String;
var
    SockAddrIn: TSockAddrIn;
    HostEnt: PHostEnt;
    WSAData: TWSAData;
begin
    WSAStartup($101, WSAData);
    SockAddrIn.sin_addr.s_addr:= inet_addr(PChar(IPAddr));
    HostEnt:= gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
    if HostEnt<>nil then
    begin
      result:=StrPas(Hostent^.h_name)
    end
    else
    begin
      result:='';
    end;
end;
使用函数:
var
Name: string;
begin
Name := InputBox('输入对方主机IP', '主机IP地址:', '');
showmessage(IpAddrToName(Name));
end;

(*)去掉twebbrowser右边的ScrollBar
WebBrowser1.oleobject.Document.body.Scroll := 'no';

(*)TTreeView的结点如何具有CheckBox
TreeView有个Images属性,做三张小图片:
全选中、部分选中、不选状态(直接拍下CheckBox三种状态的图片在加工一下就可以了),然后在点击事件中根据具体情况设置TTreeNodes的ImageIndex就OK了。
重载TreeView的CreateParams:
type
TCheckBoxTreeView = class(TTreeView)
protected
    procedure CreateParams(var Params: TCreateParams); override;
end;

procedure TCheckBoxTreeView.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.Style:=Params.Style or TVS_CHECKBOXES;
{ TVS_CHECKBOXES declared in CommCtrl }
end;

如果要Check这个CheckBox,可以用下面的代码:
function IsNodeChecked(Node :TTreeNode) :Boolean;
const
TVIS_CHECKED = $2000;
var
TvItem :TTVItem;
begin
TvItem.Mask:= TVIF_STATE;
TvItem.hItem := Node.ItemId;
TreeView_GetItem(Node.TreeView.Handle, TvItem);
Result := (TvItem.State and TVIS_CHECKED) = TVIS_CHECKED;
end;

procedure SetNodeChecked(Node :TTreeNode; Checked :Boolean);
const
TVIS_CHECKED = $2000;
var
TvItem :TTVItem;
begin
FillChar(TvItem, SizeOf(TvItem), 0);
with TvItem do begin
    hItem   := Node.ItemId;
    Mask      := TVIF_STATE;
    StateMask := TVIS_STATEIMAGEMASK;
    if Checked then
      TvItem.State :=TVIS_CHECKED
    else
      TvItem.State :=TVIS_CHECKED shr 1;
    TreeView_SetItem(Node.TreeView.Handle, TvItem);
end;
end;
可以做成事件和封装成一个类:
private OldState :Boolean;

procedure Change(Node: TTreeNode); override;
begin
inherited Change(Node);
OldState := IsNodeChecked(Node);
end;

procedure Click; override;
var
NewState :Boolean;
begin
inherited Click;
NewState := IsNodeChecked(Self.Selected);
if NewState = OldState then Exit;
if (NewState) and Assigned(OnNodeChecked) then OnNodeChecked()
else if (not NewState) and Assigned(OnNodeUnChecked) then
OnNodeUnChecked()

// OnNodeChecked & OnNodeUnchecked will be events that you declare
end;

(*)如何将stringgrid中每列的数据居中显示?
(1)将stringgrid的defaultdraw设为:false;
(2)procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;Rect: TRect; State: TGridDrawState);
var
sText: string;
begin
with StringGrid1 do
begin
    sText := Cells;
    if sText <> '' then
    begin
      Canvas.FillRect(Rect);
      DrawText(Canvas.Handle, PChar(sText), Length(sText), Rect, DT_CENTER or DT_VCENTER or DT_SINGLELINE);
    end;
end;
end;

DLL获得自己所在的路径:Windows.GetModuleFileName(HInstance,lp, 100);

Caption := Copy(Edit1.Text, 3, MaxInt); //建议,少计算一次

取得explorer句柄,线程
GetWindowThreadProcessID(FindWindow('ExploreWClass', nil), nil)
FindWindow('ExploreWClass', nil)

调用记事本
uses .... shellapi;
shellexecute(handle,nil,pchar('c:\windows\notepad'),nil,nil,sw_shownormal);

用回车代替TAB在onkeypress写
    ifkey=#13 then perform(WM_NEXTDLGCTL,0,0);


fieldName like "%'+gA_FindValue+'%"'

formatFloat('0.00',doubleValue);//格式化 Float ---> string

mailto:[email protected]?subject=subjectTille      ( &Attach=C:\Config.sys )

//返回给定月份的天数
uses SysUitls; // IsLeapYear
...
function DaysOfMonth(Date: TDateTime): integer;
var y, m, d: Word;
begin
   DecodeDate( Date, y, m, d );
   case m of
   2:if IsLeapYear(y) then
         Result:=29 else Result:=28;
   1,3,5,7,8,10,12: Result:=31;
   4,6,9,11:      Result:=30;
end;
end;

//触发一个错误
raise Exception.Create('delete table Error');

//获取 IP 地址(getIP.exe)
procedure TForm1.Button1Click(Sender: TObject);
var
wVersionRequested : WORD;
wsaData : TWSAData;
p : PHostEnt;
s : array of char;
p2 : pchar;
OutPut:array of char;
begin
{Start up WinSock}
   wVersionRequested := MAKEWORD(1, 1);
   WSAStartup(wVersionRequested, wsaData);

{Get the computer name}
   GetHostName(@s, 128);
   p := GetHostByName(@s);

{Get the IpAddress}
   p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
   StrPCopy(OutPut,'Hostname: '+Format('%s', )+#10#13+
                     'IP address: '+Format('%s',)
                     );
   WSACleanup;
   MessageBox(0,OutPut,'Get IP ! - this is your IP Address',mb_ok or mb_iconinformation);
end;
页: [1]
查看完整版本: 小知识,如Form淡出、捕捉Form最小化