项目中需要获取不同分辨率德菜单高度,必应搜索了一下,可以用GetSystemMetrics函数实现,代码如下:欢迎加入Delphi知识局QQ群:32422310
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Menus;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
N11: TMenuItem;
N2221: TMenuItem;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
MenuItemHeight:integer;
begin
MenuItemHeight:= GetSystemMetrics(SM_CYMENUCHECK);
end;
end.
如果菜单折叠,可用以下代码:
type
TMenuBarInfo = record
cbSize: DWORD;
rcBar: TRect;
hMenu: HMENU;
hwndMenu: HWND;
fBarFocused: Byte;
fFocused: Byte;
end;
{$EXTERNALSYM GetMenuBarInfo}
function GetMenuBarInfo(hend: HWND; idObject, idItem: ULONG;
var pmbi: TMenuBarInfo): BOOL; stdcall;
implementation
function GetMenuBarInfo; external user32 name 'GetMenuBarInfo';
procedure TForm1.Button1Click(Sender: TObject);
var
MenuHeight: Integer;
MenuBarInfo: TMenuBarInfo;
begin
MenuBarInfo.cbSize := SizeOf(MenuBarInfo);
if GetMenuBarInfo(Handle, OBJID_MENU, 0, MenuBarInfo) then
MenuHeight := MenuBarInfo.rcBar.Bottom - MenuBarInfo.rcBar.Top
else
...
...
end;
或者
procedure TForm1.Button1Click(Sender: TObject);
var
pm : TTPMParams;
DisplayPoint : TPoint;
r: TRect;
begin
Systemparametersinfo( spi_getworkarea, 0, @r, 0 );
r.top := r.bottom + 1;
r.bottom := screen.height;
DisplayPoint := Point( 699, r.top );
with pm, pm.rcexclude do
begin
Top := r.top;
Bottom := r.bottom;
Left := 0;
Right := screen.width;
cbSize := SizeOf(pm);
end;
TrackPopupMenuEx( PopupMenu1.Handle, TPM_VERTICAL or TPM_HORIZONTAL,
DisplayPoint.x, DisplayPoint.y, Handle, @pm );
end;
该函数也可以用于获取屏幕分辨率
GetSystemMetrics(
nIndex: Integer {参数, 详见下表}
): Integer;
举例 - 获取屏幕分辨率:var
cx,cy: Integer;
begin
{通过 GetSystemMetrics 函数获取屏幕分辨率}
cx := GetSystemMetrics(SM_CXSCREEN);
cy := GetSystemMetrics(SM_CYSCREEN);
ShowMessageFmt('Width:%d; Height:%d', [cx,cy]);
{通过 Screen 对象获取屏幕分辨率}
cx := Screen.Width;
cy := Screen.Height;
ShowMessageFmt('Width:%d; Height:%d', [cx,cy]);
end;
procedure TForm1.Button9Click(Sender: TObject);
var
frameh,captionh,menuh: Integer;
str:string;
begin
//边框高
frameh := GetSystemMetrics(SM_CXFRAME);
//标题高
captionh := GetSystemMetrics(SM_CYCAPTION);
//菜单高
menuh:=GetSystemMetrics(SM_CYMENU);
str:= inttostr( frameh)+' '+inttostr(captionh)+' '+inttostr(menuh) ;
memo1.text:=str;
end;