Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Start app, but not 'in focus' 2

Status
Not open for further replies.

Andrzejek

Programmer
Jan 10, 2006
8,569
US

I have a very simple app - one Form reminder that it is a break time. But when it starts it takes a focus from whatever application user is using at that time. So they have to click back on the app they were on, and that's annoying.

I know I can use SendKeys of Alt-Tab or something like that to get back to their app, but is there a better way to start an app, but start it not 'in focus'?


Have fun.

---- Andy
 
If you are using Shell function, take a look at its args. Below are the options that you have.

vbHide 0
vbNormalFocus 1
vbMinimizedFocus 2
vbMaximizedFocus 3
vbNormalNoFocus 4
vbMinimizedNoFocus 6

If you are on the ShellExecute way,

SW_HIDE
SW_MAXIMIZE
SW_MINIMIZE
SW_RESTORE
SW_SHOW
SW_SHOWMAXIMIZED
SW_SHOWMINIMIZED
SW_SHOWMINNOACTIVE
SW_SHOWNA
SW_SHOWNOACTIVATE
SW_SHOWNORMAL

are the values available for NShowCmd param.

 

No, I am not using Shell function.
This small app is started from Scheduled Tasks in Windows, as a reminder. When started, I just don't want it to be in focus.

Have fun.

---- Andy
 
I just tested setting Form1 Enabled property to false and when I ran it it didn't grab focus.


[gray]Experience is something you don't get until just after you need it.[/gray]
 

Thank you Error7, but if the Form is dis-abled - you can not get rid of it. it will not accept any clicks or user input. :-(



Have fun.

---- Andy
 
Of course it won't accept any clicks. It's disabled. [bigsmile]

But in your timer routine you can enable or unload the form after a suitable time period.

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Thanks.

I will go with my original solution:
Code:
Private Sub Form_Activate()
   [blue]SendKeys "%{TAB}"[/blue]
End Sub

Have fun.

---- Andy
 

Still can not make it happen.

I did try Shell, but the app takes a focus anyway.

Here is what I have:

Form
Code:
VERSION 5.00
Begin VB.Form frmBreak 
   BackColor       =   &H0080FF80&
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Break Reminder"
   ClientHeight    =   1485
   ClientLeft      =   45
   ClientTop       =   435
   ClientWidth     =   4080
   Icon            =   "frmBreak.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   1485
   ScaleWidth      =   4080
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command1 
      Caption         =   "Command1"
      Height          =   495
      Left            =   4080
      TabIndex        =   0
      Top             =   2280
      Width           =   1215
   End
   Begin VB.CommandButton cmdExit 
      BackColor       =   &H0080FF80&
      Caption         =   "Exit"
      Height          =   375
      Left            =   3120
      Style           =   1  'Graphical
      TabIndex        =   3
      TabStop         =   0   'False
      Top             =   960
      Width           =   855
   End
   Begin VB.Timer Timer1 
      Interval        =   1000
      Left            =   120
      Top             =   120
   End
   Begin VB.Label lblInfo1 
      Alignment       =   2  'Center
      AutoSize        =   -1  'True
      BackStyle       =   0  'Transparent
      Caption         =   "Label1"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   330
      Left            =   1620
      TabIndex        =   2
      Top             =   600
      Width           =   870
   End
   Begin VB.Label lblInfo 
      Alignment       =   2  'Center
      AutoSize        =   -1  'True
      BackStyle       =   0  'Transparent
      Caption         =   "Hello"
      BeginProperty Font 
         Name            =   "Arial"
         Size            =   14.25
         Charset         =   0
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   330
      Left            =   1725
      TabIndex        =   1
      Top             =   120
      Width           =   660
   End
End
Attribute VB_Name = "frmBreak"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub cmdExit_Click()
    Unload Me
End Sub

Private Sub Form_Load()

lblInfo1.Caption = "it is Break Time...."

With frmBreak
    .Top = Screen.Height - .Height
    .Left = (Screen.Width - .Width)
End With

End Sub

Private Sub Form_Resize()
StayOnTop Me
End Sub

Private Sub Timer1_Timer()
lblInfo.Caption = Time
lblInfo.Refresh
End Sub
Module
Code:
Option Explicit

Public Declare Function SetWindowPos& Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)

Const HWND_TOPMOST = -1
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2

Public Sub StayOnTop(frm As Form)

SetWindowPos frm.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE

End Sub


Have fun.

---- Andy
 

Where shell I put this SWP_NOACTIVATE ?


Have fun.

---- Andy
 
I did try:
Code:
Option Explicit

Public Declare Function SetWindowPos& Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)

Const HWND_TOPMOST = -1
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2

Public Sub StayOnTop(frm As Form)

SetWindowPos frm.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE[blue] Or SWP_NOACTIVATE[/blue]

End Sub

but I get an error on teh BLUE:
Variable not defined.
:-(


Have fun.

---- Andy
 
Well sure, you have to define the constant before you can use it, right? Just like the other two.

I would suggest that you open the file winuser.h, which should be on your machine. (You can also find a copy by googling.) Then do a search for SWP_NOACTIVATE. You will find the following code line:
Code:
#define SWP_NOACTIVATE      0x0010
which translates into VB as
Code:
Const SWP_NOACTIVATE = &H10
which line you will need to add to your code.

HTH

Bob
 
<API Text Viewer

Yes, indeed, and that's probably the way to go most of the time. However, I get more ego gratification from talking about winuser.h. :)

The other-than-selfish reason for mentioning this is that it's a little more low level, which has pedagogical value.
 
>pedagogical.

Had to look that one up Bob and then realised that only you could use a word like that. [smarty]

[gray]Experience is something you don't get until just after you need it.[/gray]
 
>pedagogical

Error7, you just needed to look back to thread222-1289534 :)
 
strongm, I've been searching Youtube all morning and, I'm pleased to say, I didn't find any evidence of Bob's singing. [bigears]

[gray]Experience is something you don't get until just after you need it.[/gray]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top