Old resources in Delphi apps

Is it possible to use dialog boxes created with Resource Workshop (Bundled with Borland C++ 4.5) in a Delphi 3.0 application, and if so how do i call them ?.

         Thanks Paul.
LVL 1
pr_wainwrightAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
rwilson032697Connect With a Mentor Commented:
Hi Paul,

This is from my PAQs from an answer by simonet:

Creating and handling a form from resource is pretty much like working with plain C: you have to create the dialog (the window) from the resource, display it, write the main loop, the handler for each message, etc. The program above does it:

{$ifndef _DEBUG}
{$O+,D-,L-,M-,Y-,R-,Q-,U-,C-}
{$endif}
program Window1;
{ Standard Windows API application written in Object Pascal }

uses
Windows, messages, ShellApi;

{$R myres.res}

const
AppName = 'APIWindow';

function CreateControls(hParent : HWnd) : boolean; forward;


function ControlsProc(Window: HWnd; AMessage, WParam, LParam: Longint): Longint; stdcall; export;
begin
  ControlsProc := 0;
  if AMessage=WM_COMMAND then
  begin
     case WParam of
        IDOK : SendMessage(GetParent(Window), WM_CLOSE, 0, 0);
     end;
  end;
end;

{
  WindowProc is the main message handler for the window.
  Messages that are not handled by this function must be passed to
  the default message handler by calling DefWindowProc()
}
function WindowProc(Window: HWnd; AMessage, WParam, LParam: Longint): Longint; stdcall; export;
begin
  WindowProc := 0;
case AMessage of
WM_DESTROY : begin
      PostQuitMessage(0);
      Exit;
      end;
     WM_CLOSE : if MessageBox(Window, 'Are you sure you want to quit?', pchar(AppName), MB_ICONQUESTION + MB_YESNO)=IDYES then
           DestroyWindow(Window)
        else
           Exit;
     WM_COMMAND : case LoWord(WParam) of
           102, 451 : SendMessage(Window, WM_CLOSE, 0, 0);
           901 : ShellAbout(Window, pchar(AppName), 'by Alex Simonetti Abreu', LoadIcon(hInstance, makeintresource(1)));
        end;
end;
  WindowProc := DefWindowProc(Window, AMessage, WParam, LParam);
end;

{ Register the Window Class }
function WinRegister: Boolean;
var
  WindowClass: TWndClass;
begin
  WindowClass.Style := cs_hRedraw or cs_vRedraw;
  WindowClass.lpfnWndProc := @WindowProc;
  WindowClass.cbClsExtra := 0;
  WindowClass.cbWndExtra := 0;
  WindowClass.hInstance := HInstance;
  WindowClass.hIcon := LoadIcon(0, idi_Application);
  WindowClass.hCursor := LoadCursor(0, idc_Arrow);
  WindowClass.hbrBackground := HBrush(Color_Window);
  WindowClass.lpszMenuName := makeintresource(1);
  WindowClass.lpszClassName := AppName;
  Result := RegisterClass(WindowClass) <> 0;
end;

{ Create the Window Class }
function WinCreate: HWnd;
var
  hWindow: HWnd;
begin
  hWindow := CreateWindow(AppName, 'Object Pascal Window',
  ws_OverlappedWindow, CW_USEDEFAULT,  CW_USEDEFAULT,
  400, 320, 0, 0, HInstance, nil);
  if hWindow <> 0 then
  begin
     CreateControls(hwindow);
     ShowWindow(hWindow, cmdShow);
     UpdateWindow(hWindow);
  end;
  Result := hWindow;
end;


function CreateControls(hParent : HWnd) : boolean;
begin
  Result := CreateDialog(hInstance, MakeIntResource(3), hParent, @ControlsProc)<>0;
end;


{ The routine below is identical to
  the C\C++ declaration :


  int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)

  In Delphi, there's no need for a WinMain function. The begin..end of the main
  program is the entry point.

  If an exit result code is necessary, pass the code to the Halt() procedure.
}

var
  AMessage: TMsg;
  hWindow: HWnd;
begin
  if not WinRegister then begin
     MessageBox(0, 'Register failed', nil, mb_Ok);
     Exit;
  end;
  hWindow := WinCreate;
  if hWindow = 0 then begin
  MessageBox(0, 'WinCreate failed', nil, mb_Ok);
  Exit;
  end;
  while GetMessage(AMessage, 0, 0, 0) do
  begin
     TranslateMessage(AMessage);
     DispatchMessage(AMessage);
  end;
  Halt(AMessage.wParam);
end.

********** EOF

Note the CreateControls function above. It takes a the handle for the window that is create and then it creates the Form (all the controls inside the window).  This form was in "myres.res" and it's resource identifier is 3.

Also note the ControlsProc and WindowsProc functions in the program. They are responsible for the Main loop. They handle the messages sent by the Controls and the Window, respectively. The controls are identified by their number, defined at the moment you created the resource.

A great book for this subject is Delphi Unleashed, by Charlie Calvert.

You can also look at this MSDN area:

http://msdn.microsoft.com/library/sdkdoc/winui/dlgboxes_7foz.htm 


Cheers,

Raymond.
0
 
simonetCommented:
Don't forget to make sure MYRES.RES is a 32-bit resource file, rather than a 16-bit resource file. Resource Workshop can handle both formats, so you better make sure.

Alex
0
 
pr_wainwrightAuthor Commented:
Thanks, I was unaware Delphi could be
used like this .

    Paul.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.