From: mav%dseg.ti.com@plearn.edu.pl (Michael Vincze)
Newsgroups: comp.lang.pascal
Subject: Re: TPW: Task to Task Messages?
Date: 8 Mar 1995 22:57:55 GMT
Organization: Texas Instruments


{ Author:   Michael Vincze  07/22/94                              }
{           vincze@lobby.ti.com                                   }
{           mav@asd470.dseg.ti.com                                }
{                                                                 }
{ Purpose:  Shows how to send CANCEL or OK to a dialog.           }
{                                                                 }
{ Usage:    Run program and follow instructions in the notepad.   }
{           Note, change the notepad file as needed before        }
{           compiling.                                            }

program SendOK;

uses
  WinTypes,
  WinProcs,
  Win31,
  ToolHelp,
  Objects,
  OWindows,
  ODialogs,
  Strings;

const
  ApplicationName: PChar = 'Send Ok/Cancel';
  NotepadFile    : PChar = 'e:\test.txt';

type
  PMainWindow = ^TMainWindow;
  TMainWindow = object (TWindow)
    W3     : Integer; { 1/3 screen width  }
    H3     : Integer; { 1/3 screen height }
    Notepad: HWND;    { notepad handle    }

    constructor Init (AParent: PWindowsObject; ATitle: PChar);
    procedure   SetupWindow; virtual;
    destructor  Done; virtual;
    procedure   Gasp;
    procedure   WriteNotepad (Keys: PChar);
    procedure   MenuNotepad  (Keys: PChar);
    procedure   WMLButtonDown  (var Msg: TMessage); virtual WM_FIRST +
 WM_LBUTTONDOWN;
    procedure   WMRButtonDown  (var Msg: TMessage); virtual WM_FIRST +
 WM_RBUTTONDOWN;
    end;

constructor TMainWindow.Init (AParent: PWindowsObject; ATitle: PChar);
begin
inherited Init (AParent, ATitle);

{ setup main window to occupy upper left ninth of screen
}
W3 := GetSystemMetrics (SM_CXSCREEN) div 3;
H3 := GetSystemMetrics (SM_CYSCREEN) div 3;
attr.x := 0;
attr.y := 0;
attr.w := W3;
attr.h := H3;
end;

destructor TMainWindow.Done;
begin
{ close notepad
}
PostMessage (Notepad, WM_CLOSE, 0, 0);
inherited Done;
end;

procedure TMainWindow.SetupWindow;
var
  fp     : TEXT;
  Command: array [0..$FF] of Char;
begin
inherited SetupWindow;

{ get file ready for notepad (make it a zero length file)
}
Assign (fp, NotepadFile);
Rewrite (fp);
Close (fp);

{ execute notepad
}
wvsprintf (Command, 'notepad %s', NotepadFile);
WinExec (Command, SW_SHOWNORMAL);

{ cheesey way of getting handle to window
}
Notepad := GetFocus;
while GetParent (Notepad) <> 0 do
  Notepad := GetParent (Notepad);

{ move notepad into lower 2 thirds of screen
}
W3 := GetSystemMetrics (SM_CXSCREEN) div 3;
H3 := GetSystemMetrics (SM_CYSCREEN) div 3;
MoveWindow (Notepad, 0, H3, W3*3, H3*3, TRUE);

{ write instructions into notepad
}
WriteNotepad ('Hello Chief'^M^M);
WriteNotepad ('To bring up the Save As... dialog and cancel:'^M);
WriteNotepad ('  Click left mouse button in main window'^M);
WriteNotepad ('To bring up the Save As... dialog and hit the default button
 (OK):'^M);
WriteNotepad ('  Click right mouse button in main window'^M^M);
WriteNotepad ('In each case look at the File Manager to verify the
 action.'^M^M);
WriteNotepad ('Close the main window to close the application and notepad.');
end;

procedure TMainWindow.Gasp;
var
  Msg: TMsg;
begin
while PeekMessage (Msg, 0, 0, 0, pm_Remove) do
  with Msg do
    if not IsDialogMessage (hWindow, Msg) then
      begin
      TranslateMessage (Msg);
      DispatchMessage (Msg);
      end;
end;

procedure TMainWindow.WriteNotepad (Keys: PChar);
var
  hFocus: HWND;
begin
SetFocus (Notepad);
hFocus := GetFocus;

{ individually send characters until end of string encountered
}
while keys^ <> #0 do
  begin
  SendMessage (hFocus, WM_CHAR, Word (keys^), 0);
  Inc (keys, 1);
  end;
end;

procedure TMainWindow.MenuNotepad (Keys: PChar);
const
  AltKey       : Longint = $20000000;
  Count        : Longint = $00000001;
  KeyState     : LongInt = $40000000;
  KeyTransition: Longint = $80000000;
var
  hFocus: HWND;
  lParam: Longint;
begin
SetFocus (Notepad);
hFocus := GetFocus;

{ activate (notepad) menu popup with first Alt key
}
lParam := AltKey or Count;  {add Alt key to key data  }
PostMessage (hFocus, WM_SYSCHAR, Word (keys^), lParam);

{ increment index to next character
}
Inc (keys, 1);

{ select (notepad) menu item(s) until end of string encountered
}
{
lParam := Count;
}
while keys^ <> #0 do
  begin
  PostMessage (Notepad, WM_SYSCHAR, Word (keys^), lParam);
  Inc (Keys, 1);
  end;
end;

procedure TMainWindow.WMLButtonDown (var Msg: TMessage);
var
  hDialog : HWND;
begin
{ activate File Save As... menu item
}
MenuNotepad ('FA');

{ yield until dialog appears
}
Gasp;

{ get dialog's handle
}
hDialog := GetLastActivePopup (Notepad);

{ send cancel message
}
PostMessage (hDialog, WM_CLOSE, 0, 0);
end;

procedure TMainWindow.WMRButtonDown (var Msg: TMessage);
var
  hDialog  : HWND;
  DefaultID: Longint;
  lParam   : Longint;
begin
{ activate File Save As... menu item
}
MenuNotepad ('FA');

{ yield until dialog appears
}
Gasp;

{ get dialog's handle
}
hDialog := GetLastActivePopup (Notepad);

{ get default button ID
}
DefaultID := LOWORD (SendMessage (hDialog, DM_GETDEFID, 0, 0));

{ assemble button click message for default button
}
lParam := BN_CLICKED;
lParam := lParam shl 16;
lParam := lParam or DefaultID;

{ send default button click message to dialog
}
PostMessage (hDialog, WM_COMMAND, DefaultID, lParam);
end;

type
  TMainApplication = object (TApplication)
    procedure InitMainWindow; virtual;
    end;

procedure TMainApplication.InitMainWindow;
begin
MainWindow := New (PMainWindow, Init (nil, ApplicationName));
end;

var
  Application: TMainApplication;

begin
Application.Init (ApplicationName);
Application.Run;
Application.Done;
end.






