VC++可謂神通廣大,如果學到家了,或者就掌握了那么一點MFC,你也會感到它的方便快捷,當然最重要的是功能強大。不是嗎,從最基本的應用程序.EXE到動態(tài)連接庫DLL,再由風靡網(wǎng)上的ActiveX控件到Internet Server API,當然,還有數(shù)據(jù)庫應用程序……瞧,我都用它來做屏幕保護程序了。一般的屏幕保護程序都是以SCR作為擴展名,并且要放在c:\windows 目錄或 c:\windows\system 目錄下,由Windows 98內(nèi)部程序調(diào)用(Windows NT 是在 c:\windows\system32 目錄下)。怎么調(diào)用?不用說了,這誰不知道。
岫巖ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
好了,我們來作一個簡單的。選擇MFC AppWizard(exe),Project Name 為MyScreensaver,[NEXT],對話框,再后面隨你了。打開菜單Project、Settings,在Debug頁、Executable for debug session項,以及Link頁中Output file name項改為c:\windows\MyScreensaver.scr,這樣,你可以調(diào)試完后,直接在VC中運行(Ctrl+F5),便可看到結果。當然,這樣做的唯一缺點是你必須手動清除Windows 目錄下的垃圾文件(當然是在看到滿意結果后;還有,你可借助SafeClean 這個小東東來幫你清除,除非你的硬盤大的讓你感到無所謂……快快快回來,看我跑到那里去了)。接下來用Class Wizard生成CMyWnd類,其基類為CWnd(在Base Class 中為generic CWnd)。這個類是我們所要重點研究的。創(chuàng)建滿屏窗口、計時器,隱藏鼠標,展示圖片,響應鍵盤、鼠標等等,這家伙全包了。至于MyScreensaverDlg.h與MyScreensaverDlg.cpp文件我們暫時不管。打開MyScreensaver.cpp,修改InitInstance()函數(shù):
BOOL CMyScreensaverApp::InitInstance()
{
AfxEnableControlContainer();
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CMyWnd* pWnd = new CMyWnd;
pWnd-Create();
m_pMainWnd = pWnd;
return TRUE;
}
當然,再這之前得先 #include “MyWnd.h" 。后面要做的都在MyWnd.h 與 MyWnd.cpp 兩文件中了。
下面給出CMyWnd 的說明:
class CMyWnd : public CWnd
{
public:
CMyWnd();
static LPCSTR lpszClassName; //注冊類名
public:
BOOL Create();
public:
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyWnd)
protected:
virtual void PostNcDestroy();
//}}AFX_VIRTUAL
public:
virtual ~CMyWnd();
protected:
CPoint m_prePoint; //檢測鼠標移動
void DrawBitmap(CDC& dc, int nIndexBit);
//{{AFX_MSG(CMyWnd)
afx_msg void OnPaint();
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnDestroy();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
afx_msg void OnActivateApp(BOOL bActive, HTASK hTask);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
MyWnd.cpp 文件:
……
CMyWnd::CMyWnd()
{
m_prePoint=CPoint(-1, -1);
}
LPCSTR CMyWnd::lpszClassName=NULL;
BOOL CMyWnd::Create()
{
if(lpszClassName==NULL)
{
lpszClassName=AfxRegisterWndClass(CS_HREDRAW CS_VREDRAW,
::LoadCursor(AfxGetResourceHandle(),MAKEINTRESOURCE(IDC_NOCURSOR)));
//注冊類;IDC_NOCURSOR為新建光標的ID,這個光標沒有任何圖案
}
CRect rect(0, 0, ::GetSystemMetrics(SM_CXSCREEN),
::GetSystemMetrics(SM_CYSCREEN));
CreateEx(WS_EX_TOPMOST, lpszClassName, _T(“”), WS_VISIBLE WS_POPUP,
rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
GetSafeHwnd(), NULL, NULL); //創(chuàng)建一個全屏窗口
SetTimer(ID_TIMER, 500, NULL);//計時器,ID_TIMER別忘了定義
return TRUE;
}
為了防止同時運行兩個相同的程序,下面兩個函數(shù)是必需的:
void CMyWnd::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CWnd::OnActivate(nState,pWndOther,bMinimized);
if (nState==WA_INACTIVE)
PostMessage(WM_CLOSE);
}
void CMyWnd::OnActivateApp(BOOL bActive, HTASK hTask)
{
CWnd::OnActivateApp(bActive, hTask);
if (!bActive) //is being deactivated
PostMessage(WM_CLOSE);
}
OnPaint()函數(shù)將全屏窗口置為黑色:
void CMyWnd::OnPaint()
{
CPaintDC dc(this);
CBrush brush(RGB(0,0,0));
CRect rect;
GetClientRect(rect);
dc.FillRect(&rect, &brush);
}
由計數(shù)器調(diào)用DrawBitmap()函數(shù),切換圖片;注意,下面兩個函數(shù)中的IDB_BITMAP1, dc.BitBlt(0,0,800,600……以及if(nIndexBit=5)中的有關數(shù)據(jù)依據(jù)你的bmp圖片個數(shù)、尺寸、位置不同而不同,我是選擇了5張800x600的bmp圖片。注意,ID值是連續(xù)的,IDB_BITMAP1最小。
void CMyWnd::DrawBitmap(CDC &dc, int nIndexBit)
{
CDC dcmem;
dcmem.CreateCompatibleDC(&dc);
CBitmap m_Bitmap;
m_Bitmap.LoadBitmap(IDB_BITMAP1+nIndexBit);
dcmem.SelectObject(m_Bitmap);
dc.BitBlt(0,0,800,600,&dcmem,0,0,SRCCOPY);
}
void CMyWnd::OnTimer(UINT nIDEvent)
{
CClientDC dc(this);
static nIndexBit=0;
if(nIndexBit=5)
nIndexBit=0;
DrawBitmap(dc, nIndexBit++);
CWnd::OnTimer(nIDEvent);
}
響應鍵盤、鼠標是屏幕保護程序不可缺少的,在OnKeyDown()、 OnLButtonDown()、 OnMButtonDown()、OnRButtonDown()、OnSysKeyDown()函數(shù)中都加入:
PostMessage(WM_CLOSE);
OnMouseMove()函數(shù)比較特殊,它應加的代碼為:
if(m_prePoint == CPoint(-1,-1))
m_prePoint = point;
else if(m_prePoint!=point)
PostMessage(WM_CLOSE);
快要完工了。在OnDestroy()函數(shù)中刪掉計時器:KillTimer(ID_TIMER);
還有啦,在CMyWnd::PostNcDestroy() 中加入: delete this;
哎呀,腰酸背疼,眼球發(fā)澀,手背奇麻(不會吧)!不過,相信你一定會迫不及待地按下Ctrl+F5, 看著一幅幅圖片在你面前輪番展示,啊,自己的屏幕保護程序!趕快趕快,換上自制的屏保,感覺就是不一樣:圖片任你挑,時間間隔任你改,鼠標?鍵盤?我想響應誰就響應誰……哎呀,誰扔的紙團:(。
其實,上面的程序還有很多可以改進的地方,比如圖片總是單一地顯示;bmp 文件太大,導致生成的屏幕保護程序也很大,遠沒有jpg合算;沒有密碼,沒有可直接控制的界面。由于InitInstance()函數(shù)的簡單處理(直接調(diào)用CMyWnd類),你會發(fā)現(xiàn)當你在桌面上右擊,選擇“屬性”、“屏幕保護程序”頁、“屏幕保護程序”下拉菜單、選中MyScreensaver時,MyScreensaver就直接預覽了(或是直接運行了);假設你確定MyScreensaver作為你的屏幕保護程序,等你第二次進入“屏幕保護程序”頁時,就直接預覽。Why? 回頭看看InitInstance()函數(shù)就明白了。為了讓它更聽話地工作,可修改InitInstance()函數(shù):
LPTSTR lpszArgv = __argv[1];
if (lpszArgv[0] ==‘/’)
lpszArgv++;
if (lstrcmpi(lpszArgv, _T(“s”))==0)
{
CMyWnd* pWnd=new CMyWnd;
pWnd-Create();
m_pMainWnd=pWnd;
return TRUE;
}
return FALSE;
不過現(xiàn)在你要是再在VC中運行這個程序,“該程序執(zhí)行了非法操作,即將關閉。將會伴隨著一超重低音供你欣賞。(???)原因是我們加了一句return FALSE; 還有,別忘了還有一個CMyScreensaverDlg類沒有用上,用它來與你的屏保直接對話再好不過了。例如,為了方便地確定時間間隔,選取圖片,加上一個編輯框和幾個按鈕就可以了。重申一點,由于生成文件較大,占用的內(nèi)存也多,如果不能運行,很可能是開的窗口太多了。這時你可以換較小的圖片。
系統(tǒng)就有這個屏保啊!~!
Option EXPlicit
Dim quitflag As Boolean '聲明終止程序標志變量
Dim lleft
'聲明隱藏或顯示鼠標的API函數(shù)
Private Declare Function ShowCursor Lib "user32"
(ByVal bShow As Long) As Long
'檢測鼠標單擊或移動
Private Sub Form_Click()
quitflag = True
End Sub
Private Sub Form_MouseMove(Button As Integer,Shift As Integer, X As Single, Y As Single)
Static xlast, ylast
Dim xnow As Single
Dim ynow As Single
xnow = X
ynow = Y
If xlast = 0 And ylast = 0 Then
xlast = xnow
ylast = ynow
Exit Sub
End If
If xnow xlast Or ynow ylast Then
quitflag = True
End If
End Sub
'檢測按鍵
Private Sub Form_KeyDown(KeyCode As Integer,Shift As Integer)
quitflag = True
End Sub
Private Sub Form_Load()
Dim X As Long
lleft = 0
'橫向滾動文字的起始X坐標
If App.PrevInstance = True Then
'用APP對象的PrevInstance屬性
Unload Me
'防止同時運行屏幕保護程序的兩個實例
Exit Sub
End If
Select Case Ucase$(Left$(Command$, 2))
'裝載命令行參數(shù)
Case "/S" '在顯示器屬性對話框中單擊了
預覽按鈕或屏幕保護程序被系統(tǒng)正常調(diào)用。
Show
'全屏顯示Form1窗體
Randomize
'初始化隨機數(shù)生成器
X = ShowCursor(False)
'隱藏鼠標
BackColor = VBBlack
Do
Timer2.Enabled = True
'啟動Timer2 ,顯示屏幕保護滾動文字
DoEvents
'轉讓控制權,以便檢測鼠標和按鍵行為
Loop Until quitflag = True
'運行屏幕保護滾動文字直至有鼠標和按鍵行為
Timer2.Enabled = False
'終止?jié)L動文字
Timer1.Enabled = True
'啟動Timer1,退出屏幕保護程序
Case Else
Unload Me
Exit Sub
End Select
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim X
X = ShowCursor(True)
'顯示鼠標
End Sub
Private Sub Timer1_Timer()
Unload Me
'退出屏幕保護程序
End Sub
Private Sub Timer2_Timer()
顯示橫向滾動文字
lleft = lleft + 100
If lleft = 11810 Then
lleft = 0
Lab1.Top = Int(Rnd * 7000)
End If
Lab1.Left = lleft
Timer2.Enabled = False
End Sub
用Visual C#編寫屏幕保護程序
Visual C#是微軟公司推出的新一代程序開發(fā)語言,是微軟.Net框架中的一個重要組成部分。屏幕保護程序是以scr為擴展名的標準Windows可執(zhí)行程序。屏幕保護程序不僅可以延長顯示器的使用壽命,還可以保護私人信息。本文向大家介紹一個.Net平臺上用C#編寫的一個動態(tài)文本及圖形的屏幕保護程序。
一、具體實現(xiàn)步驟:
(1)在Visual Studio.Net下新建一個C#的Windows應用程序工程,不妨命名為screen_saver。
(2)現(xiàn)在我們來設計程序的主界面:
先將窗體的Name屬性設置為screen、Text屬性設置為空,BackColor屬性設置為Black、Size屬性設置為(800, 600)、 ControlBox、MaximizeBox、MinimizeBox、ShowInTaskbar屬性設置均為false、FormBorderStyle屬性設置為None。再往窗體上添加Label控件、PictureBox控件、Timer控件各一個。將Label控件的Name設置為word、Text屬性設置為空;將PictureBox控件的Name設置為picture1、Image設置為一個預知圖片;將Timer控件的Name設置為timerSaver、Enabled 屬性設為true、Interval屬性設為5。
(3)現(xiàn)在我們開始編寫完整程序代碼部分:
//導入使用到的名稱空間
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
file://
namespace screen_saver
{
///
/// Form1 的摘要說明。
///
public class screen : System.Windows.Forms.Form
{
file://加入私有成員變量
private System.ComponentModel.IContainer components;
private int iSpeed = 2;
private string str="福建南紡股份公司計算機中心";
file://定義文本字體及大小
private System.Drawing.Font TextStringFont = new System.Drawing.Font ("宋體”, 10,System.Drawing.FontStyle.Bold);
private Color TextStringcolor =System.Drawing.Color.Yellow; file://文本字體顏色
private int iDistance;
private int ixStart= 0;
private int iyStart= 0;
private int speed;
private int x1,y1;
int width1,height1;
private System.Windows.Forms.Timer timerSaver; file://計時器控件
private System.Windows.Forms.PictureBox picture1; file://圖形控件
private System.Windows.Forms.Label word; file://文本顯示控件
///
/// 必需的設計器變量。
///
public screen()
{
file://
// Windows 窗體設計器支持所必需的
file://
InitializeComponent();
word.Font=TextStringFont;
word.ForeColor=TextStringcolor;
System.Windows.Forms.Cursor.Hide(); file://隱藏光標
file://
// TODO: 在 InitializeComponent 調(diào)用后添加任何構造函數(shù)代碼
file://
}
///
/// 清理所有正在使用的資源。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
///
private void InitializeComponent() file://初始化程序中使用到的組件
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new system.Resources.ResourceManger(typeof(screen));
this.word = new System.Windows.Forms.Label();
this.timerSaver = new System.Windows.Forms.Timer(this.components);
this.picture1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// 設置文本顯示控件(word)屬性
this.word.ForeColor = System.Drawing.Color.Yellow;
this.word.Location = new System.Drawing.Point(624, 8);
this.word.Name = "word";
this.word.Size = new System.Drawing.Size(168, 16);
this.word.TabIndex = 0;
this.word.Visible = false;
//
// 設置計時器控件(timerSaver)屬性
this.timerSaver.Enabled = true;
this.timerSaver.Interval = 5;
this.timerSaver.Tick += new System.EventHandler(this.timerSaver_Tick);
//
// 設置圖片控件(picture1)屬性
this.picture1.Image = ((System.Drawing.Bitmap)(resources.GetObject("picture1.Image")));
this.picture1.Location = new System.Drawing.Point(800, 600);
this.picture1.Name = "picture1";
this.picture1.Size = new System.Drawing.Size(304, 224);
this.picture1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.picture1.TabIndex = 1;
this.picture1.TabStop = false;
//
// 設置窗體(screen)屬性
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(800, 600);
this.ControlBox = false;
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.picture1,this.word});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.KeyPreview = true;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "screen";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
file://鍵盤按下響應事件
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.screen_KeyDown);
file://鼠標按下響應事件
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.screen_MouseDown);
file://窗體啟動調(diào)用事件
this.Load += new System.EventHandler(this.Form1_Load);
file://鼠標移動響應事件
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.screen_MouseMove);
this.ResumeLayout(false);
}
#endregion
///
/// 應用程序的主入口點。
///
[STAThread]
static void Main(string[] args)
{
if(args.Length==1)
if(args[0].Substring(0,2).Equals("/c"))
{
MessageBox.Show("沒有設置項功能","C# Screen Saver");
Application.Exit();
}
else if(args[0]=="/s")
Application.Run(new screen());
else if(args[0]=="/a")
{
MessageBox.Show("沒有口令功能","C# Screen saver");
Application.Exit();
}
else
Application.Run(new screen());
}
private void Form1_Load(object sender, System.EventArgs e)
{
speed=0;
System.Drawing.Rectangle ssWorkArea=System.Windows.Forms.Screen.GetWorkingArea(this);
file://屏幕顯示區(qū)域
width1=ssWorkArea.Width; file://屏幕寬度
height1=ssWorkArea.Height; file://屏幕高度
}
private void timerSaver_Tick(object sender, System.EventArgs e) file://計時器響應事件
{
word.Visible=true;
word.Text=str;
word.Height=word.Font.Height; file://設置文本的高度
word.Width=word.Text.Length*(int)word.Font.Size*2; file://設置文本的寬度
PlayScreenSaver();
}
private void PlayScreenSaver() file://自定義函數(shù)
{
file://下面設置文本顯示框的位置坐標
word.Location =new System.Drawing.Point(width1-iDistance,word.Location.Y);
word.Visible=true; file://設置為可見
iDistance+=iSpeed;
if(word.Location.X=-(word.Width))
{
iDistance=0;
if(word.Location.Y==0)
word.Location=new System.Drawing.Point(word.Location.X,height1/2);
else if(word.Location.Y==height1/2)
word.Location=new System.Drawing.Point(word.Location.X,height1-word.Height);
else
word.Location=new System.Drawing.Point(word.Location.X,0);
}
file://下面是計算圖片框移動坐標
speed++;
if(speed=2*height1)
{
x1=System.Math.Abs(width1-speed);
y1=System.Math.Abs(height1-speed);
}
else if(speed2*height1 speed=2*width1)
{
x1=System.Math.Abs(width1-speed);
y1=System.Math.Abs(height1-(speed-speed/height1*height1));
}
else if(speed2*width1 speed=3*height1)
{
x1=System.Math.Abs(width1-(speed-speed/width1*width1));
y1=System.Math.Abs(height1-(speed-speed/height1*height1));
}
else if(speed3*height1 speed4*height1)
{
x1=System.Math.Abs(width1-(speed-speed/width1*width1));
y1=System.Math.Abs(speed-speed/height1*height1);
}
else if(speed=4*height1 speed5*height1)
{
x1=System.Math.Abs(speed-speed/width1*width1);
y1=System.Math.Abs(height1-(speed-speed/height1*height1));
}
else if(speed=5*height1 speed4*width1)
{
x1=System.Math.Abs(speed-speed/width1*width1);
y1=System.Math.Abs(speed-speed/height1*height1);
}
else if(speed=4*width1 speed6*height1)
{
x1=System.Math.Abs(width1-(speed-speed/width1*width1));
y1=System.Math.Abs(speed-speed/height1*height1);
}
else if(speed=6*height1 speed5*width1)
{
x1=System.Math.Abs(width1-(speed-speed/width1*width1));
y1=System.Math.Abs(height1-(speed-speed/height1*height1));
}
else if(speed=5*width1 speed7*height1)
{
x1=System.Math.Abs(speed-speed/width1*width1);
y1=System.Math.Abs(height1-(speed-speed/height1*height1));
}
else if(speed=7*height1 speed6*width1)
{
x1=System.Math.Abs(speed-speed/width1*width1);
y1=System.Math.Abs(speed-speed/height1*height1);
}
if(speed==6*width1)
speed=0;
picture1.Location=new System.Drawing.Point(x1,y1);
}
private void StopScreenSaver() file://停止屏幕保護程序運行
{
System.Windows.Forms.Cursor.Show();
timerSaver.Enabled=false;
Application.Exit();
}
private void screen_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
file://鼠標移動事件
{
if(ixStart==0 iyStart==0)
{
ixStart=e.X;
iyStart=e.Y;
return;
}
else if(e.X!=ixStart||e.Y!=iyStart)
StopScreenSaver();
}
private void screen_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
file://鼠標按下事件
{
StopScreenSaver(); file://停止運行屏幕保護程序
}
private void screen_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
file://鍵盤按下事件
{
StopScreenSaver(); file://停止運行屏幕保護程序
}
}
}
最后運行該程序,把screen_saver.exe改為screen_saver.scr,拷入Windows系統(tǒng)目錄中,這樣就可以運行該屏幕
在窗體上建立2個文本框text1和text2,一個按鈕command1,text1里面輸入你要轉換的字符串,text2里面顯示結果,代碼如下:
Dim MyString As String
Dim EveryStr(50) As String
Dim TargetStr As String
Private Sub Command1_Click()
MyString = Text1
For i = 1 To Len(MyString)
EveryStr(i) = Right(Left(MyString, i), 1)
If Asc(EveryStr(i)) 123 And Asc(EveryStr(i)) 96 Then EveryStr(i) = \"_\"
If Asc(EveryStr(i)) 91 And Asc(EveryStr(i)) 64 Then EveryStr(i) = \"_\"
TargetStr = TargetStr EveryStr(i)
Next i
Text2 = TargetStr
TargetStr = \"\"
End Sub
引號前面怎么自動給加了個“\”?用的時候請手動把那幾個“\”去掉
網(wǎng)頁名稱:vb.net屏幕保護 vbs鎖定窗口
本文鏈接:http://muchs.cn/article0/doesiio.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供、微信小程序、移動網(wǎng)站建設、自適應網(wǎng)站、網(wǎng)站設計公司、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)