A while ago someone in VB outpatient expert, put forward a question, how to realize the preview of open image file in VB, although give a 300 points high score, answer is very few. I reference the Delphi source code in VB in the realization of its part of the image preview function, in Chinese WINDOWS98 se test Pass.
From MSDN you can know that call File Open Common dialog box calls API function GetOpenFileName, the prototype is as follows:
BOOL GetOpenFileName (Lpopenfilename lpofn);//Lpofn The address of the initialization data structure
Its parameter lpofn points to an address of type openfilename variable, Windows has left an interface for our custom File Open dialog box. To implement this custom dialog box, focus on setting the following parameters in OpenFileName:
| Flags |
Ofn_enablehook to make the hook function specified by the Lpfnhook member valid |
| |
Ofn_enabletemplate indicates that the Lptemplatename specifies a dialog template resource that exists in the module specified by hinstance |
|
Ofn_explorer You must specify this flag if you specify both of these flags |
| Lpfnhook |
Address to hook function |
| Lptemplatename |
The string name of the dialog template resource, not the ID |
This means that you want to include a dialog box template in your program, and Windows will display the common dialog box based on this template. You can tell from a lot of stuff. If you want to display a common dialog box in a custom dialog box, you must include a static in your dialog resource, its ID is STC32, the decimal value is 1119, and this Static is where the original common dialog box appears. Of course he has some requirements for the dialog box, here is no nonsense, look at MSDN yourself.
We made this dialog in an environment where we could make dialog resources (VC) and save it as a res file. From here we go into the VB IDE, add this resource file to your VB application, assuming its name is "Dlgopentemp".
Next we add a form to the VB project, assuming its name is Frmpreview, and set its borderstyle to none. and add a PictureBox or image to the form, the name is called Picture1 Bar, we will display the preview inside it. Below that is the most important step, write hook.
To add a module to the project, the name doesn't matter. Suppose our hook is called WndProc, defined as follows:
Public Function WndProc (ByVal hdlg as Long, ByVal uimsg as Long, ByVal WParam as Long, ByVal LParam as long) as long
On Error GoTo Lblexit
Select Case Uimsg
Case Wm_notify
CopyMemory Nmheader, ByVal LParam, Len (Nmheader)
Select Case Nmheader.code
Case Cdn_initdone
GetWindowRect GetDlgItem (hdlg, 1119), Staticrect
MapWindowPoints 0, Hdlg, Staticrect, 2
SetParent Frmpreview.hwnd, Hdlg
Frmpreview.visible = True
Frmpreview.move (Staticrect.right-staticrect.left +) * Screen.twipsperpixelx, 5 * screen.twipsperpixelx
Frmpreview.refresh
WndProc = 0
Case Cdn_selchange
Frmpreview.loadpic Getfilesname (HDLG)
WndProc = 0
End Select
Case Wm_destroy
Frmpreview.visible = False
SetParent Frmpreview.hwnd, 0
Unload Frmpreview
Case Else
End Select
Exit Function
Lblexit:
End Function