通用对话框资源问题??请高手解决
本人正在自学API编程~看的书是《VC++面向对象与可视化程序设计》,到了通用对话框资源,照着书上的例题输进VC++6.0,static char szFilter[]="Text Files(*.TXT)\0*.txt\0"程序可以成功编译,但是运行后点击打开或另存为没有原先预想的打开窗口或另存为窗口出现,动态调试可以看到正常经过GetOpenFileName(&ofn)函数,但是却没有响应,我把有疑问的语句都用颜色标出来了,请教各位大哥上面的文件类型应该怎样设置,打开或另存为对话框为什么显示不了~在这里先谢谢大家了~
下面的源代码,只实现个菜单和通用对话框功能而已,麻烦大家了~
//例7_5.c
#include <windows.h>
#include "例7_5.h"
#include <stdlib.h>
#include <stdio.h>
#include <commdlg.h>
OPENFILENAME ofn;//定义一个OPENFILENAME结构
HINSTANCE hInst;
BOOL CALLBACK DlgProc(HWND hdlg,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
return 1;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hdlg,0);
return 1;
}
break;
case WM_CLOSE:
EndDialog(hdlg,0);
return 1;
}
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
static char szFilter[]="Text Files(*.TXT)\0*.txt\0";
static char lpstrFileName[]=" ";
switch(message)
{
case WM_CREATE: //初始化对话框结构
ofn.Flags=0;
ofn.hInstance=NULL;
ofn.hwndOwner=hwnd;
ofn.lCustData=0;
ofn.lpfnHook=NULL;
ofn.lpstrCustomFilter=NULL;
ofn.lpstrDefExt=NULL;
ofn.lpstrFile=NULL;
ofn.lpstrFileTitle=NULL;
ofn.lpstrFilter=szFilter;
ofn.lpstrInitialDir=NULL;
ofn.lpstrTitle=NULL;
ofn.lpTemplateName=NULL;
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.nFileExtension=0;
ofn.nFileOffset=0;
ofn.nFilterIndex=1;
ofn.nMaxCustFilter=0;
ofn.nMaxFile=0;
ofn.nMaxFileTitle=0;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDM_OPEN: //设置“打开”对话框
ofn.lpstrFile=lpstrFileName;
ofn.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY;
ofn.lpstrTitle="打开";
GetOpenFileName(&ofn); //调用函数GetOpenFileName显示对话框
break;
case IDM_SAVEAS: //文件读写操作
ofn.lpstrFile=lpstrFileName; //设置“另存为”对话框
ofn.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT;
ofn.lpstrTitle="另存为";
GetSaveFileName(&ofn); //调用函数GetSaveFileName显示对话框
break;
case IDM_CLOSE:
SendMessage(hwnd,WM_DESTROY,0,0);
break;
case IDM_ABOUT:
MessageBox(hwnd,"通用对话框示例","关于",MB_OK);
break;
case IDM_EXIT:
SendMessage(hwnd,WM_DESTROY,0,0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLind,int nCmdShow)
{
HWND hwnd;
MSG msg;
HACCEL hAccel;
WNDCLASS wndclass;
char lpszMenuName[]="Menu";
char lpszClassName[]="通用对话框";
char lpszTitle[]="通用对话框样例";
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=lpszClassName;
wndclass.lpszMenuName=lpszMenuName;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
}
hwnd=CreateWindow(
lpszClassName,
lpszTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
hInst=hInstance;
hAccel=LoadAccelerators(hInst,lpszMenuName);
while(GetMessage(&msg,NULL,0,0))
{
if(!TranslateAccelerator(hwnd,hAccel,&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//"例7_5.h"
#define IDM_OPEN 15
#define IDM_SAVEAS 11
#define IDM_EXIT 12
#define IDM_ABOUT 13
#define IDM_CLOSE 14
//rc资源代码#include<windows.h>
#include"例7_5.h"
MENU MENU DISCARDABLE
{
POPUP "文件(&F)"
{
MENUITEM "打开(&O)\t Ctrl+O", IDM_OPEN
MENUITEM SEPARATOR
MENUITEM "另存为(&S)\t Ctrl+S", IDM_SAVEAS
MENUITEM SEPARATOR
MENUITEM "退出(&X)\t Ctrl+X", IDM_EXIT
}
POPUP "帮助(&H)"
{
MENUITEM "关于(&A)\t Ctrl+A", IDM_ABOUT
}
}
MENU ACCELERATORS
{
"^O", IDM_OPEN, ASCII
"^S", IDM_SAVEAS, ASCII
"^X", IDM_EXIT, ASCII
"^A", IDM_ABOUT, ASCII
}