Jump to content
Sign in to follow this  
byronsmith

Pen keeps refreshing

Recommended Posts

byronsmith

Hi All,

Consider the following code below, when I minimize the app and maximize it again the screen is cleared. Is there a way I can keep what has already been draw when maximized, I would also like the pen to keep drawing even when minimized. It may actually still be drawing whilst minimized but I cannot see because of said problem.

I was doing a little reading and perhaps I can make it a bitmap, then draw directly to the bitmap? Of course I could be talking utter rubbish.

Also, if there a way with WinAPI to smooth the line drawn? i.e some sort of anti-alias?

Your help will be greatly appreciated.

Regards

Byron

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

Local $hDC, $hPen, $hObj, $hImage

$hWnd = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0)
GUISetState()

$hDC = _WinAPI_GetWindowDC($hWnd) 
$hPen = _WinAPI_CreatePen($PS_SOLID, 1, 0xAFAFAF)
$hObj = _WinAPI_SelectObject($hDC, $hPen)

_WinAPI_MoveTo($hDC, _WinAPI_GetMousePosX(), _WinAPI_GetMousePosY())

While 1
    
    sleep(15)
    $mouse_x = _WinAPI_GetMousePosX()
    $mouse_y = _WinAPI_GetMousePosY()

    _WinAPI_LineTo($hDC, $mouse_x,  $mouse_y)

    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop

WEnd

_WinAPI_SelectObject($hDC, $hObj)
_WinAPI_DeleteObject($hPen)
_WinAPI_ReleaseDC(0, $hDC)

Share this post


Link to post
Share on other sites
martin

Hi All,

Consider the following code below, when I minimize the app and maximize it again the screen is cleared. Is there a way I can keep what has already been draw when maximized, I would also like the pen to keep drawing even when minimized. It may actually still be drawing whilst minimized but I cannot see because of said problem.

I was doing a little reading and perhaps I can make it a bitmap, then draw directly to the bitmap? Of course I could be talking utter rubbish.

Also, if there a way with WinAPI to smooth the line drawn? i.e some sort of anti-alias?

Your help will be greatly appreciated.

Regards

Byron

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <WinAPI.au3>

Local $hDC, $hPen, $hObj, $hImage

$hWnd = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0)
GUISetState()

$hDC = _WinAPI_GetWindowDC($hWnd) 
$hPen = _WinAPI_CreatePen($PS_SOLID, 1, 0xAFAFAF)
$hObj = _WinAPI_SelectObject($hDC, $hPen)

_WinAPI_MoveTo($hDC, _WinAPI_GetMousePosX(), _WinAPI_GetMousePosY())

While 1
    
    sleep(15)
    $mouse_x = _WinAPI_GetMousePosX()
    $mouse_y = _WinAPI_GetMousePosY()

    _WinAPI_LineTo($hDC, $mouse_x, $mouse_y)

    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop

WEnd

_WinAPI_SelectObject($hDC, $hObj)
_WinAPI_DeleteObject($hPen)
_WinAPI_ReleaseDC(0, $hDC)

Maybe it's easier to use a graphic control.

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>

opt("mouseCoordMode",2)
Local $hDC, $hPen, $hObj, $hImage

$hWnd = GUICreate("", @DesktopWidth, @DesktopHeight, 0,0, -1,$WS_EX_COMPOSITED)
$graphic = GUICtrlCreateGraphic(0,0);,@DesktopWidth, @DesktopHeight)
GUISetState()
Global $lastx,$lasty
$mouse = mousegetpos()
GUICtrlSetGraphic ($graphic,$GUI_GR_MOVE,$mouse[0], $mouse[1])
GUICtrlSetGraphic ($graphic,$GUI_GR_COLOR,0);$GUI_GR_NOBKCOLOR)
While 1

    $mouse = mousegetpos()
    If $lastx <> $mouse[0] Or $lasty <> $mouse[1] Then
        GUICtrlSetGraphic ($graphic,$GUI_GR_LINE,$mouse[0], $mouse[1])
        GUICtrlSetGraphic ($graphic,$GUI_GR_REFRESH)
        $lasty = $mouse[1]
        $lastx = $mouse[0]
    EndIf

    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop

WEnd

