走进windows编程的世界-----消息处理函数(4)

一 右键菜单
  1 右键菜单
    当在窗体点击鼠标右键时,弹出的菜单。
  2 右键菜单的使用
    2.1 创建菜单
      CreatePopupMenu
    2.2 菜单添加
     AppendMenu
    2.3 菜单的显示、

BOOL TrackPopupMenu(
    HMENU hMenu, //显示的菜单句柄
    UINT uFlags, //显示的方式
  int x, //菜单的X屏幕坐标
  int y, //菜单的Y屏幕坐标
  int nReserved, //保留,必须为0
  HWND hWnd, //处理菜单命令的窗体句柄
  CONST RECT *prcRect ); //忽略

    2.4 菜单的命令处理

     WM_COMMAND 
    2.5 使用右键菜单的位置
      2.5.1 WM_RBUTTONUP 消息
        在WM_RBUTTONUP中,加入菜单的创建及显示,
        右键消息坐标,转换成屏幕坐标使用.
          ClientToScreen.
      2.5.2 WM_CONTEXTMENU 消息
        用于显示右键的菜单的消息.
          WPARAM - 右键抬起时相应窗体句柄
          LPARAM - 右键抬起时鼠标的屏幕坐标位置
             LOWORD(lParam) - X屏幕坐标
             HIWORD(lParam) - Y屏幕坐标
      2.5.3 WM_RBUTTONUP和WM_CONTEXTMENU对照
        1) 坐标系不同, WM_RBUTTONUP客户区坐标,WM_CONTEXTMENU屏幕坐标
        2) 先有WM_RBUTTONUP消息,后有WM_CONTEXTMENU消息

 

/* File : winPopMenu.cpp  
 * Auth : sjin  
 * Date : 20140706  
 * Mail : 413977243@qq.com  
 */    
    
#include <Windows.h>    
#include <stdio.h>


HINSTANCE g_hInst = NULL;

void OnRButtonUp( HWND hWnd, UINT nMsg, 
        WPARAM wParam, LPARAM lParam )
{    // 创建弹出式菜单
    HMENU hPopMenu = CreatePopupMenu( );
    // 添加菜单项
    AppendMenu( hPopMenu, MF_STRING, 1001, "測试1");
    AppendMenu( hPopMenu, MF_SEPARATOR, 0, NULL );
    AppendMenu( hPopMenu, MF_STRING, 1002, "退出");
    // 获取菜单位置
    POINT point = { 0 };
    point.x = LOWORD( lParam );
    point.y = HIWORD( lParam );
    ClientToScreen( hWnd, &point );
    // 显示菜单
    TrackPopupMenu( hPopMenu, TPM_LEFTALIGN,
        point.x, point.y, 0, hWnd, NULL );
}

void OnContextMenu( HWND hWnd, UINT nMsg, 
    WPARAM wParam, LPARAM lParam )
{   // 创建弹出式菜单
    HMENU hPopMenu = CreatePopupMenu( );
    // 添加菜单项
    AppendMenu( hPopMenu, MF_STRING, 1001, "測试2");
    AppendMenu( hPopMenu, MF_SEPARATOR, 0, NULL );
    AppendMenu( hPopMenu, MF_STRING, 1002, "退出");
    // 坐标获取
    int nX = LOWORD( lParam );
    int nY = HIWORD( lParam );
    // 显示菜单
    TrackPopupMenu( hPopMenu, TPM_LEFTALIGN,
        nX, nY, 0, hWnd, NULL );
    // 删除菜单
    DestroyMenu( hPopMenu );
}

void OnCommand( HWND hWnd, UINT nMsg, 
        WPARAM wParam, LPARAM lParam )
{
    int nCmdID = LOWORD( wParam );
    switch( nCmdID )
    {
    case 1001:
        MessageBox( NULL, "Hello Popmenu",
            "PopMenu", MB_OK );
        break;
    case 1002:
        PostQuitMessage( 0 );
        break;
    }
}



