473,763 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

User Control is consuming my Paint messages. (CF)

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
Nov 16 '05 #1
0 1906

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
6196
by: Sagaert Johan | last post by:
I have made a custom control that draws a rectangle when the mouse is down, and does nothing when the mouse is up. I set/reset a flag in MouseDown/Mouse up and use this to do the drawing in the OnPaint . The recangle draws correct when i press the mouse, but when i release the mouse the background is not restored What should i do in the Onpaint to make sure the background (the form) is restored correctly ? This problem already keeps...
3
12890
by: Chris | last post by:
Hi, I need to get an image of a control (ListView) so i can save it to a file. It's pretty easy to get the Graphics with the CreateGraphics() function on every control, but I don't know how I might save the Graphics object as an image. Has anyone done this? Chris
2
7940
by: conset23 | last post by:
Hello. I'm working with WinForm. It has 30 same user control, each with 4 buttons, 3 labels, 3 textboxes, 2 picture boxes and uses override onPaint to draw gradient rectangle. Main form also has 2 panels with same gradient. I'm trying to reduce drawing speed on form activation. But at the moment it is too noticeable for each user control to be drawn. Tried to use double buffering on main form and on each user control, looks almost the...
3
1408
by: James Ramaley | last post by:
I have created a VB.NET User Control which inherits from TextBox. I have a general question regarding how functionality should be overwritten: I can either write a method which handles an event: function MyControl_Enter(...) handles MyControl.Enter or I can overwrite the relevant virtual method:
4
2534
by: Michael | last post by:
Dear all .. If I want to use develop a user control and declare a public property which the type is System.Windows.Forms.GridTableStylesCollection For example : Public Class LookAndView Inherits System.Windows.Forms.UserControl Private _Collection As GridTableStylesCollection
2
4528
by: osmarjunior | last post by:
Hello there... I have a custom user control, in which i put a panel on the top. In the Paint event of the panel, i have the following code: VisualStyleRenderer x = new VisualStyleRenderer(VisualStyleElement.ExplorerBar.NormalGroupHead.Normal); x.DrawBackground(e.Graphics, e.ClipRectangle); VisualStyleRenderer y = new
2
1847
by: Sara | last post by:
Hello there, Iam creating a user control with 2 buttons, And i have a public static variable i'll be changing the value of the variable in the button click events. When i consume the user control in the winforms application how to automatically get the value of the public variable on click of the user control.
15
1850
by: Hamed | last post by:
Have I posted the message to wrong newsgroup? Or Does the question is so much strage? Would someone please kindly direct me to a true newsgroup or resource? Best Regards Hamed
2
2700
by: =?Utf-8?B?RXJpYw==?= | last post by:
Hello All, I am currently working on an issue that has a custom control that mimics a combobox that when it has focus it continues to generate endless WM_PAINT Messages. The control in itself is primarily a windows panel with three controls added ( textbox, combo box, and picturebox). In looking at the code for the custom control I see no issue with it. Any pointers on how to determine which likely control in this custom control...
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.