Jump to content
Ian Branch

convert Procedure to a Unit??

Recommended Posts

Hi Team,

Before I stumble blindly into something I have no knowledge of.  Again.. ;-)

I have a very large procedure in an App that is called infrequently.

I would like to extract it into a separate Delphi Unit that can be created/used when/if required.

Is there a mechanism/tool to pick up the procedure and create a Unit out of it or embed it into a Unit?

 

Regards & TIA,

Ian

Share this post


Link to post

I do this often, but manually: I create new unit, move main method and all dependencies (sometimes compile is needed to discover all dependencies). I also take advantage of this to improve naming, code refactor, also usually add enums and new types.

Then I use compile or Find in files and replace to make sure all old calls are updated with new unit in uses clause.

 

I do this with a very messy code and it ends with nicely organized unit.

 

 

Share this post


Link to post
Guest

hi @Ian Branch

 

exist a tools very good for that, unfortunatelly, you broke it!

  • your brain!

 

said that, let's go ahead!

  • for know the dependence of a object/class/var/etc.. just "put the mouse over it" - then, you'll know what unit it belong!
  • add a empty "unit" in your project - pay attention on skeleton default
  • add the procedure header on "interface" secction"
  • Copy and Paste your procedure in this new unit on "implementation" seccion!
  • now, analize it and divide it in portions!!!
unit uThinkFirstWriteLater;
//
interface
  // uses  == add your units for each dependence
  // type 
  // procedure / function header to export for anothers units
  // var
  // const
implementation
  // uses  == add your units for each dependence
  // type
  // var
  // const
  // procedure / function body
.end

 

Edited by Guest

Share this post


Link to post
7 hours ago, Ian Branch said:

I would like to extract it into a separate Delphi Unit that can be created/used when/if required.

Do you mean a Class? Because a Unit cannot be created when required.

Share this post


Link to post
1 hour ago, Uwe Raabe said:

Because a Unit cannot be created when required.

Oh.  I did not know that.

Kinda defeats my intended/desired purpose then.

Oh well.  Back to the drawing board.

Thanks to all for your input.

 

Regards,

Ian

Share this post


Link to post
2 hours ago, Uwe Raabe said:

Do you mean a Class? Because a Unit cannot be created when required.

It seems I missed the point of the question... where can I read more about this?

Share this post


Link to post
2 hours ago, Mike Torrettinni said:

It seems I missed the point of the question... where can I read more about this?

The documentation explains what a unit is in Delphi 

Share this post


Link to post

Could it be you wanting namespace?    mainunithelpers.helpme   in case you have other helpme procedures.

  

I put drawing procedures in a routines.pas by adding arguments to the procedure being moved.  The 

arguments use Timage, TPanel or TForm to reduce dependences.   You simply pass the form you are working 

in to procedure into your routines.pas 

Unit routines
uses forms;
interface

//  Procedure DrawBackGroundBMP(const aWidth, aHeight: Integer; anImage: TImage); // TImage is best passing canvas is tough 
//  procedure DrawthingsEX(athingList:Tstrings; aForm: TForm);     //need to add forms to routines.pas 
//  procedure DrawThings( aPanel:TPanel; Const FileandPathName:String;    
//                          aThingList:Tstrings; aForm: TForm);   // A runtime control parent is assigned to aPanel and the controls onmousedown is assigned Aform.onMousedown

  procedure ShowBalloonTip(Control: TwinControl; Icon: integer; Title: pchar; Text: PWideChar;
             BackCL, TextCL: TColor);
 Implementation
 
 procedure ShowBalloonTip(Control: TwinControl; Icon: integer; Title: pchar; Text: PWideChar;
BackCL, TextCL: TColor);
const
  TOOLTIPS_CLASS = 'tooltips_class32';
  TTS_ALWAYSTIP = $01;
  TTS_NOPREFIX = $02;
  TTS_BALLOON = $40;
  TTF_SUBCLASS = $0010;
  TTF_TRANSPARENT = $0100;
  TTF_CENTERTIP = $0002;
  TTM_ADDTOOL = $0400 + 50;
  WM_User = $01;
  TTM_SETTITLE = (WM_USER + 32);
  ICC_WIN95_CLASSES = $000000FF;
type
  TOOLINFO = packed record
    cbSize: Integer;
    uFlags: Integer;
    hwnd: THandle;
    uId: Integer;
    rect: TRect;
    hinst: THandle;
    lpszText: PWideChar;
    lParam: Integer;
  end;
var
  hWndTip: THandle;
  ti: TOOLINFO;
  hWnd: THandle;
begin
  hWnd    := Control.Handle;
  hWndTip := CreateWindow(TOOLTIPS_CLASS, nil,
    WS_POPUP or TTS_NOPREFIX or TTS_BALLOON or TTS_ALWAYSTIP,
    0, 0, 0, 0, hWnd, 0, HInstance, nil);
  if hWndTip <> 0 then
  begin
    SetWindowPos(hWndTip, HWND_TOPMOST, 0, 0, 0, 0,
      SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
    ti.cbSize := SizeOf(ti);
    ti.uFlags := TTF_CENTERTIP or TTF_TRANSPARENT or TTF_SUBCLASS;
    ti.hwnd := hWnd;
    ti.lpszText := Text;
    Windows.GetClientRect(hWnd, ti.rect);
    SendMessage(hWndTip, TTM_SETTIPBKCOLOR, BackCL, 0);
    SendMessage(hWndTip, TTM_SETTIPTEXTCOLOR, TextCL, 0);
    SendMessage(hWndTip, TTM_ADDTOOL, 1, Integer(@ti));
    SendMessage(hWndTip, TTM_SETTITLE, Icon mod 4, Integer(Title));
  end;
end;

    

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×