GetSaveFileName弹出文件选择框居中显示

本文介绍了如何通过设置Win32 API的OPENFILENAME结构体及Hook函数,使得GetSaveFileName弹出的文件选择框在指定窗口居中显示。主要步骤包括设置OFN_ENABLEHOOK和OFN_EXPLORER标志,以及编写Hook回调函数WM_INITDIALOG,计算并调整对话框位置。
摘要由CSDN通过智能技术生成

传入的结构体参数:

OPENFILENAME ofn;

ZeroMemory(&ofn, sizeof(ofn));

ofn.lpstrFile = 初始文件名;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = _T("Text Files(*.txt)|*.txt|All Files(*.*)|*.*||");
ofn.lpstrDefExt = _T("txt");
ofn.lpstrTitle = _T("保存为");
ofn.hInstance = GetModuleHandle(NULL);
ofn.Flags = OFN_HIDEREADONLY | OFN_ENABLEHOOK | OFN_EXPLORER;
ofn.hwndOwner = 父窗口句柄;
ofn.FlagsEx = OFN_EX_NOPLACESBAR;
ofn.lpfnHook = OFNHookProc;
ofn.lpstrInitialDir = prtMainFrame->InitPaht.c_str();

ofn.lStructSize = sizeof(OPENFILENAME); 

// 让窗口居中显示,主要是设置Hook函数,在Flags标志中必须设置OFN_ENABLEHOOK | OFN_EXPLORER,具体的作用请看msdn

下面看一下Hook函数的编写方法:

UINT_PTR CALLBACK OFNHookProc(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
<

要实现WindTerm中rz命令弹出文件选择的功能,需要使用go语言编写一个WinAPI程序,并在WindTerm中调用它。以下是基本的代码示例: ```go package main import ( "fmt" "syscall" "unsafe" ) var ( comdlg32 = syscall.NewLazyDLL("comdlg32.dll") getOpenFileName = comdlg32.NewProc("GetOpenFileNameW") getSaveFileName = comdlg32.NewProc("GetSaveFileNameW") ole32 = syscall.NewLazyDLL("ole32.dll") coTaskMemFree = ole32.NewProc("CoTaskMemFree") shell32 = syscall.NewLazyDLL("shell32.dll") dragQueryFileW = shell32.NewProc("DragQueryFileW") dragFinish = shell32.NewProc("DragFinish") shellExecuteExW = shell32.NewProc("ShellExecuteExW") shellNotifyIconW = shell32.NewProc("Shell_NotifyIconW") shellGetImageLists = shell32.NewProc("SHGetImageList") user32 = syscall.NewLazyDLL("user32.dll") sendMessageW = user32.NewProc("SendMessageW") postMessageW = user32.NewProc("PostMessageW") ) type openfilename struct { structSize uint32 hwndOwner uintptr hInstance uintptr filter *uint16 customFilter *uint16 maxCustomFilter uint32 filterIndex uint32 file *uint16 maxFile uint32 fileTitle *uint16 maxFileTitle uint32 initialDir *uint16 title *uint16 flags uint32 fileOffset uint16 fileExtension uint16 defExt *uint16 custData uintptr hook uintptr templateName *uint16 reserved uintptr flagsEx uint32 } func rzFileDialog() (string, error) { var ofn openfilename ofn.structSize = uint32(unsafe.Sizeof(ofn)) ofn.flags |= 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 // OFN_EXPLORER | OFN_NOCHANGEDIR | OFN_ALLOWMULTISELECT | OFN_PATHMUSTEXIST ofn.filter = syscall.StringToUTF16Ptr("All Files (*.*)\x00*.*\x00\x00") ofn.file = syscall.StringToUTF16Ptr(string(make([]uint16, 8192))) ofn.maxFile = uint32(len([]uint16(*ofn.file))) ofn.fileTitle = syscall.StringToUTF16Ptr(string(make([]uint16, 8192))) ofn.maxFileTitle = uint32(len([]uint16(*ofn.fileTitle))) ret, _, _ := getOpenFileName.Call(uintptr(unsafe.Pointer(&ofn))) if ret == 0 { return "", syscall.GetLastError() } defer coTaskMemFree.Call(uintptr(unsafe.Pointer(ofn.file))) defer coTaskMemFree.Call(uintptr(unsafe.Pointer(ofn.fileTitle))) return syscall.UTF16ToString((*[8192]uint16)(unsafe.Pointer(ofn.file))[:]), nil } func main() { file, err := rzFileDialog() if err != nil { fmt.Println("Error:", err) return } fmt.Println("Selected file:", file) } ``` 在WindTerm中,可以使用以下命令来调用此程序: ``` rz | go run filedialog.go ``` 这将在WindTerm中启动rz命令,并弹出文件选择选择文件后,程序将在WindTerm中输出所选文件的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值