LRESULT CALLBACK WndProc( HWND   hWnd, 
                          UINT   nMsg,
                          WPARAM wParam,
                          LPARAM lParam )
{
    switch( nMsg )
    {
    case WM_RBUTTONUP:
        //OnRButtonUp( hWnd, nMsg, wParam, lParam );
        break;
    case WM_CONTEXTMENU:
        OnContextMenu( hWnd, nMsg, wParam, lParam );
        break;
    case WM_COMMAND:
        OnCommand( hWnd, nMsg, wParam, lParam );
        break;
    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;
    }
    return DefWindowProc( hWnd, nMsg,
        wParam, lParam );
}

BOOL RegisterWnd( LPSTR pszClassName )
{
    WNDCLASSEX wce = { 0 };
    wce.cbSize        = sizeof( wce );
    wce.cbClsExtra    = 0;
    wce.cbWndExtra    = 0;
    wce.hbrBackground = HBRUSH(COLOR_WINDOW);
    wce.hCursor       = NULL;
    wce.hIcon         = NULL;
    wce.hIconSm       = NULL;
    wce.hInstance     = g_hInst;
    wce.lpfnWndProc   = WndProc;
    wce.lpszClassName = pszClassName;
    wce.lpszMenuName  = NULL;
    wce.style         = CS_HREDRAW|CS_VREDRAW;

    ATOM nAtom = RegisterClassEx( &wce );
    if( 0 ==  nAtom )
    {
        return FALSE;
    }

    return TRUE;
}

HWND CreateWnd( LPSTR pszClassName )
{
    HWND hWnd = CreateWindowEx( 0,
        pszClassName, "MyWnd", 
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, NULL, NULL, g_hInst,
        NULL );
    return hWnd;
}

void DisplayWnd( HWND hWnd )
{
    ShowWindow( hWnd, SW_SHOW );
    UpdateWindow( hWnd );
}

void Message( )
{
    MSG msg = { 0 };
    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    g_hInst = hInstance;
    RegisterWnd( "MYWND" );
    HWND hWnd = CreateWnd( "MYWND" );
    DisplayWnd( hWnd );
    Message( );
    return 0;
}

二、资源的使用

  1 资源文件
    图标、光标、字符串、菜单、加速键和对话框资源,位图资源等等。
    资源脚本文件 - 扩展名为RC文件。

定义了资源和相关文件等等信息。


    资源编译器 - RC.exe
    
  2 图标资源ICON
   
    2.1 经常使用的几种大小: 16X16, 32X32,48X48
    2.2 使用
       HICON LoadIcon(
     HINSTANCE hInstance, //应用程序的句柄
      LPCTSTR lpIconName );//图标的ID字符串
    2.3 系统提供的图标
       hInstance为空, lpIconName为定义的系统图标.
    2.4 自己绘制的图标
       hInstance为图标所在的应用程序的实例句柄
    2.5 注意点:
      一个图标文件里,能够包括多种大小、颜色不同的图标,系统使用图标时,通过大小来匹配。假设未找到大小全然一致的,那么会使用大小最接近的图标格式替换。


    
  3 光标资源
    
    3.1 光标资源
      热点 Hotspot - 能够产生鼠标点击的位置
    3.2 使用
      HCURSOR LoadCursor(
    HINSTANCE hInstance, //应用程序实例句柄
      LPCTSTR lpCursorName); //光标的ID
    3.3 系统的光标
      hInstance为空,lpCursorName指定为系统的光标就可以获得
    3.4 自绘制的光标
      hInstance不能为空。


    3.5 WM_SETCURSOR消息
      当鼠标在窗体内就会产生。

能够在程序运行的过程中改动鼠标样式。
        wParam - 窗体句柄;
        LOWORD(lParam) - 所在位置的标识
        HIWORD(lParam) - 鼠标的消息ID
      SetCursour 设置当前窗体的光标

/* File : winRes.cpp   
 * Auth : sjin   
 * Date : 20140710   
 * Mail : 413977243@qq.com   
 */      
      
#include <Windows.h>      
#include <stdio.h>
#include <WinUser.h>
#include "Resource.h"

HINSTANCE g_hInst = NULL;
 
