An Original Idea

because all great software begins with an original idea

Archive for the ‘Uncategorized’ Category

Catching Application Level Mouse Events in Winforms

Posted by anoriginalidea on February 23, 2009

image

As part of my work on flick scrolling I’ve created an example of a class that can create mouse events for an entire Winforms application.

To use it, just declare it, like so:

Private WithEvents moAppMouseEvents As New ApplicationLevelMouseEvent

 

I’ve only implemented the MouseUp and MouseDown events, but these are consistent with the Form ones:

 

Private Sub moAppMouseEvents_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles moAppMouseEvents.MouseUp

 
End Sub
Private Sub moAppMouseEvents_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles moAppMouseEvents.MouseDown
 
End Sub

 

The code uses an IMessageFilter in the tradtional fashion:

 

Public Class ApplicationLevelMouseEvents
    Implements IDisposable
    Implements IMessageFilter

    Public Event MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Public Event MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    Private Const WM_LBUTTONUP As Integer = &H202
    Private Const WM_LBUTTONDOWN As Integer = &H201
    'Private Const WM_RBUTTONDOWN As Integer = &H204
    'Private Const WM_MBUTTONDOWN As Integer = &H207
    'Private Const WM_NCLBUTTONDOWN As Integer = &HA1
    'Private Const WM_NCRBUTTONDOWN As Integer = &HA4
    'Private Const WM_NCMBUTTONDOWN As Integer = &HA7
    Public Sub New()
        Application.AddMessageFilter(Me)
    End Sub

    Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
        If (m.Msg = WM_LBUTTONDOWN) Then

            Dim loArgs As New MouseEventArgs(MouseButtons.Left, 1, System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y, 0)

            RaiseEvent MouseDown(Me, loArgs)

        ElseIf (m.Msg = WM_LBUTTONUP) Then
            Dim loArgs As New MouseEventArgs(MouseButtons.Left, 1, System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y, 0)
            RaiseEvent MouseUp(Me, loArgs)
        End If
        Debug.WriteLine(m.Msg.ToString)
        Return False

    End Function

    Private disposedValue As Boolean = False        ' To detect redundant calls

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                Application.RemoveMessageFilter(Me)
            End If
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class

 

The sample could be enhanced to support the full range of events such as mouse move, so feel free to take the sample and change it.  If you post your code, please link back to this post.

 

Links

A Simple Scroll Controller for Winforms

Posted in Uncategorized | 2 Comments »

3D in Silverlight 2.0

Posted by anoriginalidea on May 26, 2008

image

So what’s the difference between WPF and Silverlight again?  Oh yes, well WPF has more advanced features.

You know, 3D graphics!

Isn’t it strange that many Silverlight demos invariably incorporate 3D style effects? 

Take a look at this article for a very rich solution:

Silverlight: Building Advanced 3D Animations with Silverlight 2.0

I wonder how long it will be before Silverlight supports 3D natively?

Posted in Uncategorized | 2 Comments »

Walking Through Passive View

Posted by anoriginalidea on June 19, 2007

walk.png 

DevX have published an interesting romp through the refactoring of a traditional forms app into a shiny new  n-tier architecture utilises MVP.

It compares the difference in coding style between traditional gui programming and MVP, then moves into unit testing, services and dependency injection.  

Take a look:  

Layered Architecture, Dependency Injection, and Dependency Inversion

For a less formal and more extensive overview, this series of articles by Jeremy Miller is great:

Build your own CAB   (No, not a taxi or a Pocket PC install, but Microsoft Patterns and Practice’s “Composite Application Block”) 

Posted in Uncategorized | Leave a Comment »

Dave Cheong’s Blog – Lots of Nuggets to Share

Posted by anoriginalidea on June 18, 2007

Although my original intent was simply to share technical information on this blog, I come across something really useful and special every now and then.

If you’re interested in personal improvement, take a look at Dave Cheong’s Blog.

There’s many useful little articles that act as a pithy reminder of what you should be doing.  Examples include:

Posted in Uncategorized | Leave a Comment »

Creating Commands in XAML only

Posted by anoriginalidea on June 6, 2007

Previously I posted an article about defining “Commands” in WPF applications and responding in code.

A way of doing this in XAML only is setting up a binding to a property on another control.

In this example, a toggle button is used to toggle the blod state on a rich text box.

For example:

<Toolbar>

<ToggleButton MinWidth="40" Command="EditingCommands.ToggleBold" CommandTarget="{Binding ElementName=XAMLRichBox}" TextBlock.FontWeight="Bold">B</ToggleButton>

</Toolbar>

 

I got this idea from this article: Mastering the WPF RichTextBox

Posted in Uncategorized | Leave a Comment »