Delphi RSS Resources, Delphi Components, Delphi Sites, Articles, Dynamic XML Feeds, Tutorials, Sources
 
 
 
Google
 
Web delphirss.com
| Server-Scripts.com | Informations for JAVA | Informations for PHP | SEO Web Links | Borland Delphi
 

Resource

Load bitmaps from .RES-file
.RES file containing multiple bitmaps
Changing a forms icon at runtime
Load a BMP in a EXE file
Loading a custom cursor
Associate an Icon with a component
Including a wave file in a Delphi EXE
Loading a bitmap from .res without losing palette
Custom cursor
Resource problems encountered with TTabbedNotebook and TNotebook
Adding new cursor to my application
Edit field cursor


Load bitmaps from .RES-file

Question

I have a .RES file that contains bitmaps. How do I get an image on a
form to display them?

Answer

A:
Be sure to bind it to the EXE by doing a {$R RESFILENAME.RES} then you can
use the LoadBitmap.

 TImage.Picture.Bitmap.Handle := LoadBitmap( Handle, 'BITMAPNAMEHERE' )


A:
A TPicture is a container of Bitmaps, icons and Meta's so..
TImage(grafCntrl).Picture.Bitmap.Handle := LoadBitmap(HInstance,'BITMAP_NAME');

A:
You are using the Picture instead of the Picture.Bitmap.
With image1.Picture.Bitmap do
begin
	ReleaseHandle;
	Handle:=LoadBitmap(HInstance,'BITMAP_NAME_IN_RESOURCE');        
end;

A:
Put the following compiler directive in the implementation section of your
unit.
{$R ResourceFilename}

Then in the OnCreate handler of your form that contains the speedbuttons do
the following:

 SpeedButton.Glyph.Handle := LoadBitmap(HInstance, 'BITMAP_NAME');

or use '#999' where 999 is the numeric resource id if you used numbers instead
of a string to identify bitmap resources in your .res file. The bitmaps for
each of the button states must be in the same bitmap resource.

A:
Try something like the follow:

procedure TForm1.Button1Click(Sender: TObject);
var
  hIconHandle : HIcon;
  Icon        : TIcon;
  aIconFile   : array [0..255] of char;
  nIconNumber : word;
begin
  StrPCopy( aIconFile, ParamStr( 0 ) );
  nIconNumber := 0;

  { Extract the icon specified by aIconFile and nIconNumber }
   hIconHandle := ExtractIcon( hInstance, aIconFile, nIconNumber );

   if ( hIconHandle < 2 )
     then
       hIconHandle := LoadIcon( 0, idi_hand );

   Icon := TIcon.Create;
   Icon.Handle := hIconHandle;

   { Draw the extracted icon }
    with Image1.Canvas do
      begin
        Pen.Color := clSilver;
        Pen.Style := psSolid;

        Brush.Color := clSilver;
        Brush.Style := bsSolid;

        Rectangle( 0, 0, 32, 32 );
        Draw( 0, 0, Icon );
      end
end;

A:
Instead of loading the file directly to the glyph, create a TBitmap
object, load the bitmap resource to the handle of that TBitmap, then
assign that TBitmap to the glyph.

.RES file containing multiple bitmaps

Question

I have a .res file that has several bitmaps in it. I'd like to use some of the bitmaps for the TSpeedButtons of a component, could anybody show me a simple sample code for that?

Answer

A:
Put the following compiler directive in the implementation section of your
unit.
{$R ResourceFilename}

Then in the OnCreate handler of your form that contains the speedbuttons do
the following:

 SpeedButton.Glyph.Handle := LoadBitmap(HInstance, 'BITMAP_NAME');

or use '#999' where 999 is the numeric resource id if you used numbers instead
of a string to identify bitmap resources in your .res file. The bitmaps for
each of the button states must be in the same bitmap resource.


Changing a forms icon at runtime

Question

I have an app that checks for new incoming files, which is basically a form
with a timer on it. When the timed event happens, if new files are found I want
to change the forms icons to something else.
I've tried the LoadFromFile routine, even assigning it from another form that's
hidden. Nothing changes the icon at runtime.
Also, what's the best way of storing multiple icons in the exe?  If it's a
resource file, how can I read the particular Icon I want?

