Archive

Archive for February, 2009

SqlCeExplorer – Alpha release

February 27th, 2009 Anuraj P No comments

Hi All

My first open source project, SQLCEExplorer, a small utility to explore the SQL CE Database, yesterday uploaded the Alpha version to codeplex.

You can get this one from sqlceexplorer on Codeplex

Comments / Feedbacks : Welcome :)
You can send mail to : anuraj dot p at live dot com

Freehand drawing with .Net Windows forms

February 26th, 2009 Anuraj P No comments

Few days back, I got a requirement to implement Free Hand drawing in .Net. I searched a lot but I didn’t get any good solution, the one I found from code project was Invalidating the picture box every time. So today I got a solution, it not seems to be a perfect solution because Paint events occurs, we need to re-paint the whole lines again.

I am attaching the code here. I will update the post once I get some perfect solution for this.

 Private m_Drawing As Boolean = False
    Private m_List As List(Of Point) = Nothing
    Private Sub MyPic_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyPic.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            m_Drawing = True
        End If
    End Sub
    Private Sub MyPic_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyPic.MouseUp
        If m_Drawing AndAlso m_List IsNot Nothing Then
            If m_List.Count >= 2 Then
                MyPic.CreateGraphics.DrawLines(New Pen(Color.Blue, 1), m_List.ToArray())
            End If
            m_Drawing = False
            m_List.Clear()
        End If
    End Sub

    Private Sub MyPic_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyPic.MouseMove
        If m_List IsNot Nothing AndAlso m_Drawing Then
            m_List.Add(e.Location)
        End If
    End Sub
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_List = New List(Of Point)
    End Sub

Code is pretty self explanatory. So I am not putting any comments ;)

Disable autorun of USB disks

February 18th, 2009 Anuraj P 2 comments

In the last post I was implemented the USB detection, but the problem become more complex, because my USB stick contains some Autorun.inf file and when you plug the stick, it start executing some program, otherwise you need to cancel it explicity. The client need to avoid this. So I have to find some solution, where, I need to disable the autorun, when my program is running. Luckly, on the first search in google only, I got the solution.

Here is the implementation.

'API
Public QueryCancelAutoPlay As Integer = 0
Public Shared Function RegisterWindowMessage(ByVal strMessage As String) As Integer
'No implementation required.
End Function

'Modified implementation of WndProc() method

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If QueryCancelAutoPlay = 0 Then
QueryCancelAutoPlay = RegisterWindowMessage("QueryCancelAutoPlay")
End If

If m.Msg = WM_DEVICECHANGE Then
Select Case m.WParam
Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL
lblMessage.Text = "USB Inserted"
Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE
lblMessage.Text = "USB Removed"
End Select
ElseIf m.Msg = QueryCancelAutoPlay Then
m.Result = CType(1, IntPtr)
Return
End If
MyBase.WndProc(m)
End Sub

I implemented it in C#, and I re-wrote it in VB.Net, and I didn’t tested it ;) If you are facing any issues, let me know.

How to detect USB insertion and removal in VB.Net

February 13th, 2009 Anuraj P 4 comments

Recently I moved to a Desktop application development team, where I have to look for the insertion of the USB. I need to read the contents of the USB drive and updates the application. I got a lot of solutions from Google to detect the USB insertion and removal. I am posting the easiest one I found, it is VB.Net, but I don’t think we can use it Class Libraries, and we may need to use the Windows Forms.

    Private WM_DEVICECHANGE As Integer = &H219

    Public Enum WM_DEVICECHANGE_WPPARAMS As Integer
        DBT_CONFIGCHANGECANCELED = &H19
        DBT_CONFIGCHANGED = &H18
        DBT_CUSTOMEVENT = &H8006
        DBT_DEVICEARRIVAL = &H8000
        DBT_DEVICEQUERYREMOVE = &H8001
        DBT_DEVICEQUERYREMOVEFAILED = &H8002
        DBT_DEVICEREMOVECOMPLETE = &H8004
        DBT_DEVICEREMOVEPENDING = &H8003
        DBT_DEVICETYPESPECIFIC = &H8005
        DBT_DEVNODES_CHANGED = &H7
        DBT_QUERYCHANGECONFIG = &H17
        DBT_USERDEFINED = &HFFFF
    End Enum

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_DEVICECHANGE Then
            Select Case m.WParam
                Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEARRIVAL
                    lblMessage.Text = "USB Inserted"
                Case WM_DEVICECHANGE_WPPARAMS.DBT_DEVICEREMOVECOMPLETE
                    lblMessage.Text = "USB Removed"
            End Select
        End If
        MyBase.WndProc(m)
    End Sub

The code is pretty clear; I am overriding the WndProc method. When a USB inserted or removed, Windows will send some message (WM_DEVICECHANGE) to all the applications. And the WParam property of that message will contains the details of the device event. The system broadcasts the DBT_DEVICEARRIVAL device event when a device or piece of media has been inserted and becomes available. The system broadcasts the DBT_DEVICEREMOVECOMPLETE device event when a device or piece of media has been physically removed.

Now I am looking into how can remove a USB programmatically.

Rounded rectangle Windows Form

February 4th, 2009 Anuraj P No comments

Few days back in Kerala Microsoft Users Group, Shoban Kumar posted a question, how to create a rounded rectangle windows form, for his Codeplex project jata. And after few days some other guy comes up with some issues in my code. Now I decided to upload the sample project on my blog.

The implementation is like the following
1) Go to the Form’s Paint event.
2) create a Graphical path object.
3) Add rectangles and lines.
4) Set the Form’s region using the Graphical path.

You can download the code from here.(Download the file, and rename the doc to zip. This is because wordpress doesn’t allow to upload Zip files.)

You can view the post over here

The application screenshot.
sampleform

Categories: .Net, .Net 3.0 / 3.5 Tags: