预览模式: 普通 | 列表

Delphi中判断当前登陆用户是否是管理员

const 
  SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority = 
    (: (0, 0, 0, 0, 0, 5)); 
  SECURITY_BUILTIN_DOMAIN_RID = $00000020; 
  DOMAIN_ALIAS_RID_ADMINS = $00000220; 

 IsAdmin: Boolean; 
var 
  hAccessToken: THandle; 
  ptgGroups: PTokenGroups; 
  dwInfoBufferSize: DWORD; 
  psidAdministrators: PSID; 
  x: Integer; 
  bSuccess: BOOL; 
begin 
  Result   := False; 
  bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, 
    hAccessToken); 
  if not bSuccess then 
  begin 
    if GetLastError = ERROR_NO_TOKEN then 
      bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, 
        hAccessToken); 
  end; 
  if bSuccess then 
  begin 
    GetMem(ptgGroups, 1024); 
    bSuccess := GetTokenInformation(hAccessToken, TokenGroups, 
      ptgGroups, 1024, dwInfoBufferSize); 
    CloseHandle(hAccessToken); 
    if bSuccess then 
    begin 
      AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2, 
        SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 
        0, 0, 0, 0, 0, 0, psidAdministrators); 
      {$R-} 
      for x := 0 to ptgGroups.GroupCount - 1 do 
        if EqualSid(psidAdministrators, ptgGroups.Groups[x].Sid) then 
        begin 
          Result := True; 
          Break; 
        end; 
      {$R+} 
      FreeSid(psidAdministrators); 
    end; 
    FreeMem(ptgGroups); 
  end; 
end;
分类:  |  评论: 0 |  引用: 0 |  查看次数: 0

IE下判断IE版本的语句...[if lte IE 6]……[endif]

<!--[if lte IE 6]>
<![endif]-->
IE6及其以下版本可见
 
<!--[if lte IE 7]>
<![endif]-->
IE7及其以下版本可见
 
<!--[if IE 6]>
<![endif]-->
只有IE6版本可见
 
<![if !IE]>
<![endif]>
除了IE以外的版本
 
<!--[if lt IE 8]>
<![endif]-->
IE8及其以下的版本可见
 
 
<!--[if gte IE 7]>
<![endif]-->
IE7及其以下的版本可见
 
 
用法:
(1)
可使用如下代码检测当前IE浏览器的版本(注意:在非IE浏览器中是看不到效果的)
 <!––[if IE]>
       <h1>您正在使用IE浏览器</h1>
       <!––[if IE 5]>
           <h2>版本 5</h2>
       <![endif]––>
       <!––[if IE 5.0]>
           <h2>版本 5.0</h2>
       <![endif]––>
       <!––[if IE 5.5]>
           <h2>版本 5.5</h2>
       <![endif]––>
       <!––[if IE 6]>
           <h2>版本 6</h2>
       <![endif]––>
       <!––[if IE 7]>
           <h2>版本 7</h2>
       <![endif]––>
<![endif]––>
那如果当前的浏览器是IE,但版本比IE5还低,该怎么办呢,可以使用<!–[if ls IE 5]>,当然,根据条件注释只能在IE5+的环境之下,所以<!–[if ls IE 5]>根本不会被执行。
lte:就是Less than or equal to的简写,也就是小于或等于的意思。
lt :就是Less than的简写,也就是小于的意思。
gte:就是Greater than or equal to的简写,也就是大于或等于的意思。
gt :就是Greater than的简写,也就是大于的意思。
! : 就是不等于的意思,跟java里的不等于判断符相同


(2)

应该如何应用条件注释
    本文一开始就说明了,因为IE各版本的浏览器对我们制作的WEB标准的页面解释不一样,具体就是对CSS的解释不同,我们为了兼容这些,可运用条件注释来各自定义,最终达到兼容的目的。比如:
<!–- 默认先调用css.css样式表 –->

<link rel="stylesheet" type="text/css" href="css.css" />
<!-–[if IE 7]>

<!–- 如果IE浏览器版是7,调用ie7.css样式表- –>

<link rel="stylesheet" type="text/css" href="ie7.css" />
<![endif]–->

<!–-[if lte IE 6]>

<!–- 如果IE浏览器版本小于等于6,调用ie.css样式表 -–>

<link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]–>

    这其中就区分了IE7和IE6向下的浏览器对CSS的执行,达到兼容的目的。同时,首行默认的css.css还能与其他非IE浏览器实现兼容。

    注意:默认的CSS样式应该位于HTML文档的首行,进行条件注释判断的所有内容必须位于该默认样式之后。
    比如如下代码,在IE浏览器下执行显示为红色,而在非IE浏览器下显示为黑色。如果把条件注释判断放在首行,则不能实现。该例题很能说明网页对IE浏览器和非IE浏览器间的兼容性问题解决。
<style type="text/css">
body{
background-color: #000;
}
</style>
<!-–[if IE]>

<style type="text/css">
body{
background-color: #F00;
}
</style>
<![endif]–->


    同时,有人会试图使用<!–-[if !IE]>来定义非IE浏览器下的状况,但注意:条件注释只有在IE浏览器下才能执行,这个代码在非IE浏览下非单不是执行该条件下的定义,而是当做注释视而不见。

    正常就是默认的样式,对IE浏览器需要特殊处理的,才进行条件注释。在HTML文件里,而不能在CSS文件中使用。

现在的DWcs4里面,已经装备了这些注释:在“窗口-->代码片段-->注释”里。其他的版本没太注意到。

分类:  |  评论: 0 |  引用: 0 |  查看次数: 28

TStringList 常用操作

//TStringList 常用方法与属性:
var
List: TStringList;
i: Integer;
begin
List := TStringList.Create;
List.Add('Strings1');           {添加}
List.Add('Strings2');
List.Exchange(0,1);             {置换}
List.Insert(0,'Strings3');      {插入}
i := List.IndexOf('Strings1'); {第一次出现的位置}
List.Sort;                      {排序}
List.Sorted := True; {指定排序}
List.Count;                     {总数}
List.Text;                      {文本集合}
List.Delete(0);                 {删除, 0是第一个数据}
List.LoadFromFile('c:\tmp.txt');{打开}
List.SaveToFile('c:\tmp.txt'); {保存}
List.Clear;                     {清空}
List.Free;                      {释放}
end;


//读入字符串
var
List: TStringList;
begin
List := TStringList.Create;
List.CommaText := 'aaa,bbb,ccc,ddd';
//相当于: List.Text := 'aaa' + #13#10 + 'bbb' + #13#10' + 'ccc' + '#13#10' + 'ddd';

ShowMessage(IntToStr(List.Count)); //4
ShowMessage(List[0]); //aaa

List.Free;
end;


//置换分隔符
var
List: TStringList;
begin
List := TStringList.Create;
List.Delimiter := '|';
List.DelimitedText := 'aaa|bbb|ccc|ddd';

ShowMessage(IntToStr(List.Count)); //4
ShowMessage(List[0]); //aaa

List.Free;
end;


//类似的哈希表操作法
var
List: TStringList;
begin
List := TStringList.Create;

List.Add('aaa=111');
List.Add('bbb=222');
List.Add('ccc=333');
List.Add('ddd=444');

ShowMessage(List.Names[1]); //bbb
ShowMessage(List.FromIndex[1]); //222
ShowMessage(List.s['bbb']); //222

//FromIndex 可以赋值:
List.FromIndex[1] := '2';
ShowMessage(List[1]); //bbb=2

//可以通过 s 赋值:
List.s['bbb'] := '22';
ShowMessage(List[1]); //bbb=22

List.Free;
end;


//避免重复值
var
List: TStringList;
begin
List := TStringList.Create;

List.Add('aaa');

List.Sorted := True; //需要先指定排序
List.Duplicates := dupIgnore; //如有重复值则放弃

List.Add('aaa');

ShowMessage(List.Text); //aaa

//Duplicates 有3个可选值:
//dupIgnore: 放弃;
//dupAccept: 结束;
//dupError: 提示错误.

List.Free;
end;


