5

I've been trying to use tomazy's FutureWindows infrastructure (see his answer at Delphi GUI Testing and Modal Forms or the home of the tool at https://github.com/tomazy/DelphiUtils), but would like to know if and how can it be used with standard Windows file open dialogs? They don't seem to be inheriting from TControl, which the FutureWindows infra seems to assume (unless I've misunderstood it).

What I'd like to do is basically to just select a file in an OpenFileDialog which is opened modally by a command within my testing, but haven't yet been able to figure out how to do this.

Community
  • 1
  • 1
DelphiUser
  • 419
  • 4
  • 12

2 Answers2

4

Use a tool like Spy++ to find out what the window class name is. For example, on my Windows 7 machine, the window class name for a system file open dialog is #32770 (Dialog).

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks, I actually noted that FutureWindows has this as a const (MESSAGE_BOX_WINDOW_CLASS) and thus I got access to the dialog as an IWindow (FutureWindows interface). Now my problem is to get this interface to object, because the interface only provides an AsControl method, which I doubt will work since the dialog is not a TControl. Without it, I don't know (yet) how to set the FileName property of the dialog... – DelphiUser Feb 23 '12 at 11:49
  • You are correct, `AsControl` cannot work. You could use `EnumChildWindows` to hunt down the target window, but there may be an easier way. Could you not mock this? – David Heffernan Feb 23 '12 at 12:19
  • I googled around a bit and ended up with this, which seems to work: Windows.SetDlgItemText(DlgHandle, 1148, PChar(FileName)); Utilizing this: http://social.msdn.microsoft.com/forums/en-US/winforms/thread/62d5db14-5497-4ceb-8af0-d7f81732e937/ So basically I send a message to set the filename to the correct control (ID 1148 works at least on my Windows 7 :) ) – DelphiUser Feb 23 '12 at 12:22
  • 1
    That sounds like an excellent plan. By the way do you know about accepting answers yet? – David Heffernan Feb 23 '12 at 17:58
  • I think I do - I was planning to summarize my result as an answer to my own question and accept it, but I didn't have the permission to do that for some hours. Now I might have... – DelphiUser Feb 24 '12 at 09:04
  • @DelphiUser Might I ask what's wrong with this answer? Did you already know about the window class at the time when you asked the question? – David Heffernan Feb 24 '12 at 09:19
  • Sorry for not noting the comment earlier, it was under "show more comments" and I didn't notice it. Basically, your answer was helpful (I marked it as that), but it didn't yet solve the actual problem, which was to be able to access and use the dialog's properties (filename etc.). For this I needed the info on how to send messages to the correct controls, which was found from the link I found via googling. Before asking the question I had noted the constant in FutureWindows, but your answer tipped me to use it also for open dialog (even though it's called message box class), so thus helpful. – DelphiUser Mar 01 '12 at 08:25
  • Well, you irked me with your actions on this question. I led you to the solution but you could not accept that. I think I'm over it now. But I'm not exactly eager to help on your question of today. No doubt somebody else will. – David Heffernan Mar 01 '12 at 08:31
  • You say "I felt that your answer in the other question, while helpful didn't solve the fundamentals of the issue". The thing is, it's not clear from this question what you believe the fundamentals to be. It reads as though the fundamental problem is the fact that file dialogs do no inherit from `TControl`. And that's what I addressed. The other issue (locating the edit control) we discussed in comments. You can't really expect others to solve every single aspect of your problems especially if you don't spell it out 100%. – David Heffernan Mar 01 '12 at 09:03
  • Ok, it's probably more clear to me as the author that the fundamental was "if and how can it be used with standard Windows file open dialogs" and to "select a file in an OpenFileDialog which is opened modally by a command within my testing". I thought that since the "final solution" including the latter point also was self-found via googling, that it wouldn't be proper to accept this as the whole answer (rather as a helpful answer), but maybe this is not how things are handled here. I can accept your answer also if pointing-to-the-right-direction answers are supposed to be accepted. – DelphiUser Mar 01 '12 at 09:14
3

My current solution is below:

TFutureWindows.Expect(MESSAGE_BOX_WINDOW_CLASS)
  .ExecProc(
    procedure (const AWindow: IWindow)
    var
      DlgHandle: HWND;
      FileName: string;
    begin
      FileName := ExpandFileName('myFileToUse.txt');
      DlgHandle := AWindow.GetHandle;
      Windows.SetDlgItemText(DlgHandle, 1148, PChar(FileName));
    end
    )
  .ExecSendKey(VK_RETURN);

So basically sending a message using Windows API. The ideas (and the ID 1148) were found from here: http://social.msdn.microsoft.com/forums/en-US/winforms/thread/62d5db14-5497-4ceb-8af0-d7f81732e937/

Possible better solutions are welcome, but this seems fine enough for me at least for now. Thanks for the comments so far!

DelphiUser
  • 419
  • 4
  • 12
  • I can't see that you will get a better solution than this. – David Heffernan Feb 24 '12 at 09:16
  • This is a summary of the (currently) final actual solution, but I marked the answer of @DavidHeffernan above as an answer to grant the recognition of helping in pointing to a helpful direction. – DelphiUser Mar 01 '12 at 09:16