Skip to content

Commit b411878

Browse files
committed
Added a menu item to copy a window text to clipboard.
1 parent a89a8d7 commit b411878

File tree

7 files changed

+196
-34
lines changed

7 files changed

+196
-34
lines changed

SmartSystemMenu/Forms/MainForm.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,16 @@ private void WindowGetMsg(object sender, WndProcEventArgs e)
402402
}
403403
break;
404404

405+
case SystemMenu.SC_COPY_TEXT_TO_CLIPBOARD:
406+
{
407+
var text = window.ExtractText();
408+
if (text != null)
409+
{
410+
Clipboard.SetText(text);
411+
}
412+
}
413+
break;
414+
405415
case SystemMenu.SC_TOPMOST:
406416
{
407417
bool r = window.Menu.IsMenuItemChecked(SystemMenu.SC_TOPMOST);

SmartSystemMenu/NativeConstants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,7 @@ static class NativeConstants
8383
public const int PROCESS_SET_INFORMATION = 0x0200;
8484
public const int PROCESS_QUERY_INFORMATION = 0x0400;
8585
public const int PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
86+
87+
public const int STD_OUTPUT_HANDLE = -11;
8688
}
8789
}

SmartSystemMenu/NativeMethods.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static class NativeMethods
5858
public static extern int GetMenuItemCount(IntPtr hMenu);
5959

6060
[DllImport("user32.dll")]
61-
public static extern UInt32 GetMenuState(IntPtr hMenu, int uIdItem, int uFlags);
61+
public static extern uint GetMenuState(IntPtr hMenu, int uIdItem, int uFlags);
6262

6363
[DllImport("User32.dll")]
6464
public static extern bool SetMenuItemInfo(IntPtr hMenu, int uIdItem, bool fByPosition, ref MenuItemInfo lpmii);
@@ -70,10 +70,10 @@ static class NativeMethods
7070
public static extern int GetAsyncKeyState(int key);
7171

7272
[DllImport("user32.dll")]
73-
public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, UInt32 crKey, Byte bAlpha, UInt32 dwFlags);
73+
public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, Byte bAlpha, uint dwFlags);
7474

7575
[DllImport("user32.dll")]
76-
public static extern bool GetLayeredWindowAttributes(IntPtr hwnd, out UInt32 crKey, out Byte bAlpha, out UInt32 dwFlags);
76+
public static extern bool GetLayeredWindowAttributes(IntPtr hwnd, out uint crKey, out Byte bAlpha, out uint dwFlags);
7777

7878
[DllImport("user32.dll")]
7979
public static extern int SetWindowLong(IntPtr handle, int nIndex, int dwNewLong);
@@ -101,7 +101,7 @@ static class NativeMethods
101101

102102
[DllImport("user32.dll")]
103103
[return: MarshalAs(UnmanagedType.Bool)]
104-
public static extern bool SetWindowPos(IntPtr handle, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, UInt32 uFlags);
104+
public static extern bool SetWindowPos(IntPtr handle, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
105105

106106
[DllImport("user32.dll")]
107107
[return: MarshalAs(UnmanagedType.Bool)]
@@ -122,16 +122,16 @@ static class NativeMethods
122122
public static extern bool ChangeWindowMessageFilter(int msg, int flag);
123123

124124
[DllImport("user32.dll")]
125-
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, UInt32 wParam, UInt32 lParam);
125+
public static extern IntPtr SendMessage(IntPtr hWnd, int msg, uint wParam, uint lParam);
126126

127127
[DllImport("user32.dll")]
128-
public static extern IntPtr PostMessage(IntPtr hWnd, int msg, UInt32 wParam, UInt32 lParam);
128+
public static extern IntPtr PostMessage(IntPtr hWnd, int msg, uint wParam, uint lParam);
129129

130130
[DllImport("user32.dll")]
131131
public static extern IntPtr PostMessage(IntPtr hWnd, int msg, UInt64 wParam, UInt64 lParam);
132132

