Archive

Posts Tagged ‘wm’

How to Focus the Next Control After Pressing Enter

December 15th, 1999 m3Rlin 1 comment

By default the focus is passed to the next control by pressing the Tab key. Well, sometimes (when filling out forms) you may want to pass to the next control after pressing Enter. Well, here’s the code: For TEdit controls only:
Select all the TEdit controls you want to include (using the Shift key), then select the Object Inspector, choose the Events tab and double click the OnKeyPress event. This way all the selected TEdit controls will use the same code. In the code editor write this code:

if Key = #13 then begin
  Perform(WM_NEXTDLGCTL, 0, 0);
  Key := #0; { Eat the key }
end;

For all the controls on the form:
This method will work for all controls because the form will take care of the event. All you have to do is set the form’s KeyPreview property to True and add this code to your form’s KeyUp event:

if (Key = VK_RETURN) and (Shift = []) then
  Perform(WM_NEXTDLGCTL, 0, 0);

How to Determine if There is Anything in the Clipboard

December 15th, 1999 m3Rlin 3 comments

When creating a text editor (or any other kind of program that uses the Clipboard you always insert code to determine if there is anything to copy or cut and to update the menus. Well, you should also do this for the paste function. The whole idea is based on Windows messages and is very easy.

Here’s the code. The sample code contains a form named MainForm and a paste menu item named miEditPaste.

uses

  Clipbrd;

...

type

  TMainForm = class(TForm)

    procedure FormCreate(Sender: TObject);

    procedure FormDestroy(Sender: TObject);

  private

    hClipboardOwner: HWnd;

    { The messages used for determing the Clipboard contents }

    procedure WMChangeCBChain(var Msg: TWMChangeCBChain); message WM_CHANGECBCHAIN;

    procedure WMDrawClipboard(var Msg: TWMDrawClipboard); message WM_DRAWCLIPBOARD;

...

Add this code to your form’s OnCreate event. It will let Windows know we want to know what’s going on with the Clipboard.

...

  hClipboardOwner := SetClipboardViewer(Handle);

...

Add this code to your form’s OnDestroy event. This will free us from the Clipboard chain.

...

  ChangeClipboardChain(Handle, hClipboardOwner);

...

This is the code for the message handling:

(* The WM_CHANGECBCHAIN message is sent to the first window in the clipboard viewer chain when
** a window is being removed from the chain
*)

procedure TMainForm.WMChangeCBChain(var Msg: TWMChangeCBChain);
begin
  if Msg.Remove = hClipboardOwner then
    hClipboardOwner := Msg.Next
  else
    SendMessage(hClipboardOwner, WM_CHANGECBCHAIN, Msg.Remove, Msg.Next);
  Msg.Result := 0; { The message has been taken care of }
end;

(* The WM_DRAWCLIPBOARD message is sent to the first window in the clipboard viewer chain when
** the content of the clipboard changes. This enables a clipboard viewer window to display the new
** content of the clipboard
*)

procedure TMainForm.WMDrawClipboard(var Msg: TWMDrawClipboard);
begin
  { Let the next viewer in the chain know that Clipboard has been changed }
  SendMessage(hClipboardOwner, WM_DRAWCLIPBOARD, 0, 0);
  Msg.Result := 0; { The message has been taken care of }
  { Insert you code here... }
  miEditPaste.Enabled := Clipboard.HasFormat(CF_TEXT); { Enabled the menu item only if there is text in the Clipboard }
end;