Control the AutoPlay feature
DelphiFAQ Home Search:

Control the AutoPlay feature

 

comments3 comments. Current rating: 5 stars (1 votes). Leave comments and/ or rate it.

You know how to stop Windows' [CD-ROM] AutoPlay from occurring by holding SHIFT or by changing Windows settings.

Here's how to detect whether an AutoPlay is about to occur from your application and then either allowing or stopping it.

We're going to ask Windows to send us a message when the AutoPlay is about to occur. In order to catch this message, first of all we have to override our default Windows message handler -- "WndProc()."

You can do this by inserting the following code in your form's (named "Form1" for example) public declarations section:

  MsgID_QueryCancelAutoPlay : Word;
  procedure WndProc (var Msg : TMessage); override;

Now, type in the following code in the "implementation" section (again, assuming that your form is named "Form1") to actually handle the Windows messages. As you can see, we're only interested in catching "QueryCancelAutoPlay" messages, so we'll let the default (or the inherited) "WndProc()" handle all other messages.

  procedure TForm1.WndProc(var Msg : TMessage);
  begin
    if Msg.Msg = MsgID_QueryCancelAutoPlay then
    begin
      { set Msg.Result
         to 1 to stop AutoPlay or
         to 0 to continue with AutoPlay }
      Msg.Result := 1;
    end
    else
      inherited WndProc( Msg );
  end;

Finally, we have to ask Windows to actually send a "QueryCancelAutoPlay" message to our message handler by inserting the following code in the "FormCreate()" event (click on your form, go to the "events" tab in the "Object Inspector" and double click on "Create"):

  MsgID_QueryCancelAutoPlay := RegisterWindowMessage('QueryCancelAutoPlay');


Comments:

2006-01-05, 11:30:10
anonymous from Croatia/Hrvatska  
rating
very,very,very helpfull..
it helps me a lot..just write a lot of this things..
2007-01-17, 04:01:51
anonymous  
It does not work If form not be active, how can i solve it ?
2007-03-13, 00:31:22
anonymous from Malaysia  
This solution is very helpful. Thank you very much.

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Los Angeles, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.