Wednesday, March 2, 2011

Control AutoPlay (dynamically) from your program

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;
  WndProc( var Msg : TMessage );
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 );
  if( MsgID_QueryCancelAutoPlay
      = Msg.Msg )then
    // set Msg.Result
    //   to 1 to stop AutoPlay or
    //   to 0 to continue with AutoPlay
    Msg.Result := 1;
  end else
    inherited WndProc( Msg );
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"):
  := RegisterWindowMessage(
       'QueryCancelAutoPlay' );

Twitter Delicious Facebook Digg Stumbleupon Favorites More

Design by Kang Iwan K-sev | Thank's for your visit To My Site - Ridwan Mulyana | Cibeureum