一个小技巧关于PrevInstance的

本文介绍了一种让已运行程序在再次启动时将其主界面切换到前台的方法。通过使用Windows API函数如FindWindow、SetForegroundWindow及SendMessage等,可以实现这一功能。此技巧适用于希望优化用户体验的软件开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们时常会看到某些软件有这样的功能.
当程序已经运行的时候再运行这个程序程序会把以前运行的主界面显示在前台来.下面代码就是这个功能.

VERSION 5.00
Begin VB.Form frmMain
   Caption         =   "李小俊是个猪头"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  '窗口缺省
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SYSCOMMAND = &H112
Private Const SC_RESTORE = &HF120&

Private Sub Form_Load()
    Me.WindowState = 1
    If App.PrevInstance Then
        Me.Caption = ""
        Dim hWnd As Long
        hWnd = FindWindow(vbNullString, "李小俊是个猪头")
        If hWnd > 0 Then
            SendMessage hWnd, WM_SYSCOMMAND, SC_RESTORE, 0
            SetForegroundWindow hWnd
            Unload Me: End
        End If
    End If
End Sub 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值