summary22、MFC 21后续、回调函数、SendMessage

MFC基础知识

  • Afx前缀的函数代表应用程序框架(Application Framework)函数。应用程序框架实际上是一套辅助我们生成应用程序的框架模型。该模型把多个类进行了一个有机的集成,可以根据该模型提供的方案来设计自己的应用程序。在MFC中,以Afx为前缀的函数都是全局函数,可以在程序的任何地方调用他们。例如:
    CWindApp* AfxGetApp();
    HINSTANCE AfxGetInstanceHandle( );
    HINSTANCE AfxGetResourceHandle( );
    int AfxMessageBox( LPCTSTR lpszText, UINT nType = MB_OK, UINT nIDHelp = 0 );

     

  • DOS是Disk Operation System(磁盘操作系统)的简称。dos是一个基于磁盘管理的操作系统,是一种命令行形式的,需要输入命令的形式才能把指令传给计算机,让计算机实现操作的。
  •  

summary21后续:

SHGetMalloc

SHGetMalloc Function

返回一个指向 Shell 的 IMalloc 接口的指针。

语法

HRESULT SHGetMalloc(

LPMALLOC *ppMalloc

);

参数

ppMalloc

用于接收 Shell 的 IMalloc 接口指针的地址。

返回值

成功,返回S_OK。

#define SUCCEEDED(hr)   (((HRESULT)(hr)) >= 0)

HRESULT

 

函数返回值。如果这个函数是执行完返回的话将包含具有实际意义的数据,如果立即返回则包含状态信息--发送成功与否,并不能说明执行的如何。

HRESULT 是一种简单的数据类型,通常被属性和 ATL 用作返回值。下表说明各种不同的值。头文件 winerror.h 中包含更多的值。

名称 说明 值

S_OK 操作成功 0x00000000

E_UNEXPECTED 意外的失败 0x8000FFFF

E_NOTIMPL 未实现 0x80004001

E_OUTOFMEMORY 未能分配所需的内存 0x8007000E

E_INVALIDARG 一个或多个参数无效 0x80070057

E_NOINTERFACE 不支持此接口 0x80004002

E_POINTER 无效指针 0x80004003

E_HANDLE 无效句柄 0x80070006

E_ABORT 操作已中止 0x80004004

E_FAIL 未指定的失败 0x80004005

E_ACCESSDENIED 一般的访问被拒绝错误 0x80070005

不能简单地把返回值与 S_OK 和 E_FAIL 比较,而要用 SUCCEEDED 和 FAILED 宏进行判断

我们在程序中如果需要判断返回值,则可以使用比较运算符号;switch开关语句;也可以使用VC提供的宏: HRESULT hr = 调用组件函数;

if( SUCCEEDED( hr ) ){...} // 如果成功

......

if( FAILED( hr ) ){...} // 如果失败

回调函数

实例

实例中 populate_array 函数定义了三个参数,其中第三个参数是函数的指针,通过该函数来设置数组的值。

实例中我们定义了回调函数 getNextRandomValue,它返回一个随机值,它作为一个函数指针传递给 populate_array 函数。

populate_array 将调用 10 次回调函数,并将回调函数的返回值赋值给数组。

实例

#include <stdlib.h>  
#include <stdio.h>
 
// 回调函数
void populate_array(int *array, size_t arraySize, int (*getNextValue)(void))
{
    for (size_t i=0; i<arraySize; i++)
        array[i] = getNextValue();
}
 
// 获取随机值
int getNextRandomValue(void)
{
    return rand();
}
 
int main(void)
{
    int myarray[10];
    populate_array(myarray, 10, getNextRandomValue);
    for(int i = 0; i < 10; i++) {
        printf("%d ", myarray[i]);
    }
    printf("\n");
    return 0;
}

编译执行,输出结果如下:

16807 282475249 1622650073 984943658 1144108930 470211272 101027544 1457850878 1458777923 2007237709

 

SendMessage function

Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.

Syntax

 

LRESULT SendMessage(
  HWND   hWnd,
  UINT   Msg,
  WPARAM wParam,
  LPARAM lParam
);

Parameters

hWnd

Type: HWND

A handle to the window whose window procedure will receive the message. If this parameter is HWND_BROADCAST ((HWND)0xffff), the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.

Message sending is subject to UIPI. The thread of a process can send messages only to message queues of threads in processes of lesser or equal integrity level.

Msg

Type: UINT

The message to be sent.

For lists of the system-provided messages, see System-Defined Messages.

wParam

Type: WPARAM

Additional message-specific information.

lParam

Type: LPARAM

Additional message-specific information.

Return Value

Type: Type: LRESULT

The return value specifies the result of the message processing; it depends on the message sent.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值