VC++相關知識

來源:互聯網
上載者:User

1 toolbar預設位元影像左上方那個點的顏色是透明色,不喜歡的話可以自己改。
2 VC++中 WM_QUERYENDSESSION WM_ENDSESSION 為系統關機訊息。
3 Java學習書推薦:《java編程思想》
4 在VC下執行DOS命令
a. system("md c://12");
b. WinExec("Cmd.exe /C md c://12", SW_HIDE);
c. ShellExecute
ShellExecute(NULL,"open","d://WINDOWS//system32//cmd.exe","/c md d://zzz","",SW_SHOW);
d. CreateProcess
下面這個樣本的函數可以把給定的DOS命令執行一遍,並把DOS下的輸出內容記錄在buffer中。同時示範了匿名管道重新導向輸出的用法:
-------------------------------------------------------------------------------------
BOOL CDOSDlg::ExecDosCmd()
{
#define EXECDOSCMD "dir c:" //可以換成你的命令

SECURITY_ATTRIBUTES sa;
HANDLE hRead,hWrite;

sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hRead,&hWrite,&sa,0))
{
return FALSE;
}
char command[1024]; //長達1K的命令列,夠用了吧
strcpy(command,"Cmd.exe /C ");
strcat(command,EXECDOSCMD);
STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = hWrite; //把建立進程的標準錯誤輸出重新導向到管道輸入
si.hStdOutput = hWrite; //把建立進程的標準輸出重新導向到管道輸入
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
//關鍵步驟,CreateProcess函數參數意義請查閱MSDN
if (!CreateProcess(NULL, command,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi))
{
CloseHandle(hWrite);
CloseHandle(hRead);
return FALSE;
}
CloseHandle(hWrite);

char buffer[4096] = {0}; //用4K的空間來儲存輸出的內容,只要不是顯示檔案內容,一般情況下是夠用了。
DWORD bytesRead;
while (true)
{
if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL)
break;
//buffer中就是執行的結果,可以儲存到文本,也可以直接輸出
AfxMessageBox(buffer); //這裡是彈出對話方塊顯示
}
CloseHandle(hRead);
return TRUE;
}
-------------------------------------------------------------------------------------
5 刪除目錄,包含刪除子檔案夾以及其中的內容
-------------------------------------------------
BOOL DeleteDirectory(char *DirName)//如刪除 DeleteDirectory("c://aaa")
{
CFileFind tempFind;
char tempFileFind[MAX_PATH];
sprintf(tempFileFind,"%s//*.*",DirName);
BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
while(IsFinded)
{
IsFinded=(BOOL)tempFind.FindNextFile();
if(!tempFind.IsDots())
{
char foundFileName[MAX_PATH];
strcpy(foundFileName,tempFind.GetFileName().GetBuffer(MAX_PATH));
if(tempFind.IsDirectory())
{
char tempDir[MAX_PATH];
sprintf(tempDir,"%s//%s",DirName,foundFileName);
DeleteDirectory(tempDir);
}
else
{
char tempFileName[MAX_PATH];
sprintf(tempFileName,"%s//%s",DirName,foundFileName);
DeleteFile(tempFileName);
}
}
}
tempFind.Close();
if(!RemoveDirectory(DirName))
{
MessageBox(0,"刪除目錄失敗!","警告資訊",MB_OK);//比如沒有找到檔案夾,刪除失敗,可把此句刪除
return FALSE;
}
return TRUE;
}
-------------------------------------------------------------
6 讓程式暫停:system("PAUSE");
7 在PreTranslateMessage中捕捉鍵盤事件

if (pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN)return TRUE; //注意return的值
8 更改按鍵訊息(下面的代碼可把斷行符號鍵訊息改為TAB鍵訊息)
-------------------------------------------------------
    BOOL CT3Dlg::PreTranslateMessage(MSG* pMsg)
   {

   if(pMsg->message == WM_KEYDOWN && VK_RETURN == pMsg->wParam)
    {
   pMsg->wParam = VK_TAB;
    }
    return CDialog::PreTranslateMessage(pMsg);
    }
