Parameter not valid exception. Extending menuItem.

P

Prats

Tried to extend the MenuItem class to have a 3Xn grid of images, following an example in C#. Below is the
code in C++

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

namespace chatTest {

public ref class EmoticonMenuItem : System::Windows::Forms::MenuItem{
public:
EmoticonMenuItem(void) {
this->OwnerDraw = true;
backgroundColor = SystemColors::ControlLightLight;
selectionColor = Color::FromArgb(50, 0, 0, 150);
selectionBorderColor = SystemColors::Highlight;
ICON_WIDTH = 19;
ICON_HEIGHT = 19;
ICON_MARGIN = 4;
}

private: int ICON_WIDTH;
private: int ICON_HEIGHT;
private: int ICON_MARGIN;
private: Color backgroundColor, selectionColor, selectionBorderColor;
public: property Image^ img;
public:
EmoticonMenuItem:):Image^ _image) {
this->OwnerDraw = true;
img = _image;
backgroundColor = SystemColors::ControlLightLight;
selectionColor = Color::FromArgb(50, 0, 0, 150);
selectionBorderColor = SystemColors::Highlight;
ICON_WIDTH = 19;
ICON_HEIGHT = 19;
ICON_MARGIN = 4;
}

protected: virtual void OnMeasureItem(MeasureItemEventArgs^ e)override {
e->ItemWidth = ICON_WIDTH + ICON_MARGIN;
e->ItemHeight = ICON_HEIGHT + 2 * ICON_MARGIN;
}

protected: virtual void OnDrawItem(DrawItemEventArgs^ e) override {
Graphics^ grp = e->Graphics;
Rectangle bounds = e->Bounds;
DrawEmoticonBackground(grp, bounds, ((e->State & DrawItemState::Selected) ==DrawItemState::Selected));
grp->DrawImage(img, bounds.X + ((bounds.Width - ICON_WIDTH) / 2), bounds.Y + ((bounds.Height - ICON_HEIGHT) / 2));
}

public : void DrawEmoticonBackground(Graphics^ grp, Rectangle bounds, bool selected) {
System::Drawing::pen^ myPen;
myPen = gcnew System::Drawing::pen(selectionBorderColor);
System::Drawing::SolidBrush^ myBrush =gcnew System::Drawing::SolidBrush(selectionBorderColor);

try{
if (selected) {
line 55: grp->DrawRectangle(myPen, bounds.X, bounds.Y,bounds.Width - 1, bounds.Height - 1);
grp->FillRectangle(myBrush, Rectangle(0,0,20,20));
//grp->DrawRectangle(gcnew Pen(System::Drawing::Color::DarkGray), 0, 0,20, 20);
}
else {
myBrush =gcnew System::Drawing::SolidBrush(backgroundColor);
grp->FillRectangle(myBrush, Rectangle(0,0,20,20));
}
delete myBrush;
delete myPen;
delete grp;
}
catch(Exception^ ex){
MessageBox::Show(ex->StackTrace);
}
}
};
}


The mainForm is like this

private: array<Image ^> ^emoticons;


private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^
e) {
emoticons = gcnew array<Image^>(6);
emoticons[0] = Image::FromFile( "Emoticons\\AngelSmile.png" );
emoticons[1] = Image::FromFile( "Emoticons\\AngrySmile.png" );
emoticons[2] = Image::FromFile( "Emoticons\\Beer.png" );
emoticons[3] = Image::FromFile( "Emoticons\\BrokenHeart.png" );
emoticons[4] = Image::FromFile(
"Emoticons\\ConfusedSmile.png" );
emoticons[5] = Image::FromFile( "Emoticons\\CrySmile.png" );
ImageMenuItem^ mnuItem;
int count = 0;
for each(Image^ emoticon in emoticons) {
mnuItem = gcnew ImageMenuItem(emoticon);
mnuItem->Click += gcnew System::EventHandler(this,
&mainform::cmenu_Emoticons_Click);
if (count % 3 == 0)
mnuItem->BarBreak= true;
contextMenuStrip1->Items->Add(reinterpret_cast<ToolStripItem
^>( mnuItem));
++count;
}

}

private: void cmenu_Emoticons_Click(System::Object^ sender,
System::EventArgs^ e){
EmoticonMenuItem^ mnuItem = (EmoticonMenuItem^) sednder;
some more code to display mnuItem->Image
}


The program compiles witout any errors. But does not run
The exception is "Parameter is not valid"
the stack Trace at time of exception is:

at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
at System.Drawing.Graphics.DrawRectangle(Pen pen, Int32 x, Int32 y, Int32 width, Int32 height)
at chatTest.EmoticonMenuItem.DrawEmoticonBackground(Graphics grp, Rectangle bounds, Boolean selected) in c:\users\prats\documents\visual studio 2008\projects\chattest\chattest\emoticonmenu.h:line 55

The parameter grp has the following values at the time of exception
System::MarshalByRefObject^ 0x01715da8 { __identity=<undefined value> } System::MarshalByRefObject^
For the majority of the attributes the value is: error: an exception of type: System::ArgumentException^ occurred

Please help me debug the code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top