Delphi 获取菜单高度、标题栏高度、边框高度函数GetSystemMetrics
項(xiàng)目中需要獲取不同分辨率德菜單高度,必應(yīng)搜索了一下,可以用GetSystemMetrics函數(shù)實(shí)現(xiàn),代碼如下:歡迎加入Delphi知識(shí)局QQ群:32422310
unit Unit1;interfaceusesWinapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Menus;typeTForm1 = class(TForm)MainMenu1: TMainMenu;N11: TMenuItem;N2221: TMenuItem;Button1: TButton;procedure Button1Click(Sender: TObject);private{ Private declarations }public{ Public declarations }end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); varMenuItemHeight:integer; beginMenuItemHeight:= GetSystemMetrics(SM_CYMENUCHECK); end;end.如果菜單折疊,可用以下代碼:
typeTMenuBarInfo = recordcbSize: 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;implementationfunction GetMenuBarInfo; external user32 name 'GetMenuBarInfo';procedure TForm1.Button1Click(Sender: TObject); varMenuHeight: Integer;MenuBarInfo: TMenuBarInfo; beginMenuBarInfo.cbSize := SizeOf(MenuBarInfo);if GetMenuBarInfo(Handle, OBJID_MENU, 0, MenuBarInfo) thenMenuHeight := MenuBarInfo.rcBar.Bottom - MenuBarInfo.rcBar.Topelse......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;
該函數(shù)也可以用于獲取屏幕分辨率
GetSystemMetrics(
? nIndex:?Integer?{參數(shù),?詳見下表}
?):?Integer;
?
舉例?-?獲取屏幕分辨率:var
? cx,cy:?Integer;
?begin
? {通過?GetSystemMetrics?函數(shù)獲取屏幕分辨率}
? 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;
?
總結(jié)
以上是生活随笔為你收集整理的Delphi 获取菜单高度、标题栏高度、边框高度函数GetSystemMetrics的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cocosbuilder3.0 使用小记
- 下一篇: Python爬取新浪英超曼联文章内页--