Form button causes brief flash to non-AutoCAD window. Focus has briefly moved?

Form button causes brief flash to non-AutoCAD window. Focus has briefly moved?

Anonymous
Not applicable
2,064 Views
17 Replies
Message 1 of 18

Form button causes brief flash to non-AutoCAD window. Focus has briefly moved?

Anonymous
Not applicable

This is my first .NET application. It is essentially complete except for one annoying behavior. There is a window flash.

 

This application shows a form. From this form it is possible to display a second form used for entering a value. The first form is visible but disabled when the second form is showing. A button on the second form commits the user's entry, which results in new entry showing up in the first form and closing out the second form and enabling the first form. Windows briefly switches to some other non-AutocAD window that happens to be open when the second form closes. It might be an Explorer window, it might be an open Word document, it might even be a window that is open on a second monitor. There would be an AutoCAD screen flash if the window coming temporarily into focus was behind the AutoCAD window. The two buttons on first form, including the one that closes the first form and also a user datagridview pick event that also closes the first form do not result in the screen flash.

 

This must be something simple causing this. I would like to know what it is if anyone would know off hand. I am not interested in changing the entry interface scheme to eliminate the second form as a workaround. I'll do that if this screen flash is unavoidable. At this point I figure its something simple resulting from sheer ignorance.

 

Thank you

0 Likes
2,065 Views
17 Replies
Replies (17)
Message 2 of 18

chiefbraincloud
Collaborator
Collaborator

If it is, then I am equally ignorant.  I have been experiencing the same behavior for 4 yrs now, (since I switched to .NET).  I have no logical explanation for it, and in my case, I don't even think it happens the same way every time.

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 3 of 18

Anonymous
Not applicable

Hi there,

I had problems with my form objects flashing inside a panel.  The way I fixed it was to enable double buffer by inserting the following line into the panel.

 

 

' Double Buffer
        GetType(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.SetProperty, Nothing, ****************Object name*******************, New Object() {True})

 

Sorry if it's not what you want, but you never know..

 

Cheers.

 

Martin.

0 Likes
Message 4 of 18

chiefbraincloud
Collaborator
Collaborator

I think you misunderstood the problem, but thanks for the info anyway.

 

The problem isn't that my (or the OP's) custom form objects are flashing, the problem is that..., well I suppose it could be a variety of actual reasons, but what appears to happen as far as the users perspective is that the AutoCAD window disappears, some other open window appears and has focus, then the AutoCAD window shows up again.

 

The OP's case seems to involve a pair of user forms, but in my case I think first of all, that it may happen in a command that has no user form at all.  And second, I think that the times at which it happens change from build to build. 

 

I am reluctant to include this next tidbit, because it seems silly to think, and in my limited amount of testing to try and prove the theory, it did not seem to be repeatable but....  I may have noticed that It was happening on the users machines at the same point in the code that I had a breakpoint set.

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 5 of 18

Anonymous
Not applicable

Right, it is not the custom form that is flashing. Focus briefly transfers to the last window other than AutoCAD that had focus. I have a dual screen setup, so I can see the window in the second screen briefly assume focus. If this last used window is behind AutoCAD then that window briefly comes to the front.

0 Likes
Message 6 of 18

Anonymous
Not applicable

While searching for something unrelated I happened upon these postings from 2006. They describe a similar behavior if not exactly the same behavior, except it was happening on form Hide. Unfortunately much of it is over my head.

 

Hiding a Form causes entire ACAD application to lo... - Autodesk Discussion Groups

 

My application uses Application.ShowModalDialog to show the first form. The first form uses Application.ShowModalDialog to show the second form. The second form uses Dispose() to close it out. The first form uses Close() to close it out. I have yet to see if using Close() instead of Dispose() will eliminate the problem. 

0 Likes
Message 7 of 18

chiefbraincloud
Collaborator
Collaborator

Ok, It's getting late, and I'm getting tired, so I need to look at that again tomorrow, but there may have been some good tidbits on that link.  Even though I know for a fact now that I have seen this problem in code that has no user form at all.

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 8 of 18

Anonymous
Not applicable

The project is attached for looking under the hood. The command to invoke is setscale. A rightclick on the form or selecting custom opens the second form. The OK button on the second form causes focus to jump. The form's close control does not. 

0 Likes
Message 9 of 18

Anonymous
Not applicable

Hi there,

I disabled "Me.Enabled = False" and the flash stopped for me,  Maybe it's because AutoCAD is trying to Handle the focus and this line is interfering with that.

 

Could this be the problem?

0 Likes
Message 10 of 18

Anonymous
Not applicable

Removing the main form disable/enable feature does eliminate the focus problem. I do like having the effect of the main form dimming down when the secondary form is active. I can see using that effect in future projects. Perhaps there is another fix?

0 Likes
Message 11 of 18

Anonymous
Not applicable

Thanks Martin, I think I'm eating cake following your lead. Moving all of the enable function back into the main form fixed it. It was foolish being in the second form.  Thanks for looking into this.

0 Likes
Message 12 of 18

chiefbraincloud
Collaborator
Collaborator

Strangely enough, when I downloaded your code to see if it could possibly help me solve my problem, I was not able to recreate the focus problem.  What version of AutoCAD are you using?  I am using 2012, but none of my users are on 2012 yet.  I wonder if the problem I have been seeing might have been fixed?

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 13 of 18

Anonymous
Not applicable

2010

 

There needs to be some other windows open like a couple of browser windows or a Word window. Put the focus in one of those and then return to AutoCAD. Run the project. Right click the table to bring up the second form. Press the OK button. Your last focused window other than AutoCAD will flash. It maybe hard to see if the window is not behind AutoCAD if you do not know what to look for.

 

