⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test-menu.cpp

📁 API函数实现MENU
💻 CPP
字号:
// Test-Menu.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Test-Menu.h"
#include <windows.h>
#include <commctrl.h>

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE			g_hInst;			// current instance
HWND				g_hWndMenuBar;		// menu bar handle

// Forward declarations of functions included in this code module:
ATOM			MyRegisterClass(HINSTANCE, LPTSTR);
BOOL			InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);


//global variable  defined by myself
COLORREF setColor=RGB(0,0,0); //new color for paint
int setWidth=1;       //new width for paint

int xPosStart=0;//the pixel (X,Y) of startpoint
int yPosStart=0;//the pixel (X,Y) of startpoint
int xPosEnd=0;//the pixel (X,Y) of endpoint
int yPosEnd=0;//the pixel (X,Y) of endpoint

enum Shape{pixel,line,ellipse,rectangle,other};
Shape currentS=other;

bool isMenuSelected=false;

HDC   hdc,hMemDc; 
HBITMAP hBitMap ;
HBRUSH hbr;
RECT        rc;



int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
    MSG msg;

    // Perform application initialization:
    if (!InitInstance(hInstance, nCmdShow)) 
    {
        return FALSE;
    }

    HACCEL hAccelTable;
    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TESTMENU));


    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
    WNDCLASS wc;

    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TESTMENU));
    wc.hCursor       = 0;
    wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName  = 0;
    wc.lpszClassName = szWindowClass;

    return RegisterClass(&wc);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;
    TCHAR szTitle[MAX_LOADSTRING];		// title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];	// main window class name

    g_hInst = hInstance; // Store instance handle in our global variable

    // SHInitExtraControls should be called once during your application's initialization to initialize any
    // of the device specific controls such as CAPEDIT and SIPPREF.
    SHInitExtraControls();

    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
    LoadString(hInstance, IDC_TESTMENU, szWindowClass, MAX_LOADSTRING);

    //If it is already running, then focus on the window, and exit
    hWnd = FindWindow(szWindowClass, szTitle);	
    if (hWnd) 
    {
        // set focus to foremost child window
        // The "| 0x00000001" is used to bring any owned windows to the foreground and
        // activate them.
        SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
        return 0;
    } 

    if (!MyRegisterClass(hInstance, szWindowClass))
    {
        return FALSE;
    }

    hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

    if (!hWnd)
    {
        return FALSE;
    }


    // When the main window is created using CW_USEDEFAULT the height of the menubar (if one
    // is created is not taken into account). So we resize the window after creating it
    // if a menubar is present
    if (g_hWndMenuBar)
    {
        RECT rc;
        RECT rcMenuBar;

        GetWindowRect(hWnd, &rc);
        GetWindowRect(g_hWndMenuBar, &rcMenuBar);
        rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);

        MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
    }



    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);


    return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    /*HDC hdc;
    HDC oldHDC;*/
    HPEN hPen;     //new paintpen
    HPEN hOldPen;  //old paintpen






    //int nResult;
    TCHAR       tszOut[] = TEXT("Testing show something!!!!");
    /*RECT        rc;*/

    static SHACTIVATEINFO s_sai;


    switch (message) 
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam); 
        wmEvent = HIWORD(wParam); 
        // Parse the menu selections:
        switch (wmId)
        {
            //TEST CODES:
            /*case IDM_A:

            nResult=MessageBox(hWnd,L"Show A",L"Show A",MB_YESNOCANCEL|MB_ICONEXCLAMATION);
            if(IDNO==nResult)
            {
            MessageBox(hWnd,L"You have chosed the no button",L"no",MB_OK);
            }
            else if(IDYES==nResult)
            {

            MessageBox(hWnd,L"You have chosed the YES button",L"YES",MB_OK);
            }

            break;
            case IDM_B:
            MessageBox(hWnd,L"Show B",L"Show B",MB_OK);
            break; */
        case IDM_COLOR_RED:
            setColor=RGB(255,0,0);
            isMenuSelected=true;
            break;
        case IDM_COLOR_GREEN:
            setColor=RGB(0,255,0);
            isMenuSelected=true;
            break;
        case IDM_WIDTH_1:
            setWidth=1;
            isMenuSelected=true;
            break;
        case IDM_WIDTH_2:
            setWidth=2;
            isMenuSelected=true;
            break;
        case IDM_WIDTH_3:
            setWidth=3;
            isMenuSelected=true;
            break;
        case IDM_WIDTH_4:
            setWidth=4;
            isMenuSelected=true;
            break;
        case IDM_WIDTH_5:
            setWidth=5;
            isMenuSelected=true;
            break;

        case IDM_PIXEL:
            currentS=pixel;
            isMenuSelected=true;
            break;
        case IDM_LINE:
            currentS=line;
            isMenuSelected=true;
            break;
        case IDM_ELLIPSE:
            currentS=ellipse;
            isMenuSelected=true;
            break;
        case IDM_RECTANGLE:
            currentS=rectangle;
            isMenuSelected=true;
            break;
        case IDM_CLEAR:
            InvalidateRect(hWnd,NULL,TRUE);
            FillRect(hMemDc,&rc,hbr);

            isMenuSelected=true;
            break;

        case IDM_HELP_ABOUT:
            hPen=CreatePen(PS_SOLID,setWidth,setColor);
            hOldPen=(HPEN)SelectObject(hdc,hPen);
            DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
            break;
        case IDM_OK:
            SendMessage (hWnd, WM_CLOSE, 0, 0);				
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_CREATE:
        SHMENUBARINFO mbi;

        memset(&mbi, 0, sizeof(SHMENUBARINFO));
        mbi.cbSize     = sizeof(SHMENUBARINFO);
        mbi.hwndParent = hWnd;
        mbi.nToolBarId = IDM_DRAW;
        mbi.hInstRes   = g_hInst;
        mbi.dwFlags    = SHCMBF_HMENU;

        if (!SHCreateMenuBar(&mbi)) 
        {
            g_hWndMenuBar = NULL;
        }
        else
        {
            g_hWndMenuBar = mbi.hwndMB;
        }

        // Initialize the shell activate info structure
        memset(&s_sai, 0, sizeof (s_sai));
        s_sai.cbSize = sizeof (s_sai);

        //create  memoryDC when the window is created (all shape is first painted in memoryDC,the copy memoryDC to the windowDC)
        hdc=GetDC(hWnd);
        GetClientRect (hWnd, (LPRECT)&rc);
        hMemDc = CreateCompatibleDC(hdc);
        hBitMap = CreateCompatibleBitmap(hdc, (rc.right-rc.left),(rc.bottom-rc.top));
        SelectObject(hMemDc, hBitMap);

        hbr=  GetSysColorBrush(COLOR_WINDOW);
        FillRect(hMemDc,&rc,hbr);
        break;
    case WM_PAINT:
        BeginPaint(hWnd, (LPPAINTSTRUCT)&ps);

        //testing function drawtext
        /*   GetClientRect (hWnd, (LPRECT)&rc);
        SetTextColor(hdc,RGB(250,100,100));
        SetBkColor(hdc,GetSysColor(COLOR_3DLIGHT+1));
        DrawText (hdc,tszOut,_tcslen(tszOut),&rc,DT_VCENTER | DT_CENTER | DT_SINGLELINE);*/


        if(false==isMenuSelected)
        {   

             hPen=CreatePen(PS_SOLID,setWidth,setColor);
             hOldPen=(HPEN)SelectObject(hMemDc,hPen);
            switch(currentS)
            {
            case pixel:
                SetPixel(hMemDc,xPosEnd,yPosEnd,setColor);
                break;
            case line:
                MoveToEx(hMemDc,xPosStart,yPosStart,NULL);
                LineTo(hMemDc,xPosEnd,yPosEnd);
                break;

            case ellipse:
                Ellipse(hMemDc,xPosStart,yPosStart,xPosEnd,yPosEnd);
                break;

            case rectangle:
                Rectangle(hMemDc,xPosStart,yPosStart,xPosEnd,yPosEnd);
                break;

            case other:
                // MessageBox(hWnd,L"Please choose the shape!",L"Attention",MB_OK);
                break;


            }

        }
        
        //just test memoryDC
        //hdc=GetDC(hWnd);
        //GetClientRect (hWnd, (LPRECT)&rc);
        //hMemDc = CreateCompatibleDC(hdc);
        //hBitMap = CreateCompatibleBitmap(hdc, (rc.right-rc.left),(rc.bottom-rc.top));
        //SelectObject(hMemDc, hBitMap);

        //first: brush it white


        /*DrawText (hMemDc,tszOut,_tcslen(tszOut),&rc,DT_VCENTER | DT_CENTER | DT_SINGLELINE);
        hPen=CreatePen(PS_SOLID,2,RGB(255,0,0) );
        hOldPen=(HPEN)SelectObject(hMemDc,hPen);
        MoveToEx(hMemDc,0,0,NULL);
        LineTo(hMemDc,100,100);

        BitBlt( hdc, 0, 0, (rc.right-rc.left),(rc.bottom-rc.top), hMemDc, 0, 0, SRCCOPY);*/


        BitBlt( hdc, 0, 0, (rc.right-rc.left),(rc.bottom-rc.top), hMemDc, 0, 0, SRCCOPY);

        //DeleteDC(hMemDc);


        EndPaint(hWnd, (LPPAINTSTRUCT)&ps);

        isMenuSelected=false;
        xPosStart=0;
        yPosStart=0;
        xPosEnd=0;
        yPosEnd=0;
        break;
    case WM_DESTROY:
        CommandBar_Destroy(g_hWndMenuBar);
        DeleteDC(hMemDc);
        PostQuitMessage(0);
        break;

    case WM_ACTIVATE:
        // Notify shell of our activate message
        SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
        break;
    case WM_SETTINGCHANGE:
        SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
        break;
    case WM_LBUTTONDOWN:

        xPosStart = LOWORD(lParam); 
        yPosStart = HIWORD(lParam); 

        break;
    case WM_LBUTTONUP:

        xPosEnd = LOWORD(lParam); 
        yPosEnd = HIWORD(lParam); 
        SendMessage(hWnd,WM_PAINT,0,0);

        break;


    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}


// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_INITDIALOG:
        {
            // Create a Done button and size it.  
            SHINITDLGINFO shidi;
            shidi.dwMask = SHIDIM_FLAGS;
            shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
            shidi.hDlg = hDlg;
            SHInitDialog(&shidi);
        }
        return (INT_PTR)TRUE;

    case WM_COMMAND:


        if (LOWORD(wParam) == IDOK)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return TRUE;
        }
        break;

    case WM_CLOSE:
        EndDialog(hDlg, message);
        return TRUE;

#ifdef _DEVICE_RESOLUTION_AWARE
    case WM_SIZE:
        {
            DRA::RelayoutDialog(
                g_hInst, 
                hDlg, 
                DRA::GetDisplayMode() != DRA::Portrait ? MAKEINTRESOURCE(IDD_ABOUTBOX_WIDE) : MAKEINTRESOURCE(IDD_ABOUTBOX));
        }
        break;
#endif
    }
    return (INT_PTR)FALSE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -