How to implement a preview function when opening an image file in VB

zhaozj2021-02-11  161

Keywords: VB preview

A problem in the VB expert clinic, how to implement the preview of the image file in VB, although given 300 points, the answer is very few. I realized some image preview function in VB after referring to the source code of Delphi, and test it under Chinese Windows98 SE.

From the MSDN, you can know that the call file opens the common dialog to call the API function getopenFileName, the original shape is as follows:

Bool getopenfilename (LPopenFileName LPOFN); // LPOFN is the address of the initial data structure

Its parameter LPOFN points to the address of the type OpenFileName variable, Windows has left the interface for our implementation of custom file open dialog. In order to implement this custom dialog, the following parameters are set in OpenFileName:

Flags off_enablehook The hook function specified by the LPFNHOOK member is valid OFN_EnableTemplate indicates that a dialog template resource is specified by lptemplatename, which exists in the module specified by Hinstance, if the above two flags must be specified, this flag LPFNHOOK points to the hook function Address LPTemplatename dialog template resource string name, not ID

That is to say, you have to include a dialog template in your program, and Windows will display a common dialog in this template. From a lot of information, you can know if you want to display a common dialog in your custom dialog box, you must include a Static in your dialog, it's STC32, the decimal value is 1119, this Static is also displayed The common dialog is placed. Of course, he also has some requirements for the dialog, it is not nonsense, and you will see MSDN.

We make this dialog in a environment where you can make a dialog resource (I use VC), and save it as a res file. From here, we entered the VB IDE, add this resource file to your VB application, assume that its name is "DLGoPENTEMP".

Next, we add a Form in the VB project, assume that its name is FRMPREVIEW, set its borderStyle to NONE. And add a PictureBox or Image to the form, name is called Picture1, and we will display the preview in it. The next thing is the most critical step, write hook.

Add a Module in the project, the name doesn't matter. Suppose our hook calls 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 10) * 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 Frm Preview Case Else End Select FunctionLbleXit: End Function Add a method LOADPIC in FRMPREVIEW, this method displays the identifiable image file selected in the FRMPREVIEW in Picture1, how to show yourself. When getting cDN_Selchange, call getFilesName to get the file selected at this time, the code is as follows:

Private Function GetFilesName (hWindow As Long) As String Dim hParent As Long Dim lRetValue As Long Dim theFileName (1024) As Byte For i = 0 To UBound (theFileName) theFileName (i) = 0 Next hParent = GetParent (hWindow) lRetValue = SendMessage (HParent, CDM_GETFILEPATH, 1024, THEFILENAME (0)) getFilesName = strconv (thefilename, vbunicode) End Function

The method of calling the getopenfilename API is as follows:

Public Function Showopenfiledlg (HParent As Long) AS Long On Error Goto Lblexit Static Strfilter As String Strfilter = "All Pictures" & Chr (0) & "* .bmp; *. Dib; *. Jpg; *. Gif; *. WMF ; *. EMF; *. ICO; *. CUR "& chr (0) & _" Bitmap (* .bmp; * .b) "& chr (0) &" * .bmp; *. dib "& chr ( 0) & _ "JPEG (* .jpg)" & chr (0) & "* .jpg" & chr (0) & _ "GIF (* .gif)" & chr (0) & "* .gif" & CHR (0) & _ "Metafile (* .wmf; *. EMF)" & chr (0) & "* .wmf; *. EMF" & chr (0) & _ "icons (* .ico; *. CUR ) "& Chr (0) &" * .ico; *. Cur "& chr (0) & _" all files (*. *) "& Chr (0) &" *. * "& Chr (0) & Chr (0) FrmPreview.Hide pointer = GetProcAddr (AddressOf wndProc) Dim MyID As Long With openfile .lStructSize = Len (openfile) .hwndOwner = hParent .hInstance = App.hInstance .lpstrFilter = strFilter .lpstrCustomFilter = "" .nMaxCustFilter = 0 .nfilterindex = 0 .lpstrfile = filesname .nmaxfile = 1023 .lpstrfileti = "" "" "" "" "" "" nitialDir = "" .lpstrTitle = "" .flags = OFN_ENABLEHOOK OFN_HIDEREADONLY OFN_ENABLETEMPLATE OFN_EXPLORER .nFileOffset = 0 .nFileExtension = 0 .lpstrDefExt = "" .lCustData = 0 .lpfnHook = pointer .lpTemplateName = "DLGOPENTEMP" End With ShowOpenFileDlg = GetopenFileName (OpenFile) Lblexit: End Function

The file is placed in the filesname, which must be pre-assigned a good space. Soryy and said nothing. These code did not have a strict test, just achieved a simple function, there is certainly a lot of bugs, if a brothers and sisters discover or give debug, don't forget to give my sister. My email address is wjxcn@263.net.

转载请注明原文地址:https://www.9cbs.com/read-5969.html

New Post(0)