------------------------------------------
9 MoveWindow: 一個可以移動、改變視窗位置和大小的函數
10 16進位轉化成10進位小數的問題
    用一個讀二進位檔案的軟體讀檔案
    二進位檔案中的一段 8F C2 F5 3C 最後變成了 0.03
    請問這是怎麼轉換過來的??
  方法一:浮點技術法,如
   DWORD dw=0x3CF5C28F;
   float d=*(float*)&dw;//0.03;
     方法二:浮點的儲存方式和整數完全兩樣,你想瞭解的話可以去
        http://www.zahui.com/html/1/3630.htm
        看一看,不過通常我們都不必瞭解它就可以完成轉換。
        char a[4] = {0x8F, 0xC2, 0xF5, 0x3C};
        float f;
        memcpy(&f,a,sizeof(float));
TRACE("%d",0x3CF5C28F);
11 EDIT控制項的 EM_SETSEL,EM_REPLACESEL訊息
12 在其它進程中監視鍵盤訊息:用SetWindowsHookEx(WH_KEYBOARD_LL,...);
13 在案頭上任意位置寫字
--------------------------------------------------
HDC deskdc = ::GetDC(0);
CString stext = "我的案頭";
::TextOut(deskdc,100,200,stext,stext.GetLength());
::ReleaseDC(0,deskdc);
------------------------------------------------------
14 HWND thread_hwnd=Findwindow(NULL,"你要監控的進程表單(用SPY++看)"),
if (thread_hwnd==NULL) 。。。。。。。。。。
else DWORD thread_id=GetWindowThreadProcessId(thread_hwnd,NULL)
15 waveOutGetVolume()可以得到波形音量大小
16 隱藏案頭表徵圖並禁用右鍵功能菜單:
------------------------------------
HWND Hwd = ::FindWindow("Progman", NULL);
if (bShowed)
::ShowWindow(Hwd, SW_HIDE);
else
::ShowWindow(Hwd, SW_SHOW);
bShowed = !bShowed;
---------------------------------------
17 獲得程式當前路徑:
---------------------------------------------
char ch[256];
GetModuleFileName(NULL,ch,255);
for(int i=strlen(ch);i && ch[i]!='//';i--);
ch[i]=0;
AfxMessageBox(ch);
----------------------------------------------
18 KeyboardProc的lParam中包含著許多按鍵資訊,其中第31位(從0開始)為0表示是按下按鍵,為1表示鬆開按鍵。
(lParam & 0x80000000)進行二進位'與'計算,效果是取第31位的值。
(lParam & 0x40000000)是取第30位,30位表示按鍵的上一個狀態,為1表示之前鍵已經是按下的,0表示鬆開。
  lParam
  [in] Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag. For more information about the lParam parameter, see Keystroke Message Flags. This parameter can be one or more of the following values.
  0-15
  Specifies the repeat count. The value is the number of times the keystroke is repeated as a result of the user's holding down the key.
  16-23
  Specifies the scan code. The value depends on the OEM.
  24
  Specifies whether the key is an extended key, such as a function key or a key on the numeric keypad. The value is 1 if the key is an extended key; otherwise, it is 0.
  25-28
  Reserved.
  29
  Specifies the context code. The value is 1 if the ALT key is down; otherwise, it is 0.
  30
  Specifies the previous key state. The value is 1 if the key is down before the message is sent; it is 0 if the key is up.
  31
  Specifies the transition state. The value is 0 if the key is being pressed and 1 if it is being released.