133133
[DllImport("user32.dll")]
134-
public static extern int SendMessageTimeout(IntPtr handle, int uMsg, UInt32 wParam, UInt32 lParam, SendMessageTimeoutFlags fuFlags, int uTimeout, out UInt32 lpdwResult);
134+
public static extern int SendMessageTimeout(IntPtr handle, int uMsg, uint wParam, uint lParam, SendMessageTimeoutFlags fuFlags, int uTimeout, out uint lpdwResult);
135135

136136
[DllImport("user32.dll")]
137137
public static extern IntPtr LoadIcon(IntPtr hInstance, string lpIconName);
@@ -154,19 +154,19 @@ static class NativeMethods
154154
public static extern bool CloseHandle(IntPtr hObject);
155155

156156
[DllImport("user32.dll", SetLastError = true)]
157-
public static extern UInt32 GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
157+
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
158158

159159
[DllImport("user32.dll", SetLastError = true)]
160-
public static extern UInt32 GetWindowThreadProcessId(IntPtr hWnd, IntPtr processId);
160+
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr processId);
161161

162162
[DllImport("kernel32.dll")]
163-
public static extern UInt32 GetCurrentThreadId();
163+
public static extern uint GetCurrentThreadId();
164164

165165
[DllImport("user32.dll")]
166166
public static extern IntPtr GetForegroundWindow();
167167

168168
[DllImport("user32.dll")]
169-
public static extern bool AttachThreadInput(UInt32 idAttach, UInt32 idAttachTo, bool fAttach);
169+
public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
170170

171171
[DllImport("user32.dll")]
172172
public static extern bool BringWindowToTop(IntPtr hWnd);
@@ -176,14 +176,32 @@ static class NativeMethods
176176
public static extern bool IsWow64Process(IntPtr hProcess, out bool wow64Process);
177177

178178
[DllImport("user32.dll", EntryPoint = "GetClassLong")]
179-
public static extern UInt32 GetClassLongPtr32(IntPtr hWnd, int nIndex);
179+
public static extern uint GetClassLongPtr32(IntPtr hWnd, int nIndex);
180180

181181
[DllImport("user32.dll", EntryPoint = "GetClassLongPtr")]
182182
public static extern IntPtr GetClassLongPtr64(IntPtr hWnd, int nIndex);
183183

184184
[DllImport("user32.dll")]
185185
public static extern IntPtr GetThreadDesktop(int threadId);
186186

