星期四, 11月 24, 2011

設定元件位置在點擊按鈕的正上方

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  point1: TPoint;
begin
  point1:=Button1.Parent.ClientToScreen(Button1.BoundsRect.TopLeft);
  point1:=ClientToScreen(point1);
  caption := 'X:'+IntToStr(point1.x)+' '+
    'Y:'+IntToStr(point1.y);
  Form2:= TForm2.Create(self);
  try
    Form2.SetPosition(point1.x, point1.y); //x右邊到元件 y上邊到元件
    Form2.ShowModal;
  finally
    Form2.Free;
  end;
end;

end.
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm2 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    procedure SetPosition(x, y: Integer);
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.SetPosition(x, y: Integer);
begin
  self.left:=x-(self.Width div 2);
  self.Top:=y-self.Height;
end;

end.

星期三, 11月 23, 2011

ms sql 檢查資料表及資料表中的資料欄是否存在


在"資料表"的"欄位"存在,傳回1;"欄位"不存在,傳回0
select count(name) from syscolumns
where id=(
select id from sysobjects
where name='資料表名稱')
and name='欄位名稱'

判斷資料表存不存在
select * from doctor..sysobjects where name='病人' and type='U')
doctor is 資料庫名稱
病人是資料表的名稱

星期四, 11月 17, 2011

combobox自動展開及關閉

新增cxcombobox,並加上item值,再加入下列事件:
procedure TForm1.cxComboBox1MouseEnter(Sender: TObject);
begin
  cxComboBox1.DroppedDown:=true;
end;

procedure TForm1.FormMouseEnter(Sender: TObject);
begin
  cxComboBox1.DroppedDown:=false;
end;

星期一, 11月 14, 2011

StrToDate要注意短日期時間格式

var
t:TDateTime;
fs:TFormatSettings;
begin
fs.ShortDateFormat:='yyyy-mm-dd';
fs.DateSeparator:='-'; //這一行一定要有
t := StrToDate('2010-01-01',fs);

取得當日開始及最後時間

//******************************************************************************
//* GetDayStartDT: 取得當日開始時間AM 00:00:01:00                              *
//******************************************************************************
function mDayStartDT(tmpDT: TDateTime): TDateTime;
var
  wYear, wMonth, wDay: WORD;
begin
  DecodeDate(tmpDT, wYear, wMonth, wDay);
  Result := EncodeDate(wYear, wMonth, wDay)+
            EncodeTime(0, 0, 0, 0);
end;
//******************************************************************************
//* GetDayEndDT: 取得當日結束時間PM 23:59:59:00                                *
//******************************************************************************
function mDayEndDT(tmpDT: TDateTime): TDateTime;
var
  wYear, wMonth, wDay: WORD;
begin
  DecodeDate(tmpDT, wYear, wMonth, wDay);
  Result := EncodeDate(wYear, wMonth, wDay)+
            EncodeTime(23, 59, 59, 0);
end;