利用 GetModuleFileName获取exe文件路径,,以显示bmp, jpg, png图片

// Common.h

 

/******************************************************************** * Name: Common.cpp * Creater: Yunhong Mi * Date: 2009-09-24 * Path: #include "Common.h" * Summary: * Note: ********************************************************************/ #include "stdafx.h" #include "common.h" #ifdef _DEBUG // TEXT("/Storage Card/res/") 用于 ppc项目中(Debug模式下不支持绝对路径, 必须把Storage Card当作根目录) string_C exePath = TEXT("D:/Project/Test/MFC/MFC/res/");// TEXT("/Storage Card/res/") #else string_C exePath; #endif bool ExePathInit(void) { #ifdef _DEBUG return false; #else TCHAR *path = new TCHAR[MAX_PATH]; DWORD len = GetModuleFileName(NULL,path,MAX_PATH); size_t pos=0; if(len>0) { exePath.assign(path); pos = exePath.find_last_of(TEXT('//')); if(pos > 0) { exePath = exePath.substr(0,pos+1); } delete[] path; return true; } else { MessageBox(NULL,L"程序目录初始化失败!",L"错误",MB_OK|MB_ICONERROR); delete[] path; return false; } #endif }

 

//Common.cpp

/******************************************************************** * Name: Common.cpp * Creater: Yunhong Mi * Date: 2009-09-24 * Path: #include "Common.h" * Summary: * Note: ********************************************************************/ #include "stdafx.h" #include "common.h" #ifdef _DEBUG // TEXT("/Storage Card/res/") 用于 ppc项目中 string_C exePath = TEXT("D:/Project/Test/MFC/MFC/res/");// TEXT("/Storage Card/res/") #else string_C exePath; #endif bool ExePathInit(void) { #ifdef _DEBUG return false; #else TCHAR *path = new TCHAR[MAX_PATH]; DWORD len = GetModuleFileName(NULL,path,MAX_PATH); size_t pos=0; if(len>0) { exePath.assign(path); pos = exePath.find_last_of(TEXT('//')); if(pos > 0) { exePath = exePath.substr(0,pos+1); } delete[] path; return true; } else { MessageBox(NULL,L"程序目录初始化失败!",L"错误",MB_OK|MB_ICONERROR); delete[] path; return false; } #endif }

 

// 调用处

#include "Common.h"

BOOL CMFCApp::InitInstance()
{

//。。。。。。。。

 ExePathInit();

//。。。。。。

}


void CMFCDlg::OnPaint()
{

  CPaintDC dc(this); // 用于绘制的设备上下文
  Graphics graphics(dc);
  string_C str = exePath + TEXT("1.jpg");
  Image image(str.c_str());
  graphics.DrawImage(&image, 10, 10);

 

}

 

 

 

你可能感兴趣的:(image,String,null,delete,exe,Path)