Automating applications using messages
Written by Harry Fairhead   
Thursday, 16 July 2009
Article Index
Automating applications using messages
Getting started
Finding a dialog
Opening a file
Doing the Demux

Opening a file

Given the getDialog method we can now move on to make use of any dialogs we open. In this case it’s an Open File dialog box with title “Open”:

IntPtr OpenHwnd = getDialog(
Hwnd,
"Open",
5000);

We now need to enter text into the dialog’s edit control which Winspector reveals has a control ID of 1152. Its window handle is:

IntPtr EditHwnd = GetDlgItem(
OpenHwnd,
1152);

We can use the WM_SETTEXT message to send a string to any edit control – the code for which is:

const uint WM_SETTEXT = 0xC;

We also need a slightly different definition of SendMessage to cope with a pointer to a string:

[DllImport("user32.dll", 
CharSet = CharSet.Auto,
SetLastError = false)]
static extern IntPtr SendMessage(
HandleRef hWnd,
uint Msg,
IntPtr wParam,
[Out, In] StringBuilder lParam);

You can enter both definitions and the system will work out which to use in any particular case. We could now just send the text, but even though the dialog and its edit control exist they could be still in the middle of initialising. This would mean that we could write the text successfully only to have it blanked out again as soon as initialisation is completed! The correct way of dealing with this is to enquire what the state of the dialog is or to set the text and then check that it is indeed set (using a GetText message).

The simplest solution is to just yield control to the other threads in the system for a tenth of a second or more:

Thread.Sleep(100);
StringBuilder file =
new StringBuilder(
@"D:\VIDEO_TS\VTS_01_1.VOB");
IntPtr set = SendMessage(
new HandleRef(VobEdit,
EditHwnd),
WM_SETTEXT,
IntPtr.Zero,
file);

The file name entered into the dialog box is the default name for the first VOB making up the main program on a DVD in drive D.

Finally we can click the Open button, control ID 1, by sending it a WM_COMMAND message:

IntPtr DlgOpenBtnHwnd = 
GetDlgItem(OpenHwnd,1);
SendMessage(
new HandleRef(VobEdit, OpenHwnd),
WM_COMMAND,
new IntPtr(1),
DlgOpenBtnHwnd);

This closes the dialog and returns its results to the application.

<ASIN:0201633582>

<ASIN:1578200679>

<ASIN:1565926315>

<ASIN:1556156774>



Last Updated ( Sunday, 19 July 2009 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.