BOOL OnSetCursor( HWND hWnd, UINT nMsg, 
    WPARAM wParam, LPARAM lParam )
{
    int nHitTest = LOWORD( lParam );
    if( HTCLIENT != nHitTest )/*为了仅仅处理客户区消息。须要添加这个设置*/
    {
       // return FALSE;
    }

    //获得窗体的客户区
    RECT rcClient = { 0 };
    GetClientRect( hWnd, &rcClient );
    //获得当前光标的位置
    POINT ptPos = { 0 };
    GetCursorPos( &ptPos );
    ScreenToClient( hWnd, &ptPos );
    //依据位置载入光标
    HCURSOR hCursor = NULL;
    if( ptPos.x < rcClient.right/2 )
    {
        if( ptPos.y < rcClient.bottom/2 )
        {
			/*HCURSOR LoadCursor(
			 *	HINSTANCE hInstance, //应用程序实例句柄
			 *	LPCTSTR lpCursorName); //光标的ID
			 *  系统光标: hInstance为NULL,lpCursorName:光标资源ID
			 *  自绘制光标:hInstance不能为NULL。lpCursorName:自绘光标资源ID
			 */
            hCursor = LoadCursor( NULL, IDC_SIZEALL );
        }
        else
        {
            hCursor = LoadCursor( NULL, IDC_CROSS );
        }
    }
    else
    {
        if( ptPos.y < rcClient.bottom/2 )
        {
            hCursor = LoadCursor( NULL, IDC_WAIT );
        }
        else
        {
            hCursor = LoadCursor( NULL, IDC_UPARROW );
        }
    }
    // 设置光标
    SetCursor( hCursor );
    return TRUE;
}

LRESULT CALLBACK WndProc( HWND   hWnd, 
                          UINT   nMsg,
                          WPARAM wParam,
                          LPARAM lParam )
{
    switch( nMsg )
    {
		/*当鼠标在窗体内就会触发这个消息。能够再程序运行过程中改动光标的样式*/
    case WM_SETCURSOR:

        if( TRUE == OnSetCursor( hWnd, nMsg, wParam, lParam ) )
        {
			/*设置光标属性生效,必须返回*/
            return 0;
        }
        break;
    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;

    }
	/*运行默认的光标属性*/
    return DefWindowProc( hWnd, nMsg,
        wParam, lParam );
}

BOOL RegisterWnd( LPSTR pszClassName )
{
    WNDCLASSEX wce = { 0 };
    wce.cbSize        = sizeof( wce );
    wce.cbClsExtra    = 0;
    wce.cbWndExtra    = 0;
    wce.hbrBackground = HBRUSH(COLOR_WINDOW);
    wce.hCursor       = LoadCursor( g_hInst, MAKEINTRESOURCE(IDC_POINTER) );
    wce.hIcon         = LoadIcon( g_hInst, MAKEINTRESOURCE(IDI_MAIN) );
    wce.hIconSm       = NULL;
    wce.hInstance     = g_hInst;
    wce.lpfnWndProc   = WndProc;
    wce.lpszClassName = pszClassName;
    wce.lpszMenuName  = NULL;
    wce.style         = CS_HREDRAW|CS_VREDRAW;

    ATOM nAtom = RegisterClassEx( &wce );
    if( 0 ==  nAtom )
    {
        return FALSE;
    }

    return TRUE;
}

HWND CreateWnd( LPSTR pszClassName )
{
    HWND hWnd = CreateWindowEx( 0,
        pszClassName, "MyWnd", 
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
        CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, NULL, NULL, g_hInst,
        NULL );
    return hWnd;
}

void DisplayWnd( HWND hWnd )
{
    ShowWindow( hWnd, SW_SHOW );
    UpdateWindow( hWnd );
}