19 複製檔案應該用到CopyFile或是CopyFileEx這兩個API
20 移動視窗的位置或改變大小:MoveWindow/SetWindowPos
21 我的程式是當前啟動並執行程式時,可以用setcursor()來設定游標的表徵圖。
而且可以用setcapture()是滑鼠移動到我得程式視窗之外時也是我設定的表徵圖
但是如果我得程式不是當前的運行程式的,滑鼠就會變會預設的。
怎樣能夠,使得不變回預設的,還是用我設定的游標?
SetSystemCursor
22 SendMessage函數的幾個用法:
控制按鈕按下的,是這麼用的
SendMessage(n1, WM_COMMAND, MAKELPARAM(ID,BN_CLICKED),(LPARAM )n2); (n1,n2是控制代碼)
而得到常值內容,是這樣用的,
SendMessage(hWnd,WM_GETTEXT,10,(LPARAM)buf),
23 處理一個單行EDIT的WM_CTLCOLOR要同時響應nCtlColor = CTLCOLOR_EDIT和CTLCOLOR_MSGBOX的兩個情況,參考http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfc_cwnd.3a3a.onctlcolor.asp
24 裝置發生改變處理函數可在CWnd::OnDeviceChange中,捕獲WMDEVICECHANGE事件不能區分諸如裝置插入、拔下訊息。
25 把字元"abc/n123"存入文字檔中時,檔案內容沒看見換行,其實用word開啟該檔案是有換行的。另外用"abc/r/n123"代替也可看見換行。
26 ::SetFocus(::GetDesktopWindow());或::BringWindowToTop(::GetDesktopWindow());
  ::GetDesktopWindow()這裡可獲得桌面視窗的控制代碼
27 數組初始化:
int a[24][34]; //聲明數組
memset(a,-1,24*34); //全部元素初始化成-1,但初始化成除0和-1以外的數值是不行的
28 SHGetFileInfo函數可獲得檔案資訊。
29 建立一個控制項:
HWND hEdit=CreateWindow("EDIT",NULL,WS_CHILD|WS_VISIBLE |ES_LEFT,50,20,50,20,hwnd,NULL,hInst,NULL); //hwnd參數為父視窗控制代碼
30 VC中對音效檔的操作:http://www.pujiwang.com/twice/Article_Print.asp?ArticleID=550
31 調用其它程式又要隱藏視窗:用CreateProcess函數調用,再拿到視窗控制代碼,然後::ShowWindow(hWnd,SW_HIDE);
32 讀取文字檔中的一行:
   用CFile類的衍生類別:CStdioFile的方法:CStdioFile::ReadString
33 刪除非空檔案夾:
------------------------------------------------
SHFILEOPSTRUCT shfileop;
shfileop.hwnd = NULL;
shfileop.wFunc = FO_DELETE ;
shfileop.fFlags = FOF_SILENT|FOF_NOCONFIRMATION;
shfileop.pFrom = "c://temp"; //要刪除的檔案夾
shfileop.pTo = "";
shfileop.lpszProgressTitle = "";
shfileop.fAnyOperationsAborted = TRUE;
int nOK = SHFileOperation(&shfileop);
-------------------------------------------------
34 函數前面加上::是什麼意思?
   叫域運算子...在MFC中表示調用API...或其它全域函數...為了區分是mfc函數還是api
   詳見:http://search.csdn.net/Expert/topic/1183/1183492.xml?temp=.9471247
35 CImageList的用法:http://www.study888.com/computer/pro/vc/desktop/200506/39027.html
36 有關控制項的一些常見問答:
http://fxstudio.nease.net/article/ocx/ <==========================很不錯的地方哦
37 在多文檔客戶區中增加位元影像底圖示範程式:
http://www.study888.com/computer/pro/vc/desktop/200506/39028.html
我的對應工程:AddBackgroundBitmap
38 用VC++6.0實現PC機與單片機之間串列通訊
http://www.zahui.com/html/1/1710.htm
39 日期到字串:
--------------------------------------------------
SYSTEMTIME sys;
GetSystemTime(&sys);
char str[100];
sprintf(str,"%d%d%d_%d%d%d",sys.wYear,sys.wMonth,sys.wDay,sys.wHour+8,sys.wMinute,sys.wSecond);
//這裡的小時數注意它的0:00點是早上8:00,所以要加上8,因為這是格林威治時間,換成我國時區要加8
--------------------------------------------------
CString m_strTemp;
SYSTEMTIME systemtime;
GetLocalTime(&systemtime); //這個函數可獲得毫秒級的目前時間
m_strTemp.Format("%d年%d月%d日%d:%d:%d:%d 星期%d",systemtime.wYear,systemtime.wMonth,systemtime.wDay,systemtime.wHour,systemtime.wMinute,systemtime.wSecond,systemtime.wMilliseconds,systemtime.wDayOfWeek);
--------------------------------------------------
40 工作列上的表徵圖閃爍:
   The FlashWindow function flashes the specified window once, whereas the FlashWindowEx function flashes a specified number of times.

