'=====================================================================

'  File:      ScribblerForm.vb

'

'

'  ---------------------------------------------------------------------

'   Copyright (C) Microsoft Corporation.  All rights reserved.

'

'  This source code is intended only as a supplement to Microsoft

'  Development Tools and/or on-line documentation.  See these other

'  materials for detailed information regarding Microsoft code samples.

'

'  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY

'  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE

'  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A

'  PARTICULAR PURPOSE.

'=====================================================================

Imports System

Imports System.Drawing

Imports System.Collections

Imports System.ComponentModel

Imports System.Windows.Forms

Imports Microsoft.WindowsCE.Forms

Imports System.Runtime.InteropServices

 

Public Enum KeyModifiers As Integer

    None = 0

    Alt = 1

    Control = 2

    Shift = 4

    Windows = 8

    Modkeyup = &H1000

End Enum

 

Public Enum HardwareButtons As Integer

    Hardware1 = 193 ' Êàëåíäàðü

    Hardware2 = 194 ' Îáìåí

    Hardware3 = 195 ' Êîíòàêòû

    Hardware4 = 196 ' Home

    Hardware5 = 202 ' Home äîëãî

    Hardware6 = 197 ' Ëåâàÿ êíîïêà

    Hardware7 = 198 ' Ïðàâàÿ êíîïêà

    Hardware8 = 203 ' Ëåâàÿ êíîïêà äîëãî

    Hardware9 = 204 ' Ïðàâàÿ êíîïêà äîëãî

    Hardware10 = 13 ' Enter

    Hardware11 = 37 ' left

    Hardware12 = 39 ' right

    Hardware13 = 38 ' up

    Hardware14 = 40 ' down

 

 

End Enum

Public Module RegisterHKeys

 

    <DllImport("coredll.dll", Entrypoint:="RegisterHotKey", setLastError:=True)> _

    Public Function RegisterHotKey( _

    ByVal hWnd As IntPtr, _

    ByVal id As Integer, _

    ByVal Modifiers As KeyModifiers, _

    ByVal key As Integer) As Boolean

    End Function

 

    <DllImport("coredll.dll")> _

    Private Function UnregisterFunc1( _

    ByVal modifiers As KeyModifiers, _

    ByVal keyID As Integer) As Boolean

    End Function

 

    Public Sub RegisterRecordKey(ByVal hWnd As IntPtr)

        UnregisterFunc1(KeyModifiers.Windows, CType(HardwareButtons.Hardware1, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware1, Integer), KeyModifiers.Windows, CType(HardwareButtons.Hardware1, Integer))

        UnregisterFunc1(KeyModifiers.Windows, CType(HardwareButtons.Hardware2, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware2, Integer), KeyModifiers.Windows, CType(HardwareButtons.Hardware2, Integer))

        UnregisterFunc1(KeyModifiers.Windows, CType(HardwareButtons.Hardware3, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware3, Integer), KeyModifiers.Windows, CType(HardwareButtons.Hardware3, Integer))

        UnregisterFunc1(KeyModifiers.Windows, CType(HardwareButtons.Hardware4, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware4, Integer), KeyModifiers.Windows, CType(HardwareButtons.Hardware4, Integer))

        UnregisterFunc1(KeyModifiers.Windows, CType(HardwareButtons.Hardware5, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware5, Integer), KeyModifiers.Windows, CType(HardwareButtons.Hardware5, Integer))

        UnregisterFunc1(KeyModifiers.Windows, CType(HardwareButtons.Hardware6, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware6, Integer), KeyModifiers.Windows, CType(HardwareButtons.Hardware6, Integer))

        UnregisterFunc1(KeyModifiers.Windows, CType(HardwareButtons.Hardware7, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware7, Integer), KeyModifiers.Windows, CType(HardwareButtons.Hardware7, Integer))

        UnregisterFunc1(KeyModifiers.Windows, CType(HardwareButtons.Hardware8, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware8, Integer), KeyModifiers.Windows, CType(HardwareButtons.Hardware8, Integer))

        UnregisterFunc1(KeyModifiers.Windows, CType(HardwareButtons.Hardware9, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware9, Integer), KeyModifiers.Windows, CType(HardwareButtons.Hardware9, Integer))

        UnregisterFunc1(KeyModifiers.None, CType(HardwareButtons.Hardware10, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware10, Integer), KeyModifiers.None, CType(HardwareButtons.Hardware10, Integer))

        UnregisterFunc1(KeyModifiers.None, CType(HardwareButtons.Hardware11, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware11, Integer), KeyModifiers.None, CType(HardwareButtons.Hardware11, Integer))

        UnregisterFunc1(KeyModifiers.None, CType(HardwareButtons.Hardware12, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware12, Integer), KeyModifiers.None, CType(HardwareButtons.Hardware12, Integer))

        UnregisterFunc1(KeyModifiers.None, CType(HardwareButtons.Hardware13, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware13, Integer), KeyModifiers.None, CType(HardwareButtons.Hardware13, Integer))

        UnregisterFunc1(KeyModifiers.None, CType(HardwareButtons.Hardware14, Integer))

        RegisterHotKey(hWnd, CType(HardwareButtons.Hardware14, Integer), KeyModifiers.None, CType(HardwareButtons.Hardware14, Integer))

    End Sub

End Module

 

Public Class ScribblerForm

    Inherits System.Windows.Forms.Form

    Friend WithEvents pGreen As System.Windows.Forms.Panel

    Friend WithEvents pBlue As System.Windows.Forms.Panel

    Friend WithEvents pTray As System.Windows.Forms.Panel

    Friend WithEvents pBlack As System.Windows.Forms.Panel

    Friend WithEvents bClear As System.Windows.Forms.Button

    Friend WithEvents pDrawWindow As System.Windows.Forms.Panel

    Friend WithEvents pMagenta As System.Windows.Forms.Panel

    Friend WithEvents pYellow As System.Windows.Forms.Panel

    Friend WithEvents pRed As System.Windows.Forms.Panel

 

    Private graphics As graphics

    Private bitmap As bitmap

    Private hasCapture As Boolean

    Private oldX, oldY As Integer

    Private currentColorPanel As Panel

 

    Dim messageWindow As myMessageWindow

 

#Region " Windows Form Designer generated code "

 

    Public Sub New()

        MyBase.New()

        'This call is required by the Windows Form Designer.

        InitializeComponent()

 

        'Add any initialization after the InitializeComponent() call

 

        ' Create offscreen drawing surface

        Me.messageWindow = New myMessageWindow(Me)

        RegisterHKeys.RegisterRecordKey(Me.messageWindow.Hwnd)

 

        Me.bitmap = New Bitmap(Me.pDrawWindow.Size.Width, Me.pDrawWindow.Size.Height)

        Me.graphics = graphics.FromImage(Me.bitmap)

        Me.graphics.FillRectangle(New SolidBrush(Color.White), 0, 0, Me.Size.Width, Me.Size.Height)

 

        ' Make the black the default color

        Me.currentColorPanel = Me.pBlack

    End Sub

 

    'Form overrides dispose to clean up the component list.

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

        MyBase.Dispose(disposing)

    End Sub

 

    'NOTE: The following procedure is required by the Windows Form Designer

    'It can be modified using the Windows Form Designer. 

    'Do not modify it using the code editor.

    Friend WithEvents pGray As System.Windows.Forms.Panel

    Private Sub InitializeComponent()

        Me.pGreen = New System.Windows.Forms.Panel

        Me.pBlue = New System.Windows.Forms.Panel

        Me.pGray = New System.Windows.Forms.Panel

        Me.pTray = New System.Windows.Forms.Panel

        Me.pBlack = New System.Windows.Forms.Panel

        Me.bClear = New System.Windows.Forms.Button

        Me.pDrawWindow = New System.Windows.Forms.Panel

        Me.pMagenta = New System.Windows.Forms.Panel

        Me.pYellow = New System.Windows.Forms.Panel

        Me.pRed = New System.Windows.Forms.Panel

        '

        'pGreen

        '

        Me.pGreen.BackColor = System.Drawing.Color.Green

        Me.pGreen.Location = New System.Drawing.Point(167, 4)

        Me.pGreen.Size = New System.Drawing.Size(22, 18)

        '

        'pBlue

        '

        Me.pBlue.BackColor = System.Drawing.Color.Blue

        Me.pBlue.Location = New System.Drawing.Point(140, 4)

        Me.pBlue.Size = New System.Drawing.Size(22, 18)

        '

        'pGray

        '

        Me.pGray.BackColor = System.Drawing.Color.Gray

        Me.pGray.Location = New System.Drawing.Point(59, 4)

        Me.pGray.Size = New System.Drawing.Size(22, 18)

        '

        'pTray

        '

        Me.pTray.BackColor = System.Drawing.Color.LightGray

        Me.pTray.Controls.Add(Me.pBlack)

        Me.pTray.Controls.Add(Me.bClear)

        Me.pTray.Location = New System.Drawing.Point(0, -1)

        Me.pTray.Size = New System.Drawing.Size(240, 28)

        '

        'pBlack

        '

        Me.pBlack.BackColor = System.Drawing.Color.Black

        Me.pBlack.Location = New System.Drawing.Point(3, 3)

        Me.pBlack.Size = New System.Drawing.Size(26, 22)

        '

        'bClear

        '

        Me.bClear.Location = New System.Drawing.Point(192, 4)

        Me.bClear.Size = New System.Drawing.Size(40, 18)

        Me.bClear.Text = "clear"

        '

        'pDrawWindow

        '

        Me.pDrawWindow.Location = New System.Drawing.Point(0, 27)

        Me.pDrawWindow.Size = New System.Drawing.Size(240, 293)

        '

        'pMagenta

        '

        Me.pMagenta.BackColor = System.Drawing.Color.Magenta

        Me.pMagenta.Location = New System.Drawing.Point(113, 4)

        Me.pMagenta.Size = New System.Drawing.Size(22, 18)

        '

        'pYellow

        '

        Me.pYellow.BackColor = System.Drawing.Color.Yellow

        Me.pYellow.Location = New System.Drawing.Point(86, 4)

        Me.pYellow.Size = New System.Drawing.Size(22, 18)

        '

        'pRed

        '

        Me.pRed.BackColor = System.Drawing.Color.Red

        Me.pRed.Location = New System.Drawing.Point(32, 4)

        Me.pRed.Size = New System.Drawing.Size(22, 18)

        '

        'ScribblerForm

        '

        Me.ClientSize = New System.Drawing.Size(240, 319)

        Me.ControlBox = False

        Me.Controls.Add(Me.pGreen)

        Me.Controls.Add(Me.pBlue)

        Me.Controls.Add(Me.pGray)

        Me.Controls.Add(Me.pMagenta)

        Me.Controls.Add(Me.pYellow)

        Me.Controls.Add(Me.pRed)

        Me.Controls.Add(Me.pTray)

        Me.Controls.Add(Me.pDrawWindow)

        Me.Text = "Scribbler"

        Me.WindowState = System.Windows.Forms.FormWindowState.Maximized

 

    End Sub

 

    Public Shared Sub Main()

        Application.Run(New ScribblerForm)

    End Sub

 

