程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 小知識,如Form淡出、捕捉Form最小化

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

編輯:Delphi

  (*)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,'*','#',[rfReplaceAll]);

  (*)把一個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[ACol, ARow];
      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 otepad'),nil,nil,sw_shownormal);

  用回車代替TAB  在onkeypress  寫
      if  key=#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[0..128] of char;
    p2 : pchar;
    OutPut:array[0..100] 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', [p^.h_Name])+#10#13+
                         'IP address: '+Format('%s',[p2])
                         );
       WSACleanup;
       MessageBox(0,OutPut,'Get IP ! - this is your IP Address',mb_ok or mb_iconinformation);
  end;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved