CnPack Forum


 
Subject: [分享]我的兩個腳本
jAmEs_
灌水部部长
Rank: 8Rank: 8



Medal No.1  
UID 886
Digest Posts 0
Credits 1134
Posts 600
点点分 1134
Reading Access 10
Registered 2005-6-5
Location 广东
Status Offline
Post at 2009-3-25 11:28  Profile | Blog | P.M. 
[分享]我的兩個腳本

很簡單的兩個腳本,CnPack的腳本本來是不錯的功能,它為用戶提供一個簡單的平臺,可以實現一些其他CnPack開發人員沒有時間實現,但是可能很實用(面向的人群有局限性)的功能,但是似乎沒有得到多少人的關注,希望能拋磚引玉

SQLToString--把當前選擇的SQL腳本轉換為字符串源碼

{*******************************************************}
{                                                       }
{       Pascal Script Source File                       }
{       Run by RemObjects Pascal Script in CnWizards    }
{                                                       }
{       Generated by CnPack IDE Wizards                 }
{                                                       }
{*******************************************************}
program SQLToString;
uses
  Windows, SysUtils, Classes, CnWizIdeUtils;
function DupeString(const AText: string; ACount: Integer): string;
var
  i: Integer;
begin
  i := ACount;
  while i > 0 do
  begin
    Result := Result + AText;
    i := i - 1;
  end;
end;
var
  Lines: TStringList;
  sTemp: string;
  i, FirstPos: Integer;
  IsDelphi: Boolean;
begin
  //IsDelphi := false;
  //IsDelphi := Compiler <> cnBCB6;
  IsDelphi := WizOptions.CompilerID <> 'CB6';
  //WriteLn(WizOptions.CompilerID);
  //IsDelphi := Pos('Delphi', Application.MainForm.Caption) > 0;
  Lines := TStringList.Create;
  try
    if IdeGetEditorSelectedLines(Lines) and (Lines.Count > 0) then
    begin
      i := 1;
      sTemp := Lines[0];
      while (sTemp[i] = ' ') do
        i := i + 1;
      FirstPos := i;
      for i := 0 to Lines.Count - 1 do
      begin
        sTemp := Lines[i];
        sTemp := Copy(sTemp, FirstPos, Length(sTemp));
        if IsDelphi then
          sTemp := QuotedStr(sTemp + ' ')
        else
        begin
          sTemp := StringReplace(sTemp, '"', '\"', [rfReplaceAll]);
          sTemp := '"' + sTemp + ' \r\n"';
        end;  
        if IsDelphi and (i <> Lines.Count - 1) then
          sTemp := sTemp + ' +';
        Lines[i] := DupeString(' ', FirstPos - 1) + sTemp;
      end;
      IdeSetEditorSelectedLines(Lines);
    end;
  finally
    Lines.Free;
  end;
end.
[i][i][i]
==================================================================
FormatSrc--簡單的格式化源碼(可以對當前選擇的源碼做一些簡單的處理,美化一下,當然各位可以根據自己需求做更多的優化)

{*******************************************************}
{                                                       }
{       Pascal Script Source File                       }
{       Run by RemObjects Pascal Script in CnWizards    }
{                                                       }
{       Generated by CnPack IDE Wizards                 }
{                                                       }
{*******************************************************}
program FormatSrc;
uses
  Windows, SysUtils, Classes, CnWizIdeUtils;
function DupeString(const AText: string; ACount: Integer): string;
var
  i: Integer;
begin
  i := ACount;
  while i > 0 do
  begin
    Result := Result + AText;
    i := i - 1;
  end;
end;
var
  Lines: TStringList;
  sTemp: string;
  i, FirstPos: Integer;
  IsDelphi: Boolean;
begin
  IsDelphi := WizOptions.CompilerID <> 'CB6';
  Lines := TStringList.Create;
  try
    if IdeGetEditorSelectedLines(Lines) and (Lines.Count > 0) then
    begin
      for i := 0 to Lines.Count - 1 do
      begin
        sTemp := Lines[i];
        if sTemp = '' then continue;
        if sTemp[Length(sTemp)] <> ';' then continue;
        while (Length(sTemp) >=2) and (sTemp[Length(sTemp) - 1] = ' ') do
          Delete(sTemp, Length(sTemp) - 1, 1);
        Lines[i] := sTemp;
      end;
      IdeSetEditorSelectedLines(Lines);
    end;
  finally
    Lines.Free;
  end;
end.[i][/i][/i][/i][/i]

[[i] 本帖最後由 jAmEs_ 於 2009-3-26 16:32 編輯 [/i]]


Attachment: [SQLToString] SQLToString.pas (2009-3-26 16:32, 1.87 K)
Download count 440


Attachment: [FormatSrc] FormatSrc.pas (2009-3-26 16:32, 1.42 K)
Download count 385
Top
Passion (LiuXiao)
管理员
Rank: 9Rank: 9Rank: 9


UID 359
Digest Posts 19
Credits 6804
Posts 3577
点点分 6804
Reading Access 102
Registered 2004-3-28
Status Offline
Post at 2009-3-26 15:36  Profile | Blog | P.M. 
写的不错。不过发帖的时候应该勾上禁用 Discuz! 代碼,否则[i]后面的东西都会变成斜体。
Top
jAmEs_
灌水部部长
Rank: 8Rank: 8



Medal No.1  
UID 886
Digest Posts 0
Credits 1134
Posts 600
点点分 1134
Reading Access 10
Registered 2005-6-5
Location 广东
Status Offline
Post at 2009-4-6 10:15  Profile | Blog | P.M. 
FormEventDemo.pas改進版

FormEventDemo改進代碼,因ADO沒有編譯進CnWiz中,所以無法直接使用ADO類,通過類名稱檢測,并通過SetPropValue來設置屬性。大家可以依照這個方法處理其他屬性。


Attachment: FormEventDemo.pas (2009-4-6 10:15, 1.82 K)
Download count 389
Top
Passion (LiuXiao)
管理员
Rank: 9Rank: 9Rank: 9


UID 359
Digest Posts 19
Credits 6804
Posts 3577
点点分 6804
Reading Access 102
Registered 2004-3-28
Status Offline
Post at 2009-4-6 10:35  Profile | Blog | P.M. 
好的,这个改进的Demo我们合并入专家包的例子中。谢谢。
Top
jAmEs_
灌水部部长
Rank: 8Rank: 8



Medal No.1  
UID 886
Digest Posts 0
Credits 1134
Posts 600
点点分 1134
Reading Access 10
Registered 2005-6-5
Location 广东
Status Offline
Post at 2009-4-6 10:51  Profile | Blog | P.M. 
對了,上面代碼,怎么好像BCB不行的?
Top
jAmEs_
灌水部部长
Rank: 8Rank: 8



Medal No.1  
UID 886
Digest Posts 0
Credits 1134
Posts 600
点点分 1134
Reading Access 10
Registered 2005-6-5
Location 广东
Status Offline
Post at 2009-4-6 10:52  Profile | Blog | P.M. 
繼續改進

這個根據不同的事件類型,處理不同,不知道代碼是否正確,執行沒有出錯。


Attachment: FormEventDemo.pas (2009-4-6 10:52, 2.01 K)
Download count 376
Top
jAmEs_
灌水部部长
Rank: 8Rank: 8



Medal No.1  
UID 886
Digest Posts 0
Credits 1134
Posts 600
点点分 1134
Reading Access 10
Registered 2005-6-5
Location 广东
Status Offline
Post at 2009-4-15 20:56  Profile | Blog | P.M. 
下面红色字部分哪里错了?如果[color=black]FormEvent.NotifyType = fetSaving,要怎么取得保存的窗口对象?[/color]

{*******************************************************}
{                                                       }
{       Pascal Script Source File                       }
{       Run by RemObjects Pascal Script in CnWizards    }
{                                                       }
{       Generated by CnPack IDE Wizards                 }
{                                                       }
{*******************************************************}
program FormEventDemo;
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ScriptEvent, TypInfo, ExtCtrls, CnDebug;
// To use this demo, you must add it to script list and check
// "smFormEditorNotify -> fetComponentCreated" item.
var
  FormEvent: TCnScriptFormEditorNotify;
  Comp, CompRoot: TComponent;
  i: Integer;
begin
  if (Event <> nil) and (Event.Mode = smFormEditorNotify) and
    (Event is TCnScriptFormEditorNotify) then
  begin
    FormEvent := TCnScriptFormEditorNotify(Event);
    CnDebugger.LogMsg(IntToStr(Ord(FormEvent.NotifyType)));
[color=red]    if FormEvent.NotifyType = fetSaving then
    begin
      if Assigned(FormEvent.FormEditor) then
        CnDebugger.LogMsg('FormEvent.FormEditor');
      if Assigned(FormEvent.Component) then
      begin
        CnDebugger.LogMsg('FormEvent');
        CompRoot := FormEvent.Component;
        CnDebugger.LogMsg(CompRoot.Name);
        if Assigned(CompRoot) then
        begin
          for i := 0 to CompRoot.ComponentCount - 1 do
          begin
            Comp := CompRoot.Components[i];
            if Comp.ClassName = 'TADOConnection' then
            begin
              if GetPropValue(Comp, 'Connected', True) then
                SetPropValue(Comp, 'Connected', False);
            end;
          end;
        end;
      end;
[/color]    end
    else if FormEvent.NotifyType = fetComponentCreated then
    begin
      if FormEvent.Component <> nil then
      begin
        Comp := FormEvent.Component;
        if Comp is TCustomMemo then
        begin
          if TCustomMemo(Comp).Lines.Text = Comp.Name then
          begin
            TCustomMemo(Comp).Lines.Clear;
            if Comp is TMemo then
              TMemo(Comp).ScrollBars := ssBoth;
          end;
        end
        else if Comp is TCustomEdit then
        begin
          if TCustomEdit(Comp).Text = Comp.Name then
            TCustomEdit(Comp).Text := '';
        end
        else if Comp is TPanel then
        begin
          if TPanel(Comp).Caption = Comp.Name then
            TPanel(Comp).Caption := '';
        end
        else if Comp is TComboBox then
        begin
          if TComboBox(Comp).Text = Comp.Name then
            TComboBox(Comp).Text := '';
        end
        else if Comp.ClassName = 'TADOConnection' then
        begin
          SetPropValue(Comp, 'LoginPrompt', False);
          SetPropValue(Comp, 'ConnectionString', '');
        end;
      end;
    end;
  end;
end.
Top
 




All times are GMT++8, the time now is 2024-9-8 09:12

    本论坛支付平台由支付宝提供
携手打造安全诚信的交易社区 Powered by Discuz! 5.0.0  © 2001-2006 Comsenz Inc.
Processed in 0.007550 second(s), 8 queries , Gzip enabled

Clear Cookies - Contact Us - CnPack Website - Archiver - WAP