首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

看到一段代码希望何位帮忙解释一下(基础)

2013-12-13 
看到一段代码希望哪位帮忙解释一下(基础)映射消息响应函数ON_WM_DEVICECHANGE(OnDeviceChange)加入afx_msg

看到一段代码希望哪位帮忙解释一下(基础)
映射消息响应函数
ON_WM_DEVICECHANGE(OnDeviceChange)

加入
afx_msg void OnDeviceChange(UINT nEventType, DWORD dwData);


void CGggfDlg::OnDeviceChange(UINT nEventType, DWORD dwData)
{
    if( nEventType ==32768) 
{
        MessageBox(_T("U盘插入"));}

    else if(nEventType ==32772)  
{
        MessageBox(_T("U盘拔出"));
}
}


----------------------------------------------------------------------------------------------------------------------------

获取U盘符

    CString str = _T("z:\");
    for( char ch = 'z'; ch >='c'; ch--)
    {
        str.SetAt(0,ch);
        UINT type = GetDriveType(str);
        if(DRIVE_REMOVABLE == type)
        {
            AfxMessageBox(str);
        }
    }

afx_msg 这里是什么意思啊?难道是表明是个消息,我记得一般都是public和private之类的。
32768这里是有外接设备插入就显示这个事件类型还是特殊的? 
(_T("U盘插入"));中的_T有什么特殊含义吗?


[解决办法]
afx_msg 这里是什么意思啊?难道是表明是个消息,我记得一般都是public和private之类的。
就是一个宏。为了表示这是一个消息。

32768这里是有外接设备插入就显示这个事件类型还是特殊的? 
就是这个事件类型,一般还可以定义一个更有意义的常数

(_T("U盘插入"));中的_T有什么特殊含义吗?
也是一个宏,根据编译选项变为字符或宽字符
[解决办法]
afx_msg 是声明,这个函数是一个消息的回调,你F12跟进去就明白了。

32768 这个数应该是程序员自定义的一个消息类型,插入U盘,驱动层会给出提示,然后操作系统截获到这个消息,发送ID为 32768 的消息给应用程序,应用程序收到消息,弹窗提示。

至于_T(x),你F12跟进去看看啊。是一个字符的转换。

当定义了Unicode,就是宽字符的,相当于L“x”.没有定义Unicode的话,就是“x”
[解决办法]

引用:
Quote: 引用:

先去看看MFC吧,消息这块走不通,MFC精通不了的
MFC看什么书呢?还是什么例子?我看的是c++


没有速成的。如果你C++基础都还没有打好,那就先继续看C++基础。

直接跳到MFC会走很多弯路,还是先建议看一下《Windows程序设计》,理解消息映射等机制。MFC只是一套框架,是对Windows程序API的封装。

你可以先去看看windows有没有定义这个消息ID。我看你给的程序,是写死的,应该是自定义消息
[解决办法]
CWnd::OnDeviceChange
afx_msg BOOL OnDeviceChange( UINT nEventType, DWORD dwData );

Parameters

nEventType

An event type. See the Remarks section for a description of the available values

dwData

The address of a structure that contains event-specific data. Its meaning depends on the given event.

Remarks

The framework calls this member function to notify an application or device driver of a change to the hardware configuration of a device or the computer.

For devices that offer software-controllable features, such as ejection and locking, the operating system typically sends a DBT_DEVICEREMOVEPENDING message to let applications and device drivers end their use of the device gracefully. 

If the operating system forcefully removes of a device, it may not send a DBT_DEVICEQUERYREMOVE message before doing so.

The nEvent parameter can be one of these values: 

DBT_DEVICEARRIVAL   A device has been inserted and is now available. 


DBT_DEVICEQUERYREMOVE   Permission to remove a device is requested. Any application can deny this request and cancel the removal.


DBT_DEVICEQUERYREMOVEFAILED   Request to remove a device has been canceled.


DBT_DEVICEREMOVEPENDING   Device is about to be removed. Cannot be denied.




DBT_DEVICEREMOVECOMPLETE   Device has been removed.


DBT_DEVICETYPESPECIFIC   Device-specific event.


DBT_CONFIGCHANGED   Current configuration has changed.


DBT_DEVNODES_CHANGED   Device node has changed. 
Note   This member function is called by the framework to allow your application to handle a Windows message. The parameters passed to your function reflect the parameters received by the framework when the message was received. If you call the base-class implementation of this function, that implementation will use the parameters originally passed with the message and not the parameters you supply to the function.

CWnd Overview 
[解决办法]
  Class Members 
[解决办法]
  Hierarchy Chart

See Also   WM_DEVICECHANGE

[解决办法]
IIRC, afx_msg 是VC++用来糊弄自己的, 它需要知道告诉自己,这是个消息响应函数,你知道它很笨的,笨人就得有笨办法。
_T 是一个宏,在define了UNICODE宏时,_T("x")会展开为L"x",否则展开为 "x"
afx_msg, _T两个宏都有#define的

那两个数字是消息类型
[解决办法]

引用:
Quote: 引用:
是《MFC Windows程序设计》这本书吗?这本书很大有什么建议吗,我学过c++,做过c#开发。请问那么多东西都要看吗?
window消息驱动机制我一直以为就是触发了一个事件,然后告诉用户这个事件被触发了而已
不是。其实我没学过C#,不过周围人有搞的。很像。《Windows程序设计》是国外一位大牛写的,很出名。网上搜一下:windows程序设计第五版。不过这本书是以SDK为切入点,也就是纯C语言+Windows API。
关于MFC的书,我就看过《VC++深入详解》、《深入浅出MFC》,都还行。后者对MFC的一些机制讲得比较清楚,而且上面也有介绍要想更深入学习应该看哪些书。

热点排行