API通用对话框

#include<windows.h>
#include<commdlg.h>
#include<stdio.h>
#include<stdlib.h>
#include"7_5.h"


OPENFILENAME ofn;//定义一个OPENFILENAME结构
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInst,LPSTR lpszCmdLine,int nCmdShow)
{
 HWND hwnd;
 MSG Msg;
 HACCEL hAccel;
 WNDCLASS wndclass;
 char lpszMenuName[]="Menu";
 char lpszClassName[]="通用对话框";
 char lpszTitle[]="通用对话框样例";

 wndclass.style=0;
 wndclass.lpfnWndProc=WndProc;
 wndclass.cbClsExtra=0;
 wndclass.cbWndExtra=0;
 wndclass.hInstance=hInstance;
 wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
 wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
 wndclass.hbrBackground=GetStockObject(WHITE_BRUSH);
 wndclass.lpszMenuName=lpszMenuName;
 wndclass.lpszClassName=lpszClassName;


 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;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
 static char szFilter[]="Text Files(*.TXT)/0*.txt/0"/"All Files(*.*/0*.*/0/0";
 static char lpstrFileName[]="";
 switch(message)
 {
 case WM_CREATE:

  //初始化对话框结构
  ofn.lStructSize=sizeof(OPENFILENAME);
  ofn.hwndOwner=hwnd;
  ofn.hInstance=NULL;
  ofn.lpstrFilter=szFilter;
  ofn.lpstrCustomFilter=NULL;
  ofn.nMaxCustFilter=0;
  ofn.nFilterIndex=1;
  ofn.lpstrFile=NULL;
  ofn.nMaxFile=0;
  ofn.lpstrFileTitle=NULL;
  ofn.nMaxFileTitle=0;
  ofn.lpstrInitialDir=NULL;
  ofn.lpstrTitle=NULL;
  ofn.Flags=0;
  ofn.nFileOffset=0;
  ofn.nFileExtension=0;
  ofn.lpstrDefExt=NULL;
  ofn.lCustData=0;
  ofn.lpfnHook=NULL;
  ofn.lpTemplateName=NULL;

  break;

 case WM_COMMAND:
  switch(LOWORD(wParam))
  {
  case IDM_NEW:
   break;
  case IDM_OPEN:
   //设置打开对话框
   ofn.lpstrFile=lpstrFileName;
   ofn.Flags=OFN_FILEMUSTEXTST|OFN_PATHMUSTEXTST|OFN_HIDEREADONLY;
   ofn.lpstrTitle="打开";
   //调用函数GetOpenFileName显示对话框
   GetOpenFileName(&ofn);
   //文件读写操作
   break;
  case IDM_SAVEAS:
   //设置"另存为"对话框
   ofn.lpstrFile=lpstrFileName;
   ofn.Flags=OFN_FILEMUSTEXTST|OFN_PATHMUSTEXTST
    |OFN_HIDEREADONLY;
   ofn.lpstrTitle="另存为";
   //调用函数GetSaveFileName显示对话框
   GetSaveFileName(&ofn);
   break;
  case IDM_CLOSE:
   break;
  case IDM_EXIT:
   SendMessage(hwnd,WM_DESTROY,0,0);
   brak;
  case IDM_ABOUT:
   MessageBox(hwnd,"通用对话框示例","关于",MB_OK);
   break;
  }
  break;

  case WM_DESTROY:
   PostQuitMessage(0);
   break;

  default:
   return DefWindowProc(hwnd,messasge.wParam,lParam);
 }
 return 0;

 

 

你可能感兴趣的:(API通用对话框)