بسم الله الر حمن الر حيم

          Pernahkah terpikirkan ketika kita mematikan PC kita, kode macam apa yang dituliskan sehingga PC kita bisa mati dengan sendirinya tanpa menekan tombol on/off (atau apalah namanya)? Seharusnya hal itu terpikirkan dalam benak kita.

          Kali ini, saya mau nulis tentang cara mematikan PC dengan bahasa C#. Semoga postingan saya kali ini bermanfaat, amin. Yuk Mari…

          Saya tuliskan kode totalnya aja yah, biar lebih mudah dipahami <Males mode on (^_^)>.

using System;

using System.Collections.Generic;

using System.Drawing;

using System.Windows.Forms;

using System.Runtime.InteropServices;

 

namespace LockComp

{

    /// <summary>

    /// Description of MainForm.

    /// </summary>

    public partial class MainForm

    {

        // Importing Windows API library

        [DllImport("user32.dll")]

        public static extern void LockWorkStation();        

        [DllImport("user32.dll")]

        public static extern int ExitWindowsEx(int uFlags, int dwReason);        

        

        [STAThread]

        public static void Main(string[] args)

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new MainForm());

        }

        

        public MainForm()

        {

            //

            // The InitializeComponent() call is required for Windows Forms designer support.

            //

            InitializeComponent();

            

            //

            // TODO: Add constructor code after the InitializeComponent() call.

            //

        }

        

        // Lock

        void BtnLockCompClick(object sender, System.EventArgs e)

        {

            LockWorkStation();

        }

        

        // Log Off

        void BtnLogOffClick(object sender, System.EventArgs e)

        {

            if(DialogResult.Yes==MessageBox.Show("Yakin nih mau Log Off?","Log Off?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))

            ExitWindowsEx(0, 0);

        }

        

        // Reboot

        void BtnRebootClick(object sender, System.EventArgs e)

        {

            if(DialogResult.Yes==MessageBox.Show("Yakin nih mau Reboot?","Reboot?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))

            ExitWindowsEx(2, 0);

        }

        

        // Shutdown

        void BtnShutdownClick(object sender, System.EventArgs e)

        {

            if(DialogResult.Yes==MessageBox.Show("Yakin nih mau Shutdown?","Shutdown?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))

            ExitWindowsEx(1, 0);

        }

        

        // Force LogOff

        void BtnForceLogOffClick(object sender, System.EventArgs e)

        {

            if(DialogResult.Yes==MessageBox.Show("Yakin nih mau force Log Off?","Force LogOff?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2))

            ExitWindowsEx(4, 0);

        }

        

        // Hibernate

        void BtnHibernateClick(object sender, System.EventArgs e)

        {

            Application.SetSuspendState(PowerState.Hibernate, true, true);

        }

        

        // Stand By

        void BtnStandbyClick(object sender, System.EventArgs e)

        {

            Application.SetSuspendState(PowerState.Suspend, true, true);

        }

    }

}

          Jika ingan download codenya, silakan download di sini.

          Alhamdulillah, semoga bermanfaat,,,

About ifqo

Kepingin melawan arus, agar tidak tenggelam,,,

2 responses »

  1. Abot Erdi says:

    pa maksd a tu taq ngerti la saia~

Tinggalkan Komentar/Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s