日期:2014-05-18  浏览次数:20919 次

用C#写屏幕锁定功能
第一个form 两个输密码的textbox 一个button 点butto就能屏幕锁定
第二个form只有一个text和一个button
但是我按以下的代码只能实现到 待机状态 不是锁屏啊
form1代码:
[color=#FF0000][/color][size=18px][/size]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace locked
{
  public partial class Form1 : Form
  {
  public Form1()
  {
  InitializeComponent();
  }
  public string pwd = "";
  private void button1_Click(object sender, EventArgs e)
  {
  if ((txtpwd1.Text == txtpwd2.Text) && txtpwd1.Text != "")
  {
  Form2 f2 = new Form2();
  pwd = txtpwd1.Text;
  this.Hide();
  f2.Show();  
  }
  if (txtpwd1.Text == "" || txtpwd2.Text == "")
  {
  MessageBox.Show("密码不能为空");
  txtpwd1.Text = "";
  txtpwd2.Text = "";
  }  
  if(txtpwd1.Text!=txtpwd2.Text)
  {
  MessageBox.Show("两次密码不一致,请重新输入");
  txtpwd1.Text = "";
  txtpwd2.Text = "";
  }
  }
  }
}

form2代码:[color=#FF0000][/color]using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace locked
{
  public partial class Form2 : Form

  {  
  public Form2()
  {
  InitializeComponent();
  }
  private void button1_Click(object sender, EventArgs e)
  {
  Form1 f1 = new Form1();
  if (f1.pwd==txtpwd.Text)
  {
  LockWorkStation();
  this.Dispose();
  }
  else
  {
  txtpwd.Text = "密码错误,请重新输入";  
  }
  }

  private void Form2_Load(object sender, EventArgs e)
  {
  Location = new Point(0, 0);
  Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
  this.TopMost = true;
  }

  private void Form2_FormClosing(object sender, FormClosingEventArgs e)
  {
  e.Cancel = true;
  }
  [DllImport("User32.dll ")]

  public static extern void LockWorkStation(); 

  }
}
求高手解决啊 form1的密码传不到form2里面啊

------解决方案--------------------
Form2 f2 = new Form2();
的时候 建议
 Form2 f2 = new Form2(pwd);
在form2的构造函数里面重载 
C# code
public Form2(string psd)
{
this.pwd = psd
}