二次开发获取solidworks激活窗体的最后一个CommandTab所在屏幕上的位置
时间: 2024-05-20 17:17:31 浏览: 7
要获取SolidWorks激活窗体的最后一个CommandTab所在屏幕上的位置,可以使用以下代码:
1. 首先,获取SolidWorks主窗口的句柄:
IntPtr solidworksHandle = Process.GetProcessesByName("SLDWORKS")[0].MainWindowHandle;
2. 然后,使用Windows API函数FindWindowEx和SendMessage获取激活窗口的句柄和CommandTab的句柄:
IntPtr activateHandle = FindWindowEx(solidworksHandle, IntPtr.Zero, "SWChildFrame", null);
IntPtr commandTabHandle = SendMessage(activateHandle, WM_COMMAND, (IntPtr)ID_ACTIVATE_COMMAND_TAB, IntPtr.Zero);
其中,ID_ACTIVATE_COMMAND_TAB是SolidWorks API中定义的常量,表示激活CommandTab的命令。
3. 最后,使用Windows API函数GetWindowRect获取CommandTab的位置:
RECT rect;
GetWindowRect(commandTabHandle, out rect);
这样就可以获取CommandTab所在屏幕上的位置了。注意,需要引入Windows API函数的声明和定义。完整代码如下:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
const int WM_COMMAND = 0x111;
const int ID_ACTIVATE_COMMAND_TAB = 0xE901;
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
IntPtr solidworksHandle = Process.GetProcessesByName("SLDWORKS")[0].MainWindowHandle;
IntPtr activateHandle = FindWindowEx(solidworksHandle, IntPtr.Zero, "SWChildFrame", null);
IntPtr commandTabHandle = SendMessage(activateHandle, WM_COMMAND, (IntPtr)ID_ACTIVATE_COMMAND_TAB, IntPtr.Zero);
RECT rect;
GetWindowRect(commandTabHandle, out rect);
Console.WriteLine($"CommandTab position: ({rect.left}, {rect.top})");
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)