need help with running application wile invisible

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 2455 Views - Last Post: 07 October 2008 - 11:21 PM Rate Topic: -----

#1 narmer93  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 317
  • Joined: 13-March 08

need help with running application wile invisible

Posted 24 August 2008 - 02:34 PM

i need help with this
this code halfly works
Public Class Form1

    Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.S Then
            Me.Visible = True'this part doesn't work'
        ElseIf e.KeyCode = Keys.H Then
            Me.Visible = False'this part works'
        End If
    End Sub
End Class


how can i press a key and make the form visible or other thing if the form is not in the focus mode?
the above code works only if the form is in focus mode
how can i make it work if it is not in the focus mode or running in the form of a tray icon?
i can it be something like the task manager ,i press ctrl +alt+del and it opens,
how can i make something like that if it is running in the form of a tray icon can be visible by clicking on it or by pressing a combination of buttons?

and this code also desn't work
Public Class Form1

    Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.S And e.KeyCode = Keys.D Then
            Me.Hide()
        End If
    End Sub
End Class


it works if it was only one key pressed but more than one ley nothing happens
any explanations or ideas or beter ways?

This post has been edited by narmer93: 24 August 2008 - 02:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: need help with running application wile invisible

#2 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: need help with running application wile invisible

Posted 24 August 2008 - 04:31 PM

Your trying to show the form when it is no longer accepting data.
Let me explain.

If a form is hidden, it can't run the code you are trying to tell it to do. Thus making it unreachable code.
Was This Post Helpful? 0
  • +
  • -

#3 narmer93  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 317
  • Joined: 13-March 08

Re: need help with running application wile invisible

Posted 24 August 2008 - 04:43 PM

if the application can't be run if it is not in focus mode ,so how does the task manager work,or
i have a prog called tuneup utilites ,there is a memory optimizer in it ,when i press ctrl+alt+o,it runs the optimizer in the tray icon
how can i make something like that then?


and why doesn't multiple buttons work? as in the second code?

if the application can't be run if it is not in focus mode ,so how does the task manager work,or
i have a prog called tuneup utilites ,there is a memory optimizer in it ,when i press ctrl+alt+o,it runs the optimizer in the tray icon
how can i make something like that then?


and why doesn't multiple buttons work? as in the second code?
Was This Post Helpful? 0
  • +
  • -

#4 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • View blog
  • Posts: 1,079
  • Joined: 05-June 08

Re: need help with running application wile invisible

Posted 24 August 2008 - 10:34 PM

As far as i know the KeyDown event will only give you information regarding the last key that was pressed


I found this somewhere

Quote

The trouble that I can see with this is it seems that the KeyDown event is fired individually for each key; you'll either have to do use booleans as you indicated or the GetAsyncKeyState windows API function
Public Class Form1

	Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal keyCode As Integer) As Short

	Private _txtBox As TextBox

	Public Sub New()


		' This call is required by the Windows Form Designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.
		UnimportantLayout()

	End Sub

	Private Sub Textonkeydown(ByVal sender As Object, ByVal e As KeyEventArgs)
		_txtBox.AppendText(GetKeyString())
	End Sub

	Private Function GetKeyString() As String
		Dim sb As New System.Text.StringBuilder()
		Dim keys(4) As Short

		keys(0) = GetAsyncKeyState(VKey.A)
		keys(1) = GetAsyncKeyState(VKey.B)
		keys(2) = GetAsyncKeyState(VKey.C)
		keys(3) = GetAsyncKeyState(VKey.D)

		If (WasKeyPressed(keys(0))) Then
			sb.Append("A, ")
		End If

		If (WasKeyPressed(keys(1))) Then
			sb.Append("B, ")
		End If

		If (WasKeyPressed(keys(2))) Then
			sb.Append("C, ")
		End If

		If (WasKeyPressed(keys(3))) Then
			sb.Append("D, ")
		End If

		If (sb.Length > 0) Then
			sb.Remove(sb.Length - 2, 2)
		End If

		sb.AppendLine()

		Return sb.ToString()
	End Function

	Private Function WasKeyPressed(ByVal state As Short) As Boolean
		' For some goofy reason, the compiler thinks that "&H8000" is not
		' representable in Short, so I had to use an integer.
		Dim highBit As Integer = &H8000

		Return ((state And highBit) = highBit)
	End Function

	Private Enum VKey
		' Find more key codes at
		' http://msdn2.microsoft.com/en-us/library/ms645540.aspx
		A = &H41
		B = &H42
		C = &H43
		D = &H44
		E = &H45
	End Enum

	Private Sub UnimportantLayout()
		_txtBox = New TextBox()
		_txtBox.Multiline = True
		_txtBox.Margin = New Padding(10)
		_txtBox.Dock = DockStyle.Fill
		_txtBox.ReadOnly = True
		AddHandler _txtBox.KeyDown, AddressOf Textonkeydown
		Me.Controls.Add(_txtBox)
	End Sub

