#GetDeviceCaps
Explore tagged Tumblr posts
Text
0 notes
Text
Place A Userform Next To The Clicked Cell
�� It may be more useful to display this userform next to the active cell that was clicked, rather than in the center of the sheet.
By declaring the GetDeviceCaps , GetDC , ReleaseDC
functions , I repositioned the userform next to each the clicked
activecell .(The template is checked in 32-bit and 64-bit Excel versions .)
"Type POINTAPI
X As Long
Y As Long
End Type
#If VBA7 Then
Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDc As LongPtr, ByVal nIndex As Long) As Long
Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hDc As LongPtr) As Long
Dim hDc As LongPtr
#Else
Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDc As Long, ByVal nIndex As Long) As Long
Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hDc As Long) As Long
Dim hDc As Long
#End If
...
"
Read more & download sample file
0 notes
Text
長さの計算って面倒くさい! ということで、単位変換系のクラス
> var len = new Length(); > len.inch = 1; > len.cm 2.54 > > len.cm = 2; > len.inch 0.7874015748031496062992125984
LengthType.cs
public enum LengthType { km, m, cm, mm, /// <summary> /// 1inch = 2.54cm /// </summary> inch, /// <summary> /// 1inch539 = 2.539cm /// </summary> inch539, px, px539, pt, pt539, /// <summary> /// 1em == 16px /// </summary> em }
Length.cs
using System; using System.Collections.Generic; using System.Runtime.InteropServices; namespace Fairies { public class Length : IComparable<Length>, IEquatable<Length> { // ========================================================================================================= xxx [DllImport("user32.dll")] internal extern static bool SetProcessDPIAware(); [DllImport("user32.dll")] internal extern static IntPtr GetWindowDC(IntPtr hwnd); [DllImport("gdi32.dll")] internal extern static int GetDeviceCaps(IntPtr hdc, int index); [DllImport("user32.dll")] internal extern static int ReleaseDC(IntPtr hwnd, IntPtr hdc); // ========================================================================================================= xxx private decimal raw; // ========================================================================================================= xxx public decimal DPI => GetDPI(); // ========================================================================================================= xxx public static Length From(LengthType type, decimal value) { return new Length(type, value); } public static Length From(Length length) { return new Length(LengthType.mm, length.mm); } public static decimal Convert(decimal value, LengthType from, LengthType to) { return new Length(from, value).Get(to); } // ========================================================================================================= xxx public Length() { raw = 0M; } public Length(LengthType type, decimal initial) { Set(type, initial); } public Length(LengthType type, decimal initial, decimal digitWeight) { Set(type, initial, digitWeight); } private void CommonInitialize() { customUnits = new Dictionary<string, (Func<Length, decimal> Getter, Action<Length, decimal> Setter)>(); } // ========================================================================================================= xxx private Dictionary<string, (Func<Length, decimal> Getter, Action<Length, decimal> Setter)> customUnits; public Length RegistUnit(string unitID, Func<Length, decimal> getter, Action<Length, decimal> setter) { if (IsUnitRegisted(unitID)) { customUnits[unitID] = (getter, setter); } else { customUnits.Add(unitID, (getter, setter)); } return this; } public Length DeleteUnit(string unitID) { customUnits.Remove(unitID); return this; } public bool IsUnitRegisted(string unitID) { return customUnits.ContainsKey(unitID); } public (Func<Length, decimal> Getter, Action<Length, decimal> Setter) GetUnit(string unitID) { return customUnits[unitID]; } // ========================================================================================================= xxx public decimal km { get => Get(LengthType.km); set => Set(LengthType.km, value); } public decimal m { get => Get(LengthType.m); set => Set(LengthType.m, value); } public decimal cm { get => Get(LengthType.cm); set => Set(LengthType.cm, value); } public decimal mm { get => Get(LengthType.mm); set => Set(LengthType.mm, value); } // ========================================================================================================= xxx public decimal inch { get => Get(LengthType.inch); set => Set(LengthType.inch, value); } public decimal inch539 { get => Get(LengthType.inch539); set => Set(LengthType.inch539, value); } // ========================================================================================================= xxx public decimal px { get => Get(LengthType.px); set => Set(LengthType.px, value); } public decimal px539 { get => Get(LengthType.px539); set => Set(LengthType.px539, value); } public decimal pt { get => Get(LengthType.pt); set => Set(LengthType.pt, value); } public decimal pt539 { get => Get(LengthType.pt539); set => Set(LengthType.pt539, value); } // ========================================================================================================= xxx public decimal em { get => Get(LengthType.em); set => Set(LengthType.em, value); } // ========================================================================================================= xxx public decimal Get(string unitID, decimal digitWeight = 1) { return customUnits[unitID].Getter(this) * digitWeight; } public decimal Get(LengthType type, decimal digitWeight = 1) { switch (type) { case LengthType.km: return (m / 1000M) * digitWeight; case LengthType.m: return (cm / 100M) * digitWeight; case LengthType.cm: return (mm / 10M) * digitWeight; case LengthType.mm: return raw * digitWeight; case LengthType.inch: return (cm / 2.54M) * digitWeight; case LengthType.inch539: return (cm / 2.539M) * digitWeight; case LengthType.px: return (inch * DPI); case LengthType.px539: return (inch539 * DPI) * digitWeight; case LengthType.pt: return (mm / (25.4M / 72M)) * digitWeight; case LengthType.pt539: return (mm / (25.39M / 72M)) * digitWeight; case LengthType.em: return (px / 16M) * digitWeight; default: throw new NotSupportedException(); } } public Length Set(string unitID, decimal value, decimal digitWeight = 1) { customUnits[unitID].Setter(this, value); return this; } public void Set(LengthType type, decimal value, decimal digitWeight = 1) { var v = value * digitWeight; switch (type) { case LengthType.km: m = (v * 1000M); break; case LengthType.m: cm = (v * 100M); break; case LengthType.cm: mm = (v * 10M); break; case LengthType.mm: raw = v; break; case LengthType.inch: cm = (v * 2.54M); break; case LengthType.inch539: cm = (v * 2.539M); break; case LengthType.px: inch = (v / DPI); break; case LengthType.px539: inch539 = (v / DPI); break; case LengthType.pt: mm = v * (25.4M / 72M); break; case LengthType.pt539: mm = v * (25.39M / 72M); break; case LengthType.em: px = (v * 16M); break; default: throw new NotSupportedException(); } } // ========================================================================================================= xxx private static decimal GetDPI() { SetProcessDPIAware(); var dc = GetWindowDC(IntPtr.Zero); var x = GetDeviceCaps(dc, 88);//x var y = GetDeviceCaps(dc, 90);//y ReleaseDC(IntPtr.Zero, dc); return x;// どっちも同じ想定で } // ========================================================================================================= xxx public bool Equals(Length other) { return raw.Equals(other.raw); } public int CompareTo(Length other) { return raw.CompareTo(other.raw); } // ========================================================================================================= operator public static Length operator +(Length a, Length b) => new Length(LengthType.mm, a.raw + b.raw); public static Length operator -(Length a, Length b) => new Length(LengthType.mm, a.raw - b.raw); public static Length operator *(Length a, Length b) => new Length(LengthType.mm, a.raw * b.raw); public static Length operator /(Length a, Length b) => new Length(LengthType.mm, a.raw / b.raw); public static Length operator %(Length a, Length b) => new Length(LengthType.mm, a.raw % b.raw); } }
1 note
·
View note