Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FindWindow() partial matches ?

1,177 views
Skip to first unread message

Jacques Cooper

unread,
Oct 13, 2002, 3:11:42 PM10/13/02
to
Hello,

I developed 2 MFC/SDI programs using VC++6.0.
One program is a graphics editor and the other
program is a text editor (the graphics editor launches
the text editor). I would like to close the text editor
when I close the graphics editor.

CWnd* pWnd = FindWindow(NULL,_T("Text Editor"));

if (pWnd== NULL)
AfxMessageBox("Program NOT FOUND!!");
else
pWnd->PostMessage(WM_USER_ON_CLOSE_TEDITOR,0,0l);

The problem is the window title of the text editor is always different
ie. "Text Editor - MyFile.txt", "Text Editor - Letter1.doc",....As such,
the text editor program is never "found", because the window title is not
an exact match.

Is there away to specify, in the FindWindow() function, to use partial
matching
(I know "Text Editor" will always be in the title)?

TIA,
Jacques


Nishant S

unread,
Oct 13, 2002, 3:25:08 PM10/13/02
to
Use EnumWindows and for each HWND returned get the title text.

Regards,
Nish [Visual C++ MVP]

"Jacques Cooper" <jco...@jcsoftware.net> wrote in message
news:On$p0wucCHA.2324@tkmsftngp08...

Scott McPhillips

unread,
Oct 13, 2002, 5:04:21 PM10/13/02
to

You can avoid this problem entirely by using the first parameter instead
of the second parameter in FindWindow. Each window has a windows class
name, which is a string (unrelated to C++ class names). You would need
to customize the winclass name of the target program's main window. To
do that register a unique name (make one up) in InitInstance by calling
AfxRegisterClass, then in PrecreateWindow of the mainframe feed your
winclass name to the cs.lpszClass parameter.

--
Scott McPhillips [VC++ MVP]

Yan-Hong Huang[MS]

unread,
Oct 13, 2002, 9:13:59 PM10/13/02
to
You can find window class name instead of its title. In this way, you can
find such windows without changing too much code.

Best regards,
yhhuang
VS.NET, Visual C++
Microsoft

This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net? http://www.gotdotnet.com

Jacques Cooper

unread,
Oct 13, 2002, 10:15:53 PM10/13/02
to
Thank you for responding to my posting. Registering the class worked great
and was easy to implement.

Tanks again,
Jacques

///////////////////////////
//PROGRAM 1:

BOOL CMyApp::InitInstance()
{

// Register your unique class name that you wish to use
WNDCLASS wndcls;

memset(&wndcls, NULL, sizeof(WNDCLASS)); // start with NULL

// Specify your own class name for using FindWindow later
wndcls.lpszClassName = _T("MyTextEditorClass");

// Register the new class and exit if it fails
if(!AfxRegisterClass(&wndcls))
{
TRACE("Class Registration Failed\n");
return FALSE;
}

[...]

}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;

// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.lpszClass = _T("MyTextEditorClass");

[...]

return true;
}

//////////////////////////////////////////////////////////////
//PROGRAM 2:

void CMainFrame::OnClose()
{
// TODO: Add your message handler code here and/or call default

CWnd* pWnd = FindWindow( _T("MyTextEditorClass"), NULL);

if (pWnd != NULL) pWnd->PostMessage(WM_CLOSE);

[...]

CFrameWnd::OnClose();
}

Nishant S

unread,
Oct 13, 2002, 10:49:57 PM10/13/02
to
Cool! BTW, instead of "MyTextEditorClass" you'd be better off using a GUID
[safer that way]

Regards,
Nish [Visual C++ MVP]

"Jacques Cooper" <jco...@jcsoftware.net> wrote in message

news:O9Ly0dycCHA.1992@tkmsftngp11...

0 new messages