(已解决)_winapi 画线问题,win7与xp效果不同,请大家帮助
本帖最后由 sellkingfly 于 2012-12-13 17:24 编辑麻烦大家看下为什么这段代码在win7下不好用,还有为什么即使能清除也还有窗口上下部分不能清除干净,谢谢!#include <WindowsConstants.au3>
#include <WinAPI.au3>
#Include <Misc.au3>
$x = @DesktopWidth
$y = @DesktopHeight
While 1
Sleep(10)
If _IsPressed("11") Then ;Ctrl to draw
_draw()
EndIf
If _IsPressed("10") Then ; shift to clear
_clear()
EndIf
If _IsPressed("1B") Then ; esc to exit
Exit
EndIf
WEnd
Func _draw()
$hDC1 = _WinAPI_GetWindowDC(0) ; DC of entire screen (desktop)
$hPen1 = _WinAPI_CreatePen($PS_SOLID, 5, 0xFF)
$obj_orig = _WinAPI_SelectObject($hDC1, $hPen1)
_WinAPI_DrawLine($hDC1, 0, $y * (1 / 3), $x, $y * (1 / 3))
_WinAPI_DrawLine($hDC1, 0, $y * (2 / 3), $x, $y * (2 / 3))
_WinAPI_DrawLine($hDC1, $x * (1 / 3), 0, $x * (1 / 3), $y)
_WinAPI_DrawLine($hDC1, $x * (2 / 3), 0, $x * (2 / 3), $y)
_WinAPI_SelectObject($hDC1, $obj_orig)
_WinAPI_DeleteObject($hPen1)
_WinAPI_ReleaseDC(0, $hDC1)
EndFunc
Func _clear()
$hDC1 = _WinAPI_GetWindowDC(0) ; DC of entire screen (desktop)
$hPen1 = _WinAPI_CreatePen($PS_SOLID, 5, 0xFF)
$obj_orig = _WinAPI_SelectObject($hDC1, $hPen1)
_WinAPI_RedrawWindow(_WinAPI_GetDesktopWindow(), 0, 0, $RDW_INVALIDATE + $RDW_ALLCHILDREN)
_WinAPI_RedrawWindow(_WinAPI_GetDesktopWindow(), 0, 0, $RDW_ERASE)
_WinAPI_SelectObject($hDC1, $obj_orig)
_WinAPI_DeleteObject($hPen1)
_WinAPI_ReleaseDC(0, $hDC1)
EndFunc
本帖最后由 xiehuahere 于 2012-12-7 14:05 编辑
回复 1# sellkingfly
这好像跟win7还是XP没关系,缺少重绘的步骤。
最好建个透明的桌面大小的GUI,直接在GUI上画,然后删除GUI就行了。 本帖最后由 xiehuahere 于 2012-12-7 14:56 编辑
回复 1# sellkingfly
#include <GDIPlus.au3>
#include <WindowsConstants.au3>
#Include <Misc.au3>
Global $hGUI, $x = @DesktopWidth, $y = @DesktopHeight
While 1
Sleep(20)
If _IsPressed("11") Then ;Ctrl to draw
_draw()
EndIf
If _IsPressed("10") Then ; shift to clear
_clear()
EndIf
If _IsPressed("1B") Then ; esc to exit
Exit
EndIf
WEnd
Func _draw()
If IsHWnd($hGUI) Then Return
$hGUI = GUICreate("", $x, $y, 0, 0, $WS_POPUP, $WS_EX_TOPMOST + $WS_EX_LAYERED, WinGetHandle(""))
GUISetBkColor(0x123456)
_WinAPI_SetLayeredWindowAttributes($hGUI, 0x123456, 255)
GUISetState()
AdlibRegister("_redraw")
EndFunc
Func _redraw()
$hDC1 = _WinAPI_GetWindowDC($hGUI)
$hPen1 = _WinAPI_CreatePen($PS_SOLID, 5, 0xFF)
$obj_orig = _WinAPI_SelectObject($hDC1, $hPen1)
_WinAPI_DrawLine($hDC1, 0, $y * (1 / 3), $x, $y * (1 / 3))
_WinAPI_DrawLine($hDC1, 0, $y * (2 / 3), $x, $y * (2 / 3))
_WinAPI_DrawLine($hDC1, $x * (1 / 3), 0, $x * (1 / 3), $y)
_WinAPI_DrawLine($hDC1, $x * (2 / 3), 0, $x * (2 / 3), $y)
_WinAPI_SelectObject($hDC1, $obj_orig)
_WinAPI_DeleteObject($hPen1)
_WinAPI_ReleaseDC($hGUI, $hDC1)
EndFunc
Func _clear()
AdlibUnRegister("_redraw")
If IsHWnd($hGUI) Then GUIDelete($hGUI)
$hGUI = 0
EndFunc 注册GUI消息WM_PAINT比较好:
#Include <WinAPI.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
Global $hGUI, $x = @DesktopWidth, $y = @DesktopHeight
While 1
Sleep(20)
If _IsPressed("11") Then ;Ctrl to draw
_draw()
EndIf
If _IsPressed("10") Then ; shift to clear
_clear()
EndIf
If _IsPressed("1B") Then ; esc to exit
Exit
EndIf
WEnd
Func _draw()
If IsHWnd($hGUI) Then Return
$hGUI = GUICreate("", $x, $y, 0, 0, $WS_POPUP, $WS_EX_TOPMOST + $WS_EX_LAYERED, WinGetHandle(""))
GUISetBkColor(0x123456)
_WinAPI_SetLayeredWindowAttributes($hGUI, 0x123456, 255)
GUIRegisterMsg($WM_PAINT, "WM_PAINT")
GUISetState()
EndFunc ;==>_draw
Func WM_PAINT($hWnd, $msg, $wParam, $lParam)
#forceref $hWnd, $Msg, $wParam, $lParam
_WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
$hDC1 = _WinAPI_GetWindowDC($hGUI)
$hPen1 = _WinAPI_CreatePen($PS_SOLID, 5, 0xFF)
$obj_orig = _WinAPI_SelectObject($hDC1, $hPen1)
_WinAPI_DrawLine($hDC1, 0, $y * (1 / 3), $x, $y * (1 / 3))
_WinAPI_DrawLine($hDC1, 0, $y * (2 / 3), $x, $y * (2 / 3))
_WinAPI_DrawLine($hDC1, $x * (1 / 3), 0, $x * (1 / 3), $y)
_WinAPI_DrawLine($hDC1, $x * (2 / 3), 0, $x * (2 / 3), $y)
_WinAPI_SelectObject($hDC1, $obj_orig)
_WinAPI_DeleteObject($hPen1)
_WinAPI_ReleaseDC($hGUI, $hDC1)
_WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
Return $GUI_RUNDEFMSG
EndFunc ;==>WM_PAINT
Func _clear()
If IsHWnd($hGUI) Then GUIDelete($hGUI)
$hGUI = 0
EndFunc ;==>_clear 回复 4# xiehuahere
多谢您的帮助,不过当编辑文件时按画线快捷键然后取消,原来正在编辑窗口就不在激活状态了,用winactivate也没有效果。 本帖最后由 xiehuahere 于 2012-12-7 16:20 编辑
回复 5# sellkingfly
winactivate也没有效果
这个理论上不成立,我这边试过是可以的。
这个只能基于你的代码来讨论了。 回复 1# sellkingfly
win7 64bit 测试没有问题,au3版本:3.3.7.15 SetForegroundWindow 試試,應比 winactivate 恰當。
页:
[1]