public
Last active

  • Download Gist
Forum1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
/*************************************************************************************
* Sample application demonstrating how to register in order to receive power state
* notifications. This particular application wants to display login box upon resume
*
* Author: Alex Feinman (C) 2003
* Contact at: sample_code@alexfeinman.com
*
*************************************************************************************/
 
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
 
namespace PowerAwareApp
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private IntPtr m_hEvtQuit;
private Thread m_th;
private IntPtr m_hQueue;
 
#region Housekeeping
 
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
 
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
//
// Form1
//
this.Menu = this.mainMenu1;
this.MinimizeBox = false;
this.Text = "Power-aware App";
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);
 
}
#endregion
 
/// <summary>
/// The main entry point for the application.
/// </summary>
 
static void Main()
{
Application.Run(new Form1());
}
 
#endregion
 
private void Form1_Load(object sender, System.EventArgs e)
{
// Create event to stop the thread
m_hEvtQuit = CreateEvent(0, true, false, null);
// Create listener thread
m_th = new Thread(WaitThreadProc);
 
try
{
// Create message queue for power broadcasts
m_hQueue = CreateMsgQueue( "PowerEventWaitQueue", new MSGQUEUEOPTIONS() );
}
catch (MissingMethodException)
{
// If we got here, it means the target device does not have CE.NET. This API is not available in Windows CE 3.0
MessageBox.Show("This application uses API not available on current platform. Supported platforms are Windows CE.NET 4.1 devices, such as Windows Mobile 2003", "Error");
Close();
return;
}
// Show login form - simulate login
Login(null, null);
// Register for power notifications.
IntPtr hNotif = RequestPowerNotifications(m_hQueue, PowerEventType.PBT_TRANSITION);
 
// Start listening
m_th.Start();
}
 
 
private void Form1_Closed(object sender, System.EventArgs e)
{
try
{
// Tell listener thread to stop
EventModify(m_hEvtQuit, EVENT_SET);
 
// Clean up
CloseHandle(m_hQueue);
}
catch
{
}
}
 
// This function will be called from a different thread via Control.Invoke -
// thus it has EventHandler signature
private void Login(object sender, EventArgs e)
{
// Show login form
Login login = new Login();
login.ShowDialog();
}
 
// Listener thread
void WaitThreadProc()
{
// Open our message queue for reading
MSGQUEUEOPTIONS opt = new MSGQUEUEOPTIONS();
opt.bReadAccess = 1;
IntPtr hQueue = CreateMsgQueue( "PowerEventWaitQueue", opt);
 
// Start waiting for Message Queue to fill up, while checking for a termination request
while ( WaitForSingleObject( m_hEvtQuit, 0 ) != WAIT_OBJECT_0 )
{
// Did queue get some data?
if ( WaitForSingleObject(hQueue, 100) == WAIT_OBJECT_0 )
{
// Yes, let's read it
// 1024 byte buffer should be well enough.
POWER_BROADCAST pb = new POWER_BROADCAST(1024);
int nRead, Flags;
ReadMsgQueue(hQueue, pb.Data, pb.Data.Length, out nRead, 0, out Flags);
// Is this an event we wanted?
if ( (pb.Message & PowerEventType.PBT_TRANSITION) == PowerEventType.PBT_TRANSITION &&
( pb.Flags & PowerState.POWER_STATE_ON ) == PowerState.POWER_STATE_ON )
{
//Notify parent - launch login dialog
this.Invoke(new EventHandler(Login));
}
 
}
}
CloseHandle(hQueue);
CloseHandle(m_hEvtQuit);
}
 
#region P/Invoke Stuff
 
[DllImport("coredll")]
static extern IntPtr CreateMsgQueue(string Name, MSGQUEUEOPTIONS Options);
 
[DllImport("coredll")]
static extern bool ReadMsgQueue(
IntPtr hMsgQ,
byte[] lpBuffer,
int cbBufferSize,
out int lpNumberOfBytesRead,
int dwTimeout,
out int pdwFlags
);
 
 
[DllImport("coredll")]
static extern IntPtr CreateEvent( int dwReserved1, bool bManualReset, bool bInitialState, string Name);
[DllImport("coredll")]
static extern bool EventModify(IntPtr hEvent, int func);
 
[DllImport("coredll")]
static extern bool CloseHandle(IntPtr h);
 
[DllImport("coredll")]
static extern IntPtr RequestPowerNotifications( IntPtr hMsgQ, PowerEventType Flags );
 
[DllImport("coredll")]
static extern int WaitForSingleObject( IntPtr hHandle, int dwMilliseconds );
 
const int WAIT_OBJECT_0 = 0;
const int EVENT_SET = 3;
#endregion
 
}
 
 
[Flags]
enum PowerEventType
{
PBT_TRANSITION = 0x00000001,
PBT_RESUME = 0x00000002,
PBT_POWERSTATUSCHANGE = 0x00000004,
PBT_POWERINFOCHANGE = 0x00000008,
}
 
[Flags]
enum PowerState
{
POWER_STATE_ON =(0x00010000),
POWER_STATE_OFF =(0x00020000),
 
POWER_STATE_CRITICAL =(0x00040000),
POWER_STATE_BOOT =(0x00080000),
POWER_STATE_IDLE =(0x00100000),
POWER_STATE_SUSPEND =(0x00200000),
POWER_STATE_RESET =(0x00800000),
}
 
class MSGQUEUEOPTIONS
{
public MSGQUEUEOPTIONS()
{
dwSize = Marshal.SizeOf(typeof(MSGQUEUEOPTIONS));
dwFlags = 0;
dwMaxMessages = 20;
cbMaxMessage = 100;
bReadAccess = 0;
}
public int dwSize;
public int dwFlags;
public int dwMaxMessages;
public int cbMaxMessage;
public int bReadAccess;
}
 
class POWER_BROADCAST
{
public POWER_BROADCAST(int size)
{
m_data = new byte[size];
}
byte[] m_data;
public byte[] Data { get { return m_data; } }
public PowerEventType Message { get { return (PowerEventType)BitConverter.ToInt32(m_data, 0); } }
public PowerState Flags { get { return (PowerState)BitConverter.ToInt32(m_data, 4); } }
public int Length { get { return BitConverter.ToInt32(m_data, 8); } }
public byte[] SystemPowerState { get { byte[] data = new byte[Length]; Buffer.BlockCopy(m_data, 12, data, 0, Length); return data; } }
}
}
Login.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
 
namespace PowerAwareApp
{
/// <summary>
/// Summary description for Login.
/// </summary>
public class Login : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
public Login()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
 
//
// TODO: Add any constructor code after InitializeComponent call
//
}
 
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
 
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 40);
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 40);
this.label1.Size = new System.Drawing.Size(64, 20);
this.label1.Text = "User ID:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 80);
this.label2.Size = new System.Drawing.Size(64, 20);
this.label2.Text = "Password:";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(88, 80);
this.textBox2.PasswordChar = '*';
this.textBox2.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(72, 136);
this.button1.Text = "Login";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Login
//
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox2);
this.Text = "Login";
 
}
#endregion
 
private void button1_Click(object sender, System.EventArgs e)
{
DialogResult = DialogResult.OK;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.