void Message( )
{
    MSG msg = { 0 };
    while( GetMessage( &msg, NULL, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    g_hInst = hInstance;
    RegisterWnd( "MYWND" );
    HWND hWnd = CreateWnd( "MYWND" );
    DisplayWnd( hWnd );
    Message( );
    return 0;
}
4 字符串资源
  
    4.1 包括字符串的资源
    4.2 使用
      int LoadString(
      HINSTANCE hInstance,//程序句柄
       UINT uID, //字符串资源的ID
        LPTSTR lpBuffer, //存放字符串的BUFF
        int nBufferMax ); //BUFF的大小
      返回获取字符串的长度
      
  5 菜单资源
    5.1 加入菜单资源
    5.2 载入菜单资源
     HMENU LoadMenu(
      HINSTANCE hInstance, //应用程序句柄
      LPCTSTR lpMenuName );//菜单ID字符串
     返回载入成功的菜单的句柄
    5.3 命令处理

      使用加入的菜单ID的宏,在WM_COMMAND消息中,处理菜单命令.

6 加速键资源
    6.1 加速键的作用
      能够使用加速键运行命令. 比如Ctrl+S存盘.
    6.2 加速键资源的加入
    6.3 加速键的使用
      6.3.1 载入
       HACCEL LoadAccelerators(
     HINSTANCE hInstance,//资源所在的应用程序句柄
        LPCTSTR lpTableName ); //加速键表的ID字符串
        载入成功返回加速键表的句柄
      6.3.2 添加消息处理

  int TranslateAccelerator(
     HWND hWnd, //处理加速键的窗体句柄
   HACCEL hAccTable, //加速键表
   LPMSG lpMsg );//MSG结构的地址

    6.4 关于加速键的消息
      TranslateAccelerator的作用是将WM_KEYDOWN或者WM_SYSKEYDOWN消息,翻译成WM_COMMAND或者WM_SYSCOMMAND消息.
      当收到KEYDOWN或者SYSKEYDOWN的消息时,会依据加速键表中按键和命令ID相应关系,找到相应的命令ID,然后调用窗体处理函数,运行WM_COMMAND或者WM_SYSCOMMAND消息.
      当找到相应命令ID并运行后,TranslateAccelerator返回非零。那么就不再运行兴许的处理,消息循环等候下一条消息。

否则,继续让消息循环中的TansnlateMessage和DispatchMessage处理。

/* File : winRes.cpp   
 * Auth : sjin   
 * Date : 20140710   
 * Mail : 413977243@qq.com   
 */      
      
#include <Windows.h>      
#include <stdio.h>
#include <WinUser.h>
#include "Resource.h"

HINSTANCE g_hInst = NULL;
 
BOOL OnSetCursor( HWND hWnd, UINT nMsg, 
    WPARAM wParam, LPARAM lParam )
{
    int nHitTest = LOWORD( lParam );
    if( HTCLIENT != nHitTest )/*为了仅仅处理客户区消息,须要添加这个设置*/
    {
       // return FALSE;
    }

    //获得窗体的客户区
    RECT rcClient = { 0 };
    GetClientRect( hWnd, &rcClient );
    //获得当前光标的位置
    POINT ptPos = { 0 };
    GetCursorPos( &ptPos );
    ScreenToClient( hWnd, &ptPos );
    //依据位置载入光标
    HCURSOR hCursor = NULL;
    if( ptPos.x < rcClient.right/2 )
    {
        if( ptPos.y < rcClient.bottom/2 )
        {
			/*HCURSOR LoadCursor(
			 *	HINSTANCE hInstance, //应用程序实例句柄
			 *	LPCTSTR lpCursorName); //光标的ID
			 *  系统光标: hInstance为NULL,lpCursorName:光标资源ID
			 *  自绘制光标:hInstance不能为NULL,lpCursorName:自绘光标资源ID
			 */
            hCursor = LoadCursor( NULL, IDC_SIZEALL );
        }
        else
        {
            hCursor = LoadCursor( NULL, IDC_CROSS );
        }
    }
    else
    {
        if( ptPos.y < rcClient.bottom/2 )
        {
            hCursor = LoadCursor( NULL, IDC_WAIT );
        }
        else
        {
            hCursor = LoadCursor( NULL, IDC_UPARROW );
        }
    }
    // 设置光标
    SetCursor( hCursor );
    return TRUE;
}

void OnCommand(HWND hWnd, UINT nMsg, 
    WPARAM wParam, LPARAM lParam )
{
	/*ID 号*/
	int nCmdID = LOWORD(wParam);

	switch(nCmdID){

	case ID_40001:
		PostQuitMessage(0);
		break;

	case ID_40002:
		break;

	default:
		break;
	}
}

LRESULT CALLBACK WndProc( HWND   hWnd, 
                          UINT   nMsg,
                          WPARAM wParam,
                          LPARAM lParam )
{
    switch( nMsg )
    {
		/*当鼠标在窗体内就会触发这个消息,能够再程序运行过程中改动光标的样式*/
    case WM_SETCURSOR:

        if( TRUE == OnSetCursor( hWnd, nMsg, wParam, lParam ) )
        {
			/*设置光标属性生效,必须返回*/
            return 0;
        }
        break;

	case WM_COMMAND:/*处理菜单的命令*/
		OnCommand(hWnd, nMsg, wParam, lParam);
		break;
    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;

    }
	/*运行默认的光标属性*/
    return DefWindowProc( hWnd, nMsg,
        wParam, lParam );
}

BOOL RegisterWnd( LPSTR pszClassName )
{
    WNDCLASSEX wce = { 0 };
    wce.cbSize        = sizeof( wce );
    wce.cbClsExtra    = 0;
    wce.cbWndExtra    = 0;
    wce.hbrBackground = HBRUSH(COLOR_WINDOW);
    wce.hCursor       = LoadCursor( g_hInst, MAKEINTRESOURCE(IDC_POINTER) );
    wce.hIcon         = LoadIcon( g_hInst, MAKEINTRESOURCE(IDI_MAIN) );
    wce.hIconSm       = NULL;
    wce.hInstance     = g_hInst;
    wce.lpfnWndProc   = WndProc;
    wce.lpszClassName = pszClassName;
    wce.lpszMenuName  = NULL;
    wce.style         = CS_HREDRAW|CS_VREDRAW;

    ATOM nAtom = RegisterClassEx( &wce );
    if( 0 ==  nAtom )
    {
        return FALSE;
    }

    return TRUE;
}

HWND CreateWnd( LPSTR pszClassName )
{
	/*载入字符串资源
	 * int LoadString(
     *      HINSTANCE hInstance,//程序句柄
     *      UINT uID, //字符串资源的ID
     *      LPTSTR lpBuffer, //存放字符串的BUFF
     *      int nBufferMax ); //BUFF的大小
     *     返回获取字符串的长度
	 *  用户:在多语言支持中使用字符串资源。

*/ char buf[256] = {'\0'}; LoadString(g_hInst,IDS_MAIN,buf,256); /*载入菜单资源 * HMENU LoadMenu( * HINSTANCE hInstance, //应用程序句柄 * LPCTSTR lpMenuName );//菜单ID字符串 * * 返回载入成功的菜单的句柄 */ HMENU hMenu = LoadMenu(g_hInst,MAKEINTRESOURCE(IDR_MAIN)); HWND hWnd = CreateWindowEx( 0, pszClassName, /*"MyWnd"*/buf, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, hMenu, g_hInst, NULL ); return hWnd; } void DisplayWnd( HWND hWnd ) { ShowWindow( hWnd, SW_SHOW ); UpdateWindow( hWnd ); } void Message(HWND hWnd ) { /*载入加速键表 * */ HACCEL hAccel = LoadAccelerators(g_hInst,MAKEINTRESOURCE(IDR_ACCEL)); MSG msg = { 0 }; while( GetMessage( &msg, NULL, 0, 0 ) ) { /*添加加速键的消息处理*/ if(!TranslateAccelerator(hWnd,hAccel,&msg)){ TranslateMessage( &msg ); DispatchMessage( &msg ); } } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInst = hInstance; RegisterWnd( "MYWND" ); HWND hWnd = CreateWnd( "MYWND" ); DisplayWnd( hWnd ); Message(hWnd ); return 0; }




posted on 2017-05-09 18:25  ljbguanli  阅读(211)  评论(0编辑  收藏  举报