Windows Develop Bookmark and Share   
 index > Windows Forms General > ListView RTL Problem
 

ListView RTL Problem

Hi Everyone.

please check code before

Code Block

public class MirroredListView : System.Windows.Forms.ListView

{

const int WS_EX_LAYOUTRTL = 0x400000;

const int WS_EX_NOINHERITLAYOUT = 0x100000;

private bool _mirrored= false;

[Description("Change to the right-to-left layout."),DefaultValue(false),

Localizable(true),Category("Appearance"),Browsable(true)]

public bool Mirrored

{

get

{

return _mirrored;

}

set

{

if (_mirrored != value)

{

_mirrored = value;

base.OnRightToLeftChanged(EventArgs.Empty);

}

}

}

protected override CreateParams CreateParams

{

get

{

CreateParams CP;

CP = base.CreateParams;

if(_mirrored)

CP.ExStyle = CP.ExStyle | WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT;

return CP;

}

}

}

After this i can mirrored my listview and all the items within a listview be Right To Left but the problems still at the columns header how i can mirrored it.

Omar Fawzi  Saturday, October 27, 2007 8:39 AM

Hi Omar

Because the column header is a Win32 control (Header control), so we can modify its extendedstyle too. The following example shows how to do this. I hope this helps.

Code Block

public class MirroredListView : System.Windows.Forms.ListView

{

const int WS_EX_LAYOUTRTL = 0x400000;

const int WS_EX_NOINHERITLAYOUT = 0x100000;

const int LVM_GETHEADER = 0x1000 + 31;

const int GWL_EXSTYLE = -20;

private bool _mirrored = false;

[Description("Change to the right-to-left layout."), DefaultValue(false),

Localizable(true), Category("Appearance"), Browsable(true)]

public bool Mirrored

{

get { return this._mirrored; }

set

{

if (this._mirrored != value)

{

this._mirrored = value;

base.OnRightToLeftChanged(EventArgs.Empty);

}

}

}

protected override CreateParams CreateParams

{

get

{

CreateParams cp;

cp = base.CreateParams;

if (_mirrored)

cp.ExStyle = cp.ExStyle | WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT;

return cp;

}

}

protected override void OnHandleCreated(EventArgs e)

{

base.OnHandleCreated(e);

if (this._mirrored)

{

IntPtr headerPtr = SendMessage(new HandleRef(this, this.Handle), (IntPtr)LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);

int headerExStyle = (int)GetWindowLong(new HandleRef(this, headerPtr), GWL_EXSTYLE) | WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT;

SetWindowLong(new HandleRef(this, headerPtr), GWL_EXSTYLE, new HandleRef(this, (IntPtr)headerExStyle));

}

}

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(HandleRef hWnd, IntPtr msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]

public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, HandleRef dwNewLong);

[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]

public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, HandleRef dwNewLong);

[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]

public static extern IntPtr GetWindowLong32(HandleRef hWnd, int nIndex);

[DllImport("user32.dll", EntryPoint = "GetWindowLongPtr", CharSet = CharSet.Auto)]

public static extern IntPtr GetWindowLongPtr64(HandleRef hWnd, int nIndex);

public static IntPtr GetWindowLong(HandleRef hWnd, int nIndex)

{

if (IntPtr.Size == 4)

{

return GetWindowLong32(hWnd, nIndex);

}

return GetWindowLongPtr64(hWnd, nIndex);

}

public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, HandleRef dwNewLong)

{

if (IntPtr.Size == 4)

{

return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);

}

return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);

}

}

Best Regards,

Wei Zhou

Wei Zhou - MSFT  Wednesday, October 31, 2007 9:30 AM

Hi Omar

Because the column header is a Win32 control (Header control), so we can modify its extendedstyle too. The following example shows how to do this. I hope this helps.

Code Block

public class MirroredListView : System.Windows.Forms.ListView

{

const int WS_EX_LAYOUTRTL = 0x400000;

const int WS_EX_NOINHERITLAYOUT = 0x100000;

const int LVM_GETHEADER = 0x1000 + 31;

const int GWL_EXSTYLE = -20;

private bool _mirrored = false;

[Description("Change to the right-to-left layout."), DefaultValue(false),

Localizable(true), Category("Appearance"), Browsable(true)]

public bool Mirrored

{

get { return this._mirrored; }

set

{

if (this._mirrored != value)

{

this._mirrored = value;

base.OnRightToLeftChanged(EventArgs.Empty);

}

}

}

protected override CreateParams CreateParams

{

get

{

CreateParams cp;

cp = base.CreateParams;

if (_mirrored)

cp.ExStyle = cp.ExStyle | WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT;

return cp;

}

}

protected override void OnHandleCreated(EventArgs e)

{

base.OnHandleCreated(e);

if (this._mirrored)

{

IntPtr headerPtr = SendMessage(new HandleRef(this, this.Handle), (IntPtr)LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);

int headerExStyle = (int)GetWindowLong(new HandleRef(this, headerPtr), GWL_EXSTYLE) | WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT;

SetWindowLong(new HandleRef(this, headerPtr), GWL_EXSTYLE, new HandleRef(this, (IntPtr)headerExStyle));

}

}

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern IntPtr SendMessage(HandleRef hWnd, IntPtr msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]

public static extern IntPtr SetWindowLongPtr32(HandleRef hWnd, int nIndex, HandleRef dwNewLong);

[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr", CharSet = CharSet.Auto)]

public static extern IntPtr SetWindowLongPtr64(HandleRef hWnd, int nIndex, HandleRef dwNewLong);

[DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]

public static extern IntPtr GetWindowLong32(HandleRef hWnd, int nIndex);

[DllImport("user32.dll", EntryPoint = "GetWindowLongPtr", CharSet = CharSet.Auto)]

public static extern IntPtr GetWindowLongPtr64(HandleRef hWnd, int nIndex);

public static IntPtr GetWindowLong(HandleRef hWnd, int nIndex)

{

if (IntPtr.Size == 4)

{

return GetWindowLong32(hWnd, nIndex);

}

return GetWindowLongPtr64(hWnd, nIndex);

}

public static IntPtr SetWindowLong(HandleRef hWnd, int nIndex, HandleRef dwNewLong)

{

if (IntPtr.Size == 4)

{

return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);

}

return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);

}

}

Best Regards,

Wei Zhou

Wei Zhou - MSFT  Wednesday, October 31, 2007 9:30 AM

thanks alot so much.
Omar Fawzi  Saturday, November 03, 2007 10:09 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGrid, Like PropertyGrid?
• RichTextBox wrapping
• problem to show hirarical data in TreeView.
• Leave Event
• ToolTip buffer limit?
• menu of mdi child appears in the parent form
• BUILD WEB BASED APPLICATION SERVER
• foreach(Form f in AllformsOfApplication)
• Property Grid with file path
• Which of the following things is considered a best practice with win forms?