187+
[DllImport("kernel32.dll", SetLastError = true)]
188+
public static extern bool AttachConsole(int processID);
189+
190+
[DllImport("kernel32.dll", SetLastError = true)]
191+
public static extern uint GetConsoleOutputCP();
192+
193+
[DllImport("kernel32.dll")]
194+
public static extern bool FreeConsole();
195+
196+
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
197+
public static extern bool ReadConsoleOutputCharacter(IntPtr hConsoleOutput, [Out] char[] lpCharacter, uint nLength, Coord dwReadCoord, out uint lpNumberOfCharsRead);
198+
199+
[DllImport("kernel32.dll", SetLastError = true)]
200+
public static extern bool GetConsoleScreenBufferInfo(IntPtr hConsoleOutput, out ConsoleScreenBufferInfo lpConsoleScreenBufferInfo);
201+
202+
[DllImport("kernel32.dll", SetLastError = true)]
203+
public static extern IntPtr GetStdHandle(int nStdHandle);
204+
187205
public static IntPtr GetClassLongPtr(IntPtr hWnd, int nIndex)
188206
{
189207
return IntPtr.Size > 4 ? GetClassLongPtr64(hWnd, nIndex) : new IntPtr(GetClassLongPtr32(hWnd, nIndex));

SmartSystemMenu/NativeTypes.cs

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,48 @@ enum Priority :int
6969
[StructLayoutAttribute(LayoutKind.Sequential)]
7070
struct MenuItemInfo
7171
{
72-
public UInt32 cbSize;
73-
public UInt32 fMask;
74-
public UInt32 fType;
75-
public UInt32 fState;
76-
public UInt32 wID;
72+
public uint cbSize;
73+
public uint fMask;
74+
public uint fType;
75+
public uint fState;
76+
public uint wID;
7777
public IntPtr hSubMenu;
7878
public IntPtr hbmpChecked;
7979
public IntPtr hbmpUnchecked;
80-
public UInt32 dwItemData;
80+
public uint dwItemData;
8181
public string dwTypeData;
82-
public UInt32 cch;
82+
public uint cch;
83+
}
84+
85+
[StructLayout(LayoutKind.Sequential)]
86+
struct Coord
87+
{
88+
public short X;
89+
public short Y;
90+
91+
public Coord(short x, short y)
92+
{
93+
X = x;
94+
Y = y;
95+
}
96+
};
97+
98+
[StructLayout(LayoutKind.Sequential)]
99+
struct SmallRect
100+
{
101+
public short Left;
102+
public short Top;
103+
public short Right;
104+
public short Bottom;
105+
}
106+
107+
[StructLayout(LayoutKind.Sequential)]
108+
struct ConsoleScreenBufferInfo
109+
{
110+
public Coord dwSize;
111+
public Coord dwCursorPosition;
112+
public ushort wAttributes;
113+
public SmallRect srWindow;
114+
public Coord dwMaximumWindowSize;
83115
}
84116
}

SmartSystemMenu/SmartSystemMenu.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@
7070
<Reference Include="System.Drawing" />
7171
<Reference Include="System.Windows.Forms" />
7272
<Reference Include="System.Xml" />
73+
<Reference Include="UIAutomationClient" />
74+
<Reference Include="UIAutomationClientsideProviders" />
75+
<Reference Include="UIAutomationProvider" />
76+
<Reference Include="UIAutomationTypes" />
7377
</ItemGroup>
7478
<ItemGroup>
7579
<Compile Include="Utils\AssemblyUtils.cs" />

SmartSystemMenu/SystemMenu.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public bool Exists
8787
public const int SC_ALIGN_CUSTOM = 0x4800;
8888
public const int SC_ALIGN_MONITOR = 0x4801;
8989
public const int SC_SAVE_SCREEN_SHOT = 0x4802;
90+
public const int SC_COPY_TEXT_TO_CLIPBOARD = 0x4803;
9091

9192
#endregion
9293

@@ -107,6 +108,7 @@ public void Create()
107108
NativeMethods.InsertMenu(windowMenuHandle, index + 1, NativeConstants.MF_BYPOSITION, SC_INFORMATION, "Information");
108109
NativeMethods.InsertMenu(windowMenuHandle, index + 2, NativeConstants.MF_BYPOSITION, SC_TOPMOST, "Always On Top");
109110
NativeMethods.InsertMenu(windowMenuHandle, index + 3, NativeConstants.MF_BYPOSITION, SC_SAVE_SCREEN_SHOT, "Save Window Screenshot");
111+
NativeMethods.InsertMenu(windowMenuHandle, index + 4, NativeConstants.MF_BYPOSITION, SC_COPY_TEXT_TO_CLIPBOARD, "Copy Text To Clipboard");
110112

111113
_sizeMenuHandle = NativeMethods.CreateMenu();
112114
NativeMethods.InsertMenu(_sizeMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_SIZE_640_480, "640x480");
@@ -126,7 +128,7 @@ public void Create()
126128
NativeMethods.InsertMenu(_sizeMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_SIZE_DEFAULT, "Default");
127129
NativeMethods.InsertMenu(_sizeMenuHandle, -1, NativeConstants.MF_BYPOSITION | NativeConstants.MF_SEPARATOR, 0, "");
128130
NativeMethods.InsertMenu(_sizeMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_SIZE_CUSTOM, "Custom...");
129-
NativeMethods.InsertMenu(windowMenuHandle, index + 4, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _sizeMenuHandle, "Resize");
131+
NativeMethods.InsertMenu(windowMenuHandle, index + 5, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _sizeMenuHandle, "Resize");
130132

