The enumeration window is used to minimize the display of programs in the tray.

Source: Internet
Author: User

I have to write something to sum up my final gains after two days of being depressed ......

The purpose is simple. A specific applicationProgramMinimize to the tray, and I need to program the implementation to display its main window.

 

1. First, you can find the window handle through findwindow by directly operating the process, and then send it a showwindow message to display it. Class "# 32770" Is the program window class name observed with spy ++. Use

Hwnd =: findwindow (_ T ("#32770"), _ T (""));

: Showwindow (hwnd, sw_shownormal );

The obtained handle is always empty. Experiments show that the window handle cannot be captured once the program is minimized to the tray. (Use another program to enumerate the currently opened window: Lp)

2. After the failure, you can turn to the tray programming and consider obtaining the relevant information in the tray:

Standard trilogy:

// Obtain the taskbar window handle

Hwnd htaskbarwnd = findwindow ("shell_traywnd", null );

// Get the notification Area Window handle

Hwnd htraypolicywnd = find?wex (htaskbarwnd, null,

"Traypolicywnd", null );

// Obtain the handle of the system tray window

Hwnd htrayclockwnd = find1_wex (htraypolicywnd,

Null, "toolbarwindow32", null );

However, the third handle (that is, the tray handle) cannot be obtained on my computer ). However, if I use trayclockwclass, we can get it normally. They all belong to the same father. Why?

You can use

Postmessage (tb_hidebutton, index, (lparam) makelong (false, 0 ));

Sendmessage (tb_pressbutton, I, (lparam) makelong (true, 0. Of course, there are also tray refresh operations:

Void ctraybar: refresh ()

{

Notifyicondataw icon;

Icon. cbsize = sizeof (policyicondataw );

Icon. hwnd = This-> m_hwnd;

Icon. uid = 0;

Icon. uflags = nif_message | nif_tip;

Icon. ucallbackmessage = wm_user + 777;

Icon. hicon = NULL;

Shell_policyiconw (nim_add, & icon );

Shell_policyiconw (nim_delete, & icon );

}

3. after obtaining the handle of the notification window, you can obtain its position and then use each icon to take the 16*16 position to launch the icon position of my specific program, simulate the mouse clicking operation to display the window.CodeAs follows:

Void showmyapp ()

{

Hwnd htraywindow;

Rect rcttrayicon;

Int niconwidth;

Int niconheight;

Cpoint cursorpos;

Int nrow;

Int ncol;

 

// Get tray window handle and bounding rectangle

Htraywindow =: find1_wex (: findwindow (

"Shell_traywnd", null), 0, "traypolicywnd", null );

If (! : Getwindowrect (htraywindow, & rcttrayicon ))

Return;

// Cwnd * pwnd = cwnd: fromhandle (htraywindow); // htraywindow

// Get small icon metrics

Niconwidth = getsystemmetrics (sm_cxsmicon );

Niconheight = getsystemmetrics (sm_cysmicon );

 

Getcursorpos (& cursorpos );

 

For (nrow = 0; nrow <(rcttrayicon. bottom-rctTrayIcon.top)/niconheight; nrow ++)

{

For (ncol = 0; ncol <(rcttrayicon. right-rctTrayIcon.left)/niconwidth; ncol ++)

{

Setcursorpos (rcttrayicon. Left + ncol * niconwidth + 10,

Rcttrayicon. Top + nrow * niconheight + 10 );

Getcursorpos (& cursorpos );

Lparam = makelparam (cursorpos. X, cursorpos. Y); // coordinates of mouse clicks

Mouse_event (mouseeventf_leftdown, cursorpos. X, cursorpos. Y, 0, null );

Mouse_event (mouseeventf_leftup, cursorpos. X, cursorpos. Y, 0, null );

 

Sleep (0 );

}

}

}

This can indeed achieve my goal, but there is a major drawback that I must know the specific location of my application in the tray, the position of the program in the tray may be different after Windows is started. Mouse clicks can be simulated only when the position is correctly determined through programming. This method still does not work.

4. Return to the process method again. Use the following code to obtain all processes in the program, including those without window handles:

Handle getprocesshandle (int nid)

{

Return OpenProcess (process_all_access, false, NID );

}

 

Handle getprocesshandle (lpctstr pname)

{

Handle hsnapshot = createconlhelp32snapshot (th32cs_snapprocess, 0 );

If (invalid_handle_value = hsnapshot ){

Return NULL;

}

Processentry32 Pe = {sizeof (PE )};

Bool Fok;

For (FOK = process32first (hsnapshot, & PE); Fok = process32next (hsnapshot, & PE )){

Afxdump <PE. szexefile <"" <PE. th32processid <"\ n ";

If (! _ Tcscmp (PE. szexefile, pname )){

Closehandle (hsnapshot );

Return getprocesshandle (PE. th32processid );

}

}

Return NULL;

}

Void cpmtimerdlg: onbnclickedok ()

{

Handle Hd = getprocesshandle (_ T ("bubbles.exe "));

}

In this way, you can get the handle of the program you want, but you cannot get its hwnd or cwnd from this handle. Because a process may have multiple windows or no windows, ms does not provide more functions in this regard for our reference.

5. Through the enumeration window, this time the goal is finally achieved. Now, it takes one whole day and one night:

// If (! : Enumwindows (wndenumproc) enumproc, 0 ))

// Afxmessagebox ("error ");

Bool callback enumproc (hwnd, lparam)

{

If (hwnd = NULL)

Return false;

If (: iswindow (hwnd) // &: iswindowvisible (hwnd ))

{

Tchar szcap [255] = {0 };

:: Getwindowtext (hwnd, szcap, 255 );

If (strlen (szcap) = 0)

Return true;

Cstring STR;

Str. Format (_ T ("% s"), szcap );

If (Str. Find (_ T ("ubbles "))! =-1)

Showwindow (hwnd, sw_shownormal );

}

Return true;

}

Difficulties solved, and I will start my easy code journey ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.