Unrelated to this, I cannot figure out why the application's viewport function works only after the first attempt to use it. I do not have any debugging capability other than including trace code.  

0 Likes
Message 14 of 18

chiefbraincloud
Collaborator
Collaborator

Oh, I realize there needs to be another window open to see the problem.  When I tested, I had an Excel Window, two Explorer windows, two Visual Studio Projects, and my Office Outlook window open.  A couple of windows were minimized, two or three windows were displayed (not minimized, not maximized, not active, but displayed).  One of the Visible Explorer windows was made active prior to starting the test.

 

From my experience, even if there are other windows open, but they are all minimized, the "focus flash" still occurs, but is much less noticeable, because the window that temporarily receives focus is minimized at the time.

 

And For what it is worth, I opened your project and (Loosely) examined the code to see if I could find a commonality between your code and mine.  It was after you seemed to have solved the problem, but I did not build your project, I tested your build.  And, based on the references you have in the project you uploaded, am I to assume you are using 2010 AutoCAD? 

 

I am really interested to see if this is a problem that Autodesk has finally solved, and frankly it is in my nature to understand the problem and the solution, as opposed to just accepting that the problem was solved.

 

 

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 15 of 18

chiefbraincloud
Collaborator
Collaborator

Also, I tried just about every combination of buttons on your form, except right clicking the table.  I was bringing up the second form by left clicking the "custom" scale.  I will try that tomorrow, but based on the code I see, I don't see an obvious difference between the two.

 

I did notice that the Viewport function wasn't working properly, but I figured that was because you had left some code out or something.  If you are still having some trouble with that, I would recommend posting a separate thread about that problem, as I don't think it applies to this thread.

Dave O.                                                                  Sig-Logos32.png
0 Likes
Message 16 of 18

Rob.O
Collaborator
Collaborator

Nope... not fixed... I still see the issues occasionally in AutoCAD 2012 (using VS2010).

0 Likes
Message 17 of 18

GTVic
Advisor
Advisor

If you disable the entire main form then I believe Windows will not be able to make it the active form when your other form closes.

 

Disabling the main form could be for two reasons, showing the user that it is not the active form or preventing the user from interacting with it while the other form is open.

 

The standard way to handle this is to make the data entry form modal so the user is forced to close it before they can go back to the main form. You won't get the dimmed appearance but you shouldn't have any issues.

 

If you want the dimmed appearance you can cycle through all of the controls on the form and disable them but that has another side effect that Windows doesn't know which control to make active. So you can have one control on the form that you don't disable (e.g. If x.Name <> "txtActive" Then x.Enabled = False). If you really want to get tricky, just for this purpose, you could make a very small textbox and hide it behind a button so it is not visible.

 

For Each x in Me.Controls
    x.Enabled = False
Next

My experience is that if you do too much of this type of thing it makes your project unnecessarily complex. If you need the user to enter a simple value, use InputBox

 

Dim str = InputBox("Type in a number:","Dialog Title","3")

If StrPtr(str) = 0 Then
    ' user pressed Cancel 
Else
    ' process entered value
End If

 

0 Likes
Message 18 of 18

williamdepadua
Enthusiast
Enthusiast

Hello! I had a similar problem and came to this old post. It seems to me that it is still unsolved... I kept looking for a solution to my problem. Now I come to share my solution. 

 

My scenario was, I have a form with a button that can hide the form. When you hid the form, an Explorer window that was open immediately came forward. 

 

Studying this scenario I realized that the focu continued with the AutoCAD program, but with the Windows Explorer window in front and dimmed. 

 

But this only occurred when the next step occurred. With autocad open, I open an Explorer window and then change the focus to Autocad again, then I show the form, click the button to hide the form and then splash, Explorer jumps in front of me. If the other program was with focu before I switched to autocad this did not happen, no Explorer window that was open was displayed. That is, the focu must be in Windows Explorer and switch to Autocad before displaying and hiding the form. 

 

My solution was to send the Windows Explorer window back from Autocad.

In place of Frm.hide I use this 

 

 

<Flags()> _
    Public Enum SetWindowPosFlags As UInteger
        AsynWindowPos = &H4000
        DeferErase = &H2000
        DrawFrame = &H20
        FrameChanged = &H20
        HideWindow = &H80
        NoActivate = &H10
        NoCopyBits = &H100
        NoMove = &H2
        NoOwnerZOrder = &H200
        NoRedraw = &H8
        NoReposition = &H200
        NoSendChanging = &H400
        NoSize = &H1
        NoZOrder = &H4
        ShowWindow = &H40
    End Enum

    <DllImport("user32.dll", EntryPoint:="SetWindowPos")> _
    Public Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As SetWindowPosFlags) As Boolean
    End Function

    <DllImport("user32.dll")>
    Public Function GetForegroundWindow() As IntPtr
    End Function


        Public Sub FormHide(Frm As Form)

            Dim hWndTop1 = W32.GetForegroundWindow
            Frm.Hide()
            Dim hWndTop2 = W32.GetForegroundWindow

            If hWndTop1 <> hWndTop2 Then
                Dim hWndCad = Autodesk.AutoCAD.ApplicationServices.Application.MainWindow.Handle
                W32.SetWindowPos(hWndTop2, hWndCad, 0, 0, 0, 0, W32.SetWindowPosFlags.NoMove Or W32.SetWindowPosFlags.NoSize)
            End If

        End Sub

 

 

 

 

@Anonymous 

 

0 Likes