//排序与倒排序
{排序函数}
DescCompareStrings(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := -AnsiCompareText(List[Index1], List[Index2]);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
List: TStringList;
begin
List := TStringList.Create;

List.Add('bbb');
List.Add('ccc');
List.Add('aaa');

//未排序
ShowMessage(List.Text); //bbb ccc aaa

//排序
List.Sort;
ShowMessage(List.Text); //aaa bbb ccc

//倒排序
List.CustomSort(DescCompareStrings); //调用排序函数
ShowMessage(List.Text); //ccc bbb aaa

//假如:
List.Sorted := True;
List.Add('999');
List.Add('000');
List.Add('zzz');
ShowMessage(List.Text); //000 999 aaa bbb ccc zzz
end;

分类:  |  评论: 0 |  引用: 0 |  查看次数: 21

来自万一老师,原帖地址:

http://www.cnblogs.com/del/archive/2011/03/29/1998703.html

 

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses RegularExpressions, msxml;

const
  patternUrl    = 'http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?'; //URL地址
  patternEmail  = '\w+([-+.'']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*'; //Email地址
  patternTel    = '(\(\d{3}\)|\d{3}-)?\d{8}';                      //电话号码
  patternIDCard = '\d{17}[\d|X]|\d{15}';                           //身份证号码


{获取网页源码的函数}
 GetWebPageText(const AUrl: string): string;
begin
  with CoXMLHTTP.Create do begin
    open('Get', AUrl, False, EmptyParam, EmptyParam);
    send(EmptyParam);
    Result := responseText;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  txt,url: string;
  match: TMatch;
begin
  Memo1.Clear;
  url := 'http://www.google.com.hk/search?hl=zh-TW&source=hp&biw=1440&bih=796&q=Email+%E7%94%B5%E8%AF%9D+%E8%BA%AB%E4%BB%BD%E8%AF%81&btnG=Google+%E6%90%9C%E5%B0%8B&aq=f&aqi=&aql=&oq=';
  txt := GetWebPageText(url);

  for match in TRegEx.Matches(txt, patternUrl) do Memo1.Lines.Add(match.);
  Memo1.Lines.Add('--------------------------');
  for match in TRegEx.Matches(txt, patternEmail) do Memo1.Lines.Add(match.);
  Memo1.Lines.Add('--------------------------');
  for match in TRegEx.Matches(txt, patternTel) do Memo1.Lines.Add(match.);
  Memo1.Lines.Add('--------------------------');
  for match in TRegEx.Matches(txt, patternIDCard) do Memo1.Lines.Add(match.);
  Memo1.Lines.Add('--------------------------');
end;

end.
分类:  |  评论: 0 |  引用: 0 |  查看次数: 6

来自万一老师,原帖地址:

http://www.cnblogs.com/del/archive/2011/03/29/1998460.html

TRegExOption = (
  roNone,              //无
  roIgnoreCase,        //不区分大小写
  roMultiLine,         //多行模式; 可使 ^ 和 $ 匹配每个行首或行尾
  roExplicitCapture,   //只捕获指定了名称或编号的子表达式
  roCompiled,          //预编译表达式; 这在反复使用更有效率
  roSingleLine,        //单行模式; 使 . 也可匹配换行符
  roIgnorePatternSpace //忽略注释和未经转义的空白
);


uses RegularExpressions;

{roIgnoreCase}
procedure TForm1.Button1Click(Sender: TObject);
const
  pattern = '[A-Z]+\d+';
  txt = 'AAA1 bbb2 aa11 bb22 A111 B222 AAAA';
var
  match: TMatch;
begin
  Memo1.Clear;
  for match in TRegEx.Matches(txt, pattern, [roIgnoreCase]) do
  begin
    Memo1.Lines.Add(match.);
  end;

  Memo1.Lines.Add('----------');

  for match in TRegEx.Matches(txt, pattern) do
  begin
    Memo1.Lines.Add(match.);
  end;
end;
{********************* AAA1 bbb2 aa11 bb22 A111 B222 ---------- AAA1 A111 B222 **********************}

{roMultiLine}
procedure TForm1.Button2Click(Sender: TObject);
const
  txt = 'Delphi Delphi Delphi'#13#10'Delphi Delphi Delphi';
var
  rStr: string;
begin
  Memo1.Clear;
  {行首}
  rStr := TRegEx.Replace(txt, '^Delphi', '......', [roMultiLine]);
  Memo1.Lines.Add(rStr);
  Memo1.Lines.Add('--------------------');
  rStr := TRegEx.Replace(txt, '^Delphi', '......');
  Memo1.Lines.Add(rStr);

  Memo1.Lines.Add('--------------------');
  {行尾}
  rStr := TRegEx.Replace(txt, 'Delphi$', '......', [roMultiLine]);
  Memo1.Lines.Add(rStr);
  Memo1.Lines.Add('--------------------');
  rStr := TRegEx.Replace(txt, 'Delphi$', '......');
  Memo1.Lines.Add(rStr);
end;
{********************* ...... Delphi Delphi ...... Delphi Delphi -------------------- ...... Delphi Delphi Delphi Delphi Delphi -------------------- Delphi Delphi ...... Delphi Delphi ...... -------------------- Delphi Delphi Delphi Delphi Delphi ...... **********************}

{roExplicitCapture}
procedure TForm1.Button3Click(Sender: TObject);
const
  pattern1 = '([A-Z]+)(\d+)';
  pattern2 = '(?<name1>[A-Z]+)(\d+)';
  pattern3 = '([A-Z]+)(?<3>\d+)';
  txt = 'AA11 BB22';
var
  match: TMatch;
  group: TGroup;
begin
  Memo1.Clear;

  for match in TRegEx.Matches(txt, pattern1, [roExplicitCapture]) do 
  begin
    for group in match.Groups do
    begin
      Memo1.Lines.Add(group.);
    end;
  end;
  Memo1.Lines.Add('--------------------');

  for match in TRegEx.Matches(txt, pattern1) do //此处把 pattern1 换做 pattern2 或 pattern3 均可
  begin
    for group in match.Groups do
    begin
      Memo1.Lines.Add(group.);
    end;
  end;
  Memo1.Lines.Add('--------------------');
  
  for match in TRegEx.Matches(txt, pattern2, [roExplicitCapture]) do 
  begin
    for group in match.Groups do
    begin
      Memo1.Lines.Add(group.);
    end;
  end;
  Memo1.Lines.Add('--------------------');
  
  for match in TRegEx.Matches(txt, pattern3, [roExplicitCapture]) do 
  begin
    for group in match.Groups do
    begin
      Memo1.Lines.Add(group.);
    end;
  end;
end;
{********************* AA11 BB22 -------------------- AA11 AA 11 BB22 BB 22 -------------------- AA11 AA BB22 BB -------------------- AA11 11 BB22 22 **********************}

{roCompiled}
procedure TForm1.Button4Click(Sender: TObject);
var
  reg: TRegEx;
begin
  reg := TRegEx.Create('\d+', [roCompiled]);
  Memo1.Text := reg.Replace('AA11 BB22', '..');  //AA.. BB..
end;

{roSingleLine}
procedure TForm1.Button5Click(Sender: TObject);
const           
  pattern = '[A-Z]{1}.{1}';
  txt = 'A B C D'#13#10'A B C D'#13#10'A B C D';
var
  rStr: string;
begin
  Memo1.Clear;
  rStr := TRegEx.Replace(txt, pattern, '11', [roSingleLine]);
  Memo1.Lines.Add(rStr);
  Memo1.Lines.Add('--------------------');
  
  rStr := TRegEx.Replace(txt, pattern, '11');
  Memo1.Lines.Add(rStr);
end;
{********************* 11111111 11111111 111111D -------------------- 111111D 111111D 111111D **********************}

{roIgnorePatternSpace}
procedure TForm1.Button6Click(Sender: TObject);
var
  rStr: string;
begin
  Memo1.Clear;
  {忽略空白}                                
  rStr := TRegEx.Replace('ABC,A B C,AB C', 'AB C', '...', [roIgnorePatternSpace]);
  Memo1.Lines.Add(rStr); //...,A B C,AB C
  rStr := TRegEx.Replace('ABC,A B C,AB C', 'AB C', '...');
  Memo1.Lines.Add(rStr); //ABC,A B C,...
  Memo1.Lines.Add('--------------------');

  {使用注释}
  rStr := TRegEx.Replace('ABC123', 'ABC#*123', '...', [roIgnorePatternSpace]);
  Memo1.Lines.Add(rStr); //...123
  rStr := TRegEx.Replace('ABC123', 'ABC#*123', '...');
  Memo1.Lines.Add(rStr); //...
end;
{********************* ...,A B C,AB C ABC,A B C,... -------------------- ...123 ... **********************}

{选项集合}
procedure TForm1.Button7Click(Sender: TObject);
const
  pattern = '^Delphi';
  txt = 'DELPHI DELPHI DELPHI'#13#10'delphi delphi delphi';
begin
  Memo1.Text := TRegEx.Replace(txt, pattern, '......', [roIgnoreCase, roMultiLine]);
end;
{********************* ...... DELPHI DELPHI ...... delphi delphi **********************}
分类:  |  评论: 0 |  引用: 0 |  查看次数: 34