首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

用 C# 設計多功能關機程式(一):介面設計與 Win32 API 宣告

     編輯:關於C語言

功能概覽

開發一套具備 定時關機、倒數關機、關機提醒、系統資訊查詢 四大功能的工具,關機時間精確到秒,同時作為 C# 呼叫 Win32 API (P/Invoke) 的入門範例。

1. WinForms 介面佈局

新增標準 Windows Forms 專案,表單名稱 frmmain,標題「關閉 Windows」。拖曳下列元件並設定屬性:

元件名稱Text / 關鍵屬性
CheckBoxcheckBox1自動關機
CheckBoxcheckBox2倒計時執行操作
CheckBoxcheckBox3定時報警
GroupBoxgroupBox1當前系統時間
GroupBoxgroupBox2設定時間
TextBoxtxtTime顯示倒數秒數
ButtonbutCancle取消
ButtonbutReOpen重新啟動
ButtonbutClose關機
ButtonbutReLogin注銷
ButtonbutSysInto系統資訊
DateTimePickersetupTimeFormat = Time
DateTimePickersetupDateFormat = Short
Timertimer1Interval = 100 (0.1 秒)

關機程式介面設計

2. 引入 P/Invoke 所需命名空間


using System.Runtime.InteropServices;
using System.Text; // StringBuilder

3. 宣告 Win32 API 函式


// 取得 Windows 目錄
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern uint GetWindowsDirectory(StringBuilder lpBuffer, uint uSize);

// 取得系統目錄
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern uint GetSystemDirectory(StringBuilder lpBuffer, uint uSize);

// 關機/重開機/登出
[DllImport("user32.dll", ExactSpelling = true)]
public static extern int ExitWindowsEx(uint uFlags, uint dwReserved);

// 關於視窗 (範例)
[DllImport("shell32.dll")]
public static extern int ShellAbout(int hWnd, string szApp, string szOtherStuff, int hIcon);

參數常數定義


const int SHUTDOWN = 1;   // 關機
const int REBOOT = 2;     // 重開機
const int LOGOFF = 0;     // 登出

4. 表單層級變數


uint dwReserved = 0;
uint sh = 0;
int counter = 0, n = 0;

5. 核心事件處理程式

Form_Load:初始化目前時間


private void frmmain_Load(object sender, EventArgs e)
{
    Time.Text = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString();
}

Timer_Tick:即時更新時間與檢查定時關機條件


private void timer1_Tick(object sender, EventArgs e)
{
    string currDate = DateTime.Now.ToShortDateString();
    string currTime = DateTime.Now.ToShortTimeString();

    // 即時顯示系統時間 (假設有一個 Label 名為 lblSysTime)
    lblSysTime.Text = currDate + " " + currTime;

    // 定時關機檢查
    if (checkBox1.Checked) // 自動關機
    {
        if (currDate == setupDate.Value.ToShortDateString() &&
            currTime == setupTime.Value.ToShortTimeString())
        {
            CloseComputer();
        }
    }

    // 倒數關機邏輯 (略)
}

關機/重開機/登出共用方法


private void CloseComputer() { sh = ExitWindowsEx(SHUTDOWN, dwReserved); }
private void RebootComputer() { sh = ExitWindowsEx(REBOOT, dwReserved); }
private void LogoffUser() { sh = ExitWindowsEx(LOGOFF, dwReserved); }

按鈕事件


private void butReOpen_Click(object s, EventArgs e) { RebootComputer(); }
private void butReLogin_Click(object s, EventArgs e) { LogoffUser(); }
private void butClose_Click(object s, EventArgs e) { CloseComputer(); }
private void butCancle_Click(object s, EventArgs e) { this.Close(); }
private void butSysInto_Click(object s, EventArgs e) { new Form2().Show(); } // 開啟系統資訊視窗

下一篇將詳細說明 Form2 系統資訊視窗的 API 呼叫與結構體定義。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved