C++ win32 開發 GDIPlus 貼圖

來源:互聯網
上載者:User

// 使用說明 需要附加入庫 Gdiplus.lib

//  需要使用三張圖片  分別命名 2.png 3.png 9.png

// 使用VC 或vs 建立空項目win32 ,添加檔案winmain.cpp ,把下面內容加到檔案中

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <gdiplus.h>

bool m_bTest = true;

WCHAR strImgName[100];

int m_number = 0;

/*  GDI+ startup token */  
ULONG_PTR gdiplusStartupToken;  

PAINTSTRUCT ps; // 點結構

int m_waveData[300];

//訊息回呼函數,包含介面訊息
LRESULT CALLBACK MyWindowProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
    );

//主函數入口
int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR    lpCmdLine,
    int       nCmdShow)
{

    /*初始化GDIpuls*/  
    Gdiplus::GdiplusStartupInput gdiInput;  
    Gdiplus::GdiplusStartup(&gdiplusStartupToken,&gdiInput,NULL);  

    //初始化圖片名稱

    wcscpy(strImgName,L"2.png");

    WNDCLASS wndclass; //表單類
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH );
    wndclass.hCursor = LoadCursor( 0, IDC_HAND );
    wndclass.hIcon = LoadIcon( 0, IDI_INFORMATION );
    wndclass.hInstance = hInstance;
    wndclass.lpfnWndProc = MyWindowProc;
    wndclass.lpszClassName = TEXT("hg");
    wndclass.lpszMenuName = NULL;
    wndclass.style = CS_HREDRAW | CS_VREDRAW ;    // 指定表單風格
    //註冊類
    if (!RegisterClass( &wndclass ) )
    {
        MessageBox( 0, TEXT("reg err"), TEXT("reg err"), 0 );
    }
    //建立表單
    HWND hwnd = CreateWindow( TEXT("hg"), TEXT("Helloworldhhh"), WS_OVERLAPPEDWINDOW, 0, 0, 1024, 768, NULL,NULL,hInstance, NULL );
    if ( !hwnd)
    {
        MessageBox( hwnd, TEXT("create windows err"), TEXT("create windows err"), 0 );
    }
    //顯示表單
    ShowWindow( hwnd, SW_SHOW );
    UpdateWindow( hwnd );

    MSG msg;
    //訊息處理
    while ( GetMessage( &msg, 0, 0, 0 ) )
    {
        TranslateMessage( &msg );
        DispatchMessage( &msg );
    }

    return 0;
}

LRESULT CALLBACK MyWindowProc(
    HWND hwnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam
    )
{
    int wmId, wmEvent;

    switch ( uMsg )
    {
    case WM_CLOSE:
        {
            Gdiplus::GdiplusShutdown(gdiplusStartupToken);
            PostQuitMessage(0);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage( 0 );
        break;
    case WM_RBUTTONDBLCLK:  //滑鼠右擊
        //MessageBox(NULL,"dd","ss",MB_OK);
        //DestroyWindow(hwnd);
        break;
    case WM_LBUTTONDOWN: //滑鼠左擊  切換顯示圖片
        if(m_bTest)
        {
            wcscpy(strImgName,L"3.png");
            m_bTest = false;
        }else
        {
            wcscpy(strImgName,L"2.png");
            m_bTest = true;
        }
        
        RedrawWindow(hwnd,NULL,NULL,RDW_INVALIDATE | RDW_UPDATENOW);
        
        break;
    case WM_KEYDOWN:  //鍵盤訊息
        {
            switch(wParam)
            {
            case VK_LEFT:   //左擊
                {
                    wcscpy(strImgName,L"3.png");
                }
                break;
            case VK_RIGHT: //右擊
                {
                    wcscpy(strImgName,L"2.png");
                }
                break;
            case VK_ESCAPE:
                {
                    Gdiplus::GdiplusShutdown(gdiplusStartupToken);
                    PostQuitMessage(0);
                }
                break;
            }
            //有介面修改時,執行更新重畫,切記勿重畫背景
            RedrawWindow(hwnd,NULL,NULL,RDW_INVALIDATE | RDW_UPDATENOW);
        }
        break;
    case WM_PAINT:
        {

            HDC hdc; // DC控制代碼
            HPEN hPen; // 筆控制代碼
            HPEN hPenOld; // 用於儲存原筆的控制代碼
            hdc = BeginPaint (hwnd , &ps); // 獲得DC控制代碼,開始繪製,其中hWnd為視窗控制代碼

            using namespace Gdiplus;   // 加入命名空間

            RECT rc;
            GetClientRect(hwnd,&rc);
            Bitmap bmp(int(rc.right),int(rc.bottom));

            Graphics bmpGraphics(&bmp);
            bmpGraphics.SetSmoothingMode(SmoothingModeAntiAlias);

            /*Drawing on bitmap*/
            //SolidBrush bkBrush(Color(0,0,0));
            //bmpGraphics.FillRectangle(&bkBrush,0,0,rc.right,rc.bottom);

            Gdiplus::Image image2(L"9.png");

            Gdiplus::Image image(strImgName);
            
            Gdiplus::Graphics graphics(hdc);  
            

            bmpGraphics.DrawImage(&image2,0,0,600,400);
            bmpGraphics.DrawImage(&image,100,100,300,100);

            /*Important! Create a CacheBitmap object for quick drawing*/

            bmpGraphics.DrawImage(&image2,0,0,600,400);
            bmpGraphics.DrawImage(&image,100,100,300,100);

            Gdiplus::FontFamily fontFamily(L"Times New Roman"); // 建立字型族對象

            Gdiplus::Font font(&fontFamily, 20); // 建立5號字大小的Times New Roman字型

            Gdiplus::SolidBrush brush(Gdiplus::Color(255, 128, 50)); // 建立綠色的實心刷(寫字串用)
            
            for(int i=0;i<4;i++)
            {
                bmpGraphics.DrawString(strImgName, -1 ,&font,Gdiplus::PointF(400, 110 *i + 20), &brush);
            }
            
             CachedBitmap cachedBmp(&bmp,&graphics);

            // 在此處一併提交,以免出現螢幕閃爍

            graphics.DrawCachedBitmap(&cachedBmp,0,0);

            Gdiplus::Pen myPen(Gdiplus::Color(255, 128, 50));

            //for(int i=0;i<300;i++)
            //{
             //   graphics.DrawLine(&myPen,0,0,300,200);
            //}

            hPen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0) ); // 建立紅色畫筆,寬3
            hPenOld = (HPEN)SelectObject(hdc, hPen); // 選筆入DC
            MoveToEx(hdc, 20, 10, NULL); // 最後一個參數是返回用的舊當前點的結構指標
            //LineTo(hdc, 200, 100) ; // 畫線

            DeleteObject(hPen); // 刪除建立的筆

            EndPaint(hwnd, &ps) ; // 繪製結束

        }
        break;
    //case WM_ERASEBKGND:
    //    break;
    default:
        return DefWindowProc( hwnd, uMsg, wParam, lParam );
    }
    return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.