mfc 打/保存文件对话框 打开目录对话框 gdal基础

// newgdalDlg.cpp : implementation file
//

#include "stdafx.h"
#include "newgdal.h"
#include "newgdalDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CNewgdalDlg dialog

CNewgdalDlg::CNewgdalDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CNewgdalDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CNewgdalDlg)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CNewgdalDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CNewgdalDlg)
        // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNewgdalDlg, CDialog)
    //{{AFX_MSG_MAP(CNewgdalDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_Ok, OnOk)
    ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
    ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
    ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNewgdalDlg message handlers

BOOL CNewgdalDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    // TODO: Add extra initialization here
    
    return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CNewgdalDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNewgdalDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

void CNewgdalDlg::OnOK()
{
    // TODO: Add extra validation here
    
    CDialog::OnOK();
}

void CNewgdalDlg::OnOk()
{
    // TODO: Add your control notification handler code here
    int i, xSize, ySize;  
   //开始调用GDAL  
     GDALDataset *poDataset;  
    //注册  
     GDALAllRegister();  
   
    double geoInfo[6];  

    //CString FilePathName;
     //获得EDIT
    CEdit* pBoxOne;
    pBoxOne = (CEdit*) GetDlgItem(IDC_EDIT1);
     //赋值
    //pBoxOne->SetWindowText(FilePathName);
     //取值
     CString str;
     pBoxOne->GetWindowText(str);


    //打开文件获取数据集  
    //(GDALDataset *)与(GDALDataset* )都可以
    poDataset = (GDALDataset *)GDALOpen(str, GA_ReadOnly);  
 
     if(poDataset != NULL)  
     {  
         poDataset->GetRasterBand(1)->GetNoDataValue(&i);  
 
         //获取图像的长宽像素值  
         xSize = poDataset->GetRasterXSize();  
         ySize = poDataset->GetRasterYSize();  
         poDataset->GetGeoTransform(geoInfo);  
        
         //AfxMessageBox("good");
         //    geoInfo[0] /* top left x, 图像左上角x坐标值*/  
         //    geoInfo[1] /* w-e pixel resolution,图像横坐标 ?米/每像素 */  
        //    geoInfo[2] /* rotation, 0 if image is "north up" */  
        //    geoInfo[3] /* top left y 图像左上角y坐标值*/  
         //    geoInfo[4] /* rotation, 0 if image is "north up"  */  
         //    geoInfo[5] /* n-s pixel resolution 图像纵坐标 ?米/每像素*/  
     }  
     else  
    {  
        exit(1);  
     }
    
     CString strxSize;
     strxSize.Format("%d",xSize);

     //获得EDIT
    CEdit* pBoxOne3;
    pBoxOne3 = (CEdit*) GetDlgItem(IDC_EDIT3);
     //赋值
    pBoxOne3->SetWindowText(strxSize);


     CString strySize;
     strySize.Format("%d",ySize);

     //获得EDIT
    CEdit* pBoxOne4;
    pBoxOne4 = (CEdit*) GetDlgItem(IDC_EDIT4);
     //赋值
    pBoxOne4->SetWindowText(strySize);

    
}

void CNewgdalDlg::OnButton3()
{
    // TODO: Add your control notification handler code here
   //AfxMessageBox("good");


    
}



void CNewgdalDlg::OnButton1()
{
    // TODO: Add your control notification handler code here
    CString FilePathName;
    CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为SAVE AS对话框
    if(dlg.DoModal()==IDOK)
    FilePathName=dlg.GetPathName();
    
    //获得EDIT
    CEdit* pBoxOne;
    pBoxOne = (CEdit*) GetDlgItem(IDC_EDIT1);
     //赋值
    pBoxOne->SetWindowText(FilePathName);
     //取值
     CString str;
     pBoxOne->GetWindowText(str);



}