Answer

A:
Set the Application.Icon property.
Store them in a Resource file then get them as needed using the WinAPI
LoadIcon function:  IE:

  if FileExists( 'Some Darn File Name Here' ) then  begin
    Application.Icon.Handle := LoadIcon( hInstance, 'ICON_NAM' );
 end;

 etc..

Note: The 'ICONNAME' is a PChar rerfereing to the Icon's Resource ID.

Load a BMP in a EXE file

Question

How i can load in an TImage a BMP stored as bytes at the end of a file EXE?

Answer

A:
Use Resource Workshop (workshop.exe) to put your bitmap into a
Resource file (example: BITMAP.RES) and give it a name (example:
Bitmap1) and save the resource file in your source directory.

Add this line to the unit that will load the bitmap:

{$R BITMAP.RES}

This line will tell Delphi to link the resouce file into you program
for you.

In this example I use MyImage as the name of the TImage component
that was placed on the form. The following lines show an example of
how to load the bitmap at runtime:

var
  ImageName : STRING[10];
...
MyImage.AutoSize := TRUE;
ImageName := 'Bitmap1' + #0;
MyImage.Picture.Bitmap.Handle := LoadBitmap(hInstance, @ImageName[1]);

Loading a custom cursor

Question

When I define a cursor with the image editor I cannot get my program
to use it. Even though I exactly the code used in the help-file. It is
probably very simple.

Here is the code I use to load the cursor.

procedure TForm1.FormCreate(Sender: TObject);
begin
  Screen.Cursors [crMikesCursor] := LoadCursor (form1.handle, 'MIKE');
  Cursor := crMikesCursor;
end;

Answer

Screen.Cursors [crMikesCursor] := LoadCursor (form1.handle, 'MIKE');

	use hInstance instead of form1.handle

A:
hInstance is defined in the System unit for you and is assigned by the
Windows environment when your application or DLL is created. When
you created your own variable by the same name, Delphi used the one
you declared locally to the FormCreate method which was probably not
assigned a valid value. For more details look in the on-line help
under hInstance.

Associate an Icon with a component

Question

I am creating my own components. Does anyone have an idea how to associate an
icon with a component?

Answer

A:
If you are refering to the pic on the comp palette, you create a bitmap in a
resource file (ext .dcr for delphi component resource). It should be 24
pixels square

A:
    See Adding Palette Bitmaps on page 77 of the Component
    Writer's Guide.

    IMPORTANT NOTE
    ================

    However, note the following misleading sentence in the
    middle of that page:

        "The resource names are not case-sensitive, but by
        convention, they are usually in upper case letters".

    In  my experience there's a particularly good reason for the
    convention:

    IT DOESN'T WORK IF YOU DON'T USE UPPER CASE.

Including a wave file in a Delphi EXE

Question

I would like to include a Wave file with my EXE. However, I don't want to
distribute the wave file as a separate file. Is it possible to include this
in my EXE, and call it from my Delphi program? If so, how?

Answer

A:
First,you can create foo.rc which is plain text file.
1.wav ,2.wav are example. Please write your waves name here.

--- foo.rc
// WAVES
//
WAVE1 WAVE PRELOAD FIXED PURE "1.WAV"
WAVE2 WAVE PRELOAD FIXED PURE "2.WAV"
WAVE3 WAVE PRELOAD FIXED PURE "3.WAV"
WAVE4 WAVE PRELOAD FIXED PURE "4.WAV"

The first words are wave resource names. You must remember these names.
The last words are wave file names.

--- foo.rc

Next,run delphi\bin\brcc.exe for making foo.res.

Last,you must the following code in your source code:

--- unit1.pas (your code)
Unit1
 :
 :
implementation

{$R foo.res}    (* your waves resources here *)
 :
 :
--- unit1.pas (your code)

  Then,I show the code how to load the wave resources.

# How to load

      hrsr := FindResource(hinst, resname, 'WAVE');
      if hrsr = 0 then begin
          (* ERROR *)
      end
      else begin
        wave[num].hglb := LoadResource(hinst, hrsr) ;
        wave[num].lpstr := LockResource(wave[num].hglb) ;
        if wave[num].lpstr <> nil then
         wave[num].load := True
      end;

  (notes)
    resname : 'WAVE1','WAVE2',... wave resource name
    hinst   : value of System.HInstance
    num     : wave number. You can manage waves by number. 

