3 个解决方案
#1
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
TCHAR szClassName[] = _T("ConsoleWindowClass");
HWND hwnd;
RECT rect;
hwnd = NULL;
do
{
hwnd = FindWindowEx(NULL,hwnd,szClassName,NULL);
if (hwnd != NULL)
{
GetWindowRect(hwnd,&rect);
printf("hwnd = %u (%d,%d)--(%d,%d)\n",
hwnd,rect.left,rect.top,rect.right,rect.bottom);
}
}while(hwnd != NULL);
printf("Hello World!\n");
return 0;
}
#include <tchar.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
TCHAR szClassName[] = _T("ConsoleWindowClass");
HWND hwnd;
RECT rect;
hwnd = NULL;
do
{
hwnd = FindWindowEx(NULL,hwnd,szClassName,NULL);
if (hwnd != NULL)
{
GetWindowRect(hwnd,&rect);
printf("hwnd = %u (%d,%d)--(%d,%d)\n",
hwnd,rect.left,rect.top,rect.right,rect.bottom);
}
}while(hwnd != NULL);
printf("Hello World!\n");
return 0;
}
#2
GetWindowRect(hwnd,&rect);
ClientToScreen(hwnd,&rect); // 关键点,转换为屏幕坐标
ClientToScreen(hwnd,&rect); // 关键点,转换为屏幕坐标
#3
试试这个,不太完善,每次都打出两遍坐标(如果开多个Cmd窗口则更多),不过打出来的第一个应该就是当前窗口坐标了,因为FindWindowEx是从最顶层窗口开始搜索……
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
[StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
unsafe class Program
{
static unsafe void Main(string[] args)
{
Console.Title = "MyProgram";
string szClassName = "ConsoleWindowClass";
IntPtr hwnd = IntPtr.Zero;
RECT rect = new RECT();
do
{
hwnd = FindWindowEx(IntPtr.Zero, hwnd, szClassName, Console.Title);
if (hwnd != null)
{
GetWindowRect(hwnd, &rect);
Console.WriteLine(rect.left + " " + rect.top);
}
}
while (hwnd != IntPtr.Zero);
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetWindowRect(IntPtr hWnd, RECT* lpRect);
}
}
#1
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
TCHAR szClassName[] = _T("ConsoleWindowClass");
HWND hwnd;
RECT rect;
hwnd = NULL;
do
{
hwnd = FindWindowEx(NULL,hwnd,szClassName,NULL);
if (hwnd != NULL)
{
GetWindowRect(hwnd,&rect);
printf("hwnd = %u (%d,%d)--(%d,%d)\n",
hwnd,rect.left,rect.top,rect.right,rect.bottom);
}
}while(hwnd != NULL);
printf("Hello World!\n");
return 0;
}
#include <tchar.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
TCHAR szClassName[] = _T("ConsoleWindowClass");
HWND hwnd;
RECT rect;
hwnd = NULL;
do
{
hwnd = FindWindowEx(NULL,hwnd,szClassName,NULL);
if (hwnd != NULL)
{
GetWindowRect(hwnd,&rect);
printf("hwnd = %u (%d,%d)--(%d,%d)\n",
hwnd,rect.left,rect.top,rect.right,rect.bottom);
}
}while(hwnd != NULL);
printf("Hello World!\n");
return 0;
}
#2
GetWindowRect(hwnd,&rect);
ClientToScreen(hwnd,&rect); // 关键点,转换为屏幕坐标
ClientToScreen(hwnd,&rect); // 关键点,转换为屏幕坐标
#3
试试这个,不太完善,每次都打出两遍坐标(如果开多个Cmd窗口则更多),不过打出来的第一个应该就是当前窗口坐标了,因为FindWindowEx是从最顶层窗口开始搜索……
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication4
{
[StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
unsafe class Program
{
static unsafe void Main(string[] args)
{
Console.Title = "MyProgram";
string szClassName = "ConsoleWindowClass";
IntPtr hwnd = IntPtr.Zero;
RECT rect = new RECT();
do
{
hwnd = FindWindowEx(IntPtr.Zero, hwnd, szClassName, Console.Title);
if (hwnd != null)
{
GetWindowRect(hwnd, &rect);
Console.WriteLine(rect.left + " " + rect.top);
}
}
while (hwnd != IntPtr.Zero);
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool GetWindowRect(IntPtr hWnd, RECT* lpRect);
}
}