Hi,
I have created a control which draws a title bar and provides a drop down
menu for a Smart Device Application. It seemed to work fine until I came to
add an event handler to act on Paint messages in the form which has drawn
the control. Evidently, the control is consuming all of these messages. How
can I pass them back/on? I have a reference to the owner form but calling
Refresh() via this reference isn't helping. Help! Do I need to do something
with Invoke? Its late and my brain hurts!
Code posted below:
Control code:
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Threadin g;
using System.IO;
namespace Testing
{
/// <summary>
/// Summary description for TachoMenu.
/// </summary>
public class TachoMenu : System.Windows. Forms.Control
{
private System.Windows. Forms.Panel pnlTitleBar;
private System.Windows. Forms.Label lblTitle;
private System.Windows. Forms.Button btnDropDown;
private System.Windows. Forms.Panel pnlDropDown;
private System.Windows. Forms.Button btnDrive;
private System.Windows. Forms.Button btnRest;
private System.Windows. Forms.Button btnBreak;
private System.Windows. Forms.Button btnOnDuty;
private System.Componen tModel.Containe r components = null;
private Form clientForm;
private static WriteDelay writeDelay = new WriteDelay();
private static TachoState currentState =
TachoMenu.Tacho State.None;
public enum TachoState
{
None,
Break,
Drive,
OnDuty,
Rest
}
public TachoMenu()
{
InitializeCompo nent();
}
protected override void OnPaint(PaintEv entArgs e)
{
base.OnPaint (e);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
public void AddMenuToForm(F orm clientForm)
{
this.clientForm = clientForm;
this.Visible = true;
clientForm.Cont rolBox = false;
clientForm.Widt h = MyApp.SCREEN_WI DTH;
clientForm.Heig ht = MyApp.SCREEN_HE IGHT;
clientForm.Form BorderStyle = FormBorderStyle .None;
clientForm.Cont rols.Add(this);
return;
}
#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 InitializeCompo nent()
{
this.pnlTitleBa r = new System.Windows. Forms.Panel();
this.btnDropDow n = new System.Windows. Forms.Button();
this.lblTitle = new System.Windows. Forms.Label();
this.pnlDropDow n = new System.Windows. Forms.Panel();
this.btnOnDuty = new System.Windows. Forms.Button();
this.btnBreak = new System.Windows. Forms.Button();
this.btnRest = new System.Windows. Forms.Button();
this.btnDrive = new System.Windows. Forms.Button();
//
// pnlTitleBar
//
this.pnlTitleBa r.BackColor = System.Drawing. Color.Black;
this.pnlTitleBa r.Controls.Add( this.btnDropDow n);
this.pnlTitleBa r.Controls.Add( this.lblTitle);
this.pnlTitleBa r.Size = new System.Drawing. Size(320, 48);
//
// btnDropDown
//
this.btnDropDow n.Font = new System.Drawing. Font("Microsoft
Sans Serif", 16F, System.Drawing. FontStyle.Regul ar);
this.btnDropDow n.Size = new System.Drawing. Size(112, 48);
this.btnDropDow n.Text = "Off Duty";
this.btnDropDow n.Click += new
System.EventHan dler(this.btnDr opDown_Click);
this.btnDropDow n.LostFocus += new
System.EventHan dler(this.btnDr opDown_LostFocu s);
//
// lblTitle
//
this.lblTitle.F ont = new System.Drawing. Font("Microsoft Sans
Serif", 18F, System.Drawing. FontStyle.Bold) ;
this.lblTitle.F oreColor = System.Drawing. Color.White;
this.lblTitle.L ocation = new System.Drawing. Point(112, 8);
this.lblTitle.S ize = new System.Drawing. Size(208, 32);
this.lblTitle.T ext = "Morrisons" ;
this.lblTitle.T extAlign =
System.Drawing. ContentAlignmen t.TopCenter;
//
// pnlDropDown
//
this.pnlDropDow n.Controls.Add( this.btnOnDuty) ;
this.pnlDropDow n.Controls.Add( this.btnBreak);
this.pnlDropDow n.Controls.Add( this.btnRest);
this.pnlDropDow n.Controls.Add( this.btnDrive);
this.pnlDropDow n.Location = new System.Drawing. Point(0, 48);
this.pnlDropDow n.Size = new System.Drawing. Size(112, 256);
this.pnlDropDow n.Visible = false;
this.pnlDropDow n.LostFocus += new
System.EventHan dler(this.pnlDr opDown_LostFocu s);
//
// btnOnDuty
//
this.btnOnDuty. Font = new System.Drawing. Font("Microsoft Sans
Serif", 16F, System.Drawing. FontStyle.Regul ar);
this.btnOnDuty. Location = new System.Drawing. Point(0, 192);
this.btnOnDuty. Size = new System.Drawing. Size(112, 64);
this.btnOnDuty. Text = "On Duty";
this.btnOnDuty. Click += new
System.EventHan dler(this.btnOn Duty_Click);
//
// btnBreak
//
this.btnBreak.F ont = new System.Drawing. Font("Microsoft Sans
Serif", 16F, System.Drawing. FontStyle.Regul ar);
this.btnBreak.L ocation = new System.Drawing. Point(0, 128);
this.btnBreak.S ize = new System.Drawing. Size(112, 64);
this.btnBreak.T ext = "Break";
this.btnBreak.C lick += new
System.EventHan dler(this.btnBr eak_Click);
//
// btnRest
//
this.btnRest.Fo nt = new System.Drawing. Font("Microsoft Sans
Serif", 16F, System.Drawing. FontStyle.Regul ar);
this.btnRest.Lo cation = new System.Drawing. Point(0, 64);
this.btnRest.Si ze = new System.Drawing. Size(112, 64);
this.btnRest.Te xt = "Rest";
this.btnRest.Cl ick += new
System.EventHan dler(this.btnRe st_Click);
//
// btnDrive
//
this.btnDrive.F ont = new System.Drawing. Font("Microsoft Sans
Serif", 16F, System.Drawing. FontStyle.Regul ar);
this.btnDrive.S ize = new System.Drawing. Size(112, 64);
this.btnDrive.T ext = "Drive";
this.btnDrive.C lick += new
System.EventHan dler(this.btnDr ive_Click);
//
// TachoMenu
//
this.BackColor = System.Drawing. Color.Transpare nt;
this.ClientSize = new System.Drawing. Size(320, 480);
this.Controls.A dd(this.pnlDrop Down);
this.Controls.A dd(this.pnlTitl eBar);
}
#endregion
#region InitTachoStateC hange
private void InitTachoStateC hange(TachoMenu .TachoState newState)
{
this.pnlDropDow n.Visible = false;
clientForm.Cont rols.SetChildIn dex(this, 100);
switch (currentState)
{
case TachoMenu.Tacho State.Break:
this.btnBreak.E nabled = true;
break;
case TachoMenu.Tacho State.Drive:
this.btnDrive.E nabled = true;
break;
case TachoMenu.Tacho State.OnDuty:
this.btnOnDuty. Enabled = true;
break;
case TachoMenu.Tacho State.Rest:
this.btnRest.En abled = true;
break;
}
switch (newState)
{
case TachoMenu.Tacho State.Break:
currentState = newState;
this.btnBreak.E nabled = false;
this.btnDropDow n.Text = "Break";
break;
case TachoMenu.Tacho State.Drive:
currentState = newState;
this.btnDrive.E nabled = false;
this.btnDropDow n.Text = "Drive";
break;
case TachoMenu.Tacho State.OnDuty:
currentState = newState;
this.btnOnDuty. Enabled = false;
this.btnDropDow n.Text = "On Duty";
break;
case TachoMenu.Tacho State.Rest:
currentState = newState;
this.btnRest.En abled = false;
this.btnDropDow n.Text = "Rest";
break;
}
writeDelay.Star tCountDownToWri te = true;
}
#endregion
#region Event Handlers
private void btnDropDown_Cli ck(object sender, System.EventArg s e)
{
this.pnlDropDow n.Visible = !this.pnlDropDo wn.Visible;
if(this.pnlDrop Down.Visible)
clientForm.Cont rols.SetChildIn dex(this, 0);
else
clientForm.Cont rols.SetChildIn dex(this, 100);
}
private void btnDropDown_Los tFocus(object sender, System.EventArg s
e)
{
this.pnlDropDow n.Visible = false;
clientForm.Cont rols.SetChildIn dex(this, 100);
}
private void pnlDropDown_Los tFocus(object sender, System.EventArg s
e)
{
this.pnlDropDow n.Visible = false;
clientForm.Cont rols.SetChildIn dex(this, 100);
}
private void btnDrive_Click( object sender, System.EventArg s e)
{
if(currentState == TachoMenu.Tacho State.Drive)
return;
InitTachoStateC hange(TachoMenu .TachoState.Dri ve);
}
private void btnRest_Click(o bject sender, System.EventArg s e)
{
if(currentState == TachoMenu.Tacho State.Rest)
return;
InitTachoStateC hange(TachoMenu .TachoState.Res t);
}
private void btnBreak_Click( object sender, System.EventArg s e)
{
if(currentState == TachoMenu.Tacho State.Break)
return;
InitTachoStateC hange(TachoMenu .TachoState.Bre ak);
}
private void btnOnDuty_Click (object sender, System.EventArg s e)
{
if(currentState == TachoMenu.Tacho State.OnDuty)
return;
InitTachoStateC hange(TachoMenu .TachoState.OnD uty);
}
#endregion
#region Properties
public string Title
{
get { return this.lblTitle.T ext; }
set { this.lblTitle.T ext = value; }
}
#endregion
#region Internal class - WriteDelay
public class WriteDelay
{
private Thread writeThread;
private bool StartWriteCount ;
private static bool keepAlive;
public WriteDelay()
{
keepAlive = true;
writeThread = new Thread( new ThreadStart(
WaitForWriteReq uest ));
writeThread.Pri ority = ThreadPriority. Normal;
writeThread.Sta rt();
}
public bool StartCountDownT oWrite
{
set {StartWriteCoun t = value;}
}
public static bool KeepAlive
{
set {keepAlive = value;}
}
private void WaitForWriteReq uest()
{
int iCountdownSecs = 10;
bool waitingToWrite = false;
while(keepAlive )
{
if(StartWriteCo unt == true)
{
waitingToWrite = true;
StartWriteCount = false;
iCountdownSecs = 10;
}
Thread.Sleep(10 00);
if(waitingToWri te)
{
--iCountdownSecs;
if(iCountdownSe cs == 0)
{
waitingToWrite = false;
if(!RequestWrit er.WriteTachoRe quest(TachoMenu .currentState))
{
//handle error
}
}
}
}
}
}
#endregion
}
}
Client Form code:
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Drawing. Drawing2D;
namespace Testing
{
/// <summary>
/// Summary description for TestForm.
/// </summary>
public class TestForm : System.Windows. Forms.Form
{
private System.Windows. Forms.Button button1;
private TachoMenu tachoMenu;
public TestForm()
{
InitializeCompo nent();
tachoMenu = new TachoMenu();
tachoMenu.Title = "Morrisons" ;
tachoMenu.AddMe nuToForm(this);
this.Paint += new PaintEventHandl er(TestForm_Pai nt);
}
public void TestForm_Paint( object sender, PaintEventArgs e)
{
int iNumTanks = 7;
int iYOffSet = 60;
Graphics graphics = null;
Pen pen = null;
graphics = e.Graphics;
pen = new Pen(Color.Black );
int iTankWidth = ((MyApp.SCREEN_ WIDTH - (iNumTanks *
MyApp.GRAPHIC_P ADDING_PIX))) / iNumTanks;
for(int i = 0 ; i < iNumTanks ; i++)
{
graphics.DrawRe ctangle( pen,
MyApp.GRAPHIC_P ADDING_PIX
+ i * (iTankWidth + MyApp.GRAPHIC_P ADDING_PIX),
iYOffSet,
iTankWidth,
iTankWidth);
}
pen.Dispose();
}
/// <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 InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
//
// button1
//
this.button1.Fo nt = new System.Drawing. Font("Microsoft Sans
Serif", 20.25F, System.Drawing. FontStyle.Regul ar);
this.button1.Lo cation = new System.Drawing. Point(88, 408);
this.button1.Si ze = new System.Drawing. Size(128, 48);
this.button1.Te xt = "Exit";
this.button1.Cl ick += new
System.EventHan dler(this.butto n1_Click);
this.FormBorder Style =
System.Windows. Forms.FormBorde rStyle.None;
//
// TestForm
//
this.ClientSize = new System.Drawing. Size(320, 480);
this.ControlBox = false;
this.Controls.A dd(this.button1 );
this.Text = "TestForm";
}
#endregion
private void button1_Click(o bject sender, System.EventArg s e)
{
MyApp.CleanUp() ;
Application.Exi t();
}
}
}
Thanks for help/suggestions