# How to play waves

   if wave[num].load = True then begin
       sndPlaySound(wave[num].lpstr, SND_ASYNC or SND_MEMORY)
   end;


A:
You will need a resource editor such as Resource Workshop to load the
.WAV file into a resource file. In the example below, I called my
resource type "SOUND", but you could call it "WAV" or whatever you 
like. I think it needs to be all uppercase, as does the resource name 
itself.

BTW -- don't use the resource file made by Delphi. Make a new one, 
and add a resource statement for it in your file.

{$R SOUNDS.RES}

procedure PlayMyNoise;
var
  rhMyNoise : THandle;
  pMyNoise : Pointer;
  hMyNoise : THandle;

begin
  rhMyNoise := FindResource(HInstance, 'MYNOISE', 'SOUND');
  hMyNoise := LoadResource(HInstance, rhMyNoise);
  pMyNoise := LockResource(hMyNoise);
  sndPlaySound(pMyNoise, SND_SYNC or SND_MEMORY);
  FreeResource(hMyNoise);
end;

A:
If you have Resource Workshop, you're in luck. You
need to create a new resource type and load in the wave file and save the 
resource with a filename that's different from your project. Then in your unit, 
following the {R *.DFM) add the line {R WHATEVERYOUNAMEDYOURFILE.RES}. Then 
comes the fun part of how to use it. I worked on this a long time but finally 
got it right. 

Following is a short unit that will load a wave file as a resource and play it 
for as long as you hold your mousebutton down. Hope it helps!

Syl

unit Wavemain;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, Buttons, MMSystem;

type
  TForm1 = class(TForm)
    SpeedButton1: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure SpeedButton1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure SpeedButton1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    p : pointer;
    HResource : THandle;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
{$R LAUGH.RES}

procedure TForm1.FormCreate(Sender: TObject);
var
  RName : array[0..20] of char;
  RType : array[0..20] of char;
begin
  StrPCopy(RName,'LAUGH');     {the name you gave the resource in Workshop}
  StrPCopy(RType,'WAVSOUND');  {the name you gave the new 'type' in Workshop}
  try
    HResource := LoadResource(HInstance,(FindResource(HInstance,RName,RType)));
    P := LockResource(HResource);
  except
    GlobalFree(HResource);
  end;
end;

procedure TForm1.SpeedButton1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  SndPlaySound(P,snd_Async or snd_Loop or snd_Memory);  
end;

procedure TForm1.SpeedButton1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  SndPlaySound(nil,0);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  try
    GlobalUnlock(HResource);
  except
    abort;
  end;
  try
    GlobalFree(HResource);
  except
    abort;
  end;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SndPlaySound(nil,0);
end;

end.

Loading a bitmap from .res without losing palette

Question

Loading a bitmap from .res without losing palette?

Answer

A:
I'm going to manipulate your code a little bit in my explanation. Here goes:

procedure loadgraphic(naam:string);
var
  { I've moved these in here, so they exist only during the lifetime of the
    procedure. }
  HResInfo: THandle;
  BMF: TBitmapFileHeader;
  MemHandle: THandle;
  Stream: TMemoryStream;
  ResPtr: PByte;
  ResSize: Longint;
  null:array [0..8] of char;
  
