Solved

Old resources in Delphi apps

Posted on 2000-02-15
3
184 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:pr_wainwright
3 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 100 total points
ID: 2524599
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
 
LVL 15

Expert Comment

by:simonet
ID: 2525038
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
 
LVL 1

Author Comment

by:pr_wainwright
ID: 2527140
Thanks, I was unaware Delphi could be
used like this .

    Paul.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question