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?
LVL 3
light_bulbAsked:
Who is Participating?
 
Geert GOracle 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
 
Geert GOracle 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 GOracle 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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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 GOracle 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
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.