EDIT: Oh, welcome to the AutoIt forums byronsmith :D

Edited by martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
byronsmith

Hi Martin,

Very kind of you to provide an alternative to what I was trying to achieve. Although I did have a little play around with the GUI functions I found that the $GUI_GR_REFRESH was a little slow i.e would present a flicker when drawing and so in turn opted for the WinAPI UDF.

I will do some more reading on the GUI method and see if there is a way around this issue.

Thank you for welcoming me to the forum and again many thanks for your help!

Regards

Byron

Share this post


Link to post
Share on other sites
martin

Hi Martin,

Very kind of you to provide an alternative to what I was trying to achieve. Although I did have a little play around with the GUI functions I found that the $GUI_GR_REFRESH was a little slow i.e would present a flicker when drawing and so in turn opted for the WinAPI UDF.

I will do some more reading on the GUI method and see if there is a way around this issue.

Thank you for welcoming me to the forum and again many thanks for your help!

Regards

Byron

Well my example should not have any flicker, did you try it? I used the $WS_EX_COMPOSITED style and the lines seem to be drawn pretty fast.

Maybe using GDI would be better. You just need a bitmap in the background to draw on and when the window is repainted copy it to the window DC.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites
byronsmith

Hi Martin,

I did indeed try your script and it worked a treat except for the flicker. I have been doing a little research and found out that $WS_EX_COMPOSITED does not work in Win7 :D

So I did a little bit more reading on the GDI+ UDF and came up with this!

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>

Global $GuiSizeX = @DesktopWidth, $GuiSizeY = @DesktopHeight

Local $lasty, $lastx

$hGui = GUICreate("Draw", $GuiSizeX, $GuiSizeY)
GUISetState()

_GDIPlus_Startup()
$hGraphicGUI = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hBMPBuff = _GDIPlus_BitmapCreateFromGraphics($GuiSizeX, $GuiSizeY, $hGraphicGUI)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hBMPBuff)
$hPen = _GDIPlus_PenCreate (0x20000000,1,2)

$startpos = MouseGetPos()
    
While 1
    
    $mouse = MouseGetPos()
    
    If IsInt($lastx) = 0 And IsInt($lasty) = 0 Then
        $startmousex = $startpos[0]
        $startmousey = $startpos[1]
    Else
        $startmousex = $lastx
        $startmousey = $lasty
    EndIf

    _GDIPlus_GraphicsDrawLine($hGraphic, $startmousex, $startmousey, $mouse[0], $mouse[1],$hPen)     
    ;_GDIPlus_GraphicsDrawLine($hGraphicGUI, $startmousex, $startmousey, $mouse[0], $mouse[1],$hPen) 
    _GDIPlus_GraphicsDrawImage($hGraphicGUI, $hBMPBuff, 0, 0)

    $lasty = $mouse[1]
        $lastx = $mouse[0]
    
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

The problem I have with this is Alpha blending, you will see one line commented out, on this line I am specfiying $hGraphicsGUI instead of $hGraphics. With $hGraphicsGUI the alpha blending works a treat but when minimized it clears and I'm back to square one, however with $hGraphics the image does not clear when minimized but the alpha blending is no more :huggles:

Can you offer some light on this?

Regards

Byron

Share this post


Link to post
Share on other sites
picea892

You need to register a repaint. See Malkey's first response for a good working template.

Share this post


Link to post
Share on other sites
byronsmith

You need to register a repaint. See Malkey's first response for a good working template.

Howzit picea, thanks for the reply, however are you responding to the last reply or to the original post?

Regards

Byron

Share this post


Link to post
Share on other sites
byronsmith

Howzit picea, thanks for the reply, however are you responding to the last reply or to the original post?

Regards

Byron

Nevermind. Thanks for the working template.

Share this post


Link to post
Share on other sites
picea892

I read "when I minimize the app and maximize it again the screen is cleared" and usually the answer is the link I provided.

Perhaps I should have explained more sorry.

First off, let me say that I think Martin's example is your best, easiest solution.

The problem as I see it is you have not saved what you draw. If you saved what you draw as a bitmap or graphic, then you could redraw it on a max event using the technique in the link I provided.

Martin save his to a graphic control using GUICtrlSetGraphic, that's why it is there when you maximize.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×