Solved

Old resources in Delphi apps

Posted on 2000-02-15
3
179 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now