|
发表于 2023-10-7 21:09:45
|
显示全部楼层
以前写的 tcp服务端,你可以参考一下。
- #region ;**** directives created by autoit3wrapper_gui ****
- #autoit3wrapper_icon= ;程序图标
- #autoit3wrapper_usex64=n ;是否编译为64位程序(y/n)
- #autoit3wrapper_outfile= ;输出的exe名称
- #autoit3wrapper_outfile_x64= ;64位输出的exe名称
- #autoit3wrapper_useupx=n ;是否使用upx压缩(y/n) 注:开启压缩极易引起误报问题
- #autoit3wrapper_res_comment=http://www.autoitscript.com/autoit3/ ;程序注释
- #autoit3wrapper_res_description=autoit team ;文件说明
- #autoit3wrapper_res_fileversion=1.0.0.40
- #autoit3wrapper_res_field=productname|autoit v3 ;产品名称
- #autoit3wrapper_res_productversion=1.0 ;产品版本
- #autoit3wrapper_res_legal爱体育 copyright=©1999-2021 autoit team ;爱体育的版权
- #autoit3wrapper_res_language=2052 ;语言, 英语=2057/中文=2052
- #autoit3wrapper_res_field=originalfilename|autoit.exe ;原始文件名
- #autoit3wrapper_res_field=internalname|autoit_exe ;内部名称
- #autoit3wrapper_res_fileversion_autoincrement=y ;自动更新版本 y/n/p=自动/不自动/询问
- #autoit3wrapper_res_requestedexecutionlevel=asinvoker ;请求权限: none/asinvoker/highestavailable/requireadministrator
- #autoit3wrapper_run_tidy=n ;编译前自动整理脚本(y/n)
- #autoit3wrapper_res_hidpi=p ;支持dpi感知
- #au3stripper_parameters=/sf=1 /sv=1
- #endregion ;**** directives created by autoit3wrapper_gui ****
- #cs ____________________________________
- 欢迎使用 autoit v3 中文版 !
- au3版本: 3.3.14.5
- 脚本作者:
- 脚本功能:
- 更新日志:
- 爱体育的联系方式:
- #ce _______________脚本开始_________________
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- opt("mustdeclarevars", 1)
- opt("guioneventmode", 1)
- opt("guicloseonesc", 0)
- global const $fd_read = 1
- global const $fd_write = 2
- global const $fd_accept = 8
- global const $fd_connect = 16
- global const $fd_close = 32
- global const $fd_setsize = 32 ;定义监听客户端数量
- global const $tagfd_set = "uint fd_count;uint fd_array[" & $fd_setsize & "]"
- global $hdll_ws2_32 = dllopen("ws2_32.dll")
- global const $fd_max_events = 10
- global $netevent = dllstructcreate("long lnetworkevents;int ierrorcode[" & $fd_max_events & "];")
- global $udp_event = dllstructcreate("handle event;")
- global $udp_socket
- global $udp_port = 6800 ;定义监听端口
- global $tcp_max = 2
- global $tcp_port[$tcp_max] = [20001, 20002] ;定义监听端口
- global $tcp_socket[$tcp_max]
- global $tcp_event = dllstructcreate("handle event[" & $tcp_max & "];")
- global $client_socket = dllstructcreate($tagfd_set)
- global $test, $hgui, $idedit, $idlistview
- global $sendgui, $button1, $input, $num, $data
- global $fd_array[$fd_setsize][10]
- opt("guiresizemode", 1)
- global $hidpi
- if @compiled then
- if _isprocessdpiaware() then
- $hidpi = _getdpi()
- else
- $hidpi = 1
- endif
- else
- _setprocessdpiaware()
- $hidpi = _getdpi()
- endif
- tcpstartup()
- $udp_socket = udpopen("255.255.255.255", $udp_port, 1)
- udpsend($udp_socket, binary("0x01000000a00f00000000000000000000")) ;发送广播消息,查询是否存在服务。
- $data = udprecv($udp_socket, 4096, 1) ;接收消息,耗时 50毫秒。
- udpclosesocket($udp_socket)
- if $data <> "" then
- exit msgbox(262144 65536 64, "警告", "发现内网服务端:" & binarytostring($data) & " 退出程序.")
- endif
- creategui()
- #region 创建监听端口
- for $i = 0 to $tcp_max - 1
- $tcp_socket[$i] = tcplisten("0.0.0.0", $tcp_port[$i], 1024)
- if @error then exit msgbox(262144 65536 64, "tcp端口冲突", "tcp端口被占用:" & $tcp_port[$i])
- _echo($idedit, "监听tcp端口:" & $tcp_port[$i])
- $tcp_event.event($i 1) = _winapi_createevent(0, true, false) ;创建事件对象, 为无信号,手动重置
- _sockeventselect($tcp_socket[$i], $tcp_event.event($i 1), $fd_accept) ;设置到事件模式
- next
- $udp_socket = udpbind("0.0.0.0", $udp_port)
- if @error then exit msgbox(262144 65536 64, "udp端口冲突", "udp端口被占用:" & $udp_port)
- _echo($idedit, "监听udp端口:" & $udp_port)
- $udp_event.event(1) = _winapi_createevent(0, true, false) ;创建事件对象, 为无信号,手动重置
- _sockeventselect($udp_socket[1], $udp_event.event(1), $fd_read) ;设置到事件模式
- #endregion 创建监听端口
- _echo($idedit, "启动完成。")
- onautoitexitregister("_exit")
- adlibregister("_timeout", 1000 * 60) ;活动连接心跳检测
- while 1
- _tcp_event($client_socket)
- _udp_event()
- _client_event($client_socket)
- sleep(5)
- wend
- func _data_out($isocket, $idate)
- if binarylen($idate) < 20 then
- _tcpsend($isocket, "x")
- ;_echo($idedit, "收到心跳包:" & $idate)
- else
- _echo($idedit, "收到数据:" & $idate)
- endif
- endfunc ;==>_data_out
- func _udp_event() ;处理udp网路事件
- local $iobjects, $data, $aret
- local $datelen = 4096
- $iobjects = _winapi_waitformultipleobjects(1, $udp_event, false, 0) ;检测信号状态,等待5毫秒
- switch $iobjects
- case 0
- _winapi_resetevent($udp_event.event($iobjects 1)) ;设置指定事件对象为无信号状态
- $aret = dllcall("ws2_32.dll", "int", "ioctlsocket", "uint", $udp_socket[1], "long", 0x4004667f, "ulong*", 0) ;查询接收缓冲区数据长度
- if $aret[0] = 0 then $datelen = $aret[3]
- $data = udprecv($udp_socket, $datelen, 3)
- if not @error then
- if string($data[0]) == "0x01000000a00f00000000000000000000" then
- $udp_socket[2] = $data[1]
- $udp_socket[3] = $data[2]
- udpsend($udp_socket, @ipaddress1 & ";" & $tcp_port[0] & ";" & $tcp_port[1])
- endif
- endif
- case -1 ;检测信号状态失败
- endswitch
- endfunc ;==>_udp_event
- func _tcp_event(byref $tfd_set) ;处理tcp接入请求事件
- local $iobjects, $isocket, $newip, $ilistitem, $index
- $iobjects = _winapi_waitformultipleobjects($tcp_max, $tcp_event, false, 0) ;检测信号状态,等待5毫秒
- switch $iobjects
- case 0 to $tcp_max - 1
- ;_winapi_resetevent($tcp_event.event($iobjects 1)) ;设置指定事件对象为无信号状态
- ;//添加的代码
- if _wsaenumnetworkevents($tcp_socket[$iobjects], $tcp_event.event($iobjects 1)) then ;择网络事件
- if bitand($netevent.lnetworkevents(1), $fd_accept) then
- if $netevent.ierrorcode(3) = 0 then
- $isocket = tcpaccept($tcp_socket[$iobjects])
- if $isocket <> -1 then
- $newip = _sockettoip($isocket)
- if $tfd_set.fd_count(1) >= $fd_setsize then
- _echo($idedit, "!超出最大连接数:" & $fd_setsize & " 断开新连接")
- tcpclosesocket($isocket)
- else
- _fd_set($isocket, $tfd_set)
- $index = _array_set($isocket, $fd_array)
- $fd_array[$index][2] = guictrlcreatelistviewitem($newip[0], $idlistview)
- $fd_array[$index][3] = $newip[0]
- $fd_array[$index][4] = timerinit()
- _echo($idedit, "新连接,来自ip:" & $newip[0] & " 源端口:" & $newip[1] & " 目的端口:" & $newip[3] & " 连接数:" & $tfd_set.fd_count(1))
- endif
- endif
- else
- consolewrite("wsaenumnetworkevents failed with error code:" & $netevent.ierrorcode(3))
- endif
- endif
- endif
- ;//添加的代码
- case -1 ;检测信号状态失败
- endswitch
- endfunc ;==>_tcp_event
- func _timeout()
- local $i
- for $i = $fd_setsize - 1 to 0 step -1
- if $fd_array[$i][4] == "" then continueloop
- if timerdiff($fd_array[$i][4]) > 1000 * 10 then ;活动连接无心跳10秒,关闭连接
- guictrldelete($fd_array[$i][2])
- tcpclosesocket($fd_array[$i][1])
- _echo($idedit, "连接超时,来自ip:" & $fd_array[$i][3])
- _fd_clr($fd_array[$i][1], $client_socket)
- _array_clr($fd_array[$i][1], $fd_array)
- endif
- next
- endfunc ;==>_timeout
- func _tcprecv($isocket, $idatalen)
- local $datelen
- local $index = _array_isset($isocket, $fd_array)
- if $index = -1 then return
- $fd_array[$index][4] = timerinit() ;写入时间戳
- local $idata = tcprecv($isocket, $idatalen, 1)
- if @error then return seterror(@error, @extended, "") ;错误处理 待完善
- if isbinary($fd_array[$index][5]) then ;检查缓存
- $fd_array[$index][5] = binary($fd_array[$index][5] $idata)
- else
- $fd_array[$index][5] = $idata
- endif
- while 1
- if binarylen($fd_array[$index][5]) < 4 then exitloop
- $datelen = int(binarymid($fd_array[$index][5], 1, 4))
- if binarylen($fd_array[$index][5]) < $datelen 4 then
- exitloop
- elseif binarylen($fd_array[$index][5]) = $datelen 4 then
- _data_out($isocket, binarymid($fd_array[$index][5], 5))
- $fd_array[$index][5] = ""
- exitloop
- endif
- _data_out($isocket, binarymid($fd_array[$index][5], 5, $datelen))
- $fd_array[$index][5] = binarymid($fd_array[$index][5], $datelen 5)
- wend
- endfunc ;==>_tcprecv
- func _tcpsend($isocket, $ddata)
- if $ddata == "" then return
- if not isbinary($ddata) then $ddata = stringtobinary($ddata)
- local $datalen = binarylen($ddata)
- local $byte = binary(binary(ptr($datalen)) $ddata)
- local $iret = tcpsend($isocket, $byte)
- if @error then return seterror(@error, 0, 0)
- return $iret
- endfunc ;==>_tcpsend
- func _echo($hid, $stext = "")
- if not stringisascii($stext) then $stext = string($stext)
- local $logtime = @year & "-" & @mon & "-" & @mday & " " & @hour & ":" & @min & ":" & @sec & " " & $stext & @crlf
- local $hwnd = guictrlgethandle($hid)
- local $len = stringlen($logtime)
- local $text = $logtime & guictrlread($hid)
- if _guictrledit_getlinecount($hwnd) > 300 then
- $len = _guictrledit_lineindex($hwnd, 299)
- $text = stringleft($text, $len)
- endif
- guictrlsetdata($hid, $text)
- endfunc ;==>_echo
- func creategui()
- local $width = 880, $height = 680, $idcontextmenu
- $hgui = guicreate("广播转发 - 支持" & $fd_setsize & "接入", $width, $height 48, -1, -1) ;创建窗口
- guisetonevent($gui_event_close, "_exit")
- guictrlcreategroup("客户端列表", 5, 10, $width - 10, 500)
- $idlistview = guictrlcreatelistview("编号|", 10, 25, $width - 20, 480, $lvs_showselalways, $lvs_ex_fullrowselect $lvs_ex_gridlines $lvs_ex_doublebuffer) ;,$lvs_sortdescending)
- _guictrllistview_setcolumnwidth($idlistview, 0, $lvscw_autosize_useheader)
- guictrlcreategroup("消息日志", 5, 520, $width - 10, 166)
- $idedit = guictrlcreateedit("", 15, 540, $width - 30, 136, 0x50210804, $ws_ex_staticedge)
- $idcontextmenu = guictrlcreatecontextmenu($idlistview)
- ;创建菜单“所有客户机”主控件-全部
- guictrlcreatemenuitem("关闭连接", $idcontextmenu)
- guictrlsetonevent(-1, "_selected")
- guictrlcreatemenuitem("发送消息", $idcontextmenu)
- guictrlsetonevent(-1, "_selected_send")
- guisetstate(@sw_show)
- endfunc ;==>creategui
- func _client_event(byref $tfd_set) ;处理客户端的网路事件
- local $isocket, $idata, $newip, $aret, $index, $datelen
- local $count = $tfd_set.fd_count(1)
- if $count < 1 then return
- local $fd_socket = dllstructcreate($tagfd_set)
- local $tfd_set_dat = dllstructcreate("byte[" & $count * 4 4 & "];", dllstructgetptr($tfd_set))
- local $fd_socket_dat = dllstructcreate("byte[" & $count * 4 4 & "];", dllstructgetptr($fd_socket))
- dllstructsetdata($fd_socket_dat, 1, dllstructgetdata($tfd_set_dat, 1)) ;创建结构体副本
- if _select($fd_socket) then ;查询是否有网络事件
- for $i = 0 to $fd_socket.fd_count(1) - 1
- $isocket = $fd_socket.fd_array($i 1)
- if _fd_isset($isocket, $fd_socket) then ;检查套接字是否存在
- $newip = _sockettoip($isocket) ;查询套接字的源地址-目标地址
- local $datelen = 4096
- $aret = dllcall("ws2_32.dll", "int", "ioctlsocket", "uint", $isocket, "long", 0x4004667f, "ulong*", 0) ;查询套接字接收缓冲区中的数据长度
- if $aret[0] = 0 then
- if $aret[3] > 0 then
- _tcprecv($isocket, $aret[3])
- else
- tcpclosesocket($isocket)
- _fd_clr($isocket, $tfd_set)
- $index = _array_isset($isocket, $fd_array) ;查询套接字在数组中的位置
- if not @error then
- guictrldelete($fd_array[$index][2])
- _array_clr($isocket, $fd_array) ;清理数组
- endif
- _echo($idedit, "断开连接,来自ip:" & $newip[0] & " 源端口:" & $newip[1] & " 目的端口:" & $newip[3])
- endif
- else
- _tcprecv($isocket, 4096)
- endif
- endif
- next
- endif
- endfunc ;==>_client_event
- func _select(byref $tfd_set)
- local $timeval = dllstructcreate("int tv_sec;int tv_usec")
- local $result = dllcall("ws2_32.dll", "int", "select", "int", null, "struct*", $tfd_set, "ptr", 0, "ptr", 0, "struct*", $timeval)
- if $result[0] <= 0 then return false
- return true
- endfunc ;==>_select
- func _array_set($isocket, byref $tarray_set)
- if not isarray($tarray_set) then return seterror(2, false, false)
- if $tarray_set[0][0] = "" then $tarray_set[0][0] = 0
- local $rows = ubound($tarray_set)
- local $icount = $tarray_set[0][0]
- for $i = 0 to $icount - 1
- if $tarray_set[$i][1] = $isocket then
- exitloop
- endif
- next
- if $i = $icount then
- if $icount < $rows then
- $tarray_set[$i][1] = $isocket
- $tarray_set[0][0] = $icount 1
- endif
- endif
- return $tarray_set[0][0] - 1
- endfunc ;==>_array_set
- func _array_clr($isocket, byref $tarray_set)
- local $columns = ubound($tarray_set, 2)
- local $icount = $tarray_set[0][0]
- for $i = 0 to $icount - 1
- if $tarray_set[$i][1] = $isocket then
- while ($i < $icount - 1)
- for $s = 1 to $columns - 1
- $tarray_set[$i][$s] = $tarray_set[$i 1][$s]
- next
- $i = 1
- wend
- for $s = 1 to $columns - 1
- $tarray_set[$i][$s] = null
- next
- $tarray_set[0][0] = $icount - 1
- exitloop
- endif
- next
- endfunc ;==>_array_clr
- func _array_isset($isocket, byref $tarray_set)
- if not isarray($tarray_set) then return seterror(2, false, -1)
- if $tarray_set[0][0] = "" then $tarray_set[0][0] = 0
- local $ret
- local $columns = ubound($tarray_set, 2)
- local $icount = $tarray_set[0][0]
- for $i = 0 to $icount - 1
- if $tarray_set[$i][1] = $isocket then
- $ret = $i
- exitloop
- endif
- next
- if $ret == "" then return seterror(2, false, -1)
- return $ret
- endfunc ;==>_array_isset
- func _fd_set($isocket, byref $tfd_set)
- local $icount = dllstructgetdata($tfd_set, "fd_count")
- for $i = 0 to $icount - 1
- if dllstructgetdata($tfd_set, "fd_array", $i 1) = $isocket then
- exitloop
- endif
- next
- if $i = $icount then
- if $icount < $fd_setsize then
- dllstructsetdata($tfd_set, "fd_array", $isocket, $i 1)
- dllstructsetdata($tfd_set, "fd_count", $icount 1)
- endif
- endif
- endfunc ;==>_fd_set
- func _fd_clr($isocket, byref $tfd_set)
- local $icount = dllstructgetdata($tfd_set, "fd_count")
- for $i = 0 to $icount - 1
- if dllstructgetdata($tfd_set, "fd_array", $i 1) = $isocket then
- while ($i < $icount - 1)
- dllstructsetdata($tfd_set, "fd_array", dllstructgetdata($tfd_set, "fd_array", $i 2), $i 1)
- $i = 1
- wend
- dllstructsetdata($tfd_set, "fd_array", 0, $i 1)
- dllstructsetdata($tfd_set, "fd_count", $icount - 1)
- exitloop
- endif
- next
- endfunc ;==>_fd_clr
- func _fd_isset($isocket, byref $tfd_set)
- local $aret = dllcall($hdll_ws2_32, "int", "__wsafdisset", "uint", $isocket, "struct*", $tfd_set)
- return $aret[0]
- endfunc ;==>_fd_isset
- func _wsaenumnetworkevents($hsocket, $hwnd)
- local $iret = dllcall($hdll_ws2_32, "int", "wsaenumnetworkevents", "uint", $hsocket, "hwnd", $hwnd, "struct*", $netevent)
- if @error then return seterror(1, @error, false)
- if $iret[0] <> 0 then
- $iret = dllcall($hdll_ws2_32, "dword", "wsagetlasterror")
- return seterror(2, $iret[0], false)
- endif
- return true
- endfunc ;==>_wsaenumnetworkevents
- func _sockeventselect($hsocket, $hwnd, $ievent = 0)
- local $iret = dllcall($hdll_ws2_32, "int", "wsaeventselect", "uint", $hsocket, "hwnd", $hwnd, "int", $ievent)
- if @error then return seterror(1, @error, false)
- if $iret[0] <> 0 then
- return seterror(2, _wsagetlasterror(), false)
- endif
- return true
- endfunc ;==>_sockeventselect
- func _wsagetlasterror()
- local $iret = dllcall($hdll_ws2_32, "int", "wsagetlasterror")
- if @error then
- setextended(1)
- return 0
- endif
- return $iret[0]
- endfunc ;==>_wsagetlasterror
- func _sockettoip($hsocket)
- local $sockaddr, $aret, $aarray[4], $error
- $error = false
- $sockaddr = dllstructcreate("short;ushort;uint;char[8]")
- $aret = dllcall($hdll_ws2_32, "int", "getpeername", "int", $hsocket, _
- "struct*", $sockaddr, "int*", dllstructgetsize($sockaddr))
- if @error or $aret[0] <> 0 then
- $error = true
- else
- $aret = dllcall($hdll_ws2_32, "str", "inet_ntoa", "int", dllstructgetdata($sockaddr, 3))
- if not @error then $aarray[0] = $aret[0]
- $aret = dllcall($hdll_ws2_32, "ushort", "ntohs", "ushort", dllstructgetdata($sockaddr, 2))
- if not @error then $aarray[1] = $aret[0]
- endif
- $aret = dllcall($hdll_ws2_32, "int", "getsockname", "int", $hsocket, _
- "struct*", $sockaddr, "int*", dllstructgetsize($sockaddr))
- if @error or $aret[0] <> 0 then
- $error = true
- else
- $aret = dllcall($hdll_ws2_32, "str", "inet_ntoa", "int", dllstructgetdata($sockaddr, 3))
- if not @error then $aarray[2] = $aret[0]
- $aret = dllcall($hdll_ws2_32, "ushort", "ntohs", "ushort", dllstructgetdata($sockaddr, 2))
- if not @error then $aarray[3] = $aret[0]
- endif
- $sockaddr = 0
- return seterror($error, false, $aarray)
- endfunc ;==>_sockettoip
- func _selected()
- local $column, $num, $isocket
- $column = _guictrllistview_getselectedcount($idlistview)
- if $column <> 1 then return
- $num = _guictrllistview_getselectedindices($idlistview, false)
- _echo($idedit, "关闭连接" & $fd_array[$num][3])
- $isocket = $fd_array[$num][1]
- tcpclosesocket($isocket)
- guictrldelete($fd_array[$num][2])
- _fd_clr($isocket, $client_socket)
- _array_clr($isocket, $fd_array)
- endfunc ;==>_selected
- func _selected_send()
- local $column, $num
- local $ahotkey[1][2]
- $column = _guictrllistview_getselectedcount($idlistview)
- if $column <> 1 then return
- $num = _guictrllistview_getselectedindices($idlistview, false)
- $sendgui = guicreate("发送数据窗口", 600, 160, -1, -1, default, default, $hgui)
- guisetonevent($gui_event_close, "close_gui")
- $button1 = guictrlcreatebutton("发送", 260, 112, 85, 25)
- guictrlsetonevent($button1, "_button")
- $input = guictrlcreateinput("", 32, 48, 520, 32)
- $ahotkey[0][0] = "{enter}"
- $ahotkey[0][1] = $button1
- guisetaccelerators($ahotkey, $sendgui)
- guisetstate(@sw_show, $sendgui)
- controlfocus($sendgui, "", $input)
- endfunc ;==>_selected_send
- func _button()
- local $column, $isocket, $idata, $num
- $column = _guictrllistview_getselectedcount($idlistview)
- if $column <> 1 then return
- $num = _guictrllistview_getselectedindices($idlistview, false)
- $idata = guictrlread($input)
- $isocket = $fd_array[$num][1]
- if $idata <> "" and $isocket <> "" then
- _echo($idedit, "发送消息" & $idata)
- _tcpsend($isocket, $idata)
- endif
- endfunc ;==>_button
- func close_gui()
- guisetaccelerators(0, $sendgui)
- guidelete($sendgui)
- endfunc ;==>close_gui
- func _exit()
- for $i = 0 to $tcp_max - 1
- tcpclosesocket($tcp_socket[$i])
- next
- for $i = 0 to $fd_setsize - 1
- if $fd_array[$i][1] <> "" then tcpclosesocket($fd_array[$i][1])
- next
- udpclosesocket($udp_socket)
- dllclose($hdll_ws2_32)
- tcpshutdown()
- exit
- endfunc ;==>_exit
- #region dpi函数
- func _setprocessdpiaware()
- local $aret
- if @osbuild < 6000 then return ;判断系统版本 windows vista
- if @osbuild < 9600 then ;判断系统版本 win8.1(9600)
- $aret = dllcall("user32.dll", "bool", "isprocessdpiaware") ;检查进程是否支持dpi感知
- return $aret[0]
- if $aret[0] = false then
- $aret = dllcall('user32.dll', 'bool', 'setprocessdpiaware') ;设置:系统dpi感知
- if @error then return seterror(@error, 0, false)
- return
- endif
- else
- $aret = dllcall("shcore.dll", "bool", "getprocessdpiawareness", "hwnd", 0, "int*", 0) ;检查进程是否支持dpi感知
- if @error then return seterror(@error, 0, false)
- if $aret[2] <> 2 then ;0不支持dpi感知 1,系统dpi感知 2,监视器dpi感知
- $aret = dllcall("shcore.dll", "bool", "setprocessdpiawareness", "int", 2) ;设置:监视器dpi感知
- if @error then return seterror(@error, 0, false)
- return
- endif
- endif
- endfunc ;==>_setprocessdpiaware
- func _getdpi()
- local $idpi
- if @osbuild < 6000 then return 1 ;判断系统版本 windows vista
- if @osbuild < 9600 then ;判断系统版本 win8.1(9600)
- $idpi = regread("hkcu\control panel\desktop\windowmetrics", "applieddpi")
- else
- local $hwnd = _winapi_monitorfromwindow(_winapi_getdesktopwindow())
- local $aret = dllcall("shcore.dll", "bool", "getdpiformonitor", "handle", $hwnd, "int", 0, "int*", 0, "int*", 0)
- if @error then return seterror(@error, 0, false)
- $idpi = $aret[3]
- endif
- if $idpi < 96 or $idpi > 480 then $idpi = 96
- return $idpi / 96
- endfunc ;==>_getdpi
- func _isprocessdpiaware()
- local $aret, $adpi = 0
- if @osbuild < 6000 then return false ;判断系统版本 windows vista
- if @osbuild < 9600 then ;判断系统版本 win8.1(9600)
- $aret = dllcall("user32.dll", "bool", "isprocessdpiaware") ;检查进程是否支持dpi感知
- if @error then return seterror(@error, 0, false)
- $adpi = $aret[0]
- else
- $aret = dllcall("shcore.dll", "bool", "getprocessdpiawareness", "hwnd", 0, "int*", 0) ;检查进程是否支持dpi感知
- if @error then return seterror(@error, 0, false)
- $adpi = $aret[2]
- endif
- return $adpi
- endfunc ;==>_isprocessdpiaware
- #endregion dpi函数
复制代码 |
|