前几天,我开始开发我的新项目。应该有一个MDI表格,上面有一些子表格。但是当我开始开发时,我遇到了以下问题:当主窗体变成MDI形式时,它会在内部绘制一个可怕的边框(斜面)。我不能把它带走。您可以在屏幕截图中看到这种情况:
相反,MDI-Child表格没有相同的斜角。
该项目包含两种形式,Form1和Form2。 Form1是主要的MDI形式。
Form1源代码:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 346
ClientWidth = 439
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
FormStyle = fsMDIForm
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
end
Form2源代码:
object Form2: TForm2
Left = 0
Top = 0
Caption = 'Form2'
ClientHeight = 202
ClientWidth = 331
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
FormStyle = fsMDIChild
OldCreateOrder = False
Visible = True
PixelsPerInch = 96
TextHeight = 13
end
请告诉我如何从主表单中取出这个斜角。
答案 0 :(得分:18)
绘制边框是因为MDI客户端窗口具有扩展窗口样式WS_EX_CLIENTEDGE
。这样描述了这种风格:
窗口有一个带有凹陷边缘的边框。
然而,我第一次删除该样式的简单尝试失败了。例如,您可以尝试以下代码:
procedure TMyMDIForm.CreateWnd;
var
ExStyle: DWORD;
begin
inherited;
ExStyle := GetWindowLongPtr(ClientHandle, GWL_EXSTYLE);
SetWindowLongPtr(ClientHandle, GWL_EXSTYLE,
ExStyle and not WS_EX_CLIENTEDGE);
SetWindowPos(ClientHandle, 0, 0,0,0,0, SWP_FRAMECHANGED or
SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE or SWP_NOZORDER);
end;
此代码确实删除了WS_EX_CLIENTEDGE
。但是您看不到任何视觉上的变化,如果您使用Spy ++之类的工具检查窗口,那么您将看到MDI客户端窗口保留WS_EX_CLIENTEDGE
。
那么,是什么给出的?事实证明,MDI客户端窗口的窗口过程(在VCL代码中实现)正在强制显示客户端边缘。这会覆盖您为删除样式而进行的任何尝试。
有问题的代码如下:
procedure ShowMDIClientEdge(ClientHandle: THandle; ShowEdge: Boolean);
var
Style: Longint;
begin
if ClientHandle <> 0 then
begin
Style := GetWindowLong(ClientHandle, GWL_EXSTYLE);
if ShowEdge then
if Style and WS_EX_CLIENTEDGE = 0 then
Style := Style or WS_EX_CLIENTEDGE
else
Exit
else if Style and WS_EX_CLIENTEDGE <> 0 then
Style := Style and not WS_EX_CLIENTEDGE
else
Exit;
SetWindowLong(ClientHandle, GWL_EXSTYLE, Style);
SetWindowPos(ClientHandle, 0, 0,0,0,0, SWP_FRAMECHANGED or
SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE or SWP_NOZORDER);
end;
end;
....
procedure TCustomForm.ClientWndProc(var Message: TMessage);
....
begin
with Message do
case Msg of
....
$3F://!
begin
Default;
if FFormStyle = fsMDIForm then
ShowMDIClientEdge(ClientHandle, (MDIChildCount = 0) or
not MaximizedChildren);
end;
因此,您只需要覆盖此$3F
消息的处理。
这样做:
type
TMyMDIForm = class(TForm)
protected
procedure ClientWndProc(var Message: TMessage); override;
end;
procedure TMyMDIForm.ClientWndProc(var Message: TMessage);
var
ExStyle: DWORD;
begin
case Message.Msg of
$3F:
begin
ExStyle := GetWindowLongPtr(ClientHandle, GWL_EXSTYLE);
ExStyle := ExStyle and not WS_EX_CLIENTEDGE;
SetWindowLongPtr(ClientHandle, GWL_EXSTYLE, ExStyle);
SetWindowPos(ClientHandle, 0, 0,0,0,0, SWP_FRAMECHANGED or
SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE or SWP_NOZORDER);
end;
else
inherited;
end;
end;
最终结果如下:
请注意,上面的代码不会调用默认窗口过程。我不确定这是否会导致其他问题,但其他MDI行为会受到影响是非常合理的。因此,您可能需要实现更强大的行为补丁。希望这个答案能够为您提供所需的知识,使您的应用程序以您希望的方式运行。
我正在考虑如何实现一个全面的解决方案,以确保为$3F
消息调用默认窗口过程,无论该消息是什么。由于默认窗口过程存储在私有字段FDefClientProc
中,因此实现并非易事。这使得它很难达到。
我想你可以使用类助手来破解私人成员。但我更喜欢不同的方法。我的方法是完全保留窗口过程,并将VCL代码调用挂钩到SetWindowLong
。每当VCL尝试为MDI客户端窗口添加WS_EX_CLIENTEDGE
时,钩子代码就可以阻止该样式。
实现如下:
type
TMyMDIForm = class(TForm)
protected
procedure CreateWnd; override;
end;
procedure PatchCode(Address: Pointer; const NewCode; Size: Integer);
var
OldProtect: DWORD;
begin
if VirtualProtect(Address, Size, PAGE_EXECUTE_READWRITE, OldProtect) then
begin
Move(NewCode, Address^, Size);
FlushInstructionCache(GetCurrentProcess, Address, Size);
VirtualProtect(Address, Size, OldProtect, @OldProtect);
end;
end;
type
PInstruction = ^TInstruction;
TInstruction = packed record
Opcode: Byte;
Offset: Integer;
end;
procedure RedirectProcedure(OldAddress, NewAddress: Pointer);
var
NewCode: TInstruction;
begin
NewCode.Opcode := $E9;//jump relative
NewCode.Offset := NativeInt(NewAddress)-NativeInt(OldAddress)-SizeOf(NewCode);
PatchCode(OldAddress, NewCode, SizeOf(NewCode));
end;
function SetWindowLongPtr(hWnd: HWND; nIndex: Integer; dwNewLong: LONG_PTR): LONG_PTR; stdcall; external user32 name 'SetWindowLongW';
function MySetWindowLongPtr(hWnd: HWND; nIndex: Integer; dwNewLong: LONG_PTR): LONG_PTR; stdcall;
var
ClassName: array [0..63] of Char;
begin
if GetClassName(hWnd, ClassName, Length(ClassName))>0 then
if (ClassName='MDIClient') and (nIndex=GWL_EXSTYLE) then
dwNewLong := dwNewLong and not WS_EX_CLIENTEDGE;
Result := SetWindowLongPtr(hWnd, nIndex, dwNewLong);
end;
procedure TMyMDIForm.CreateWnd;
var
ExStyle: DWORD;
begin
inherited;
// unless we remove WS_EX_CLIENTEDGE here, ShowMDIClientEdge never calls SetWindowLong
ExStyle := GetWindowLongPtr(ClientHandle, GWL_EXSTYLE);
SetWindowLongPtr(ClientHandle, GWL_EXSTYLE, ExStyle and not WS_EX_CLIENTEDGE);
end;
initialization
RedirectProcedure(@Winapi.Windows.SetWindowLongPtr, @MySetWindowLongPtr);
或者,如果您更喜欢使用私有成员类帮助程序破解的版本,则如下所示:
type
TFormHelper = class helper for TCustomForm
function DefClientProc: TFarProc;
end;
function TFormHelper.DefClientProc: TFarProc;
begin
Result := Self.FDefClientProc;
end;
type
TMyMDIForm = class(TForm)
protected
procedure ClientWndProc(var Message: TMessage); override;
end;
procedure TMyMDIForm.ClientWndProc(var Message: TMessage);
var
ExStyle: DWORD;
begin
case Message.Msg of
$3F:
begin
Message.Result := CallWindowProc(DefClientProc, ClientHandle, Message.Msg, Message.wParam, Message.lParam);
ExStyle := GetWindowLongPtr(ClientHandle, GWL_EXSTYLE);
ExStyle := ExStyle and not WS_EX_CLIENTEDGE;
SetWindowLongPtr(ClientHandle, GWL_EXSTYLE, ExStyle);
SetWindowPos(ClientHandle, 0, 0,0,0,0, SWP_FRAMECHANGED or
SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE or SWP_NOZORDER);
end;
else
inherited;
end;
end;
最后,我感谢你提出这个非常有趣的问题。探索这个问题当然很有趣!
答案 1 :(得分:2)
您可以使用我的开源组件NLDExtraMDIProps
(可从here
下载),该组件具有ShowClientEdge
属性。 (代码类似于David's的代码,虽然我是拦截WM_NCCALCSIZE
,而不是$3F
)。
除此之外,该组件还具有以下方便的MDI属性:
BackgroundPicture
:来自磁盘,资源或DFM的图像,将在客户端窗口的中心绘制。CleverMaximizing
:通过双击标题栏重新排列多个MDI客户端,从而最大化到MDI表单中的最大可用空间。ShowScrollBars
:将客户端拖到MDI表单之后,打开或关闭MDI表单的滚动条。