In one old project, which I was instructed to develop, there is a field of type TScrollBox.
FScroll : TScrollBox;
To be able to handle the events of navigation buttons, the class must contain a WM_GETDLGCODE message handler. So I created a new class:
TScrollBoxArrowBtn = class(TScrollBox)
protected
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
end;
Implementation
procedure TScrollBoxArrowBtn.WMGetDlgCode(var Message: TWMGetDlgCode);
begin
Message.Result := DLGC_WANTARROWS;
end;
And replaced the TScrollBox type with TScrollBoxArrowBtn.
FScroll : TScrollBoxArrowBtn;
The component began to respond to pressing the arrow button. But the copy, delete, SelectAll methods stopped working. This happened because the previous developer added to the verification methods like this:
"VariableName".ClassType = TScrollBox
I replaced them for verification:
"VariableName" is TScrollBox
After this methods of editing began to work. But I'm not sure that such a test will not be applied elsewhere in the project. So I decided to leave
FScroll : TScrollBox;
And made TScrollBoxArrowBtn an helper class:
TScrollBoxArrowBtn = class helper for TScrollBox
protected
procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
end;
Unfortunately this method does not work. Verifications like "VariableName".ClassType = TScrollBox began to work perfectly, but project stopped responding to events arrow button. What did I do wrong?
I'm convinced that my version of IDE supports helper methods.
I did not find the answer specifically about the message methods in the helpers classes, but I found a way to solve my problem. In addition, I learned about many other bad features of the helper class, which finally convinced me to abandon their use. So my answer is - do not use the class helpers. At this time this is a very unstable tool. Perhaps in the future it will be improved.
Now about my decision. As I feared, the problem of checking the type of the following kind:
"VariableName".ClassType = TScrollBox
again appeared when merging the previously created branches. So I decided to replace the TScrollBox window procedure. I added field in the TScrollBox-field container class and I added a new window procedure for TScrollBox-field in the container class:
TCADParamsGroupBlockBaseScheme = class (TCADGroupBlockParams)
.....................................................
protected
Old_FScroll_WindowProc : TWndMethod;
procedure New_FScroll_WindowProc(var Message: TMessage);
.....................................................
end;
implementation
procedure TCADParamsGroupBlockBaseScheme.New_FScroll_WindowProc(var Message:
TMessage);
begin
//Для обработки событий нажатий Key_Up/Down/Left/Right в DoKeyDown
if Message.Msg = WM_GETDLGCODE then
Message.Result := DLGC_WANTARROWS
else Old_FScroll_WindowProc(Message);
end;
And in the constructor of the container class, I saved the pointer to the old TScrollBox-field window procedure and assigned it a new window procedure:
constructor TCADParamsGroupBlockBaseScheme.Create(const AOwner: TWinControl);
begin
...........................................
FScroll := TScrollBox.Create(FHost.Owner);
Old_FScroll_WindowProc := FScroll.WindowProc;
FScroll.WindowProc := New_FScroll_WindowProc;
............................................
end;
Related
One day ago I had started to rewrite one of my old components and I decided to improve its readability.
My component is a typical TWinControl that has overridden WndProc to handle a lot of messages of my own. There are so many code for each message and it became a problem for me to read code.
So, looking for a solution to improve code inside WndProc, I have organized these large pieces of code in procedures that called each time when appropriate message has delivered in WndProc. That's how it looks now:
procedure TMyControl.WndProc(var Message: TMessage);
begin
case Message.Msg of
WM_WINDOWPOSCHANGED:
WMWINDOWPOSCHANGED(Message);
WM_DESTROY:
WMDESTROY(Message);
WM_STYLECHANGED:
WMSTYLECHANGED(Message);
// lots of the same procedures for Windows messages
// ...
MM_FOLDER_CHANGED:
MMFOLDERCHANGED(Message);
MM_DIRECTORY_CHANGED:
MMDIRECTORYCHANGED(Message);
// lots of the same procedures for my own messages
// ...
else
Inherited WndProc(Message);
end;
end;
Unfortunately Inherited word in these procedures doesn't work anymore!
Important note: in some of WM_XXX messages I didn't call Inherited to perform my own handling of such message, so code shown below will break down my efforts to implement some features.
procedure TMyControl.WndProc(var Message: TMessage);
begin
Inherited WndProc(Message);
case Message.Msg of
WM_WINDOWPOSCHANGED:
WMWINDOWPOSCHANGED(Message);
// further messages
// ...
end;
end;
I also want to avoid inserting Inherited after each message-ID as shown below, because it looks awful and I think there is exists more elegant way to override WndProc.
procedure TMyControl.WndProc(var Message: TMessage);
begin
case Message.Msg of
WM_WINDOWPOSCHANGED:
begin
Inherited WndProc(Message);
WMWINDOWPOSCHANGED(Message);
end;
// further messages
// ...
end;
end;
So my question is:
how to properly override WndProc to have an ability to use code grouped in procedures and to be able to call for original window procedure only for some messages?
As RM's answer stated, your message handling methods can call inherited WndProc(Message) instead of just inherited, and that will work fine.
However, by introducing methods with the same names as the messages they are processing, you are exposing knowledge of the specific messages you are processing. So you may find it easier to just use Message Methods instead of overriding WndProc, eg:
type
TMyControl = class(...)
private
procedure WMWindowPosChanged(var Message: TMessage); message WM_WINDOWPOSCHANGED;
procedure WMDestroy(var Message: TMessage); message WM_DESTROY;
procedure WMStyleChanged(var Message: TMessage); message WM_STYLECHANGED;
// and so on ...
end;
Your message methods can then call inherited (or not) as needed, eg:
procedure TMyControl.WMWindowPosChanged(var Message: TMessage);
begin
inherited;
//...
end;
Calling inherited WndProc from WMWINDOWPOSCHANGED will call the inherited one. So you can do it like this:
procedure WMWINDOWPOSCHANGED(var Message: TMessage)
begin
// call inherited WndProc if you need to
inherited WndProc(Message);
.. do you own processing
end;
There is a TFrame descendant class as follows:
TCustomHistoryFrame = class(TFrame)
tbMainFunction: TToolBar;
// there's more, of course, but that is irrelevant to the question
end;
I noticed, that when I set Enabled property of this frame to False, its component tbMainFunction won't get (visually) disabled.
My first idea was to override virtual method TControl.SetEnabled. Looking at its implementation, I saw that it performs control message CM_ENABLEDCHANGED when the value of actually differs.
I am not sure on how to apply the frame's Enabled state to the toolbar the right way.
What would be the common way to do? As this question would be primarily opinion based, let me rephrase it:
What advantages and disadvantages are there for either overriding SetEnabled or handling CM_ENABLEDCHANGED?
Things, I thought of myself:
override SetEnabled:
I would have to recheck, whether the new value differs from the old value. That would be a redundancy. (Which would have no significant influence on performance, but - call me a hair-splitter - smells to me.)
handling CM_ENABLEDCHANGED:
How do I sustain inherited code for this message? There are implementations for this message (at least) in TControl and TWinControl. Would they still be executed, if I handle the message in my class TCustomHistoryFrame?
Handling CM_ENABLEDCHANGED is the correct solution. Such CM_... messages are specifically designed to allow descendant classes to react to changes to properties that are declared in base classes.
For example:
TCustomHistoryFrame = class(TFrame)
tbMainFunction: TToolBar;
private
procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
end;
procedure TCustomHistoryFrame.CMEnabledChanged(var Message: TMessage);
begin
inherited;
tbMainFunction.Enabled := Enabled;
end;
Alternatively:
TCustomHistoryFrame = class(TFrame)
tbMainFunction: TToolBar;
protected
procedure WndProc(var Message: TMessage); override;
end;
procedure TCustomHistoryFrame.WndProc(var Message: TMessage);
begin
inherited;
if Message.Msg = CM_ENABLEDCHANGED then
tbMainFunction.Enabled := Enabled;
end;
I want use messagesin my program and i've a question: Can I use messages in a class procedure or Can I use messages in a procedure without class?
Here is my code:
const
WM_CUSTOM_TCP_CLIENT = WM_USER + 10;
type
TFeedbackEvent = class
public
class procedure feedback(var msg: TMessage); message WM_CUSTOM_TCP_CLIENT;
end;
The Delphi returns the following message:
[Error] unit.pas(33): Invalid message parameter list
Thank you very much.
There is a very nice article on the topic: Handling Messages in Delphi 6. This is a must read.
Handling or processing a message means that your application responds
in some manner to a Windows message. In a standard Windows
application, message handling is performed in each window procedure.
By internalizing the window procedure, however, Delphi makes it much
easier to handle individual messages; instead of having one procedure
that handles all messages, each message has its own procedure. Three
requirements must be met for a procedure to be a message-handling
procedure:
The procedure must be a method of an object.
The procedure must take one var parameter of a TMessage or other message-specific record type.
The procedure must use the message directive followed by the constant value of the message you want to process.
As you can read in the article, the procedure must be a method of an object, not a class. So you cannot just use message handlers in a class procedure.
A possible workaround to handle messages in a class instance (also in object instance or window-less applications), is to manually create window handle via AllocateHWND, and process messages yourself via a WndProc procedure.
There is a good example on this in delphi.about.com: Sending messages to non-windowed applications (Page 2):
The following sample is a version of the above example, modified to work with class method. (If using class method is not really required, use original example from the link above instead):
First, you need to declare a window handle field and a WndProc procedure:
TFeedbackEvent = class
private
FHandle: HWND;
protected
class procedure ClassWndProc(var msg: TMessage);
end;
procedure WndProc(var msg: TMessage);
Then, process the messages manually:
procedure WndProc(var msg: TMessage);
begin
TFeedbackEvent.ClassWndProc(msg);
end;
procedure TFeedbackEvent.ClassWndProc(var msg: TMessage);
begin
if msg.Msg = WM_CUSTOM_TCP_CLIENT then
// TODO: Handle your message
else
// Let default handler process other messages
msg.Result := DefWindowProc(FHandle, msg.Msg, msg.wParam, msg.lParam);
end;
Finally, at the end of the file, declare initialization and finalization section to create/destroy the handle:
initialization
FHandle := AllocateHWND(WndProc);
finalization
DeallocateHWnd(FHandle);
Of course, this is not the recommended way to do this (especially watch for problems with initialization/finalization), it was just an example to show that it is possible.
Unless you have some very strange requirement to use class method, its better to use regular class method and object constructor and destructor instead initialization and finalization sections (as shown in Sending messages to non-windowed applications (Page 2)).
I faced a redrawing bug that is not very pleasant to see (Delphi 5, Windows 7 64 bit, Classic theme)
If one creates resizable dialog with client-aligned RichEdit in it and provide the function
procedure TQueryDlg.ShowDialog(const Txt: string);
begin
RichEdit.Text:=Txt;
ShowModal;
end;
then at least on Windows 7 when resizing the dialog, the lines are not re-wrapped, but rather the pixels from the chars keeps filling the space and it looks like the whole area is never invalidated. The richedit starts working correctly when the control is activated with the mouse.
I suppose it has something to do with message queue of forms and dialogs in Delphi, but probably is specific to RichEdits of particular version. My
System32/Richedit32.dll - v6.1.7601.17514
System32/RichEdit20.dll - v3.1, 5.31.23.1230
Probably some workaround information would be great.
I had a similar problem with the TRichEdit control. I found it wouldn't paint itself unless it was visible (which wasn't always the case in my app). And I found times that it rendered incorrectly until the user set focus to it. Both very irritating.
What worked for me was to create my own class and add a Render() method to it. This way I could tell it to paint whenever I wanted (e.g., when resizing a form, or when the component wasn't visible).
Here is a very stripped down version of what I did:
interface
uses
Winapi.Messages, Vcl.ComCtrls;
type
TMyRichEdit = class(TRichEdit)
private
procedure WMPaint(var Message: TMessage); message WM_PAINT;
public
procedure DoExit; override;
procedure DoEnter; override;
procedure Render;
end;
var
PaintMsg: TMessage;
implementation
procedure TMyRichEdit.DoEnter;
begin
inherited;
WMPaint(PaintMsg);
end;
procedure TMyRichEdit.DoExit;
begin
inherited;
WMPaint(PaintMsg);
end;
procedure TMyRichEdit.Render;
begin
WMPaint(PaintMsg);
end;
procedure TMyRichEdit.WMPaint(var Message: TMessage);
begin
// eliminated custom code to tweak the text content...
inherited;
end;
initialization
PaintMsg.Msg := WM_PAINT;
PaintMsg.WParam := 0;
PaintMsg.LParam := 0;
PaintMsg.Result := 0;
end.
I added WMPaint() because I needed to tweak how the text content before it was rendered. But none of that code is needed for what you are doing. So, instead of declaring WMPaint() and handling the WM_PAINT message, you could probably just post the PaintMsg from the DoExit(), DoEnter() and Render() methods. Sorry I don't have time to compile the code or try eliminating WMPaint() and using PostMessage()...
I saw your tip about : TTouchKeyboard: send keystroke to other program
How can I send the keys to the other form in the same Delphi application?
And how can I call the form with the TTouchKeyboard? (Show, showModal, parameters?)
Thanks!
ShowModal is a bad idea... you focus the caller...
You can still use the same tip with the form which contains the keyboard, in order to stay disable...
Then, you can add a property with the handle of the form which should get the keystroke.
And finally, you hack the TTouchKeyboard to set the focus to the form with the handle you previously set...
For instance, your TTouchKeyboard hack could be like this:
type
TMyKeyboard = class(TTouchKeyboard)
protected
procedure WndProc(var Message: TMessage); override;
end;
type
TForm1 = class(TForm)
...
private
fHandleOfTheTargetForm: HWND;
public
property HandleOfTheTargetForm: HWND read fHandleOfTheTargetForm write fHandleOfTheTargetForm;
...
procedure TMyKeyboard.WndProc(var Message: TMessage);
begin
if (Assigned(Form1)) then
begin
if Form1.HandleOfTheTargetForm <> 0 then
begin
SetForegroundWindow(HandleOfTheTargetForm);
end;
end;
inherited;
end;
You can find a quick demo project here.