?? nativemethods.cs
字號(hào):
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWPOS
{
public IntPtr hwnd;
public IntPtr hwndInsertAfter;
public int x;
public int y;
public int cx;
public int cy;
public int flags;
}
// Various flavours of SendMessage: plain vanilla, and passing references to various structures
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessageLVItem(IntPtr hWnd, int msg, int wParam, ref LVITEM lvi);
//[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
//private static extern IntPtr SendMessageLVColumn(IntPtr hWnd, int m, int wParam, ref LVCOLUMN lvc);
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessageHDItem(IntPtr hWnd, int msg, int wParam, ref HDITEM hdi);
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageHDHITTESTINFO(IntPtr hWnd, int Msg, IntPtr wParam, [In, Out] HDHITTESTINFO lParam);
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTOOLINFO(IntPtr hWnd, int Msg, int wParam, NativeMethods.TOOLINFO lParam);
// Entry points used by this code
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool GetScrollInfo(IntPtr hWnd, int fnBar, SCROLLINFO si);
[DllImport("user32.dll", EntryPoint = "GetUpdateRect", CharSet = CharSet.Auto)]
private static extern int GetUpdateRectInternal(IntPtr hWnd, ref Rectangle r, bool eraseBackground);
[DllImport("comctl32.dll", CharSet = CharSet.Auto)]
private static extern bool ImageList_Draw(IntPtr himl, int i, IntPtr hdcDst, int x, int y, int fStyle);
//[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
//public static extern bool SetScrollInfo(IntPtr hWnd, int fnBar, SCROLLINFO si, bool fRedraw);
[DllImport("user32.dll", EntryPoint = "ValidateRect", CharSet = CharSet.Auto)]
private static extern IntPtr ValidatedRectInternal(IntPtr hWnd, ref Rectangle r);
public static bool DrawImageList(Graphics g, ImageList il, int index, int x, int y, bool isSelected)
{
int flags = ILD_TRANSPARENT;
if (isSelected)
flags |= ILD_BLEND25;
bool result = ImageList_Draw(il.Handle, index, g.GetHdc(), x, y, flags);
g.ReleaseHdc();
return result;
}
/// <summary>
/// Make sure the ListView has the extended style that says to display subitem images.
/// </summary>
/// <remarks>This method must be called after any .NET call that update the extended styles
/// since they seem to erase this setting.</remarks>
/// <param name="list">The listview to send a m to</param>
public static void ForceSubItemImagesExStyle(ListView list) {
SendMessage(list.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_SUBITEMIMAGES, LVS_EX_SUBITEMIMAGES);
}
/// <summary>
/// Calculates the number of items that can fit vertically in the visible area of a list-view (which
/// must be in details or list view.
/// </summary>
/// <param name="list">The listView</param>
/// <returns>Number of visible items per page</returns>
public static int GetCountPerPage(ListView list)
{
return (int)SendMessage(list.Handle, LVM_GETCOUNTPERPAGE, 0, 0);
}
/// <summary>
/// For the given item and subitem, make it display the given image
/// </summary>
/// <param name="list">The listview to send a m to</param>
/// <param name="itemIndex">row number (0 based)</param>
/// <param name="subItemIndex">subitem (0 is the item itself)</param>
/// <param name="imageIndex">index into the image list</param>
public static void SetSubItemImage(ListView list, int itemIndex, int subItemIndex, int imageIndex) {
LVITEM lvItem = new LVITEM();
lvItem.mask = LVIF_IMAGE;
lvItem.iItem = itemIndex;
lvItem.iSubItem = subItemIndex;
lvItem.iImage = imageIndex;
SendMessageLVItem(list.Handle, LVM_SETITEM, 0, ref lvItem);
}
/// <summary>
/// Setup the given column of the listview to show the given image to the right of the text.
/// If the image index is -1, any previous image is cleared
/// </summary>
/// <param name="list">The listview to send a m to</param>
/// <param name="columnIndex">Index of the column to modifiy</param>
/// <param name="order"></param>
/// <param name="imageIndex">Index into the small image list</param>
public static void SetColumnImage(ListView list, int columnIndex, SortOrder order, int imageIndex) {
IntPtr hdrCntl = NativeMethods.GetHeaderControl(list);
if (hdrCntl.ToInt32() == 0)
return;
HDITEM item = new HDITEM();
item.mask = HDI_FORMAT;
IntPtr result = SendMessageHDItem(hdrCntl, HDM_GETITEM, columnIndex, ref item);
item.fmt &= ~(HDF_SORTUP | HDF_SORTDOWN | HDF_IMAGE | HDF_BITMAP_ON_RIGHT);
if (NativeMethods.HasBuiltinSortIndicators()) {
if (order == SortOrder.Ascending)
item.fmt |= HDF_SORTUP;
if (order == SortOrder.Descending)
item.fmt |= HDF_SORTDOWN;
}
else {
item.mask |= HDI_IMAGE;
item.fmt |= (HDF_IMAGE | HDF_BITMAP_ON_RIGHT);
item.iImage = imageIndex;
}
result = SendMessageHDItem(hdrCntl, HDM_SETITEM, columnIndex, ref item);
}
/// <summary>
/// Does this version of the operating system have builtin sort indicators?
/// </summary>
/// <returns>Are there builtin sort indicators</returns>
/// <remarks>XP and later have these</remarks>
public static bool HasBuiltinSortIndicators( ) {
return OSFeature.Feature.GetVersionPresent(OSFeature.Themes) != null;
}
/// <summary>
/// Return the bounds of the update region on the given control.
/// </summary>
/// <remarks>The BeginPaint() system call validates the update region, effectively wiping out this information.
/// So this call has to be made before the BeginPaint() call.</remarks>
/// <param name="cntl">The control whose update region is be calculated</param>
/// <returns>A rectangle</returns>
public static Rectangle GetUpdateRect(Control cntl) {
Rectangle r = new Rectangle();
GetUpdateRectInternal(cntl.Handle, ref r, false);
return r;
}
/// <summary>
/// Validate an area of the given control. A validated area will not be repainted at the next redraw.
/// </summary>
/// <param name="cntl">The control to be validated</param>
/// <param name="r">The area of the control to be validated</param>
public static void ValidateRect(Control cntl, Rectangle r) {
ValidatedRectInternal(cntl.Handle, ref r);
}
/// <summary>
/// Select all rows on the given listview
/// </summary>
/// <param name="list">The listview whose items are to be selected</param>
public static void SelectAllItems(ListView list) {
NativeMethods.SetItemState(list, -1, 2, 2);
}
/// <summary>
/// Deselect all rows on the given listview
/// </summary>
/// <param name="list">The listview whose items are to be deselected</param>
public static void DeselectAllItems(ListView list) {
NativeMethods.SetItemState(list, -1, 2, 0);
}
/// <summary>
/// Set the item state on the given item
/// </summary>
/// <param name="list">The listview whose item's state is to be changed</param>
/// <param name="itemIndex">The index of the item to be changed</param>
/// <param name="mask">Which bits of the value are to be set?</param>
/// <param name="value">The value to be set</param>
public static void SetItemState(ListView list, int itemIndex, int mask, int value) {
LVITEM lvItem = new LVITEM();
lvItem.stateMask = mask;
lvItem.state = value;
SendMessageLVItem(list.Handle, LVM_SETITEMSTATE, itemIndex, ref lvItem);
}
/// <summary>
/// Scroll the given listview by the given deltas
/// </summary>
/// <param name="list"></param>
/// <param name="dx"></param>
/// <param name="dy"></param>
/// <returns>true if the scroll succeeded</returns>
public static bool Scroll(ListView list, int dx, int dy) {
return SendMessage(list.Handle, LVM_SCROLL, dx, dy) != IntPtr.Zero;
}
/// <summary>
/// Return the handle to the header control on the given list
/// </summary>
/// <param name="list">The listview whose header control is to be returned</param>
/// <returns>The handle to the header control</returns>
public static IntPtr GetHeaderControl(ListView list) {
return SendMessage(list.Handle, LVM_GETHEADER, 0, 0);
}
/// <summary>
/// Return the index of the divider under the given point. Return -1 if no divider is under the pt
/// </summary>
/// <param name="handle">The list we are interested in</param>
/// <param name="pt">The client co-ords</param>
/// <returns>The index of the divider under the point, or -1 if no divider is under that point</returns>
public static int GetDividerUnderPoint(IntPtr handle, Point pt) {
const int HHT_ONDIVIDER = 4;
return NativeMethods.HeaderControlHitTest(handle, pt, HHT_ONDIVIDER);
}
/// <summary>
/// Return the index of the column of the header that is under the given point.
/// Return -1 if no column is under the pt
/// </summary>
/// <param name="handle">The list we are interested in</param>
/// <param name="pt">The client co-ords</param>
/// <returns>The index of the column under the point, or -1 if no column header is under that point</returns>
public static int GetColumnUnderPoint(IntPtr handle, Point pt) {
const int HHT_ONHEADER = 2;
return NativeMethods.HeaderControlHitTest(handle, pt, HHT_ONHEADER);
}
private static int HeaderControlHitTest(IntPtr handle, Point pt, int flag) {
HDHITTESTINFO testInfo = new HDHITTESTINFO();
testInfo.pt_x = pt.X;
testInfo.pt_y = pt.Y;
IntPtr result = NativeMethods.SendMessageHDHITTESTINFO(handle, HDM_HITTEST, IntPtr.Zero, testInfo);
if ((testInfo.flags & flag) != 0)
return result.ToInt32();
else
return -1;
}
/// <summary>
/// Get the scroll position of the given scroll bar
/// </summary>
/// <param name="handle"></param>
/// <param name="horizontalBar"></param>
/// <returns></returns>
public static int GetScrollPosition(IntPtr handle, bool horizontalBar) {
int fnBar = (horizontalBar ? SB_HORZ : SB_VERT);
SCROLLINFO si = new SCROLLINFO();
si.fMask = SIF_POS;
if (GetScrollInfo(handle, fnBar, si))
return si.nPos;
else
return -1;
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -