找回密码
 加入
搜索
查看: 7154|回复: 8

[图形处理] (已解决)_winapi 画线问题,win7与xp效果不同,请大家帮助

  [复制链接]
发表于 2012-12-7 11:35:41 | 显示全部楼层 |阅读模式
本帖最后由 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
发表于 2012-12-7 13:52:39 | 显示全部楼层
本帖最后由 xiehuahere 于 2012-12-7 14:05 编辑

回复 1# sellkingfly

这好像跟win7还是XP没关系,缺少重绘的步骤。
最好建个透明的桌面大小的GUI,直接在GUI上画,然后删除GUI就行了。
发表于 2012-12-7 14:54:32 | 显示全部楼层
本帖最后由 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("[CLASS:Progman]"))
        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
发表于 2012-12-7 15:16:13 | 显示全部楼层
注册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("[CLASS:Progman]"))
        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
 楼主| 发表于 2012-12-7 15:55:35 | 显示全部楼层
回复 4# xiehuahere


    多谢您的帮助,不过当编辑文件时按画线快捷键然后取消,原来正在编辑窗口就不在激活状态了,用winactivate也没有效果。
发表于 2012-12-7 16:18:34 | 显示全部楼层
本帖最后由 xiehuahere 于 2012-12-7 16:20 编辑

回复 5# sellkingfly

winactivate也没有效果

这个理论上不成立,我这边试过是可以的。
这个只能基于你的代码来讨论了。
发表于 2012-12-8 07:30:37 | 显示全部楼层
回复 1# sellkingfly
win7 64bit 测试没有问题,au3版本:3.3.7.15
发表于 2012-12-9 15:53:40 | 显示全部楼层
SetForegroundWindow    試試,應比 winactivate 恰當。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-28 07:26 , Processed in 0.078398 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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