c ++如何初始化打开文件对话框(GetOpenFileName)

时间:2013-07-20 15:08:09

标签: c++ c winapi mingw

我的代码如下。

OPENFILENAMEA open;
ZeroMemory(&open, sizeof(open));

open.lStructSize = sizeof(LPOPENFILENAMEA);
open.lpstrFilter = "Képek\0*.jpg;*.jpeg;*.gif;*.png;*.bmp\0\0";
open.nFileOffset = 1;
open.lpstrFile[0] = '\0';
open.nMaxFile = 2048;
open.lpstrTitle = "Képek kiválasztása..";
open.Flags = OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST;

OPENFILENAME selected = GetOpenFileName(open);

我的问题是,在尝试构建时出现以下错误: 当我致电error: cannot convert 'OPENFILENAME {aka tagOFNA}' to 'LPOPENFILENAMEA {aka tagOFNA*}' for argument '1' to 'BOOL GetOpenFileNameA(LPOPENFILENAMEA)'

GetOpenFileName

如果我使用open参数作为ptr GetOpenFileName(&open)来调用它,我会得到以下error: conversion from 'BOOL {aka int}' to non-scalar type 'OPENFILENAME {aka tagOFNA}' requested

问题:做什么?

2 个答案:

答案 0 :(得分:3)

GetOpenFileName返回BOOL,而不是OPENFILENAME

如果用户选择文件并单击“确定”返回,则返回非零值。如果他们点击“取消”,它将返回0.

如果返回true,则会修改open的内容以反映用户选择的内容。

所以,你通常使用它:

if (GetOpenFileName(&open)) {
    // use open.whatever to get data about the selected file
}
else
   // The user clicked cancel -- typically do nothing.

答案 1 :(得分:1)

试试这个:

BOOL selected = GetOpenFileName(&open);

我不熟悉WinAPI,但请查看错误消息:

  

错误:无法将'OPENFILENAME {aka tagOFNA}'转换为'LPOPENFILENAMEA {aka tagOFNA *}'以将参数'1'转换为'BOOL GetOpenFileNameA(LPOPENFILENAMEA)'

表示您应该将指向OPENFILENAME的指针作为参数传递给GetOpenFileName

  

错误:从'BOOL {aka int}'转换为非标量类型'OPENFILENAME {aka tagOFNA}''

表示GetOpenFileName的返回类型应为BOOL