bcb6.0多窗体时 实现屏保功能,先隐藏应用程序里打开的窗体,模拟登录后,在将窗体显示出来
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include "Unit3.h"//屏保
#include "Unit4.h"
#include <vector.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
std::vector<HWND> vecHand;
std::vector<HWND> vecAllHand;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
bIsLock = false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Timer1->Enabled = false;
Button1Click(Sender);
}
//---------------------------------------------------------------------------
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
char str[100];
memset(str, 0, 100);
vecAllHand.push_back(hwnd);
return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char str[100];
memset(str, 0, 100);
EnumWindows((WNDENUMPROC)EnumWindowsProc, NULL);
//HWND pWnd = FindWindow(NULL, "Form1");
HWND pWnd = Application->Handle;
if(pWnd)
{
DWORD dwtheadid;
DWORD dwProcessId;
//根据窗口得到线程ID,进程ID
dwtheadid = GetWindowThreadProcessId(pWnd, &dwProcessId);
//std::list<HWND> list1;
//枚举线程的所有窗口
//EnumThreadWindows(dwtheadid, (WNDENUMPROC)EnumThreadWndProc, (LPARAM)&list1);
EnumThreadWindows(dwtheadid, (WNDENUMPROC)EnumThreadWndProc, (LPARAM)&vecHand);
if (!bIsLock)
{
HWND pFirWnd = FindWindow(NULL, "Form1");
for(size_t i = 0; i < vecHand.size(); ++i)
{
for (size_t j = 0; j < vecAllHand.size(); ++j)
{
if (vecHand[i] == vecAllHand[j] && vecHand[i] != pFirWnd)
{
ShowWindow(vecHand[i], SW_HIDE);
GetWindowText(vecHand[i], str, sizeof(str));
Form1->ListBox1->Items->Add(AnsiString(str) + "隐藏");
}
}
}
TForm3 *Form3 = new TForm3(Application);
Form3->Left = Form1->Left+6;
Form3->Top = Form1->Top + 28;//ok
Form3->Height = Form1->ClientHeight;
Form3->Width = Form1->ClientWidth;
//HWND pHand = FindWindow(NULL, "Form3");
//SetForegroundWindow(pHand);
Form3->Show();
}
}
}
//---------------------------------------------------------------------------
BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM lParam )
{
std::vector<HWND>* pvector = (std::vector<HWND>*)lParam;
char str[100];
memset(str, 0, 100);
GetWindowText(hwnd, str, sizeof(str));
if (AnsiString(str) == "Form1")
{
//return false;
}
pvector->push_back(hwnd);
/*
// 窗口是否可视
if (!IsWindowVisible(hwnd))
{
//return true;
}
// 窗口是否可激活
if (!IsWindowEnabled(hwnd))
{
//return true;
} */
//GetClassName(hwnd, str, sizeof(str));
//Form1->ListBox1->Items->Add(AnsiString(str));
return true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Application->MessageBox("勤勤是个瓜皮", "提示", MB_YESNO|MB_ICONINFORMATION);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
TForm2 *Form2 = new TForm2(NULL);
Form2->ShowModal();
delete Form2;
Form2 = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
Application->OnMessage = AppMessage;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::AppMessage(tagMSG &Msg, bool &Handled)
{
bool flag = false;
switch(Msg.message)
{
case WM_LBUTTONDOWN:
flag = false;
break;
default:
flag = true;
break;
}
if (flag)
{
if (!Timer1->Enabled)
{
Timer1->Enabled = true;
}
}
else
{
if (Timer1->Enabled)
{
Timer1->Enabled = false;
}
}
Handled = false;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit3.h"
#include "Unit1.h"
#include <vector.h>
#include "Unit4.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm3 *Form3;
//---------------------------------------------------------------------------
__fastcall TForm3::TForm3(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm3::FormMouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
char str[100];
memset(str, 0, 100);
Application->MessageBox("解锁登录", "解除锁定", MB_OK);
HWND pFirWnd = FindWindow(NULL, "Form1");
for(size_t i = vecHand.size(); i > 0; --i)
{
for (size_t j = vecAllHand.size(); j > 0; --j)
{
if (vecHand[i-1] == vecAllHand[j-1] && vecHand[i] != pFirWnd)
{
ShowWindow(vecHand[i-1], SW_SHOW);
GetWindowText(vecHand[i-1], str, sizeof(str));
Form1->ListBox1->Items->Add(AnsiString(str) + "显示");
}
}
}
bIsLock = false;
this->Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm3::FormShow(TObject *Sender)
{
bIsLock = true;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#include <vcl.h>
#ifndef Unit4H
#define Unit4H
#include <vector.h>
//---------------------------------------------------------------------------
extern bool bIsLock;
extern std::vector<HWND> vecHand;
extern std::vector<HWND> vecAllHand;
#endif