Setting background image of the ListView Control

  • Thread starter Thread starter Tryst
  • Start date Start date
T

Tryst

Hi all,

is it at all possible for me to add a background image to the ListView
Control?

Thanks

Tryst
 
Hi,

This feature is not supported by the Compact Framework.
You can create your own class inherit from ListView, add new
BackGroundImage property and overload the OnPaint method.

BR


Fabien Decret
Windows Embedded Consultant
C# MCP

ADENEO (ADESET)
http://www.adeneo.adetelgroup.com/




Tryst a écrit :
 
I tried to do what you asked, but when you extend on the ListView
Control the OnPaint() method cannot be overridden because the CF
doesn't not call OnPaint() on the ListView. It is called lower down
in the OS.

Anyway, I have managed to use a class that catches Windows Messages
(WM_Paint) on the ListView Control and then calls a method in my
ListViewExtender class (this is through a delegate). Now I can get the
ListView to display the background image perfectly. But the
ListViewItems that I add to the ListViewExtender instance do not get
displayed. Is this because I have used the DrawImage() method which
draws the images on top of the ListView items? Or is it a case that I
must also re-draw the ListViewItems?

The following code is what I have in my ListViewExtender Class...

// Constructor that registers this Control to be hooked.
public BGListView()
{
// Messages required to override
// in this control's window procedure.
WndProcHooker.HookWndProc(this,
new WndProcHooker.WndProcCallback(this.WM_Paint_Handler),
Win32.WM_PAINT);
}

// The callback called when the window receives a WM_PAINT message.
// We draw the button in the appropriate state.
// hwnd - The handle to the window that received the message
// wParam - Indicates whether various virtual keys are down.
// lParam - The coordinates of the cursor
// handled - Set to true if we don't want to pass this message on to
the original window procedure
// Returns zero if we process this message.
int WM_Paint_Handler(
IntPtr hwnd, uint msg, uint wParam, int lParam, ref bool handled)
{
Win32.PAINTSTRUCT ps = new Win32.PAINTSTRUCT();

Graphics gr = Graphics.FromHdc(Win32.BeginPaint(hwnd, ref
ps));
//DrawButton(gr, this.Capture &&
//
(this.ClientRectangle.Contains(lastCursorCoordinates)));
DrawListView(gr);
gr.Dispose();
Win32.EndPaint(hwnd, ref ps);
handled = true;
return 0;
}

// Gets a Graphics object for the provided window handle and then calls
DrawButton(Graphics, bool).
// hwnd - The handle to the window to draw as a button
// pressed - If true, the button is draw in the depressed state
void DrawListView(Graphics gr)
{
//DrawButton(gr, pressed);
gr.DrawImage(m_BackgroundImage,
this.ClientRectangle,
new Rectangle(0, 0, m_BackgroundImage.Width,
m_BackgroundImage.Height),
GraphicsUnit.Pixel);
}

I have used the following classes (supplied by MS) to catch and handle
the Windows Messages (WM_Paint) for the ListView Control...

http://msdn2.microsoft.com/en-US/library/ms229658.aspx
http://msdn2.microsoft.com/en-US/library/ms229683.aspx

Thanks in advance if your able to help me out with this.
 
What you are trying should work (although I have never tried it) true setting
handled = false and the return value to hwnd. The CF probably still needs
to process the message to draw the list items. Again...never tried it so
don't know if it will work.

To make life easier you can try the OpenNETCF.Windows.Forms.ListBox2 control
in the Smart Device Framework which does support background images.

See here for sample usage http://blog.opennetcf.org/ayakhnin/PermaLink,guid,40f8d4c0-26ae-4d02-af0b-0c80de605288.aspx

You can download the Smart Device Framework @ www.smartdeviceframework.com

HTH
 
Hi Mark, and thanks for the reply.

I have tried setting handled to false, but that doesn't seem to do
anything. If I return the hwnd then my solution throughs all kinds of
errors as the return type for the WM_PAINT_HANDLER method is type 'int'
not type 'IntPtr'. I am starting to guess that my problem is here with
this call...

Win32.EndPaint(hwnd, ref ps);

....as I assume it is telling the OS to stop painting for this ListView
Control (it has only done the background, and the ListView Items
haven't even been drawn).

So do I need to call something inbetween the suggest code below?

int WM_Paint_Handler(IntPtr hwnd, uint msg, uint wParam, int lParam,
ref bool handled)
{
Win32.PAINTSTRUCT ps = new Win32.PAINTSTRUCT();
Graphics gr = Graphics.FromHdc(Win32.BeginPaint(hwnd, ref ps));

DrawListView(gr);
// DO I NEED TO ADD SOMETHING HERE?!?
gr.Dispose();
Win32.EndPaint(hwnd, ref ps);
handled = false;
return 0;
}

Hope you can help me on this as we cant afford to purchase the
OpenNETCF lisence of yet (hopefully in the future, though).

Thanks
 
Back
Top