具体代码:
/** _NET_WM_NAME: 类似Window: 126 WM_NAME: XStoreName设置的值 */ bool get_window_name(Display* dpy, Window window, char* pName) { #if 1 char **srname = (char **)malloc(sizeof(char *)); XFetchName(dpy, window, srname); printf("%s-%d %s\n", __func__, __LINE__, srname[0]); #else //尝试使用_NET_WM_NAME方式获取窗口的名称 XTextProperty tp; Status s1 = XGetTextProperty(dpy,window,&tp,XInternAtom(dpy, "WM_NAME", False)); if (tp.nitems <= 0) { return false; } int count = 0; char **list = NULL; int ret = XmbTextPropertyToTextList(dpy, &tp, &list, &count); if ((ret == Success || ret > 0) && list != NULL) { for (int i=0; i<count; i++) { sprintf(pName, "%s", list[i]); } XFreeStringList(list); } else { sprintf(pName, "%s", tp.value); } #endif return true; }