• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1480
  • Last Modified:

Find Component as TWincontrol at runtime

Hi,

I need to run a procedure, that takes a control for the first parameter.  The control is created at runtime.

Here's my procedure:

procedure SetElevationRequiredState(aControl: TWinControl; Required: Boolean);
var
  lRequired: Integer;
begin
  lRequired := Integer(Required);
  SendMessage(aControl.Handle, BCM_SETSHIELD, 0, lRequired);
end;


Here's how I'm calling it it my program:

  SetElevationRequiredState( TWinControl(FindComponent('G4_Button7')), True);


When I run it in the debugger, I get something like:  "First chance exception at $7C812AEB. Exception class EAccessViolation with message 'Access violation at address 004BDF0F in module 'runtime.exe'. Read of address 000001B4'. Process runtime.exe (3224)"

How can I run this procedure using the control as the first parameter?
0
light_bulb
Asked:
light_bulb
  • 4
  • 2
1 Solution
 
Geert GruwezOracle dbaCommented:
you did't check for nil

procedure SetElevationRequiredState(aControl: TWinControl; Required: Boolean);
var
  lRequired: Integer;
begin
  if aControl <> nil then
  begin
    lRequired := Integer(Required);
    SendMessage(aControl.Handle, BCM_SETSHIELD, 0, lRequired);
  end;
end;
0
 
Geert GruwezOracle dbaCommented:
you could also directly cast the boolean
or use a const array

procedure SetElevationRequiredState(aControl: TWinControl; Required: Boolean);
begin
  if aControl <> nil then
    SendMessage(aControl.Handle, BCM_SETSHIELD, 0, Integer(Required));
end;

procedure SetElevationRequiredState(aControl: TWinControl; Required: Boolean);
const IRequired: array[Boolean] of integer = (0, 1);
begin
  if aControl <> nil then
    SendMessage(aControl.Handle, BCM_SETSHIELD, 0, IRequred[Required]);
end;
0
 
Geert GruwezOracle dbaCommented:
and are you sure the button is allways a TWinControl
(not a TSpeedButton) ?

procedure TForm1.Test;
var aComponent: TComponent;
begin
  aComponent := FindComponent('G4_Button7');
  if aComponent is TWinControl then
    SetElevationRequiredState(TWinControl(aComponent), True);
end;
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
light_bulbAuthor Commented:
I think your third answer may at least get me on the right track here.  The control I wanted to send the message to does not descend from TWinControl, thus my problem: It doesn't have a handle and can't receive messages directly.

What I'm trying to do here is send a message that will enable the UAC shield for a control in Vista.

I wonder if there's a way to send a message to such a control?
0
 
Geert GruwezOracle dbaCommented:
it depends
if it's a Delphi descendant of TControl you can do

aControl.Perform(Msg: Cardinal; WParam, LParam: Longint): Longint;

0
 
light_bulbAuthor Commented:
It not, apparently, I can't use the Perform method with it.

Thanks for your help.  I can see with this custom component that it not going to do what I want.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now