Page 1 of 2 12 LastLast
Results 1 to 40 of 41

Thread: Drawing on the screen

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Drawing on the screen

    Wrote a program for drawing filled rectangles on a monitor screen using the functions of the GDI library. Everything works fine when running on Windows7. When launched on Windows 10, the squares appear and then disappear. What is the problem?
    Code:
    Option Explicit
    Public Declare Function RoundRect Lib "gdi32" (ByVal hDC As Long, ByVal X1 As Long, ByVal Y1 As Long, _
                ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
    Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    Public Declare Function SelectObject Lib "gdi32" (ByVal _
                hDC As Long, ByVal hObject As Long) As Long
    Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Public Declare Function GetDeviceCaps Lib "gdi32" _
                (ByVal hDC As Long, ByVal nIndex As Long) As Long
    Public Declare Function GetDesktopWindow Lib "user32" () As Long
    Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function ReleaseDC Lib "user32" _
                (ByVal hwnd As Long, ByVal hDC As Long) As Long
    Public storona As Integer, N As Long
    Public WidthF As Integer, Nreal As Integer
    Public rv As Long
    Public i As Integer, j As Integer, X1 As Long, Y1 As Long
    Public retval As Long
    Public hbrush As Long  ' new brush handle
    Public holdbrush As Long  ' default brush handle
    Private Const HORZRES = 8
    Private Const VERTRES = 10
    
    Sub main()
    Dim Deskhwnd As Long
    Dim DeskDc As Long
    Dim Widthm As Long
    Dim Heightm As Long
    ' Get the desktop descriptor
    Deskhwnd = GetDesktopWindow
    ' Retrieving the Desktop Device Context
    DeskDc = GetDC(Deskhwnd)
    ' Get the screen width
    Widthm = GetDeviceCaps(DeskDc, HORZRES)
    ' Get the screen height
    Heightm = GetDeviceCaps(DeskDc, VERTRES)
    
    
    N = Int(InputBox("Number of squares "))
        storona = Int(InputBox("Square side size  (pixel)"))
        Nreal = Widthm \ storona
            If N * storona < Widthm Then GoTo Prd
            rv = MsgBox(N & " squares will not fit on the screen." & vbCrLf & " I can place  " _
                & Nreal & " Squares . " & "Place? ", vbCritical + vbYesNo, _
                    "Error!")
                    N = Nreal
            If rv = vbNo Then Exit Sub
    Prd:
    X1 = 0
    Y1 = storona
    j = 1
    For i = 1 To N
        If j >= 15 Then j = 0
    
    hbrush = CreateSolidBrush(QBColor(j))                   'create a solid brush
    ' Save the old brush to restore it after the end of the program
    holdbrush = SelectObject(DeskDc, hbrush)
    ' Draw a rectangle filled with the set color
        retval = RoundRect(DeskDc, X1, 0, Y1, storona, 10, 5)
            X1 = X1 + storona
            Y1 = Y1 + storona
        j = j + 1
    ' Recovering an old brush
    retval = SelectObject(DeskDc, holdbrush)
    retval = DeleteObject(hbrush)  ' Destroying the brush
    Next i
        MsgBox "The squares are drawn! ", vbOKOnly + vbInformation
    ' Freeing up resources
    ReleaseDC GetDesktopWindow(), DeskDc
        End
    End Sub

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Drawing on the screen

    Have you tried changing the compatibility settings for vb6?

  3. #3
    Fanatic Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    578

    Re: Drawing on the screen

    Works fine for me in the latest 21H1 build. In the IDE the squares disappear when End is reached. Compiled the squares stay visible until I right click the desktop and select Refresh.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    Quote Originally Posted by Peter Swinkels View Post
    Have you tried changing the compatibility settings for vb6?
    IDE VB 6.0 is installed on Win7. I cannot establish compatibility with Win10.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    Quote Originally Posted by Steve Grant View Post
    Works fine for me in the latest 21H1 build.
    Unfortunately, I don't know what is the difference between Win7 and Win10 in terms of organizing the display on the monitor screen. Therefore, I asked the question.

  6. #6
    Fanatic Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    578

    Re: Drawing on the screen

    I just took your posted code and put it into a module. It worked exactly as outlined above. Why don't you explain what it does on Win7 so we can compare. I run my VB6 SP6 with no compatibility settings other than running elevated and with a comctl6 manifest.

  7. #7
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Drawing on the screen

    As did I Steve...the squares were briefly displayed and then disappeared (as OP stated in #1)...I am on Win 10. (I did NOT compile and run.) I believe OP stated that the IDE run ON Win7 produced squares that did NOT disappear.
    Last edited by SamOscarBrown; May 31st, 2021 at 07:18 AM.
    Sam I am (as well as Confused at times).

  8. #8
    Fanatic Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    578

    Re: Drawing on the screen

    Sam, in the IDE the squares remain visible until I dismiss the msgbox. Is that not true for you?

  9. #9
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Drawing on the screen

    I have gotten the chance to take a closer look at the code, while the points I am going to make don't address the question directly, here they are:

    1. I see the End statement has been used to terminate the program. This is unnecessary in this particular case as the program would quit anyway at the end of the Main procedure. If you must explicitly close the program use Exit Sub in this case.
    2. I see a GoTo statement where an If/End If block could be used.
    3. The program throws an error when cancelling the input dialog. You should check whether the user specified a valid number before trying to convert it to a numeric data type.

    I will have another look once I have the time.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    Quote Originally Posted by Steve Grant View Post
    I just took your posted code and put it into a module. It worked exactly as outlined above. Why don't you explain what it does on Win7 so we can compare.
    This is a computer science challenge for students. Initially, it consisted of the user entering the number of squares, the length of their side, and outputting them to the form, painting them with a random color. The solution using the Form.Line function is trivial and I decided to draw the output to the screen for myself.
    VB 6.0 Sp6 is installed on Windows7 with administrator rights.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    Quote Originally Posted by SamOscarBrown View Post
    I believe OP stated that the IDE run ON Win7 produced squares that did NOT disappear.
    Yes. On Win7, the squares do not disappear even after exiting the program.

  12. #12
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Drawing on the screen

    To continue my previous post:

    4. I see that ReleaseDC is not called at every exit point inside the Main procedure while GetDC has been called already.
    5. The usage of an arithmetic operator to combine message box style flags. The Or bitwise operator should be used.
    Last edited by Peter Swinkels; May 31st, 2021 at 09:46 AM.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    Quote Originally Posted by Peter Swinkels View Post
    To continue my previous post:

    4.
    At the moment, none of this matters.
    The key is to understand why the display is different in different operating systems. There is a suspicion that Win10 rewrites the screen differently than Win7. And it is not clear what needs to be done so that the drawn image does not disappear before exiting the program.

  14. #14
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Drawing on the screen

    Poor coding practices and especially memory leaks caused by sloppy handle management do matter to me. Also, taking the time to write clear code can prevent issues or make it easier to find them. Having said that I am sorry to say I have no idea what is causing your issue with the squares disappearing.

    I believe you mentioned running vb6 on win7, correct? If I run your code from vb6, which in my case is running on Windows 10, the squares don't disappear. I have noticed that there are subtle differences in how threading is handled between when a program is running from the IDE or as a stand alone exe. It's possible it has to do with that.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    Quote Originally Posted by Peter Swinkels View Post
    Poor coding ....
    I agree.
    This is a sketch of the code. With correct data entry, there are no problems.
    Quote Originally Posted by Peter Swinkels View Post
    I believe you mentioned running vb6 on win7, correct?
    Yes.
    Quote Originally Posted by Peter Swinkels View Post
    I have noticed that there are subtle differences in how threading is handled between when a program is running from the IDE or as a stand alone exe.
    The .exe works well on Win7 and behaves strangely on Win10.

  16. #16
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Drawing on the screen

    I am sorry, but your code is leaking handles (failing to always call ReleaseDC upon exiting) and also crashing itself using the End statement. The rest of my points are I will admit more trivial. I am curious as to whether your code would work when rewritten in vb.net. That might rule out a few potential causes.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    Quote Originally Posted by Peter Swinkels View Post
    I am sorry, but your code is leaking handles (failing to always call ReleaseDC upon exiting) and also crashing itself using the End statement.
    Simplified the code. Draws a yellow square in the upper left corner of the monitor. The .exe works fine in Win7. In Win10, the square appears and then disappears.
    Code:
    Option Explicit
    Public Declare Function RoundRect Lib "gdi32" (ByVal hDC As Long, ByVal X1 As Long, ByVal Y1 As Long, _
                ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
    Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    Public Declare Function SelectObject Lib "gdi32" (ByVal _
                hDC As Long, ByVal hObject As Long) As Long
    Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Public Declare Function GetDesktopWindow Lib "user32" () As Long
    Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    Public Declare Function ReleaseDC Lib "user32" _
                (ByVal hwnd As Long, ByVal hDC As Long) As Long
    
    Public retval As Long
    Public hbrush As Long  ' new brush handle
    Public holdbrush As Long  ' default brush handle
    
    Sub main()
    Dim Deskhwnd As Long
    Dim DeskDc As Long
    ' Get the desktop descriptor
    Deskhwnd = GetDesktopWindow
    ' Retrieving the Desktop Device Context
    DeskDc = GetDC(Deskhwnd)
    
    hbrush = CreateSolidBrush(RGB(255, 255, 0))                   'create a yellow solid brush
    ' Save the old brush to restore it after the end of the program
    holdbrush = SelectObject(DeskDc, hbrush)
    ' Draw a rectangle filled with the set color
        retval = RoundRect(DeskDc, 0, 0, 400, 400, 10, 5)
    ' Recovering an old brush
    retval = SelectObject(DeskDc, holdbrush)
    retval = DeleteObject(hbrush)  ' Destroying the brush
    
        MsgBox "The square are drawn! ", vbOKOnly + vbInformation
    
    ' Freeing up resources
    ReleaseDC GetDesktopWindow(), DeskDc
    End Sub

  18. #18
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Drawing on the screen

    That looks much better. :-)

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    Peter Swinkels, What should be done so that the drawn square does not disappear before exiting the program?

  20. #20
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Drawing on the screen

    @Steve....no, it (a yellow square-in the latest posted version) disappears within a second from running (both in IDE and Exe).
    Sam I am (as well as Confused at times).

  21. #21
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Drawing on the screen

    @argus19:
    I don’t know. I tried your idea in vb.net, it didn’t work at all.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    Set the program priority to high. Nothing changed.
    There is a suspicion that the Windows event queue must be stopped while the program is running.
    Last edited by Argus19; May 31st, 2021 at 02:22 PM.

  23. #23
    Fanatic Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    578

    Re: Drawing on the screen

    Ok I've tried with the new version and the first thing I noticed was the Yellow square was hiding behind the IDE. You could just see the thinnest Yellow line. Moving the IDE down and removing the msgbox (which causes a refresh in the IDE when dismissed) left me with the first of the images.

    Compiled it is as it was before, the Yellow box is visible until you do a refresh or click on something else.
    Attached Images Attached Images   

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    Quote Originally Posted by Steve Grant View Post
    Compiled it is as it was before, the Yellow box is visible until you do a refresh or click on something else.
    This only works for me on Windows7.

  25. #25
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Drawing on the screen

    Quote Originally Posted by Argus19 View Post
    This only works for me on Windows7.
    You might have some clock widget on your Windows 10 desktop that forces regular redraws.

    cheers,
    </wqw>

  26. #26
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Drawing on the screen

    The big difference between Windows 7 and Windows 10, is that Windows 10's desktop is composited, which means that the desktop image is a composite of the images from all the programs running on it.

    In the earlier versions of windows, the desktop was a shared resource, and each program when getting a handle to hDC (0) would write to that shared resource, and each program would have to refresh its area of the desktop if it was "dirtied" in some way, i.e. it would get paint events and have to repaint its area of the desktop.

    With Windows 10, each program draws its windows in its own virtual desktop space and drawings to the desktop also occur in that virtual desktop space, and then windows composites what is seen on the screen from all the individual virtual desktop spaces. If some window overlaps your window and is drawn on top, it shows that way on the screen, but it doesn't actually clobber your drawn window in your virtual space. If the overlapping window is moved, the compositing will show the part of your window revealed from your virtual space, saving your program from having to redraw its own window multiple times because of some other window overlapping it and being moved.

    Likewise, if your program exits, then any drawing done in its virtual desktop space will not be redrawn the next time Windows 10 composites the desktop again from the active programs' windows and virtual desktop spaces, thus your drawing to the desktop will disappear fairly quickly, whereas in Windows 7 and earlier it only disappeared if Windows actively redrew the complete desktop, meaning it would have to clear the desktop and sent paint events to all the active windows so they could redraw themselves. This would be a rather distracting screen flash, so it would not be something that Windows 7 would have done regularly, so stuff drawn on the desktop could hang around for a long time.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  27. #27

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    In this case, the drawing is done in the context of the desktop, i.e. the program does not have its own window, and when redrawing the desktop window, Win10 ignores this image?

  28. #28
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Drawing on the screen

    Quote Originally Posted by Argus19 View Post
    In this case, the drawing is done in the context of the desktop, i.e. the program does not have its own window, and when redrawing the desktop window, Win10 ignores this image?
    I just replaced the MsgBox "The square are drawn! " with Sleep 10000 and the square remains visible until other windows are redrawn over it or the timeout expires.

    The point is that on Win10 it is the very MsgBox code in MSVBVM60.DLL that forces desktop redraw which erases your square.

    cheers,
    </wqw>

  29. #29
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,807

    Re: Drawing on the screen

    @wqweto:
    If it's the MsgBox function in the Visual Basic run-time, perhaps the issue could be by-passed using an API function such as: https://docs.microsoft.com/en-us/win...-messageboxexa.

  30. #30

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    Quote Originally Posted by wqweto View Post
    The point is that on Win10 it is the very MsgBox code in MSVBVM60.DLL that forces desktop redraw which erases your square.
    Thank you.
    There was an idea to use the GetMessage function in a loop. If it does not read, but actually retrieves the message from the queue.

  31. #31

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    When running .exe on Win10, the square disappears despite using the Sleep or GetMessage function.

  32. #32
    Fanatic Member
    Join Date
    Jul 2007
    Location
    Essex, UK.
    Posts
    578

    Re: Drawing on the screen

    Just to make sure, you did remove the form from the project before creating the exe from the bas module?

  33. #33

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    I deleted the form immediately after adding the module. And only then I began to write the code.

  34. #34
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Drawing on the screen

    trying to follow the thread----but posts 29 and 30 may hint at a solution....(I don't know what that is of course, but simply in this post wanted to show that the MsgBox DOES do what it was mentioned earlier. I simply put a debug stop at the MsgBox line. The yellow 'square' is displayed and stays visible, until I continue the running of the program (executing the MsgBox display line). Hope that helps a tiny bit.
    Sam I am (as well as Confused at times).

  35. #35

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    1). Put Sleep (10000) in front of MsgBox. The square appears, disappears almost immediately and after 10 seconds the MsgBox appears.
    2). Instead of MsgBox, I put a loop
    Code:
             Do While GetMessage(wMsg, 0&, 0&, 0&) <> -1
        If wMsg.message = WM_RBUTTONDOWN Then
        MsgBox "The square are drawn! ", vbOKOnly + vbInformation
        Exit Do
        End If
             Loop
    Both options work in Win7 and don't work in Win10.

  36. #36
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,120

    Re: Drawing on the screen

    Quote Originally Posted by Argus19 View Post
    Both options work in Win7 and don't work in Win10.
    Works on Win7 and Win10 here. Doesn't work on your particular Win10 setup.

    There is an app on your box that is constantly redrawing the desktop. Could be a VNC server or some other kind of remote administration tool (RAT) including a malicious one.

    cheers,
    </wqw>

  37. #37
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Drawing on the screen

    @wq....could be something else, NO? Per your suggestion, I would think the square would also disappear while the program is halted (at the MsgBox function).
    Sam I am (as well as Confused at times).

  38. #38
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,996

    Re: Drawing on the screen

    Quote Originally Posted by wqweto View Post
    Works on Win7 and Win10 here.
    It works on Windows 10 here too.

  39. #39
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Drawing on the screen

    I am using Windows 10 HOME Version 10.0.19041.985

    Could THAT be the difference between yours (Eduardo and wqweto), the OP's and mine?
    Sam I am (as well as Confused at times).

  40. #40

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2020
    Posts
    370

    Re: Drawing on the screen

    Windows 10 PRO 20H2 19042.985

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width