End Class


What I found with this program is that what keys can be detected (at least on my keyboard) is reliant upon the key's location. I could make no combination of C and D work, but A B C, and A B D were OK. You'll have to fill in the codes for more keys to see how it works out, but this would probably be easier than managing several globabl booleans.

This post has been edited by Damage: 24 August 2008 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • View blog
  • Posts: 1,079
  • Joined: 05-June 08

Re: need help with running application wile invisible

Posted 24 August 2008 - 11:06 PM

Quote

try something like this:

If (((e.Alt And (e.Shift Or e.Control)) And e.KeyCode = Keys.D) Then
' do something
End If

Or

If ((e.Alt And e.Shift) And e.KeyCode = Keys.D) OrElse _
((e.Alt And e.Control) And e.KeyCode = Keys.D) Then
' do something
End If

Was This Post Helpful? 0
  • +
  • -

#6 narmer93  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 317
  • Joined: 13-March 08

Re: need help with running application wile invisible

Posted 25 August 2008 - 04:42 AM

ok well any of them work if the form is minimized or not in focus mode?
Was This Post Helpful? 0
  • +
  • -

#7 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: need help with running application wile invisible

Posted 25 August 2008 - 07:55 AM

Try this
//TODO CODE HIDE
Me.Hide()

//TODO CODE SHOW
Me.Show()



Hope this helps

This post has been edited by gbertoli3: 25 August 2008 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

#8 WayneSpangler  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 106
  • Joined: 22-March 08

Re: need help with running application wile invisible

Posted 25 August 2008 - 09:23 AM

What you need is a hot key. Here is the code I use. If you have any questions just ask.
	' Windows API declarations
	Private Const WM_HOTKEY As Integer = &H312
	Public Const MOD_ALT As Integer = &H1	   ' alt key down
	Public Const MOD_CONTROL As Integer = &H2   ' alt control down
	Public Const MOD_SHIFT As Integer = &H4	 ' alt shift down

	Public Declare Auto Function RegisterHotKey Lib "user32" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Boolean

	Public Declare Auto Function UnregisterHotKey Lib "user32" (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		' you can combine alt, control and/or shift
		' where I have 1 you can use any number but for each hotkey it must be unique so you can't have another 1
		' You can register more than one hot key
		'RegisterHotKey(Me.Handle, 1, MOD_ALT Or MOD_CONTROL, Keys.Up)
		RegisterHotKey(Me.Handle, 1, MOD_ALT, Keys.M)
		RegisterHotKey(Me.Handle, 2, MOD_ALT Or MOD_CONTROL, Keys.Down)
	End Sub

	Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
		UnregisterHotKey(Me.Handle, 1)
	End Sub

	Protected Overrides Sub WndProc(ByRef message As System.Windows.Forms.Message)
		If (message.Msg = WM_HOTKEY) Then
			Dim hotKeyId As Integer = message.WParam.ToInt32()

			If hotKeyId = 1 Then
				If Me.WindowState = FormWindowState.Minimized Then Me.WindowState = FormWindowState.Normal
			End If
			If hotKeyId = 2 Then
				If Me.WindowState <> FormWindowState.Minimized Then Me.WindowState = FormWindowState.Minimized
			End If
		End If
		MyBase.WndProc(message) '<<<<<< ALLWAYS ADD THIS = COMPUTER CRASH
	End Sub

Was This Post Helpful? 0
  • +
  • -

#9 narmer93  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 317
  • Joined: 13-March 08

Re: need help with running application wile invisible

Posted 25 August 2008 - 02:55 PM

is there a shorter way or easier code that can be used when the form is minimized or not in focus,and i press 2 or 3 keys and the form appears
is there one to make it easier than the one u wrote?
Was This Post Helpful? 0
  • +
  • -

#10 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: need help with running application wile invisible

Posted 25 August 2008 - 04:56 PM

View Postnarmer93, on 25 Aug, 2008 - 02:55 PM, said:

is there a shorter way or easier code that can be used when the form is minimized or not in focus,and i press 2 or 3 keys and the form appears
is there one to make it easier than the one u wrote?


Your never going to get anywhere in programming if you think that is too long. You should try making a program. There are lots of lines of code, but as programmers we enjoy writing it. But in this case you were just handed the code for free, and complain about it being too long. You should be more grateful and say thank you.
Was This Post Helpful? 0
  • +
  • -

#11 narmer93  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 317
  • Joined: 13-March 08

Re: need help with running application wile invisible

Posted 25 August 2008 - 06:31 PM

that is too long and i always say thanks when i get what i wanted and this code seems to be longer than to do this
vb.net uses simple codes
once i was trying to learn java and i was asking about how to make it play an mp3 file or an audio file and i got a long code while i can easily play .wav files in vb.net with a short line
and this is the only part in the code that i understad
            If hotKeyId = 1 Then
                If Me.WindowState = FormWindowState.Minimized Then Me.WindowState = FormWindowState.Normal
            End If
            If hotKeyId = 2 Then
                If Me.WindowState <> FormWindowState.Minimized Then Me.WindowState = FormWindowState.Minimized
            End If
        End If


all the rest of the code is not easy to understand,at least not for me
and currently i think i will learn C or C++ language and i am looking for the programming programme and i want a new version not a win95 version and before using it ,i have to be sure that it doesn't need anything like the .netframework or JRE and can work on normal windows
is there something like that?

oh sorry cuz i didn't say thanks in the previous posts:)
Was This Post Helpful? 0
  • +
  • -

#12 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: need help with running application wile invisible

Posted 25 August 2008 - 06:37 PM

What you say is either
I don't understand.
or
Could you explain in the code?
Was This Post Helpful? 0
  • +
  • -

#13 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • View blog
  • Posts: 1,079
  • Joined: 05-June 08

Re: need help with running application wile invisible

Posted 25 August 2008 - 08:38 PM

I hate to break this to you, but there are going to be times when even .NET libraries will not be sufficient to condense all the code needed into one tiny little line of text
Was This Post Helpful? 0
  • +
  • -

#14 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: need help with running application wile invisible

Posted 25 August 2008 - 08:42 PM

Well they could, but they would never do it because that is way too much work for them.

But if your boss says do it, you do it.
Was This Post Helpful? 0
  • +
  • -

#15 Damage  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 27
  • View blog
  • Posts: 1,079
  • Joined: 05-June 08

Re: need help with running application wile invisible

Posted 25 August 2008 - 09:17 PM

Quote

But if your boss says do it, you do it.



Quote

Theirs not to make reply,
Theirs not to reason why,
Theirs but to do & die,

This post has been edited by Damage: 25 August 2008 - 09:18 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2