// 使用說明 需要附加入庫 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;
}