Thread: [Mwinapi-commits] SF.net SVN: mwinapi: [62] trunk/ManagedWinapi
Status: Beta
Brought to you by:
schierlm
From: <sch...@us...> - 2008-03-25 22:39:01
|
Revision: 62 http://mwinapi.svn.sourceforge.net/mwinapi/?rev=62&view=rev Author: schierlm Date: 2008-03-25 15:38:59 -0700 (Tue, 25 Mar 2008) Log Message: ----------- Set default event for ClipboardNotifier, Crosshair and Hotkey, Add properties Location and Size and methods IsValid and SendClose to SystemWindow. [suggested by Frank Koch] Modified Paths: -------------- trunk/ManagedWinapi/ClipboardNotifier.cs trunk/ManagedWinapi/Crosshair.cs trunk/ManagedWinapi/Hotkey.cs trunk/ManagedWinapi/SystemWindow.cs Modified: trunk/ManagedWinapi/ClipboardNotifier.cs =================================================================== --- trunk/ManagedWinapi/ClipboardNotifier.cs 2008-03-04 19:47:03 UTC (rev 61) +++ trunk/ManagedWinapi/ClipboardNotifier.cs 2008-03-25 22:38:59 UTC (rev 62) @@ -31,6 +31,7 @@ /// <summary> /// Specifies a component that monitors the system clipboard for changes. /// </summary> + [DefaultEvent("ClipboardChanged")] public class ClipboardNotifier : Component { Modified: trunk/ManagedWinapi/Crosshair.cs =================================================================== --- trunk/ManagedWinapi/Crosshair.cs 2008-03-04 19:47:03 UTC (rev 61) +++ trunk/ManagedWinapi/Crosshair.cs 2008-03-25 22:38:59 UTC (rev 62) @@ -14,6 +14,7 @@ /// on screen. This is useful to select other programs by dragging the crosshair /// to a program window. /// </summary> + [DefaultEvent("CrosshairDragged")] public partial class Crosshair : UserControl { Image myImage; Modified: trunk/ManagedWinapi/Hotkey.cs =================================================================== --- trunk/ManagedWinapi/Hotkey.cs 2008-03-04 19:47:03 UTC (rev 61) +++ trunk/ManagedWinapi/Hotkey.cs 2008-03-25 22:38:59 UTC (rev 62) @@ -31,6 +31,7 @@ /// <summary> /// Specifies a component that creates a global keyboard hotkey. /// </summary> + [DefaultEvent("HotkeyPressed")] public class Hotkey : Component { Modified: trunk/ManagedWinapi/SystemWindow.cs =================================================================== --- trunk/ManagedWinapi/SystemWindow.cs 2008-03-04 19:47:03 UTC (rev 61) +++ trunk/ManagedWinapi/SystemWindow.cs 2008-03-25 22:38:59 UTC (rev 62) @@ -680,6 +680,50 @@ } /// <summary> + /// The window's location inside its parent or on the screen. + /// </summary> + public Point Location + { + get + { + return Position.Location; + } + + set + { + WINDOWPLACEMENT wp = new WINDOWPLACEMENT(); + wp.length = Marshal.SizeOf(wp); + GetWindowPlacement(_hwnd, ref wp); + wp.rcNormalPosition.Bottom = value.X + wp.rcNormalPosition.Height; + wp.rcNormalPosition.Right = value.Y + wp.rcNormalPosition.Width; + wp.rcNormalPosition.Top = value.X; + wp.rcNormalPosition.Left = value.Y; + SetWindowPlacement(_hwnd, ref wp); + } + } + + /// <summary> + /// The window's size. + /// </summary> + public Size Size + { + get + { + return Position.Size; + } + + set + { + WINDOWPLACEMENT wp = new WINDOWPLACEMENT(); + wp.length = Marshal.SizeOf(wp); + GetWindowPlacement(_hwnd, ref wp); + wp.rcNormalPosition.Right = wp.rcNormalPosition.Left + value.Width; + wp.rcNormalPosition.Bottom = wp.rcNormalPosition.Top + value.Height; + SetWindowPlacement(_hwnd, ref wp); + } + } + + /// <summary> /// The window's position in absolute screen coordinates. Use /// <see cref="Position"/> if you want to use the relative position. /// </summary> @@ -942,6 +986,25 @@ } } + /// <summary> + /// Whether this SystemWindow represents a valid window that existed + /// when this SystemWindow instance was created. To check if a window + /// still exists, better check its <see cref="ClassName"/> property. + /// </summary> + public bool IsValid() + { + return _hwnd != IntPtr.Zero; + } + + /// <summary> + /// Send a message to this window that it should close. This is equivalent + /// to clicking the "X" in the upper right corner or pressing Alt+F4. + /// </summary> + public void SendClose() + { + SendSetMessage(WM_CLOSE, 0); + } + internal int SendGetMessage(uint message) { return SendGetMessage(message, 0); @@ -1202,6 +1265,8 @@ [DllImport("user32.dll")] static extern IntPtr GetDC(IntPtr hWnd); + private const int WM_CLOSE = 16; + private enum GetWindow_Cmd { GW_HWNDFIRST = 0, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <sch...@us...> - 2008-04-27 17:52:26
|
Revision: 67 http://mwinapi.svn.sourceforge.net/mwinapi/?rev=67&view=rev Author: schierlm Date: 2008-04-27 10:52:10 -0700 (Sun, 27 Apr 2008) Log Message: ----------- CodepointRange.cs: Get codepoints supported by a font Modified Paths: -------------- trunk/ManagedWinapi/ManagedWinapi.csproj Added Paths: ----------- trunk/ManagedWinapi/CodepointRange.cs Added: trunk/ManagedWinapi/CodepointRange.cs =================================================================== --- trunk/ManagedWinapi/CodepointRange.cs (rev 0) +++ trunk/ManagedWinapi/CodepointRange.cs 2008-04-27 17:52:10 UTC (rev 67) @@ -0,0 +1,122 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Drawing; +using System.Runtime.InteropServices; + +namespace ManagedWinapi +{ + /// <summary> + /// The unicode range of codepoints supported by a font. + /// </summary> + public class CodepointRange + { + char[] ranges; + readonly int codepointCount; + + /// <summary> + /// Creates a new CodepointRange object for a font. + /// </summary> + public CodepointRange(Font font) + { + List<char> rangeList = new List<char>(); + Graphics g = Graphics.FromImage(new Bitmap(1, 1)); + IntPtr hdc = g.GetHdc(); + IntPtr hFont = font.ToHfont(); + IntPtr oldFont = SelectObject(hdc, hFont); + uint size = GetFontUnicodeRanges(hdc, IntPtr.Zero); + IntPtr glyphSet = Marshal.AllocHGlobal((int)size); + GetFontUnicodeRanges(hdc, glyphSet); + codepointCount = Marshal.ReadInt32(glyphSet, 8); + int tmp = 0; + int count = Marshal.ReadInt32(glyphSet, 12); + for (int i = 0; i < count; i++) + { + char firstIncluded = (char)Marshal.ReadInt16(glyphSet, 16 + i * 4); + char firstExcluded = (char)(firstIncluded + Marshal.ReadInt16(glyphSet, 18 + i * 4)); + tmp += firstExcluded - firstIncluded; + rangeList.Add(firstIncluded); + rangeList.Add(firstExcluded); + } + SelectObject(hdc, oldFont); + Marshal.FreeHGlobal(glyphSet); + g.ReleaseHdc(hdc); + g.Dispose(); + if (tmp != codepointCount) throw new Exception(font.FontFamily.Name); + ranges = rangeList.ToArray(); + if (ranges.Length < 2) throw new Exception(); + } + + /// <summary> + /// The number of codepoints supported by this font. + /// </summary> + public int SupportedCodepointCount { get { return codepointCount; } } + + /// <summary> + /// The first (lowest) supported codepoint. + /// </summary> + public char FirstCodepoint { get { return ranges[0]; } } + + /// <summary> + /// The last (highest) supported codepoint. + /// </summary> + public char LastCodepoint { get { return (char)(ranges[ranges.Length - 1] - 1); } } + + /// <summary> + /// Tests whether a specific codepoint is supported by this font. + /// </summary> + public bool IsSupported(char codepoint) + { + bool result = false; + foreach (char c in ranges) + { + if (c > codepoint) break; + result = !result; + } + return result; + } + + /// <summary> + /// Finds the next codepoint that is either supported or not. + /// </summary> + public char FindNext(char from, bool supported) + { + if (IsSupported(from) == supported) return from; + foreach (char c in ranges) + { + if (c > from) return c; + } + return (char)0xFFFF; + } + + /// <summary> + /// Returns a <see cref="String"/> representation of this codepoint range. + /// </summary> + public override string ToString() + { + StringBuilder sb = new StringBuilder("["); + for (int i = 0; i < ranges.Length; i++) + { + if (i % 2 == 1) + { + if (ranges[i] == ranges[i - 1] + 1) continue; + sb.Append("-"); + } + else if (i != 0) + { + sb.Append(", "); + } + sb.Append(((int)ranges[i] - i % 2).ToString("X4")); + } + return sb.Append("]").ToString(); + } + + #region PInvoke Declarations + [DllImport("gdi32.dll")] + private static extern uint GetFontUnicodeRanges(IntPtr hdc, IntPtr lpgs); + + [DllImport("gdi32.dll")] + private extern static IntPtr SelectObject(IntPtr hDC, IntPtr hObject); + #endregion + } +} Modified: trunk/ManagedWinapi/ManagedWinapi.csproj =================================================================== --- trunk/ManagedWinapi/ManagedWinapi.csproj 2008-04-06 18:49:24 UTC (rev 66) +++ trunk/ManagedWinapi/ManagedWinapi.csproj 2008-04-27 17:52:10 UTC (rev 67) @@ -46,6 +46,7 @@ </Compile> <Compile Include="Contents\AccessibleWindowParser.cs" /> <Compile Include="ExtendedFileInfo.cs" /> + <Compile Include="CodepointRange.cs" /> <Compile Include="Hook.cs"> <SubType>Component</SubType> </Compile> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <sch...@us...> - 2008-06-14 19:02:22
|
Revision: 72 http://mwinapi.svn.sourceforge.net/mwinapi/?rev=72&view=rev Author: schierlm Date: 2008-06-14 12:02:21 -0700 (Sat, 14 Jun 2008) Log Message: ----------- PrivilegedActions: Shutdown and set system time Modified Paths: -------------- trunk/ManagedWinapi/CodepointRange.cs trunk/ManagedWinapi/ManagedWinapi.csproj Added Paths: ----------- trunk/ManagedWinapi/PrivilegedActions.cs Modified: trunk/ManagedWinapi/CodepointRange.cs =================================================================== --- trunk/ManagedWinapi/CodepointRange.cs 2008-06-14 18:49:09 UTC (rev 71) +++ trunk/ManagedWinapi/CodepointRange.cs 2008-06-14 19:02:21 UTC (rev 72) @@ -164,6 +164,8 @@ } #region Equals and HashCode + + /// public override bool Equals(object obj) { CodepointRange cr = obj as CodepointRange; @@ -181,6 +183,7 @@ return true; } + /// public override int GetHashCode() { return 3 * codepointCount + 7 * ranges.Length + 9 * FirstCodepoint + 11 * LastCodepoint; @@ -192,10 +195,10 @@ private static extern uint GetFontUnicodeRanges(IntPtr hdc, IntPtr lpgs); [DllImport("gdi32.dll")] - private extern static IntPtr SelectObject(IntPtr hDC, IntPtr hObject); + private static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject); [DllImport("gdi32.dll")] - public static extern bool DeleteObject(IntPtr hObject); + private static extern bool DeleteObject(IntPtr hObject); #endregion } } Modified: trunk/ManagedWinapi/ManagedWinapi.csproj =================================================================== --- trunk/ManagedWinapi/ManagedWinapi.csproj 2008-06-14 18:49:09 UTC (rev 71) +++ trunk/ManagedWinapi/ManagedWinapi.csproj 2008-06-14 19:02:21 UTC (rev 72) @@ -66,6 +66,7 @@ <Compile Include="ShortcutBox.Designer.cs"> <DependentUpon>ShortcutBox.cs</DependentUpon> </Compile> + <Compile Include="PrivilegedActions.cs" /> <Compile Include="SystemAccessibleObject.cs" /> <Compile Include="ApiHelper.cs" /> <Compile Include="ClipboardNotifier.cs"> Added: trunk/ManagedWinapi/PrivilegedActions.cs =================================================================== --- trunk/ManagedWinapi/PrivilegedActions.cs (rev 0) +++ trunk/ManagedWinapi/PrivilegedActions.cs 2008-06-14 19:02:21 UTC (rev 72) @@ -0,0 +1,168 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace ManagedWinapi +{ + /// <summary> + /// Collection of miscellaneous actions that cannot be performed as + /// a non-administrative user, like shutdown or setting the system time. + /// </summary> + public static class PrivilegedActions + { + /// <summary> + /// Shutdown the system. + /// </summary> + public static void ShutDown(ShutdownAction action) + { + ShutDown(action, ShutdownForceMode.NoForce); + } + + /// <summary> + /// Shutdown the system. + /// </summary> + public static void ShutDown(ShutdownAction action, ShutdownForceMode forceMode) + { + ApiHelper.FailIfZero(ExitWindowsEx((uint)action | (uint)forceMode, SHTDN_REASON_FLAG_PLANNED)); + } + + /// <summary> + /// Get or set the system time in the local timezone. + /// </summary> + public static DateTime LocalTime + { + get + { + SYSTEMTIME st = new SYSTEMTIME(); + ApiHelper.FailIfZero(GetLocalTime(ref st)); + return st.ToDateTime(); + } + + set + { + SYSTEMTIME st = new SYSTEMTIME(value); + // Set it twice due to possible daylight savings change + ApiHelper.FailIfZero(SetLocalTime(ref st)); + ApiHelper.FailIfZero(SetLocalTime(ref st)); + } + } + + /// <summary> + /// Get or set the system time, in UTC. + /// </summary> + public static DateTime SystemTime + { + get + { + SYSTEMTIME st = new SYSTEMTIME(); + ApiHelper.FailIfZero(GetSystemTime(ref st)); + return st.ToDateTime(); + } + + set + { + SYSTEMTIME st = new SYSTEMTIME(value); + ApiHelper.FailIfZero(SetLocalTime(ref st)); + } + } + + /// <summary> + /// Actions that can be performed at shutdown. + /// </summary> + public enum ShutdownAction : uint + { + /// <summary> + /// Log off the currently logged-on user. + /// </summary> + LogOff = 0x00, + + /// <summary> + /// Shut down the system. + /// </summary> + ShutDown = 0x01, + + /// <summary> + /// Reboot the system. + /// </summary> + Reboot = 0x02, + + /// <summary> + /// Shut down the system and power it off. + /// </summary> + PowerOff = 0x08, + + /// <summary> + /// Reboot the system and restart applications that are running + /// now and support this feature. + /// </summary> + RestartApps = 0x40, + } + + /// <summary> + /// Whether shutdown should be forced if an application cancels it + /// or is hung. + /// </summary> + public enum ShutdownForceMode : uint + { + /// <summary> + /// Do not force shutdown, applications can cancel it. + /// </summary> + NoForce = 0x00, + + /// <summary> + /// Force shutdown, even if application cancels it or is hung. + /// </summary> + Force = 0x04, + + /// <summary> + /// Force shutdown if application is hung, but not if it cancels it. + /// </summary> + ForceIfHung = 0x10 + } + + #region PInvoke Declarations + + [DllImport("user32.dll", SetLastError = true)] + static extern int ExitWindowsEx(uint uFlags, uint dwReason); + + const uint SHTDN_REASON_FLAG_PLANNED = 0x80000000; + + struct SYSTEMTIME + { + internal ushort wYear, wMonth, wDayOfWeek, wDay, + wHour, wMinute, wSecond, wMilliseconds; + + internal SYSTEMTIME(DateTime time) + { + wYear = (ushort)time.Year; + wMonth = (ushort)time.Month; + wDayOfWeek = (ushort)time.DayOfWeek; + wDay = (ushort)time.Day; + wHour = (ushort)time.Hour; + wMinute = (ushort)time.Minute; + wSecond = (ushort)time.Second; + wMilliseconds = (ushort)time.Millisecond; + } + + internal DateTime ToDateTime() + { + return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds); + } + } + + [DllImport("kernel32.dll", SetLastError = true)] + static extern int GetSystemTime(ref SYSTEMTIME lpSystemTime); + + [DllImport("kernel32.dll", SetLastError = true)] + static extern int SetSystemTime(ref SYSTEMTIME lpSystemTime); + + [DllImport("kernel32.dll", SetLastError = true)] + static extern int GetLocalTime(ref SYSTEMTIME lpSystemTime); + + [DllImport("kernel32.dll", SetLastError = true)] + static extern int SetLocalTime(ref SYSTEMTIME lpSystemTime); + + #endregion + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |