• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 593
  • Last Modified:

Having the old resize style with the standard TForm class?

I've made a few complicated components that needs lots of CPU power to draw when resizing the form (their alClient aligned). When I resize the form it redraws it dozens of times, causing 50% CPU on my HT cpu (would otherwize cause 100%), while resizing. My application needs much CPU power already (it's a P2P client), so it would be nice to stop this resizing from causing that high CPU load. That's why I thought of the way old windowses resizes forms (it draws a simple rectangle, and when I let the mousebutton go, it redraws the form at that position). I know that is possible in XP too, but I can't really ask each user to enable the old resizing style, to save CPU power when using my app, so there must be a simple way (using a few modifications to original TForm code, or WinAPI's) to get the same effect on any windows OS. I looked through all settings (I think) in the default TForm, and found nothing (only for moving the form). Any ideas?
0
chrml0606
Asked:
chrml0606
  • 2
  • 2
2 Solutions
 
mokuleCommented:
I've got such an idea.
In a Form's event OnCanResize when mouse button is down set Align := alNone for Your components.
In a MouseUp event set Align to alClient.
0
 
mokuleCommented:
Hey, did You try DisableAlign?
0
 
chrml0606Author Commented:
Good idea. I just tried DisableAlign and EnableAlign, and it seems to work fine, but if I disable it more times than I enable it, it won't be enabled again (of some very weird reason). That's no problem fixing that though.

However, MouseUp doesn't seem to trigger when I end the resizing (it triggers when the mouse goes up, but not when I let the mouse go after resizing for example). Are there any other way notifying when the mouse goes down/up, cuz it shouldn't bring any problem if I just disable/enable it every time the mouse goes down/up.
0
 
Russell LibbySoftware Engineer, Advisory Commented:
How about this though, might give you a little better control of what you are after...


unit dragform;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TFullDragForm     =  class(TForm)
  private
     // Private declarations
     FOldRect:      TRect;
     FSizeRect:     TRect;
     procedure      DrawDragRect(R: TRect; Erase: Boolean);
  protected
     // Protected declarations
     procedure      WMSizing(var Message: TMessage); message WM_SIZING;
     procedure      WMMoving(var Message: TMessage); message WM_MOVING;
     procedure      WMEnterSizeMove(var Message: TMessage); message WM_ENTERSIZEMOVE;
     procedure      WMExitSizeMove(var Message: TMessage); message WM_EXITSIZEMOVE;
  public
     // Public declarations
  end;

type
  TForm1 = class(TFullDragForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

procedure TFullDragForm.WMSizing(var Message: TMessage);
begin

  // Remove the frame rect
  DrawDragRect(FSizeRect, True);

  // Inherited
  inherited;

  // Get new window rect
  GetWindowRect(Handle, FSizeRect);

  // Draw new fram rect
  DrawDragRect(FSizeRect, False);

end;

procedure TFullDragForm.WMMoving(var Message: TMessage);
begin

  // Remove the frame rect
  DrawDragRect(FSizeRect, True);

  // Inherited
  inherited;

  // Get new window rect
  GetWindowRect(Handle, FSizeRect);

  // Draw new fram rect
  DrawDragRect(FSizeRect, False);

end;

procedure TFullDragForm.DrawDragRect(R: TRect; Erase: Boolean);
var  DesktopWindow:    HWND;
     DC:               HDC;
     OldBrush:         HBrush;
     DrawRect:         TRect;
     PenSize:          Integer;
begin

  PenSize:=4;
  DrawRect:=R;
  DesktopWindow:=GetDesktopWindow;
  DC:=GetDCEx(DesktopWindow, 0, DCX_CACHE or DCX_LOCKWINDOWUPDATE);
  try
     OldBrush:=SelectObject(DC, Canvas.Brush.Handle);
     with DrawRect do
     begin
        PatBlt(DC, Left + PenSize, Top, Right - Left - PenSize, PenSize, PATINVERT);
        PatBlt(DC, Right - PenSize, Top + PenSize, PenSize, Bottom - Top - PenSize, PATINVERT);
        PatBlt(DC, Left, Bottom - PenSize, Right - Left - PenSize, PenSize, PATINVERT);
        PatBlt(DC, Left, Top, PenSize, Bottom - Top - PenSize, PATINVERT);
     end;
     SelectObject(DC, OldBrush);
  finally
     ReleaseDC(DesktopWindow, DC);
  end;

end;

procedure TFullDragForm.WMEnterSizeMove(var Message: TMessage);
begin

  // Get the current window rect
  GetWindowRect(Handle, FOldRect);
  FSizeRect:=FOldRect;

  // Perform inherited
  inherited;

  // Lock the window
  Perform(WM_SETREDRAW, 0, 0);

  // Draw the frame rect
  DrawDragRect(FSizeRect, False);

end;

procedure TFullDragForm.WMExitSizeMove(var Message: TMessage);
begin

  // Perform inherited
  inherited;

  // Remove the last drag frame
  DrawDragRect(FSizeRect, True);

  // Unlock the window
  Perform(WM_SETREDRAW, 1, 0);

  // Repaint the desktop
  InvalidateRect(0, nil, False);

  // Repaint our window
  InvalidateRect(Handle, nil, True);

end;

end.

--------

Regards,
Russell
0
 
chrml0606Author Commented:
Nice, nice. Thanks :). Accepted.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now