登录站点

用户名

密码

学习VC中所得的点点技术心得(一)

已有 564 次阅读  2011-01-10 21:35   标签java  Java 

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)
{
   //鼠标在窗体中空白处,即不在任何控件或子窗口当中
}


上一篇: 用skinmagic给VC6开发的软件换肤 下一篇: 学习VC中所得的点点技术心得(二)

分享 举报