/// <summary> /// Helper method to send the right message /// </summary> /// <param name="scrollBarCommand">ScrollBarCommands enum to specify where to scroll</param> /// <returns>true if this was possible</returns> private bool SendScrollMessage(ScrollBarCommands scrollBarCommand) { switch (ScrollBarType) { case ScrollBarTypes.Horizontal: User32Api.SendMessage(ScrollingWindow.Handle, WindowsMessages.WM_HSCROLL, scrollBarCommand, 0); return(true); case ScrollBarTypes.Vertical: User32Api.SendMessage(ScrollingWindow.Handle, WindowsMessages.WM_VSCROLL, scrollBarCommand, 0); return(true); default: return(false); } }
/// <summary> /// Get the icon for a hWnd /// </summary> /// <typeparam name="TIcon">The return type for the icon, can be Icon, Bitmap or BitmapSource</typeparam> /// <param name="window">IInteropWindow</param> /// <param name="useLargeIcons">true to try to get a big icon first</param> /// <returns>TIcon</returns> public static TIcon GetIconFromWindow <TIcon>(this IInteropWindow window, bool useLargeIcons = false) where TIcon : class { var iconSmall = IntPtr.Zero; var iconBig = new IntPtr(1); var iconSmall2 = new IntPtr(2); IntPtr iconHandle; if (useLargeIcons) { iconHandle = User32Api.SendMessage(window.Handle, WindowsMessages.WM_GETICON, iconBig, IntPtr.Zero); if (iconHandle == IntPtr.Zero) { iconHandle = User32Api.GetClassLongWrapper(window.Handle, ClassLongIndex.IconHandle); } } else { iconHandle = User32Api.SendMessage(window.Handle, WindowsMessages.WM_GETICON, iconSmall2, IntPtr.Zero); } if (iconHandle == IntPtr.Zero) { iconHandle = User32Api.SendMessage(window.Handle, WindowsMessages.WM_GETICON, iconSmall, IntPtr.Zero); } if (iconHandle == IntPtr.Zero) { iconHandle = User32Api.GetClassLongWrapper(window.Handle, ClassLongIndex.SmallIconHandle); } if (iconHandle == IntPtr.Zero) { iconHandle = User32Api.SendMessage(window.Handle, WindowsMessages.WM_GETICON, iconBig, IntPtr.Zero); } if (iconHandle == IntPtr.Zero) { iconHandle = User32Api.GetClassLongWrapper(window.Handle, ClassLongIndex.IconHandle); } if (iconHandle != IntPtr.Zero) { return(IconHelper.IconHandleTo <TIcon>(iconHandle)); } // Nothing found return(default(TIcon)); }
/// <summary> /// Apply position from the scrollInfo /// </summary> /// <param name="scrollInfo">SCROLLINFO ref</param> /// <returns>bool</returns> private bool ApplyPosition(ref ScrollInfo scrollInfo) { if (ShowChanges) { User32Api.SetScrollInfo(ScrollBarWindow.Handle, ScrollBarType, ref scrollInfo, true); } switch (ScrollBarType) { case ScrollBarTypes.Horizontal: User32Api.SendMessage(ScrollingWindow.Handle, WindowsMessages.WM_HSCROLL, 4 + 0x10000 * scrollInfo.Position, 0); break; case ScrollBarTypes.Vertical: case ScrollBarTypes.Control: User32Api.SendMessage(ScrollingWindow.Handle, WindowsMessages.WM_VSCROLL, (int)((uint)ScrollBarCommands.SB_THUMBPOSITION + (scrollInfo.Position << 16)), 0); break; } return(true); }
public static void CloseWindows(IntPtr winIntPtr) { User32Api.SendMessage(winIntPtr, (int)WinMsg.WM_CLOSE, 0, 0); }
/// <summary> /// 发送消息到窗口 /// </summary> /// <param name="intPtr"></param> /// <param name="msg"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> public static int SendMessage(IntPtr intPtr, int msg, int wParam = 1, uint lParam = 0) { return(User32Api.SendMessage(intPtr, msg, wParam, lParam)); }
private void ResizeWindow(Direction direction) { User32Api.SendMessage(_handle, User32Api.WM_SYSCOMMAND, (IntPtr)(User32Api.SC_SIZE + direction), IntPtr.Zero); }
private void DragMoveWindow() { User32Api.SendMessage(_handle, User32Api.WM_SYSCOMMAND, (IntPtr)User32Api.SC_MOVE, IntPtr.Zero); }
/// <summary> /// Displays XML in the main rich text box. /// </summary> /// <param name="xml">XML to display.</param> private void DisplayXml(string xml) { //clear text _xmlRichTextBox.Text = ""; //stop redrawing User32Api.SendMessage(_xmlRichTextBox.Handle, User32Api.WM_SETREDRAW, 0, IntPtr.Zero); //if we have some xml, display it if (xml != null && xml.Length > 0) { StringReader sr = null; try { //create a new bold font Font standardFont = _xmlRichTextBox.SelectionFont; Font boldFont = new Font(standardFont.Name, standardFont.Size, (FontStyle)((int)standardFont.Style + (int)FontStyle.Bold)); //load xml into a text reader XmlTextReader reader = null; sr = new StringReader(xml); reader = new XmlTextReader(sr); int tabCount = -1; XmlNodeType previousNodeType = XmlNodeType.XmlDeclaration; bool previousElementEmpty = false; //itterate thru the XML, drawing out while (reader.Read()) { //figure out if we need to start a new line, and configure the tabcount bool isNewLineRequired = false; if (previousNodeType == XmlNodeType.EndElement || previousElementEmpty) //(previousElementEmpty && reader.NodeType == XmlNodeType.EndElement) { tabCount--; } if (reader.NodeType != XmlNodeType.XmlDeclaration) { if (reader.IsStartElement()) { tabCount++; if (tabCount > 0) { isNewLineRequired = true; } } else if (previousNodeType == XmlNodeType.EndElement || previousElementEmpty) { isNewLineRequired = true; } //if necessary, create a new line if (isNewLineRequired) { _xmlRichTextBox.SelectedText = "\n" + GetIndentString(tabCount); } } previousElementEmpty = false; //write the XML node switch (reader.NodeType) { //TODO validate workings of entities, notations case XmlNodeType.Document: case XmlNodeType.DocumentFragment: case XmlNodeType.DocumentType: case XmlNodeType.Entity: case XmlNodeType.EndEntity: case XmlNodeType.EntityReference: case XmlNodeType.Notation: case XmlNodeType.XmlDeclaration: _xmlRichTextBox.SelectionColor = _xmlColours.SpecialCharacter; _xmlRichTextBox.SelectedText = string.Format("<{0}/>\n", reader.Value); break; case XmlNodeType.Element: _xmlRichTextBox.SelectionColor = _xmlColours.SpecialCharacter; _xmlRichTextBox.SelectedText = "<"; _xmlRichTextBox.SelectionColor = _xmlColours.ElementName; _xmlRichTextBox.SelectedText = reader.Name; bool isEmptyElement = reader.IsEmptyElement; while (reader.MoveToNextAttribute()) { _xmlRichTextBox.SelectionColor = _xmlColours.AttributeName; _xmlRichTextBox.SelectedText = " " + reader.Name; _xmlRichTextBox.SelectionColor = _xmlColours.SpecialCharacter; _xmlRichTextBox.SelectedText = "=\""; _xmlRichTextBox.SelectionColor = _xmlColours.AttributeValue; _xmlRichTextBox.SelectionFont = boldFont; _xmlRichTextBox.SelectedText = reader.Value; _xmlRichTextBox.SelectionFont = standardFont; _xmlRichTextBox.SelectionColor = _xmlColours.SpecialCharacter; _xmlRichTextBox.SelectedText = "\""; } _xmlRichTextBox.SelectionColor = _xmlColours.SpecialCharacter; if (isEmptyElement) { _xmlRichTextBox.SelectedText = " /"; previousElementEmpty = true; } _xmlRichTextBox.SelectedText = ">"; break; case XmlNodeType.Text: _xmlRichTextBox.SelectionColor = _xmlColours.ElementValue; _xmlRichTextBox.SelectionFont = boldFont; _xmlRichTextBox.SelectedText = reader.Value; _xmlRichTextBox.SelectionFont = standardFont; break; case XmlNodeType.EndElement: _xmlRichTextBox.SelectionColor = _xmlColours.SpecialCharacter; _xmlRichTextBox.SelectedText = "</"; _xmlRichTextBox.SelectionColor = _xmlColours.ElementName; _xmlRichTextBox.SelectedText = reader.Name; _xmlRichTextBox.SelectionColor = _xmlColours.SpecialCharacter; _xmlRichTextBox.SelectedText = ">"; break; case XmlNodeType.CDATA: _xmlRichTextBox.SelectionColor = _xmlColours.SpecialCharacter; _xmlRichTextBox.SelectedText = "<![CDATA["; _xmlRichTextBox.SelectionColor = _xmlColours.ElementValue; _xmlRichTextBox.SelectionFont = boldFont; _xmlRichTextBox.SelectedText = reader.Value; _xmlRichTextBox.SelectionFont = standardFont; _xmlRichTextBox.SelectionColor = _xmlColours.SpecialCharacter; _xmlRichTextBox.SelectedText = "]]>"; break; } //store the current node for later use previousNodeType = reader.NodeType; } //tidy up and position the cursor _xmlRichTextBox.SelectedText = "\n"; _xmlRichTextBox.Select(0, 0); } catch { _xmlRichTextBox.Text = xml; } finally { if (sr != null) { sr.Close(); } } } //stop redrawing User32Api.SendMessage(_xmlRichTextBox.Handle, User32Api.WM_SETREDRAW, 1, IntPtr.Zero); _xmlRichTextBox.Refresh(); }