c 获取本身程序窗口的标题

c 获取本身程序窗口的标题,第1张

findwindow 的两个参数是输入参数,所以不能解决问题。

如果你在自己程序中获取标题,你可以用 MFC

CWnd::GetWindowText

void GetWindowText( CString& rString ) const;

或API

int GetWindowText( HWND hWnd, LPTSTR lpString, int nMaxCount );

hWnd 用 NULL 或 this 试试。

程序以外参考下面方法列出所有窗口的szClass,和 标题,

对比 你要得程序名,若等于 szClass,则选中标题。

// 编译时,别忘了链接User32lib

#include<iostream>

#include <Windowsh>

#include <iomanip>

using namespace std;

int iCount=0;

BOOL CALLBACK EnumWindowProc(HWND hWnd, LPARAM lParam)

{

TCHAR szClass[256]={0};

TCHAR szWindow[256]={0};

::GetWindowText(hWnd, szWindow, 255);

::GetClassName(hWnd, szClass, 255);

if ( ::IsWindow(hWnd) &&

::IsWindowVisible(hWnd) &&

(::GetWindowLong(hWnd, GWL_EXSTYLE)&WS_EX_TOOLWINDOW)!

=WS_EX_TOOLWINDOW &&

::GetWindowLong(hWnd, GWL_HWNDPARENT)==0 )

{

int i=strlen(szClass);

cout << "["<< szClass <<setw(15-strlen(szClass))<< "] "<< szWindow << endl;

iCount++;

}

return TRUE;

}

/ main User32lib/

int main(int argc, char argv[])

{

EnumWindows(EnumWindowProc, NULL);

cout<<"Find "<< iCount << " windows"<<endl;

return 0;

}

它是一个子窗口,没有标题,每次登陆时类名就变了,怎么得到它的窗口句柄呢?用Findwindow没用啊!

你是什么语言??VB还是C++?

给你个VB CODE :

Function GetQQpath()

Dim ps, s '获取QQ进程所存在的路径

s = "qqexe"

For Each ps In GetObject("winmgmts:\\\root\cimv2:win32_process")instances_ '循环进程

If UCase(psName) = UCase(s) Then

Form1Label1Caption = psexecutablepath

GoTo q

End If

Next

q:

End Function

这个可以用知道进程名就可以知道程序路径了,原理很简单。自己摸索、

(针对新手: 结果显示在label1上面。否则会出现缺少对象)

我查了下后发现,这个Crash的进程是个C#的窗口进程,其中有个窗口属性ShowInTaskBar == False,这样该进程就不会再显示在任务栏上了。但是由于我们的产品有一套自己的Crash处理机制,能够根据窗口Title判断是否重起整个软件以保证后续功能不受影响。调查代码后发现是Net的API不支持获取窗口的Tilte了,这是微软的bug:ProcessGetProcessById(processId);如果ShowInTaskBar = True, 窗口的Title,Handle都可以正常拿到。如果ShowInTaskBar = False,窗口的Title,Handle都是Null 所以原有的机制无法正常工作了。通过查询MSDN,寻找到如下Win32 API来获取窗口Title:EnumWindows(); //系统会遍历所有的窗口,通过它调用并传递Handle给你的CallBack FunctionGetWindowText(); --------------------------------------------------------------------------------------下面是简单使用范例(C#)usingSystemRuntimeInteropServices;publicclassTest{ [DllImportAttribute("user32dll")] publicstaticexternintGetWindowThreadProcessId(IntPtrhwnd,outintpid); //delegate function declaration publicdelegateboolEnumWinCallBack(IntPtrhwnd,intlParam); [DllImportAttribute("user32dll")] publicstaticexternIntPtrEnumWindows(EnumWinCallBackcback,intlParam); [DllImportAttribute("user32dll")] publicstaticexternintGetWindowText(IntPtrhwnd,StringBuilderstrTitle,intnMaxCount); public void int fun1() { EnumWinCallBackwinCallBack=newEnumWinCallBack(TestGetWindowTitle); //这个函数会调用你提供的CallBack func,逐个传递所有的窗口的hwnd EnumWindows(winCallBack, 0); } //Here is your call back function public staticboolGetWindowTitle(IntPtrhwnd,intlParam) { //hwnd就是窗口的Handle了,有了它,很多针对窗口的 *** 作都可以正常进行了 StringBuilderwinTitle=newStringBuilder(100); GetWindowText(hwnd,winTitle,winTitleCapacity); }} 这样就可以拿到所有的窗口handle,通过相应的API进行 *** 作了。

以上就是关于c 获取本身程序窗口的标题全部的内容,包括:c 获取本身程序窗口的标题、通过api获取窗口句柄是通过类名和标题得到,如果我软件标题不固定。还有什么其它的方法得到窗口句柄、[C#]如何获取窗口Title当ShowInTaskBar==False等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9291880.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-26
下一篇2023-04-26

发表评论

登录后才能评论

评论列表(0条)

    保存