#include "stdafx.h"
#include "DemoTest.h"
#define MAX_LOADSTRING 100
HINSTANCE hInst; TCHAR szTitle[MAX_LOADSTRING]; TCHAR szWindowClass[MAX_LOADSTRING];
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_pGdiToken;
HWND g_hWnd = 0;
Image *g_pImageBack=0;
BLENDFUNCTION g_Blend;
int g_BakWidth, g_BakHeight;
bool isMouseDown=false;
POINT mousePos;
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void Update();
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
GdiplusStartup(&m_pGdiToken,&m_gdiplusStartupInput,NULL);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MSG msg;
HACCEL hAccelTable;
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_DEMOTEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DEMOTEST));
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
GdiplusShutdown(m_pGdiToken);
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DEMOTEST));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_DEMOTEST);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance;
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
g_Blend.SourceConstantAlpha = int(0 * 2.55); g_Blend.BlendOp=0; g_Blend.BlendFlags=0; g_Blend.AlphaFormat=1; g_Blend.SourceConstantAlpha=255; DWORD dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
SetWindowLong(hWnd, GWL_STYLE, dwExStyle ^ WS_EX_TOOLWINDOW);
dwExStyle = ::GetWindowLong(hWnd, GWL_EXSTYLE);
g_pImageBack = Image::FromFile(_T("C:\\Users\\Cmma\\Desktop\\2.png"));
ImageType t = g_pImageBack->GetType();
g_BakWidth = g_pImageBack->GetWidth();
g_BakHeight = g_pImageBack->GetHeight();
g_hWnd=hWnd;
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
Update();
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_LBUTTONDOWN:
isMouseDown=true;
GetCursorPos(&mousePos);
ScreenToClient(hWnd,&mousePos);
break;
case WM_MOUSEMOVE:
if(isMouseDown)
{
hdc=::GetDC(hWnd);
Graphics g(hdc);
Pen pen(RGB(255,0,0),3);
g.DrawLine(&pen,0,0,100,100);
::ReleaseDC(hWnd,hdc);
SendMessage(hWnd,WM_PAINT,0,0);
}
break;
case WM_LBUTTONUP:
isMouseDown=false;
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
Update();
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
void Update()
{
HDC hdcTemp= ::GetDC(g_hWnd);
HDC hdcMemory=CreateCompatibleDC(hdcTemp);
HBITMAP hBitMap=CreateCompatibleBitmap(hdcTemp, g_BakWidth, g_BakHeight);
SelectObject(hdcMemory, hBitMap);
HDC hdcScreen=::GetDC (g_hWnd);
RECT rct;
::GetWindowRect(g_hWnd, &rct);
POINT ptWinPos={rct.left,rct.top};
Graphics graph(hdcMemory);
Point points[] = { Point(0, 0),
Point(g_BakWidth, 0),
Point(0, g_BakHeight)
};
graph.DrawImage(g_pImageBack, points, 3);
POINT ptDst;
ptDst.x = rct.left;
ptDst.y = rct.top;
SIZE size={g_BakWidth, g_BakHeight};
POINT pt;
pt.x = 0;
pt.y = 0;
DWORD dwExStyle = GetWindowLong(g_hWnd,GWL_EXSTYLE);
if((dwExStyle & WS_EX_LAYERED) != WS_EX_LAYERED)
{ SetWindowLong(g_hWnd, GWL_EXSTYLE,dwExStyle^WS_EX_LAYERED);
}
if (!UpdateLayeredWindow( g_hWnd, hdcScreen, &ptWinPos,
&size, hdcMemory, &pt, 0, &g_Blend, 2))
{
DWORD dwError = ::GetLastError();
printf("failed");
}
graph.ReleaseHDC(hdcMemory);
::DeleteObject(hBitMap);
::DeleteDC(hdcMemory);;
::ReleaseDC(g_hWnd, hdcTemp);
::ReleaseDC(g_hWnd, hdcScreen);
}
|