Modify the path of the opened dialog box-Modify file attributes and find a specified window

Source: Internet
Author: User
The last little thing to do is to control another one. Program The Object Storage dialog box is designed to save only to the specified position: P. I want to control the stored data.
We know that cfiledialog is easy to set the initial browsing path of the dialog box. You only need to change lpstrinitialdir with your own directory in the openfilename structure of the cfiledialog class:
Cfiledialog DLG;
DLG. m_ofn.lpstrinitialdir = "C: \ Windows"; // set the browsing directory in the dialog box.
DLG. domodal ();
But what I want to control is a displayed dialog box. At this time, setting it with m_ofn.lpstrinitialdir will be ineffective! I found the handle of this dialog box through findwindow,
Cwnd * pwnd = findwindow (null, "Save ");
If (pwnd)
{
... // How can I change it here?
}
The first method is to use a hook to intercept the message when the dialog box is opened and change the ink recognition path before it is displayed. Well, I don't want to be so troublesome. I will try again later. Later, I found a convenient method from a reply.
In practice, if we enter a directory name in the file name editing box and press OK, the dialog box is not closed, but switched to the new directory. In this way, we can change the browsing directory of the displayed dialog box. The procedure is as follows:
1. First, save the value of the file name edit box.
2. Set the new directory name in the edit box.
3. Simulate the mouse and click "OK". At this time, the dialog box switches to the new directory.
4. restore the value of the original edit box.
Note that the wm_settext message must be sent when the directory is set in step 2, but the setwindowtext function cannot be used directly because it has problems during cross-process usage and has no effect.
The following are some key points: Code :
Cwnd * pwnd = findwindow (null, "Save ");
Cstring sctrlname;
Cwnd * pedit;
Cstring filename;
Tchar Buf [512];
If (pwnd)
{
Cwnd * pwnd = pwnd-> getwindow (gw_child); // enumerate all child controls in the SAVE dialog box
Char * Buf = new char [512];
While (pwnd! = NULL)
{
: Getclassname (pwnd-> getsafehwnd (), Buf, 512); // type of the enumerated control,
Cstring STR = Buf;
Str. trimright ();
If (STR = "edit") // if it is enumerated to the Edit
{
Pedit = pwnd; // Save the edit handle for sending messages
Pwnd-> sendmessage (wm_gettext, sizeof (BUF)/sizeof (tchar), (lparam) (void *) BUF );
Filename = Buf; // The first step is to save the original file name.
}
If (STR = "button") // enumerate to the button, which has the SAVE and cancel buttons.
{
Pwnd-> sendmessage (wm_gettext, sizeof (BUF)/sizeof (tchar), (lparam) (void *) BUF );
// Message sending to get the button title
Cstring STR = Buf;
Str. trimright ();
If (STR = "Save (& S)") // If the button is saved
{
Lstrcpy (BUF, (lpctstr) Spath); // Spath indicates the browsing directory of the dialog box to be set
// Set the file name Edit to our path name for sending messages.
Pedit-> sendmessage (wm_settext, 0, (lparam) BUF );
// Send a message and click the Save button.
: Sendmessage (pwnd-> getsafehwnd (), wm_lbuttondown, 0, 0 );
: Sendmessage (pwnd-> getsafehwnd (), wm_lbuttonup, 0, 0 );
}
}
}
Pwnd = pwnd-> getnextwindow ();
}
Pedit-> sendmessage (wm_settext, 0, (lparam) filename); // send a message to restore the file name in edit
Delete Buf;

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.