+ Reply to Thread
Results 1 to 2 of 2

Thread: Make your Own power plan Utility

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    41

    Make your Own power plan Utility

    Hello , currently when you are using your laptop and want to switch between power plans , you kind of go to power manager and switch it from there , is there a library inside .net that switch power plans ?
    Last edited by hassan; 11-12-2012 at 03:00 AM.

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    36
    i created a power plan switcher , which is accessed from tray icon , okay lets check the code , but firstly we need to define what we need ,
    1- Power plans win32 values :
    Code:
    Power Saver : a1841308-3541-4fab-bc81-f71556f20b4a
    Balanced : 381b4222-f694-41f0-9685-ff5bb260df2e
    High-Performance : 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
    2- three different images for the power plans

    Here is the overall code :
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Reflection;
    using Microsoft.Win32;
    using System.IO;
    using System.Resources;
    using System.Runtime.InteropServices; // for dll import
    
    namespace Power_Plan_change
    {
        public partial class Form1 : Form
        {
            //Using the system pre-defined MSDN constants that can be used by the SendMessage() function .
            public int WM_SYSCOMMAND = 0x0112;
            public int SC_MONITORPOWER = 0xF170;
    
            //To call a DLL function from C#, you must provide this declaration .
            [DllImport("user32.dll")]
            private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);
    
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void highPerformanceToolStripMenuItem_Click(object sender, EventArgs e)
            {
                try
                {
                    string green = Application.StartupPath + "/green.ico";
                    ProcessStartInfo psi = new ProcessStartInfo("cmd", "/c " + "powercfg -setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c");
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    Process.Start(psi);
    
                    balancedToolStripMenuItem.Checked = false;
                    powerSaverToolStripMenuItem.Checked = false;
    
                    notifyIcon1.Text = "Currunt : High Performance";
    
                    notifyIcon1.Icon = new System.Drawing.Icon(green);
                }
                catch(Exception) 
                {
                }
            }
    
            private void balancedToolStripMenuItem_Click(object sender, EventArgs e)
            {
                try
                {
                    string orange = Application.StartupPath + "/orange.ico";
                    ProcessStartInfo psi = new ProcessStartInfo("cmd", "/c " + "powercfg -setactive 381b4222-f694-41f0-9685-ff5bb260df2e");
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    Process.Start(psi);
    
                    highPerformanceToolStripMenuItem.Checked = false;
                    powerSaverToolStripMenuItem.Checked = false;
    
                    notifyIcon1.Text = "Currunt : Balanced";
    
                    notifyIcon1.Icon = new System.Drawing.Icon(orange);
                }
                catch (Exception)
                {
                }
            }
    
            private void powerSaverToolStripMenuItem_Click(object sender, EventArgs e)
            {
                try
                {
                    string red = Application.StartupPath + "/red.ico";
                    ProcessStartInfo psi = new ProcessStartInfo("cmd", "/c " + "powercfg -setactive a1841308-3541-4fab-bc81-f71556f20b4a");
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    Process.Start(psi);
    
                    highPerformanceToolStripMenuItem.Checked = false;
                    balancedToolStripMenuItem.Checked = false;
    
                    notifyIcon1.Text = "Currunt : Power Saver";
    
                    notifyIcon1.Icon = new System.Drawing.Icon(red);
                }
                catch (Exception)
                {
                }
            }
    
            private void Form1_Shown(object sender, EventArgs e)
            {
                this.Hide();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                try
                {
                    // 1 - create the reg key
                    RegistryKey read = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    string valu = Convert.ToString(read.GetValue("PowerManager"));
                    read.Close();
    
                    if (valu == "")
                    {
                        RegistryKey doit = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                        doit.SetValue("PowerManager", Application.ExecutablePath.ToString());
                        doit.Close();
                    }
                }
                catch (Exception)
                {
                    RegistryKey doit = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
                    doit.SetValue("PowerManager", Application.ExecutablePath.ToString());
                    doit.Close();
                }
    
                try
                {
                    string green = Application.StartupPath + "/green.ico";
                    ProcessStartInfo psi = new ProcessStartInfo("cmd", "/c " + "powercfg -setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c");
                    psi.WindowStyle = ProcessWindowStyle.Hidden;
                    Process.Start(psi);
    
                    highPerformanceToolStripMenuItem.Checked = true;
    
                    notifyIcon1.Text = "Currunt : High Performance";
    
                    notifyIcon1.Icon = new System.Drawing.Icon(green);
                }
                catch (Exception)
                {
                }
            }
    
            private void exitToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void notifyIcon1_DoubleClick(object sender, EventArgs e)
            {
                // dll function to turn off display
                SendMessage(this.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, 2);
            }
        }
    }

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts