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,则选中标题。

// 编译时,别忘了链接User32.lib

#include<iostream>

#include <Windows.h>

#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 User32.lib.*/

int main(int argc, char* argv[])

{

EnumWindows(EnumWindowProc, NULL)

cout<<"Find "<<iCount <<" windows."<<endl

return 0

}

1. EnumThreadWindows

函数枚举所有与一个线程相关联的非子窗口,办法是先将句柄传送给每一个窗口,随后传送给应用程序定义的回调函数。EnumThreadWindows函数继续直到所有窗口枚举完为止或回调函数返回FALSE为止。要枚举一个特定窗口的所有子窗口,使用EnumChildWindows函数。

2. EnumWindows

该函数枚举所有屏幕上的顶层窗口,办法是先将句柄传给每一个窗口,然后再传送给应用程序定义的回调函数

GetCursorPos不管鼠标在哪里都可以获取位置的

问题是

只有OnMouseMove的时候才获取鼠标位置

但是OnMouseMove又是你对话框的成员函数

所以说

只有鼠标在对话框内移动才会显示坐标的

但是并不是没办法实现的

首先

你可以在OnInitDialog里设置一个定时器

CDialog::OnInitDialog()

//

Add

"About..."

menu

item

to

system

menu.

//

IDM_ABOUTBOX

must

be

in

the

system

command

range.

ASSERT((IDM_ABOUTBOX

&

0xFFF0)

==

IDM_ABOUTBOX)

ASSERT(IDM_ABOUTBOX

<

0xF000)

CMenu*

pSysMenu

=

GetSystemMenu(FALSE)

if

(pSysMenu

!=

NULL)

{

CString

strAboutMenu

strAboutMenu.LoadString(IDS_ABOUTBOX)

if

(!strAboutMenu.IsEmpty())

{

pSysMenu->AppendMenu(MF_SEPARATOR)

pSysMenu->AppendMenu(MF_STRING,

IDM_ABOUTBOX,

strAboutMenu)

}

}

//

Set

the

icon

for

this

dialog.

The

framework

does

this

automatically

//

when

the

application's

main

window

is

not

a

dialog

SetIcon(m_hIcon,

TRUE)

//

Set

big

icon

SetIcon(m_hIcon,

FALSE)

//

Set

small

icon

//

TODO:

Add

extra

initialization

here

SetTimer(1,100,NULL)//时间设置短一点

显示的也快点

return

TRUE

//

return

TRUE

unless

you

set

the

focus

to

a

control

然后为对话框添加WM_TIME消息

void

CAdcDlg::OnTimer(UINT

nIDEvent)

{

//

TODO:

Add

your

message

handler

code

here

and/or

call

default

POINT

pos

GetCursorPos(&pos)

//取鼠标的坐标

CString

str

str.Format("%d,%d",pos.x,pos.y)

m_dd=str

UpdateData(FALSE)

CDialog::OnTimer(nIDEvent)

}

m_dd为绑定在一个静态label空间上的CString

试试吧

这样就可以获得鼠标在任何时候的坐标了


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

原文地址:https://54852.com/yw/11139437.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存