Results 1 to 8 of 8

Thread: [RESOLVED] Fire MouseDown and MouseUp in same Event

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Location
    Portugal
    Posts
    12

    Resolved [RESOLVED] Fire MouseDown and MouseUp in same Event

    Hello,

    I'm wondering if someone can help me.
    I have a ToolStrip in top of a form and i use it as a Title Menu with a label for Title and a Button to close the form.
    I use it because it's rendered with the aspect that i like.
    I can move the form with MouseMove Event
    What i can't do in same click is change the opacity of the Form when Mouse is Down and change again when Mouse is Up.
    At this moment i need 2 clicks to this operation.

    In MouseDown event i have me.opacity=0.5
    In MouseUp event i have: me.opacity=1

    When i press the mouse button the opacity is changed (50%), but when i release the mouse nothing happens. Clicking again and the opacity is changed to 100% again.

    I don't know if this have something to do with DoubleClick but can't discover the solution.
    Thanks in advanced and sorry my poor English.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Fire MouseDown and MouseUp in same Event

    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Fire MouseDown and MouseUp in same Event

    Got to say it works absolutely fine for me without the extra comand suggested by db so I guess we need to see code to find what other factors may be affecting it.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Location
    Portugal
    Posts
    12

    Re: Fire MouseDown and MouseUp in same Event

    Quote Originally Posted by dunfiddlin View Post
    Got to say it works absolutely fine for me without the extra comand suggested by db so I guess we need to see code to find what other factors may be affecting it.
    And i must say it works fine for me too...
    I explaine. I didn't told you but i have an instruction to move the borderless form at MouseMove.
    Disable this instruction at MouseMove, all works fine with MouseDown and MouseUp Events.
    At this point my problem is to move the borderless form with opacity at 50% and make it available at 100% when stop moving the form.

    My code in MouseMove Event is this:
    Code:
            If e.Button = MouseButtons.Left Then
                ReleaseCapture()
                SendMessage(varForm.Handle.ToInt32, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
            End If
    You must first declare some stuff:

    Code:
        Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        Declare Sub ReleaseCapture Lib "User32" ()
        Const WM_NCLBUTTONDOWN As Integer = &HA1
        Const HTCAPTION As Integer = 2
    I can move the form with opacity at 50%, but releasing the mouse the form don't turn to 100% of the opacity!

    Any Help!? Thank you both for your suggestions!

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Fire MouseDown and MouseUp in same Event

    If e.Button = MouseButtons.Left Then
    ReleaseCapture()
    SendMessage(Me.Handle.ToInt32, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
    Else
    Me.Opacity = 1
    End If
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Fire MouseDown and MouseUp in same Event

    @dunfiddlin, that will only restore the opacity if you right or middle click on the form.

    The thing is that normally when a WM_NCLBUTTONDOWN message is sent it will be followed by a WM_NCLBUTTONUP message when you release the mouse button. However before that message is sent a WM_NCHITTEST message is sent and the UP message will only be sent if that results in HTCAPTION. Since you send the WM_NCLBUTTONDOWN, HTCAPTION while the mouse isn't over the caption area the WM_NCLBUTTONUP message will never be sent (unless you release the mouse button and move the cursor over the caption area). But the key here is that a WM_NCHITTEST message will always be sent and you can use that by overriding the default windows procedure.
    Code:
      Protected Overrides Sub DefWndProc(ByRef msg As Message)
        Const WM_NCHITTEST As Integer = &H84
        Const HTCLIENT As Integer = 1
        If msg.Msg = WM_NCHITTEST Then
          MyBase.DefWndProc(msg)
          If msg.Result = HTCLIENT Then
            Me.Opacity = 1.0
          End If
        Else
          MyBase.DefWndProc(msg)
        End If
      End Sub

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Fire MouseDown and MouseUp in same Event

    Quote Originally Posted by aemule View Post
    I can move the form with opacity at 50%, but releasing the mouse the form don't turn to 100% of the opacity!

    Any Help!? Thank you both for your suggestions!
    Because you released the capture, when mouse is released any code after sendmessage then runs, example...

    Code:
    Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        If e.Button = MouseButtons.Left Then
            Me.Opacity = 0.5
            ReleaseCapture()
            SendMessage(Me.Handle.ToInt32, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
            Me.Opacity = 1.0
        End If
    End Sub

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Location
    Portugal
    Posts
    12

    Resolved Re: Fire MouseDown and MouseUp in same Event

    Thank you all for your help.
    It's working now.

    DECLARATIONS

    Code:
    Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Const WM_NCLBUTTONDOWN As Integer = &HA1
    Const HTCAPTION As Integer = 2
    CODE

    Code:
    Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        If e.Button = MouseButtons.Left Then
            Me.Opacity = 0.5
            SendMessage(Me.Handle.ToInt32, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
            Me.Opacity = 1.0
        End If
    End Sub
    I share the result with you!

    Sample Image:
    Name:  Program.JPG
Views: 2463
Size:  9.2 KB

    Sample Application With Code:
    Program.zip

Tags for this Thread

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