走进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;
}

走进windows编程的世界-----消息处理函数(4),布布扣,bubuko.com

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

上一篇:Windows 消息机制


下一篇:System.Windows.Markup.IQueryAmbient 在未被应用的程序集中定义