#End Region

 

    Private Sub bClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bClear.Click

        Me.graphics.FillRectangle(New SolidBrush(Color.White), 0, 0, Me.pDrawWindow.Size.Width, Me.pDrawWindow.Size.Height)

        Me.pDrawWindow.Invalidate()

    End Sub

 

    Private Sub pDrawWindow_Paint(ByVal sender As System.Object, ByVal e As PaintEventArgs) Handles pDrawWindow.Paint

        e.Graphics.DrawImage(Me.bitmap, 0, 0)

    End Sub

 

    Private Sub pDrawWindow_MouseDown(ByVal sender As System.Object, ByVal e As MouseEventArgs) Handles pDrawWindow.MouseDown

        Me.hasCapture = True

        Me.oldX = e.X

        Me.oldY = e.Y

    End Sub

 

    Private Sub pDrawWindow_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles pDrawWindow.MouseMove

        Dim x, y, Diameter As Integer

        If Me.hasCapture Then

            x = e.X

            y = e.Y

 

            ' Draw line from last hit point to current hit point

            '            Me.graphics.DrawLine(New Pen(Me.currentColorPanel.BackColor), Me.oldX, Me.oldY, x, y)

            Diameter = 4

            Me.graphics.FillEllipse(New System.Drawing.SolidBrush(Me.currentColorPanel.BackColor), New Rectangle(x - (Diameter / 2), y - (Diameter / 2), Diameter, Diameter))

            '            Me.graphics.DrawLine(New Pen(Me.currentColorPanel.BackColor), Me.oldX, Me.oldY, x, y)

            '            Me.pDrawWindow.Invalidate(New Rectangle(Math.Min(x, Me.oldX) - 2, Math.Min(y, Me.oldY) - 2, Math.Abs(Me.oldX - x) + 4, Math.Abs(Me.oldY - y) + 4))

            Me.pDrawWindow.Invalidate(New Rectangle(x - (Diameter / 2), y - (Diameter / 2), Diameter + 2, Diameter + 2))

            Me.oldX = x

            Me.oldY = y

        End If

    End Sub

 

    Private Sub pDrawWindow_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles pDrawWindow.MouseUp

        Me.hasCapture = False

    End Sub

 

    Private Sub colorPanel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pBlack.Click, pBlue.Click, pGreen.Click, pMagenta.Click, pRed.Click, pYellow.Click, pGray.Click

        ' deselect the current color panel

        Me.currentColorPanel.Location = New System.Drawing.Point(Me.currentColorPanel.Location.X + 2, Me.currentColorPanel.Location.Y + 2)

        Me.currentColorPanel.Size = New System.Drawing.Size(22, 18)

 

        ' select the panel that was clicked on

        Dim selectedPanel As Panel = CType(sender, Panel)

        selectedPanel.Location = New System.Drawing.Point(selectedPanel.Location.X - 2, selectedPanel.Location.Y - 2)

        selectedPanel.Size = New System.Drawing.Size(26, 22)

        Me.currentColorPanel = selectedPanel

    End Sub

    Public Sub ButtonPressed(ByVal button As Integer)

        Select Case button

            Case HardwareButtons.Hardware1

                '                MessageBox.Show("Íàæàòà êíîïêà 'Êàëåíäàðü'")

                Exit Sub

            Case HardwareButtons.Hardware2

                '                MessageBox.Show("Íàæàòà êíîïêà 'WLAN'!")

                Exit Sub

            Case HardwareButtons.Hardware3

                '                MessageBox.Show("Íàæàòà êíîïêà 'Êîíòàêòû'!")

                Exit Sub

            Case HardwareButtons.Hardware4

                '                MessageBox.Show("Íàæàòà êíîïêà 'Today'!")

                Exit Sub

            Case HardwareButtons.Hardware5

                '               MessageBox.Show("Óäåðæèâàåòñÿ êíîïêà 'Today'!")

                Exit Sub

            Case HardwareButtons.Hardware6

                '               MessageBox.Show("Íàæàòà ëåâàÿ òîðöåâàÿ êíîïêà!")

                Exit Sub

            Case HardwareButtons.Hardware7

                '               MessageBox.Show("Íàæàòà ïðàâàÿ òîðöåâàÿ êíîïêà!")

                Exit Sub

            Case HardwareButtons.Hardware8

                '               MessageBox.Show("Óäåðæèâàåòñÿ ëåâàÿ òîðöåâàÿ êíîïêà!" & Chr(13) & "Âûâàëèâàþñü!")

                Me.Close() ' Ñåêðåòíûé âûõîä èç ïðîãðàììû

                Exit Sub

            Case HardwareButtons.Hardware9

                '                MessageBox.Show("Óäåðæèâàåòñÿ ïðàâàÿ òîðöåâàÿ êíîïêà!")

                Exit Sub

            Case HardwareButtons.Hardware10

                '                MessageBox.Show("Íàæàòà êíîïêà 'Enter'!")

                Exit Sub

            Case HardwareButtons.Hardware11

                '                MessageBox.Show("Íàæàòà êíîïêà 'Âëåâî'!")

                Exit Sub

            Case HardwareButtons.Hardware12

                '                MessageBox.Show("Íàæàòà êíîïêà 'Âïðàâî'!")

                Exit Sub

            Case HardwareButtons.Hardware13

                '                MessageBox.Show("Íàæàòà êíîïêà 'Ââåðõ'!")

                Exit Sub

            Case HardwareButtons.Hardware14

                '                MessageBox.Show("Íàæàòà êíîïêà 'Âíèç'!")

                Exit Sub

        End Select

    End Sub

    Public Class myMessageWindow

        Inherits messageWindow

 

        Public Const WM_HOTKEY = &H312

        Dim example As ScribblerForm

        Public Sub New(ByVal example As ScribblerForm)

            Me.example = example

        End Sub

 

        Protected Overrides Sub WndProc(ByRef msg As Message)

            Select Case msg.Msg

                Case WM_HOTKEY

                    example.ButtonPressed(msg.WParam.ToInt32())

                    Return

            End Select

            MyBase.WndProc(msg)

        End Sub

    End Class

 

End Class