By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

User Control is consuming my Paint messages. (CF)

P: n/a
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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
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.ComponentModel.Container components = null;

private Form clientForm;

private static WriteDelay writeDelay = new WriteDelay();
private static TachoState currentState =
TachoMenu.TachoState.None;

public enum TachoState
{
None,
Break,
Drive,
OnDuty,
Rest
}

public TachoMenu()
{
InitializeComponent();
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

public void AddMenuToForm(Form clientForm)
{
this.clientForm = clientForm;
this.Visible = true;
clientForm.ControlBox = false;
clientForm.Width = MyApp.SCREEN_WIDTH;
clientForm.Height = MyApp.SCREEN_HEIGHT;
clientForm.FormBorderStyle = FormBorderStyle.None;
clientForm.Controls.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 InitializeComponent()
{
this.pnlTitleBar = new System.Windows.Forms.Panel();
this.btnDropDown = new System.Windows.Forms.Button();
this.lblTitle = new System.Windows.Forms.Label();
this.pnlDropDown = 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.pnlTitleBar.BackColor = System.Drawing.Color.Black;
this.pnlTitleBar.Controls.Add(this.btnDropDown);
this.pnlTitleBar.Controls.Add(this.lblTitle);
this.pnlTitleBar.Size = new System.Drawing.Size(320, 48);
//
// btnDropDown
//
this.btnDropDown.Font = new System.Drawing.Font("Microsoft
Sans Serif", 16F, System.Drawing.FontStyle.Regular);
this.btnDropDown.Size = new System.Drawing.Size(112, 48);
this.btnDropDown.Text = "Off Duty";
this.btnDropDown.Click += new
System.EventHandler(this.btnDropDown_Click);
this.btnDropDown.LostFocus += new
System.EventHandler(this.btnDropDown_LostFocus);
//
// lblTitle
//
this.lblTitle.Font = new System.Drawing.Font("Microsoft Sans
Serif", 18F, System.Drawing.FontStyle.Bold);
this.lblTitle.ForeColor = System.Drawing.Color.White;
this.lblTitle.Location = new System.Drawing.Point(112, 8);
this.lblTitle.Size = new System.Drawing.Size(208, 32);
this.lblTitle.Text = "Morrisons";
this.lblTitle.TextAlign =
System.Drawing.ContentAlignment.TopCenter;
//
// pnlDropDown
//
this.pnlDropDown.Controls.Add(this.btnOnDuty);
this.pnlDropDown.Controls.Add(this.btnBreak);
this.pnlDropDown.Controls.Add(this.btnRest);
this.pnlDropDown.Controls.Add(this.btnDrive);
this.pnlDropDown.Location = new System.Drawing.Point(0, 48);
this.pnlDropDown.Size = new System.Drawing.Size(112, 256);
this.pnlDropDown.Visible = false;
this.pnlDropDown.LostFocus += new
System.EventHandler(this.pnlDropDown_LostFocus);
//
// btnOnDuty
//
this.btnOnDuty.Font = new System.Drawing.Font("Microsoft Sans
Serif", 16F, System.Drawing.FontStyle.Regular);
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.EventHandler(this.btnOnDuty_Click);
//
// btnBreak
//
this.btnBreak.Font = new System.Drawing.Font("Microsoft Sans
Serif", 16F, System.Drawing.FontStyle.Regular);
this.btnBreak.Location = new System.Drawing.Point(0, 128);
this.btnBreak.Size = new System.Drawing.Size(112, 64);
this.btnBreak.Text = "Break";
this.btnBreak.Click += new
System.EventHandler(this.btnBreak_Click);
//
// btnRest
//
this.btnRest.Font = new System.Drawing.Font("Microsoft Sans
Serif", 16F, System.Drawing.FontStyle.Regular);
this.btnRest.Location = new System.Drawing.Point(0, 64);
this.btnRest.Size = new System.Drawing.Size(112, 64);
this.btnRest.Text = "Rest";
this.btnRest.Click += new
System.EventHandler(this.btnRest_Click);
//
// btnDrive
//
this.btnDrive.Font = new System.Drawing.Font("Microsoft Sans
Serif", 16F, System.Drawing.FontStyle.Regular);
this.btnDrive.Size = new System.Drawing.Size(112, 64);
this.btnDrive.Text = "Drive";
this.btnDrive.Click += new
System.EventHandler(this.btnDrive_Click);
//
// TachoMenu
//
this.BackColor = System.Drawing.Color.Transparent;
this.ClientSize = new System.Drawing.Size(320, 480);
this.Controls.Add(this.pnlDropDown);
this.Controls.Add(this.pnlTitleBar);

}
#endregion

#region InitTachoStateChange
private void InitTachoStateChange(TachoMenu.TachoState newState)
{
this.pnlDropDown.Visible = false;
clientForm.Controls.SetChildIndex(this, 100);

switch (currentState)
{
case TachoMenu.TachoState.Break:
this.btnBreak.Enabled = true;
break;

case TachoMenu.TachoState.Drive:
this.btnDrive.Enabled = true;
break;

case TachoMenu.TachoState.OnDuty:
this.btnOnDuty.Enabled = true;
break;

case TachoMenu.TachoState.Rest:
this.btnRest.Enabled = true;
break;
}

switch (newState)
{
case TachoMenu.TachoState.Break:
currentState = newState;
this.btnBreak.Enabled = false;
this.btnDropDown.Text = "Break";
break;

case TachoMenu.TachoState.Drive:
currentState = newState;
this.btnDrive.Enabled = false;
this.btnDropDown.Text = "Drive";
break;

case TachoMenu.TachoState.OnDuty:
currentState = newState;
this.btnOnDuty.Enabled = false;
this.btnDropDown.Text = "On Duty";
break;

case TachoMenu.TachoState.Rest:
currentState = newState;
this.btnRest.Enabled = false;
this.btnDropDown.Text = "Rest";
break;
}
writeDelay.StartCountDownToWrite = true;
}
#endregion

#region Event Handlers
private void btnDropDown_Click(object sender, System.EventArgs e)
{
this.pnlDropDown.Visible = !this.pnlDropDown.Visible;

if(this.pnlDropDown.Visible)
clientForm.Controls.SetChildIndex(this, 0);
else
clientForm.Controls.SetChildIndex(this, 100);
}

private void btnDropDown_LostFocus(object sender, System.EventArgs
e)
{
this.pnlDropDown.Visible = false;
clientForm.Controls.SetChildIndex(this, 100);
}

private void pnlDropDown_LostFocus(object sender, System.EventArgs
e)
{
this.pnlDropDown.Visible = false;
clientForm.Controls.SetChildIndex(this, 100);
}

private void btnDrive_Click(object sender, System.EventArgs e)
{
if(currentState == TachoMenu.TachoState.Drive)
return;
InitTachoStateChange(TachoMenu.TachoState.Drive);
}

private void btnRest_Click(object sender, System.EventArgs e)
{
if(currentState == TachoMenu.TachoState.Rest)
return;
InitTachoStateChange(TachoMenu.TachoState.Rest);
}

private void btnBreak_Click(object sender, System.EventArgs e)
{
if(currentState == TachoMenu.TachoState.Break)
return;
InitTachoStateChange(TachoMenu.TachoState.Break);
}

private void btnOnDuty_Click(object sender, System.EventArgs e)
{
if(currentState == TachoMenu.TachoState.OnDuty)
return;
InitTachoStateChange(TachoMenu.TachoState.OnDuty);
}
#endregion

#region Properties
public string Title
{
get { return this.lblTitle.Text; }
set { this.lblTitle.Text = 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(
WaitForWriteRequest ));
writeThread.Priority = ThreadPriority.Normal;
writeThread.Start();
}

public bool StartCountDownToWrite
{
set {StartWriteCount = value;}
}

public static bool KeepAlive
{
set {keepAlive = value;}
}

private void WaitForWriteRequest()
{
int iCountdownSecs = 10;
bool waitingToWrite = false;

while(keepAlive)
{
if(StartWriteCount == true)
{
waitingToWrite = true;
StartWriteCount = false;
iCountdownSecs = 10;
}

Thread.Sleep(1000);

if(waitingToWrite)
{
--iCountdownSecs;

if(iCountdownSecs == 0)
{
waitingToWrite = false;
if(!RequestWriter.WriteTachoRequest(TachoMenu.curr entState))
{
//handle error
}
}
}
}
}
}
#endregion
}
}

Client Form code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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()
{
InitializeComponent();
tachoMenu = new TachoMenu();
tachoMenu.Title = "Morrisons";
tachoMenu.AddMenuToForm(this);

this.Paint += new PaintEventHandler(TestForm_Paint);

}

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_PADDING_PIX))) / iNumTanks;

for(int i = 0 ; i < iNumTanks ; i++)
{
graphics.DrawRectangle( pen,
MyApp.GRAPHIC_PADDING_PIX
+ i * (iTankWidth + MyApp.GRAPHIC_PADDING_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 InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
//
// button1
//
this.button1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 20.25F, System.Drawing.FontStyle.Regular);
this.button1.Location = new System.Drawing.Point(88, 408);
this.button1.Size = new System.Drawing.Size(128, 48);
this.button1.Text = "Exit";
this.button1.Click += new
System.EventHandler(this.button1_Click);
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.None;
//
// TestForm
//
this.ClientSize = new System.Drawing.Size(320, 480);
this.ControlBox = false;
this.Controls.Add(this.button1);

this.Text = "TestForm";

}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{
MyApp.CleanUp();
Application.Exit();
}
}
}
Thanks for help/suggestions
Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.