2018年12月17日 星期一

[C#]開啟資料夾 獲得資料夾路徑

Button--->中加入事件

FolderBrowserDialog path = new FolderBrowserDialog();
path.ShowDialog();
this.txtPath.Text = path.SelectedPath;

TextBox-->更改名稱 txtPath



測試環境 :c#

2018年11月29日 星期四

[C++]檢查檔案是否存在

C++ _access和_waccess的使用方法 
概述 
頭文件:<io.h> 判斷文件的訪問權限 
原型 
int _access( 
    const char *path,     int mode ); 
int _waccess(  
    const wchar_t *path,     int mode ); 
參數 
path 
文件或目錄路徑 
mode 
訪問權限設定 
返回值 
如果文件具有指定的訪問權限,則函數返回0;如果文件不存在或者不能訪問指定的權限,則返回-1。 
備註 
當path爲文件時,_access函數判斷文件是否存在,並判斷文件是否可以用
mode值指定的模式進行訪問。當path爲目錄時,_access只判斷指定的目錄是否存
在,在Windows NT和Windows 2000中,所有的目錄都有讀寫權限。 
mode的值和含義如下表所示: mode值 
檢查文件 

00 只檢查文件是否存在 
02 寫權限 
04 讀權限
06 讀寫權限 

_waccess是_access的寬字符版本,_waccess的參數path爲寬字符的字符串,
其他與_access相同。


CString FileName = L"C:\\123.txt";

int buf = 0;
buf = _waccess(FileName , 04);
if (buf != 0)
{
 AfxMessageBox(L"Files error");
 return false;
}

測試環境:MFC

[c++]MFC使用dlg 指定開啟資料夾獲取路徑

// Example code
CFolderPickerDialog m_dlg;
CString m_Folder;
 
m_dlg.m_ofn.lpstrTitle = _T("Put your title here");
m_dlg.m_ofn.lpstrInitialDir = _T("C:\\");
if (m_dlg.DoModal() == IDOK) {
       m_Folder = m_dlg.GetPathName();   // Use this to get the selected folder name 
                                         // after the dialog has closed
 
       // May need to add a '\' for usage in GUI and for later file saving, 
       // as there is no '\' on the returned name
       m_Folder += _T("\\");
       UpdateData(FALSE);   // To show updated folder in GUI
 
       // Debug
       TRACE("\n%S", m_Folder);
}
// End of example

測試環境:MFC

2018年7月15日 星期日

[C++]MFC控制項 DDX

以ComboBox為範例

在*.h中新增
CComboBox m_cCUTCOMBO;

在*.cpp中新增
需新增在 DoDataExchange(CDataExchangepDX) 中

//控制項
DDX_Control(pDXIDC_COMBCUT, m_cCUTCOMBO);


測試環境:MFC

2018年7月12日 星期四

C#取得檔案名稱、路徑、副檔名、資料夾位置

using System.IO;


取得程式當下檔案目錄資訊:  string sPath = this.GetType().Assembly.Location;


string filename = @"C:\Users\123.text";
if (File.Exists(filename))
{
    MessageBox.Show("取得檔名(不包含附檔名)" + Path.GetFileNameWithoutExtension(filename));
    MessageBox.Show("取得副檔名:" + Path.GetExtension(filename));
    MessageBox.Show("目錄資料夾路徑:" + Path.GetDirectoryName(filename));
    MessageBox.Show("資料根目錄:" + Path.GetPathRoot(filename));
    MessageBox.Show("取得路徑:" + Path.GetFullPath(filename));
}

測試環境:c#.net 2015

[C++]cstring to char*

CString csbuf;
USES_CONVERSION;
char* chPath = T2A((LPTSTR)(LPCTSTR)csbuf);

測試環境 mfc  Unicode

[C++]顯式連結、動態連結

宣告句柄與類定義的指標 宣告一個新函式替代原來的
在初始化的地方 
使用"LoadLibrary"呼叫dll
使用"GetProcAddress"定義函式
最後再要使用的地方 直接使用替代的函式 即可

==============================以下範例================================

HMODULE hmod;
typedef DXF_PARSE*(*MyAdd)();
typedef DXF_PREVIEW*(*MyAdd2)();
MyAdd Add;
MyAdd2 Add2;


寫在init

BOOL CDxftoNcfileDlg::OnInitDialog()
{
 CDialogEx::OnInitDialog();

 // TODO: 在此加入額外的初始設定
 
 hmod = LoadLibrary(L"DxfLib2.dll"); //load dll
 if (hmod == NULL)
 {
  return 0;
 }
  Add = (MyAdd)GetProcAddress(hmod, "GetParsesPtr");
 if (!Add) {
  MessageBox(L"error_GetParsesPtr");
  return 0;
 }
 Add2 = (MyAdd2)GetProcAddress(hmod, "GetPreviewsPtr");
 if (!Add2) {
  MessageBox(L"error_GetPreviewsPtr");
  return 0;
 }

 return TRUE;  // 傳回 TRUE,除非您對控制項設定焦點

}


再欲使用的地方加上直接替換
add()   
add2()


測試環境:MFC

[SQL]顯示千分位與小數顯示

  CONVERT ( data_type [ ( length ) ] , expression [ , style ] ) CONVERT style參數說明 1  (expression為 money 或 smallmoney型別): 0 : 預設,保留小數位後兩位,並四捨...