新工具完成了!!!上班時間只能用指定的部分程式了。唉,有時太過火了~~

自由程式設計師發表於2006-11-26

我的工具終於完成了!!!以下是全部程式碼,在vs2005+xp環境下執行通過。以前我發貼問的問題,也解決了,感謝網友們的幫助,特別是raozhiven(朗屹) 的提示,感謝!以前我總是認為程式碼不對,其實是正確的,主要是居然不知道改了登錄檔,並不是立即生效的。後來,在網上找資料,發現了,不用重啟立即生效的方法。以下是改良過的程式程式碼。歡迎大家來研討。更感謝能提出更好方法的朋友。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace restrict
{
    public partial class Form1 : Form
    {
        int delflag = 1;
        IntPtr result1;

        // SendMessageTimeout tools
        [Flags]
        public enum SendMessageTimeoutFlags : uint
        {
            SMTO_NORMAL = 0x0000,
            SMTO_BLOCK = 0x0001,
            SMTO_ABORTIFHUNG = 0x0002,
            SMTO_NOTIMEOUTIFNOTHUNG = 0x0008
        }
        const int WM_SETTINGCHANGE = 0x001A;
        const int HWND_BROADCAST = 0xffff;

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessageTimeout(
           IntPtr windowHandle,
           uint Msg,
           IntPtr wParam,
           IntPtr lParam,
           SendMessageTimeoutFlags flags,
           uint timeout,
           out IntPtr result
           );
        //*******以下為版權資訊*******
        //作者:花純春
        //時間:2006年11月26日
        //請聯絡我的部落格:http://ike.126.com
        //描述:此程式通過修改登錄檔,實現在工作時間只允許指定程式的執行。在中午休息時間,自動取消限制,可以自由使用。
        //歡迎轉載和使用,但請保持版權資訊的完整!
        //*******以上為版權資訊*******

        public Form1()
        {
            InitializeComponent();
           
           
            RegistryKey hkcu = Registry.CurrentUser;

            RegistryKey cu = hkcu.CreateSubKey(@"Software/Microsoft/Windows/CurrentVersion/Policies/Explorer/RestrictRun");
            RegistryKey cu1 = hkcu.CreateSubKey(@"SOFTWARE/MICROSOFT/WINDOWS/CURRENTVERSION/Policies/Explorer");
            RegistryKey cu2 = hkcu.OpenSubKey(@"SOFTWARE/MICROSOFT/WINDOWS/CURRENTVERSION/RUN", true);
            string aa = Application.ExecutablePath;
            cu2.SetValue("RestrictFun", aa);
           
            cu1.SetValue("RestrictRun", 1);
           

            cu.SetValue("1", "notepad.exe");
            cu.SetValue("2", "gpedit.msc");
            cu.SetValue("3", "regedit.exe");
            cu.SetValue("4", "tmshell.exe");
            cu.SetValue("5", "kav.exe");
            cu.SetValue("6", "TTraveler.exe");
            cu.SetValue("7", "iexplore.exe");
            cu.SetValue("8", "mmc.exe");
            cu.SetValue("9", "restrict.exe");

            hkcu.Close();
            // Tell all open programs that this change occurred.
                  SendMessageTimeout(
                     new IntPtr(HWND_BROADCAST),
                     WM_SETTINGCHANGE,
                     IntPtr.Zero,
                     IntPtr.Zero,
                     SendMessageTimeoutFlags.SMTO_NORMAL,
                     1000,
                     out result1);
                   


        }

       
       
       public void delres()//刪除相關注冊表項,清除限制
        {
            RegistryKey hkcu = Registry.CurrentUser;

 

 

            RegistryKey cu = hkcu.OpenSubKey(@"SOFTWARE/MICROSOFT/WINDOWS/CURRENTVERSION/Policies/Explorer", true);
            cu.DeleteValue("RestrictRun");
            cu.DeleteSubKey("RestrictRun");
            hkcu.Close();
           
           // Tell all open programs that this change occurred.
            SendMessageTimeout(
                     new IntPtr(HWND_BROADCAST),
                     WM_SETTINGCHANGE,
                     IntPtr.Zero,
                     IntPtr.Zero,
                     SendMessageTimeoutFlags.SMTO_NORMAL,
                     1000,
                     out result1);
                   

            delflag = 0;

        }
       
        private void timer1_Tick(object sender, EventArgs e)
        {
           
         if ((DateTime.Now.Hour >= 12)&&(DateTime.Now.Hour <= 14)&&(delflag==1)) //中午12點到下午2點之間,取消限制
            {
               
              delres();
 
            }

        }
    }
}


 學習在於愛好!

相關文章