ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 3345|回复: 7

[求助] GetSystemMetrics的参数值

[复制链接]

TA的精华主题

TA的得分主题

发表于 2009-2-25 13:25 | 显示全部楼层 |阅读模式
本帖已被收录到知识树中,索引项:Windows API应用
获取屏幕分辨率(1024X768),以下这段代码显示 1024X1024,
    Dim cxScreen As Long: Dim cyScreen As Long
    cxScreen = GetSystemMetrics(SM_CXSCREEN)
    cyScreen = GetSystemMetrics(SM_CYSCREEN)
    MsgBox cxScreen & "X" & cyScreen
一定要写入两个参数的数值0和1吗?
如果我不知道具体的值该怎么查呢?GetSystemMetrics好像有几十个参数,都对应哪些值呢

TA的精华主题

TA的得分主题

 楼主| 发表于 2009-2-25 13:48 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
另外我想在屏幕中心画一个图形,用sxScreen/2,syScreen/2并不是显示在屏幕中心?求助解决方法   
    Dim sxScreen As Long : Dim syScreen As Long
    sxScreen = GetSystemMetrics(0)
    syScreen = GetSystemMetrics(1)
    With Shapes.AddShape(msoShapeDiamond, sxScreen / 2, syScreen / 2, 200, 200).Fill
        .ForeColor.SchemeColor = 13
        .OneColorGradient msoGradientFromTitle, 4, 1
    End With

TA的精华主题

TA的得分主题

发表于 2009-2-25 14:17 | 显示全部楼层
楼主GetSystemMetrics的用法是没有错的,但是AddShape(Type, Left, Top, Width, Height)中的参数Left和Top是相对于文档左上角而非屏幕左上角的,要在屏幕中心画,需要把Application的Left、Top,ActiveWindow的Left、Top以及ActiveSheet的ScrollTop、ScrollLeft属性

TA的精华主题

TA的得分主题

 楼主| 发表于 2009-2-25 14:56 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2009-2-25 16:30 | 显示全部楼层
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Private Const SM_CXSCREEN = 0
Private Const SM_CYSCREEN = 1
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDC As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
Private Const LOGPIXELSX = 88        '  Logical pixels/inch in X
Private Const POINTS_PER_INCH As Long = 72
Dim PointsPerPixel As Single

Sub DrawShape()
    Dim oShape As Shape
    Set oShape = DrawCenterShape(msoShapeRectangle, 150, 150)
End Sub

Function DrawCenterShape(ShapeType As MsoAutoShapeType, lWidth As Long, lHeight As Long) As Shape
    If PointsPerPixel = 0 Then PointsPerPixel = GetPixelSize
    Dim ScreenWidth As Integer, ScreenHeight As Integer
    Dim ScreenTop As Integer, ScreenLeft As Integer
    Dim lLeft As Integer, lTop As Integer
    Dim lLeftWorkarea As Long, lTopWorkarea As Long
    Dim hwnd As Long
    Dim oRect As RECT
    hwnd = FindWindowEx(0, 0, "XLMAIN", Application.Caption)
    hwnd = FindWindowEx(hwnd, 0, "XLDESK", vbNullString)
    GetWindowRect hwnd, oRect
    ScreenWidth = GetSystemMetrics(SM_CXSCREEN)
    ScreenHeight = GetSystemMetrics(SM_CYSCREEN)
    lLeft = ((ScreenWidth - lWidth) / 2 - oRect.Left - 42) * PointsPerPixel + (ActiveSheet.Columns(ActiveWindow.ScrollColumn).Left - ActiveWindow.Left)
    lTop = ((ScreenHeight - lHeight) / 2 - oRect.Top - 51) * PointsPerPixel + (ActiveSheet.Rows(ActiveWindow.ScrollRow).Top - ActiveWindow.Top)
    Set DrawCenterShape = ActiveSheet.Shapes.AddShape(ShapeType, lLeft, lTop, lWidth, lHeight)
End Function

Function GetPixelSize() As Single
    Dim hDC As Long
    hDC = GetDC(0)
    GetPixelSize = POINTS_PER_INCH / GetDeviceCaps(hDC, LOGPIXELSX)
    ReleaseDC 0, hDC
End Function

[ 本帖最后由 小fisher 于 2009-2-25 16:49 编辑 ]
未标题-1.jpg

TA的精华主题

TA的得分主题

 楼主| 发表于 2009-2-25 17:26 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
好详细,感谢解答,慢慢看

TA的精华主题

TA的得分主题

发表于 2009-5-4 19:01 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2022-1-16 19:23 | 显示全部楼层
本帖最后由 lingtcp 于 2022-1-16 19:40 编辑

很好的屏幕座标计算相关例子!fisher大能!
  lLeft = ((ScreenWidth - lWidth) / 2 - oRect.Left - 42) * PointsPerPixel + (ActiveSheet.Columns(ActiveWindow.ScrollColumn).Left - ActiveWindow.Left)
    lTop = ((ScreenHeight - lHeight) / 2 - oRect.Top - 51) * PointsPerPixel + (ActiveSheet.Rows(ActiveWindow.ScrollRow).Top - ActiveWindow.Top)
我理解:式中的42、51分别是工作表行标题栏的宽度和列标题栏高度+窗口标题栏的高度?如果活动窗口最大化或不显示“行各列标题”的话,位置还是不对?

您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-3-29 02:49 , Processed in 0.052097 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表