131133
_alignmentMenuHandle = NativeMethods.CreateMenu();
132134
NativeMethods.InsertMenu(_alignmentMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_ALIGN_MONITOR, "Monitor");
@@ -144,7 +146,7 @@ public void Create()
144146
NativeMethods.InsertMenu(_alignmentMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_ALIGN_DEFAULT, "Default");
145147
NativeMethods.InsertMenu(_alignmentMenuHandle, -1, NativeConstants.MF_BYPOSITION | NativeConstants.MF_SEPARATOR, 0, "");
146148
NativeMethods.InsertMenu(_alignmentMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_ALIGN_CUSTOM, "Custom...");
147-
NativeMethods.InsertMenu(windowMenuHandle, index + 5, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _alignmentMenuHandle, "Alignment");
149+
NativeMethods.InsertMenu(windowMenuHandle, index + 6, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _alignmentMenuHandle, "Alignment");
148150

149151
_transparencyMenuHandle = NativeMethods.CreateMenu();
150152
NativeMethods.InsertMenu(_transparencyMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_TRANS_00, "0% (opaque)");
@@ -162,7 +164,7 @@ public void Create()
162164
NativeMethods.InsertMenu(_transparencyMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_TRANS_DEFAULT, "Default");
163165
NativeMethods.InsertMenu(_transparencyMenuHandle, -1, NativeConstants.MF_BYPOSITION | NativeConstants.MF_SEPARATOR, 0, "");
164166
NativeMethods.InsertMenu(_transparencyMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_TRANS_CUSTOM, "Custom...");
165-
NativeMethods.InsertMenu(windowMenuHandle, index + 6, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _transparencyMenuHandle, "Transparency");
167+
NativeMethods.InsertMenu(windowMenuHandle, index + 7, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _transparencyMenuHandle, "Transparency");
166168

167169
_priorityMenuHandle = NativeMethods.CreateMenu();
168170
NativeMethods.InsertMenu(_priorityMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_PRIORITY_REAL_TIME, "Real Time: 24");
@@ -171,12 +173,12 @@ public void Create()
171173
NativeMethods.InsertMenu(_priorityMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_PRIORITY_NORMAL, "Normal: 8");
172174
NativeMethods.InsertMenu(_priorityMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_PRIORITY_BELOW_NORMAL, "Below Normal: 6");
173175
NativeMethods.InsertMenu(_priorityMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_PRIORITY_IDLE, "Idle: 4");
174-
NativeMethods.InsertMenu(windowMenuHandle, index + 7, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _priorityMenuHandle, "Priority");
176+
NativeMethods.InsertMenu(windowMenuHandle, index + 8, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _priorityMenuHandle, "Priority");
175177

176178
_systemTrayMenuHandle = NativeMethods.CreateMenu();
177179
NativeMethods.InsertMenu(_systemTrayMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_MINIMIZE_TO_SYSTEMTRAY, "&Mimimize To Tray");
178180
NativeMethods.InsertMenu(_systemTrayMenuHandle, -1, NativeConstants.MF_BYPOSITION, SC_MINIMIZE_ALWAYS_TO_SYSTEMTRAY, "Mimimize To Tray Always");
179-
NativeMethods.InsertMenu(windowMenuHandle, index + 8, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _systemTrayMenuHandle, "System Tray");
181+
NativeMethods.InsertMenu(windowMenuHandle, index + 9, NativeConstants.MF_BYPOSITION | NativeConstants.MF_POPUP, _systemTrayMenuHandle, "System Tray");
180182
}
181183

182184
public void Destroy()

0 commit comments

Comments
 (0)