begin
  { In this first part, you are retrieving the bitmap from the resource.
    The bitmap that you retrieve is almost, but not quite, the same as a
    .BMP file (complete with palette information). }

  strpcopy (null, naam);
  HResInfo := FindResource(HInstance, null, RT_Bitmap);
  ResSize := SizeofResource(HInstance, HResInfo);
  MemHandle := LoadResource(HInstance, HResInfo);
  ResPtr := LockResource(MemHandle);

  { Think of a MemoryStream almost as a "File" that exists in memory.
    With a Stream, you can treat either the same way! }

  Stream := TMemoryStream.Create;

  try
    Stream.SetSize(ResSize + SizeOf(BMF));

    { Next, you effectively create a .BMP file in memory by first writing
      the header (missing from the resource, so you add it)... }
    BMF.bfType := $4D42;
    Stream.Write(BMF, SizeOf(BMF));

    { Then the data from the resource. Now the stream contains a .BMP file }
    Stream.Write(ResPtr^, ResSize);

    { So you point to the beginning of the stream... }
    Stream.Seek(0, 0);

    { ...and let Delphi's TBitmap load it in }
    Bitmap:=tbitmap.create;
    Bitmap.LoadFromStream(Stream);

    { At this point, you are done with the stream and the resource. }
  finally
    Stream.Free;
  end;
  FreeResource(MemHandle);
end;

Custom cursor

Question

Could someone send me a sample program that uses a custom cursor...

Answer

A:
const
  SpinC_1    = MakeIntResource(10005);
  SpinC_2    = MakeIntResource(10006);
  SpinC_3    = MakeIntResource(10007);
  SpinC_4    = MakeIntResource(10008);

initialization
  Screen.Cursors[101] := LoadCursor(HInstance, SpinC_1);
  Screen.Cursors[102] := LoadCursor(HInstance, SpinC_2);
  Screen.Cursors[103] := LoadCursor(HInstance, SpinC_3);
  Screen.Cursors[104] := LoadCursor(HInstance, SpinC_4);
end;

to use them in side your code just:

  Screen.Cursor:=101; { or which one you want }

Resource problems encountered with TTabbedNotebook and TNotebook

Question

Everybody knows about the resource problems encountered with TTabbedNotebook
and TNotebook. How can the be overcome?

Answer

A:
I use the following code. I have tested it by checking which TWinControls
have handles allocated at run time, and it does deallocate handles for
controls on inactive pages.
However, If I load ~6 forms, all with TTabbedNotebooks, I still get 'Unable
to create window' errors. Before this code, I could only open 2 windows. (I
have LOTS of controls on each page)
Does anybody know how many window handles are normally available? Also,
anybody have a way to find out how many are available/used ??

{in the OnChange event of TTabbedNotebook}

if AllowChange then
begin
        {Deallocate unneccessary window handles}
	with TTabbedNotebook(Sender) do
        	if PageIndex <> NewTab then
		begin
			LockWindowUpdate(Handle);
			THintWindow(Pages.Objects[PageIndex]).ReleaseHandle;
			TWinControl(Pages.Objects[NewTab]).HandleNeeded;
			LockWindowUpdate(0);

		end;
end;

Adding new cursor to my application

Question

How i can add a new cursor to my application ?

Answer

In order to simplify this explanation I will use LoadCursor instead of
CreateCursor. The main difference is with LoadCursor you must have a
previous defined cursor inside a .RES file (with Resource Workshop you
can create it), and with CreateCursor you must define the bitmap in
RunTime.
The code looks like this:

const
  MyCursor = 5;

procedure TMyForm.FormCreate(Sender:TObject);
begin
  Screen.Cursors[MyCursor]:=LoadCursor( Hinstance,
'NameOfCursorInResourceFile' );
  Cursor := MyCursor;
end;

Hinstance is a predefined variable containing the application instance
in Windows. Simply write Hinstance and Delphi will understand you.

Be sure to bind the resource file containing the cursor to the EXE by
doing a {$R FILENAME.RES} in order to use the LoadCursor.

Edit field cursor

Question

Does any one knows how to change the cursor (not mouse cursor) in the edit
fields to a custom cursor.

Answer

1. Design a cursor with the image editor. Make it part of a .RES
file and give the cursor resource a name.
2. Include {$R filename} in the implementation part of your unit,
where filename is the name of the file you created in step one. This
includes your cursor resource in the unit and finally in the .EXE.
2. Define a constant that identifies your cursor, for example
crMyCursor = 5.
3. Load the cursor into the Cursors property of the Screen component
with the following API function:
  Screen.Cursors[crMoveExpression] :=
    LoadCursor(HInstance, 'CURSORRESOURCE');

where CURSORRESOURCE is the name of the cursor resource you assigned
with the image editor. The call to LoadCursor will make the cursor
available to your program.
4. Assign the constant (crMyCursor, or whatever you named it) to the
Cursor property of the controls you want to be using the cursor.
5. Watch the result!









© DelphiRSS.com. All Rights Reserved.