void CNewgdalDlg::OnButton2()
{
    // TODO: Add your control notification handler code here
    // BROWSEINFO 结构
    ////////////////////////////////////
    // typedef struct _browseinfo {
    //   HWND hwndOwner;
    //   LPCITEMIDLIST pidlRoot;
    //   LPSTR pszDisplayName;
    //   LPCSTR lpszTitle;
    //   UINT ulFlags;
    //   BFFCALLBACK lpfn;
    //   LPARAM lParam;
    //   int iImage;
    //   } BROWSEINFO;
    ///////////////////////////////////


    CString sFolderPath;
    BROWSEINFO bi;
    char Buffer[MAX_PATH];

    //初始化入口参数bi开始
    bi.hwndOwner = NULL;
    bi.pidlRoot =NULL;//初始化制定的root目录很不容易,
    bi.pszDisplayName = Buffer;//此参数如为NULL则不能显示对话框
    bi.lpszTitle = "修改接收路径";
    //bi.ulFlags = BIF_BROWSEINCLUDEFILES;//包括文件
    bi.ulFlags = BIF_EDITBOX;//包括文件

    bi.lpfn = NULL;
    bi.iImage=IDR_MAINFRAME;
    //初始化入口参数bi结束
    LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框
    if(pIDList)
    {
       SHGetPathFromIDList(pIDList, Buffer);
       //取得文件夹路径到Buffer里
       sFolderPath = Buffer;//将路径保存在一个CString对象里
    }
    LPMALLOC lpMalloc;
    if(FAILED(SHGetMalloc(&lpMalloc))) return;
    //释放内存
    lpMalloc->Free(pIDList);
    lpMalloc->Release();

    //MessageBox(sFolderPath,"Title",MB_OK);

    //获得EDIT
    CEdit* pBoxOne;
    pBoxOne = (CEdit*) GetDlgItem(IDC_EDIT2);
     //赋值
    pBoxOne->SetWindowText(sFolderPath);
     //取值
     CString str;
     pBoxOne->GetWindowText(str);



}

void CNewgdalDlg::OnButton4()
{
    // TODO: Add your control notification handler code here
     OGRRegisterAll();
     OGRDataSource  *poDS;
     CString file="C:\\Documents and Settings\\zhangjun\\桌面\\1738 shp\\复件 原始影像数据矢量文件.shp";
     poDS = OGRSFDriverRegistrar::Open(file,FALSE);
     if( poDS == NULL )
     {
        printf( "Open failed.\n" );
        exit( 1 );
     }

          OGRLayer  *poLayer;

    poLayer = poDS->GetLayerByName( "复件 原始影像数据矢量文件" );

    OGRFeature *poFeature;

    poLayer->ResetReading();
    while( (poFeature = poLayer->GetNextFeature()) != NULL )
    {
        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
        int iField;

        for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
        {
            OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );

            if( poFieldDefn->GetType() == OFTInteger )
                printf( "%d,", poFeature->GetFieldAsInteger( iField ) );
            else if( poFieldDefn->GetType() == OFTReal )
                printf( "%.3f,", poFeature->GetFieldAsDouble(iField) );
            else if( poFieldDefn->GetType() == OFTString )
            {  
                printf( "%s,", poFeature->GetFieldAsString(iField) );
                AfxMessageBox(poFeature->GetFieldAsString(iField));

            }
            else
                printf( "%s,", poFeature->GetFieldAsString(iField) );
        }

        OGRGeometry *poGeometry;

        poGeometry = poFeature->GetGeometryRef();
        if( poGeometry != NULL
            && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
        {
            OGRPoint *poPoint = (OGRPoint *) poGeometry;

            printf( "%.3f,%3.f\n", poPoint->getX(), poPoint->getY() );
        }
        else
        {
            printf( "no point geometry\n" );
        }       
        OGRFeature::DestroyFeature( poFeature );
    }

    OGRDataSource::DestroyDataSource( poDS );

    
}


原文地址:https://www.cnblogs.com/zhangjun1130/p/1893947.html