BOOL FlashWindow(
HWND hWnd, // handle to window to flash
BOOL bInvert // flash status
);//閃爍一次
FlashWindowEx()//閃爍多次
41 十六進位字元轉浮點數:http://community.csdn.net/Expert/topic/4379/4379713.xml?temp=.7092096
   long lValue = 0xB28A43;
float fValue;
memcpy(&fValue,&lValue,sizeof(float));
42 在一個由漢字組成的字串裡,由於一個漢字由兩個位元組組成,怎樣判斷其中一個位元組是漢字的第一個位元組,還是第二個位元組,使用IsDBCSLeadByte函數能夠判斷一個字元是否是雙字的第一個位元組,試試看:)
_ismbslead
_ismbstrail
43 如何?對話方塊面板上的控制項隨著對話方塊大小變化自動調整
   在OnSize中依其比例用MoveWindow同等縮放.http://www.codeproject.com/dialog/dlgresizearticle.asp
44 向CListCtrl中插入資料後,它總是先縱向再橫向顯示,我希望他先橫向再縱向
在CListCtrl的ReDraw()中處理(見http://community.csdn.net/Expert/topic/4383/4383963.xml?temp=.3442041)
如:
m_list.ReDraw(FALSE);
m_list.ReDraw(TRUE);
45 給你的程式加上splash:http://www.vckbase.com/document/finddoc.asp?keyword=splash
如何添加閃屏:Project->Add to Project->Components and Controls->Gallery//Visual C++ Components->Splash screen
46 實現象快速啟動欄的"顯示/隱藏案頭"一樣的功能:http://fxstudio.nease.net/article/form/55.txt
47 如何設定listview某行的顏色:
   CSDN上的貼子:http://community.csdn.net/Expert/topic/4386/4386904.xml?temp=2.422512E-03
   Codeguru上相關連結:http://www.codeguru.com/Cpp/controls/listview/backgroundcolorandimage/article.php/c1093/
48 如何得到視窗標題列尺寸:http://community.csdn.net/Expert/topic/4387/4387830.xml?temp=.6934168
GetSystemMetrics(SM_CYCAPTION或者SM_CYSMCAPTION);

SM_CYCAPTION Height of a caption area, in pixels.
SM_CYSMCAPTION Height of a small caption, in pixels.
--------------------------------------------------------
GetWindowRect(&rect);
rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) + 3;
--------------------------------------------------------
49 如何將16進位的byte轉成CString:
---------------------------------
BYTE p[3];
p[0]=0x01;
p[1]=0x02;
p[2]=0x12;
CString str;
str.Format("%02x%02x%02x", p[0], p[1], p[2]);
-------------------------------------
50 怎樣尋找到正處在滑鼠下面的視窗(具體到子視窗和菜單),無論是這個視窗是否具有焦點:
-----------------------------------------------------------
POINT pt;
CWnd* hWnd; // Find out which window owns the cursor
GetCursorPos(&pt);
hWnd=CWnd::WindowFromPoint(pt);
if(hWnd==this)
{
//滑鼠在表單中空白處,即不在任何控制項或子視窗當中
}

51 得到CListCtrl控制項點擊事件時點擊的位置:
-----------------------------------------------
void CTest6Dlg::OnClickList1(NMHDR* pNMHDR, LRESULT* pResult)
{NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if(pNMListView->iItem != -1)
{
CString strtemp;
strtemp.Format("單擊的是第%d行第%d列",
pNMListView->iItem, pNMListView->iSubItem);
AfxMessageBox(strtemp);
}
*pResult = 0;
}
------------------------------------------------
52 如何在clistctrl的儲存格裡添加圖片?http://community.csdn.net/Expert/topic/4388/4388748.xml?temp=.2233393

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.