Last active
January 21, 2018 10:19
-
-
Save smourier/12630d3ba111c2686ee54ed740042d8a to your computer and use it in GitHub Desktop.
Testing with IListView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.IO; | |
using System.Runtime.InteropServices; | |
using System.Windows.Forms; | |
namespace WindowsFormsApp1 | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
string curDir = Directory.GetCurrentDirectory(); | |
webBrowser1.Navigate(curDir); | |
} | |
// add a button somewhere, the IListView cannot be retrieved too early (it will crash when calling any of its method) | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
var view = GetIListView(FindWindowEx(webBrowser1.Handle, IntPtr.Zero, null, null)); | |
if (view != null) | |
{ | |
view.SetBackgroundColor(Color.Black); | |
//view.SetTextColor(Color.White); | |
//view.SetTextBackgroundColor(Color.Black); | |
for (int i = 0; i < view.GetItemCount(); i++) | |
{ | |
view.UpdateItem(i); | |
} | |
} | |
} | |
private static IListView GetIListView(IntPtr parent) | |
{ | |
IListView view = null; | |
EnumChildWindows(parent, (h, p) => | |
{ | |
SendMessage(h, LVM_QUERYINTERFACE, typeof(IListView).GUID, out IListView lv); | |
if (lv == null) | |
return true; | |
view = lv; | |
return false; | |
}, IntPtr.Zero); | |
return view; | |
} | |
[DllImport("user32.dll")] | |
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); | |
[DllImport("user32.dll")] | |
private static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam); | |
[DllImport("user32.dll")] | |
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, [MarshalAs(UnmanagedType.LPStruct)] Guid wParam, out IListView lParam); | |
private delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam); | |
private const int LVM_QUERYINTERFACE = 0x10BD; | |
[Guid("e5b16af2-3990-4681-a609-1f060cd14269"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] | |
private interface IListView | |
{ | |
// IOleWindow | |
void GetWindow(); | |
void ContextSensitiveHelp(); | |
// IListView | |
void GetImageList(); | |
void SetImageList(); | |
Color GetBackgroundColor(); | |
void SetBackgroundColor(Color color); | |
Color GetTextColor(); | |
void SetTextColor(Color color); | |
Color GetTextBackgroundColor(); | |
void SetTextBackgroundColor(Color color); | |
Color GetHotLightColor(); | |
void SetHotLightColor(Color color); | |
int GetItemCount(); | |
// unused | |
void SetItemCount(); | |
void GetItem(); | |
void SetItem(); | |
void GetItemState(); | |
void SetItemState(); | |
void GetItemText(); | |
void SetItemText(); | |
void GetBackgroundImage(); | |
void SetBackgroundImage(); | |
void GetFocusedColumn(); | |
void SetSelectionFlags(); | |
void GetSelectedColumn(); | |
void SetSelectedColumn(); | |
void GetView(); | |
void SetView(); | |
void InsertItem(); | |
void DeleteItem(); | |
void DeleteAllItems(); | |
void UpdateItem(int index); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment