一些VC編程中的技巧[持續更新]

來源:互聯網
上載者:User

1.int轉字串
int n = 10;
char str[255];
sprintf(str,"%d",nLstItem);

2.返回WCHAR/CHAR類型字串
調用ATL的一個宏來實現:
sprintf(str,"%d",50);
USES_CONVERSION;    //這句很重要,這個宏裡面定義了一些變數用於轉換的變數
LPCTSTR   pT   =   A2T(str);      //anscii轉tchar調用A2T這個宏,反之則是調用T2A

3.讓CListCtrl選擇一行
m_LstCtrl.SetExtendedStyle(m_LstCtrl.GetExtendedStyle()|LVS_EX_FULLROWSELECT );

4.在CListCtrl中插入行
m_LstCtrl.InsertItem(0,_T("1"));
m_LstCtrl.SetItemText(0,1,_T("2"));
m_LstCtrl.SetItemText(0,2,_T("3"));

5.設定表頭
int n = 0;
m_LstCtrl.InsertColumn(n,_T("編號"),LVCFMT_CENTER);
m_LstCtrl.SetColumnWidth(n++ ,40);

m_LstCtrl.InsertColumn(n,_T("程式路徑"),LVCFMT_CENTER);
m_LstCtrl.SetColumnWidth(n++ ,250);

m_LstCtrl.InsertColumn(n,_T("快速鍵"),LVCFMT_CENTER);
m_LstCtrl.SetColumnWidth(n ,80);
寫在OnInitDialog()函數中

6.擷取FileDialog完整路徑
CString str = FileDlg.GetPathName();   

7.擷取所有進程到CComboBox中
m_comboBox.ResetContent();

PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(pe32);
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hProcessSnap == INVALID_HANDLE_VALUE)
{
    AfxMessageBox(_T("擷取進程列表失敗"));

}
BOOL bMore = ::Process32First(hProcessSnap,&pe32);
while(bMore)
{
    CString str;
    str.Format(_T("%s\r\n"),pe32.szExeFile);
    bMore = ::Process32Next(hProcessSnap,&pe32);
    m_comboBox.InsertString(0,str);
}

8.瀏覽檔案
CFileDialog dlg(TRUE,
    _T("DLL Files (*.DLL)"),
    NULL,
    OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |OFN_ALLOWMULTISELECT|OFN_ENABLESIZING,
    _T("DLL Files (*.DLL|*dll||"),
    NULL);
dlg.DoModal();
CString file = dlg.GetPathName();

9.自訂訊息

(1) 手工定義訊息,可以這麼寫
#define WM_MY_MESSAGE(WM_USER+100),
MS 推薦的至少是 WM_USER+100
(2)寫訊息處理函數,用WPARAM,LPARAM返回LRESULT.
LRESULT CMainFrame::OnMyMessage(WPARAM wparam,LPARAM lParam)
{
temp目錄: Use "GetTempPath"
//加入你的處理函數 irectory"
}

10.在MFC中中初始化SOCKET
調用AfxSocketInit即可

11.出現重複定義類的情況,要檢查所包含標頭檔有沒有加#pragma once,加上一般不會出現重複定義的問題

12.共用體……共用同一塊記憶體。。才知道

13.對話方塊的DDV,首先用ClassWizard將某變數與要進行資料交換的控制項進行綁定,然後對資料進行更新,之後調用UpdateData函數。

14.比較檔案用fc命令,格式為:fc 檔案1 檔案2 /b

15.一些對DC的操作:
擷取當前案頭的DC:
bool GetScreenDC(ScreenInfo* psi)
{
    psi->pDC = new CDC;
    psi->pDC->CreateDC(_T("Display"), NULL, NULL, NULL);
    psi->nBitPixel = psi->pDC->GetDeviceCaps(BITSPIXEL);//獲得顏色模式
    psi->nWidth = psi->pDC->GetDeviceCaps(HORZRES);
    psi->nHeight = psi->pDC->GetDeviceCaps(VERTRES);

    if(psi->pDC)
    {
        return true;
    }
    else
    {
        return false;
    }
}

將DC轉化成數組:
bool GetDCBytes(ScreenInfo* psi,COLORREF** p)
{
    CDC memDC;//記憶體DC
    memDC.CreateCompatibleDC(psi->pDC);

    CBitmap memBitmap;//建立和螢幕相容的bitmap
    memBitmap.CreateCompatibleBitmap(psi->pDC, psi->nWidth, psi->nHeight);

    memDC.SelectObject(memBitmap);

    memDC.BitBlt(0, 0, psi->nWidth, psi->nHeight, psi->pDC, 0, 0, SRCCOPY);
    *p = new COLORREF[psi->nWidth * psi->nHeight];//申請記憶體儲存位元影像資料

    memBitmap.GetBitmapBits(sizeof(COLORREF)*psi->nWidth*psi->nHeight, *p);
    return true;
}

將DC寫入到檔案:
bool WriteDCToFile(ScreenInfo* psi,char* bmpFile)
{
    CDC memDC;//記憶體DC
    memDC.CreateCompatibleDC(psi->pDC);

    CBitmap memBitmap, *oldmemBitmap;//建立和螢幕相容的bitmap
    memBitmap.CreateCompatibleBitmap(psi->pDC, psi->nWidth, psi->nHeight);

    oldmemBitmap = memDC.SelectObject(&memBitmap);//將memBitmap選入記憶體DC
    memDC.BitBlt(0, 0, psi->nWidth, psi->nHeight, psi->pDC, 0, 0, SRCCOPY);//複製螢幕映像到記憶體DC

    //以下代碼儲存memDC中的位元影像到檔案
    BITMAP bmp;
    memBitmap.GetBitmap(&bmp);//獲得位元影像資訊

    FILE *fp = fopen(bmpFile, "w+b");

    BITMAPINFOHEADER bih = {0};//位元影像資訊頭
    bih.biBitCount = bmp.bmBitsPixel;//每個像素位元組大小
    bih.biCompression = BI_RGB;
    bih.biHeight = bmp.bmHeight;//高度
    bih.biPlanes = 1;
    bih.biSize = sizeof(BITMAPINFOHEADER);
    bih.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;//映像資料大小
    bih.biWidth = bmp.bmWidth;//寬度

    BITMAPFILEHEADER bfh = {0};//位元影像檔案頭
    bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);//到位元影像資料的位移量
    bfh.bfSize = bfh.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;//檔案總的大小
    bfh.bfType = (WORD)0x4d42;

    fwrite(&bfh, 1, sizeof(BITMAPFILEHEADER), fp);//寫入位元影像檔案頭

    fwrite(&bih, 1, sizeof(BITMAPINFOHEADER), fp);//寫入位元影像資訊頭

    byte * p = new byte[bmp.bmWidthBytes * bmp.bmHeight];//申請記憶體儲存位元影像資料

    GetDIBits(memDC.m_hDC, (HBITMAP) memBitmap.m_hObject, 0, psi->nHeight, p,
        (LPBITMAPINFO) &bih, DIB_RGB_COLORS);//擷取位元影像資料

    fwrite(p, 1, bmp.bmWidthBytes * bmp.bmHeight, fp);//寫入位元影像資料

    delete [] p;

    fclose(fp);

    memDC.SelectObject(oldmemBitmap);
    return true;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.