Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody.

I made a list box with the owner draw variable property enabled and handled both draw and measure item events to customize it.

the problem is:

I used a gradient brush for the selection but the color changes from the top of the list box to its bottom.:confused: I wish to make one gradient color for all the items like this.

http://img198.imageshack.us/img198/1360/illustrationn.png

I also wish to implement hot tracking with the same color.

The code I used:
private: System::Void listBox1_DrawItem(System::Object^ sender, System::Windows::Forms::DrawItemEventArgs^  e) 
{
  if ((e->State & DrawItemState::Selected) == DrawItemState::Selected)
  {
    Graphics^ G = listBox1->CreateGraphics();
    System::Drawing::Drawing2D::LinearGradientBrush^ myBrush =  gcnew System::Drawing::Drawing2D::LinearGradientBrush(ClientRectangle,Color::Azure, Color::Violet, System::Drawing::Drawing2D::LinearGradientMode::Vertical);
    System::Drawing::Drawing2D::LinearGradientBrush^ myBrush2 =  gcnew System::Drawing::Drawing2D::LinearGradientBrush(ClientRectangle,Color::LightBlue, Color::White, System::Drawing::Drawing2D::LinearGradientMode::Horizontal);
    System::Drawing::Pen^ P = gcnew System::Drawing::Pen(myBrush2);
    e->Graphics->FillRectangle(myBrush, e->Bounds);
    e->Graphics->DrawRectangle(P,e->Bounds);
  System::Drawing::Font^ F = gcnew System::Drawing::Font( "Microsoft Sans Serif",8 );
    e->Graphics->DrawString("Size:  600.4 MB	Last Modified::12/12/2012",F, Brushes::Black, e->Bounds.X+1, e->Bounds.Y+20);
    e->Graphics->DrawString("Type: Folder	Extension:ELF",F, Brushes::Black, e->Bounds.X+1, e->Bounds.Y+40);
    e->Graphics->DrawString("C:\\Files\\Folders\\Some Folder",F, Brushes::Green, e->Bounds.X+1, e->Bounds.Y+60);
  }
  else
  {
    e->Graphics->FillRectangle(Brushes::White, e->Bounds);
    System::Drawing::Pen^ Pe = gcnew System::Drawing::Pen(Color::White);					    e->Graphics->DrawRectangle(Pe,e->Bounds);
  }
  try
  {
    System::Drawing::Font^ F = gcnew System::Drawing::Font( "Microsoft Sans Serif",12 );
    e->Graphics->DrawString(listBox1->Items[e->Index]->ToString(),F, Brushes::Blue, e->Bounds.X, e->Bounds.Y);
// Draw the focus rectangle around the selected item.
				 e->DrawFocusRectangle();}
				 catch(Exception^ ex){}

			 }
private: System::Void listBox1_MeasureItem(System::Object^  sender, System::Windows::Forms::MeasureItemEventArgs^  e) {
				 e->ItemHeight += 65;
				 e->Index::get();
			 }


I appreciate any help.
Thanks alot.
Posted
Updated 22-Nov-10 21:18pm
v4
Comments
Alain Rist 23-Nov-10 3:19am    
Changed C++ tag to C++/CLI

1 solution

I really should have concentrated.

I changed [ClientRectangle] to [e->Bounds] and it went alright.

but still don't know about Hot-tracking :((

could anybody give me logic or code. I found it's related to Mouse Enter, Move, and Leave events but couldn't understand the code. It wasn't in C++.

and yes, how can I make the selection rectangle rounded from the edges?:confused:

Thanks again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900