// PaintTest.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "PaintTest.h"
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_PAINTTEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_PAINTTEST));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
// 注释:
//
// 仅当希望
// 此代码与添加到 Windows 95 中的“RegisterClassEx”
// 函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
// 这样应用程序就可以获得关联的
// “格式正确的”小图标。
//
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_PAINTTEST));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_PAINTTEST);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
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;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
#include
using namespace std;
#define WINDOW_CX 800
#define WINDOW_CY 600
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
char debugString[100];
static HBITMAP hCloseBit;
static int cxClient, cyClient, cxWindow, cyWindow,cxBord, cyBord;
static BITMAP bitMapClose;
static HDC hdcClose =NULL;
static HDC hdcTemp;
static BOOL isMouseOver = FALSE;
static RECT closeRect;
BOOL isInRect = FALSE;
POINT movePos;
switch (message)
{
case WM_MOUSEMOVE:
//这里是设备逻辑坐标,不是窗口的。映射对其没有影响。
movePos.x = LOWORD(lParam);
movePos.y = HIWORD(lParam);
//转化设备坐标为窗口坐标,注意大转小,还是小转大。还注意double,
movePos.x = (movePos.x - 0)*((double)WINDOW_CX /cxClient ) + 0; //大转化小
movePos.y = (movePos.y - 0)*((double)WINDOW_CY /cyClient) + 0;
//
isMouseOver = PtInRect(&closeRect,movePos);
//将刷新区域的坐标转化为窗口逻辑坐标,将无效区域变小
closeRect.right = 50 *((double)cxClient /WINDOW_CX);
closeRect.bottom = 25 *((double)cyClient /WINDOW_CY);
sprintf(debugString,"right is % d bottom is %d \n",closeRect.right,closeRect.bottom);
OutputDebugStringA(debugString);
InvalidateRect(hWnd,&closeRect,TRUE);
break;
case WM_CREATE:
hCloseBit = LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE(IDB_BITMAP1));
if(hCloseBit==NULL)
OutputDebugStringA("load error\n");
hdcTemp = GetDC(hWnd);
hdcClose = CreateCompatibleDC(hdcTemp);
SelectObject(hdcClose,hCloseBit);
GetObject(hCloseBit,sizeof(BITMAP),&bitMapClose);
SetRect(&closeRect,0,0,50,25);
ReleaseDC(hWnd,hdcTemp);
break;
case WM_LBUTTONDOWN:
movePos.x = LOWORD(lParam);
movePos.y = HIWORD(lParam);
//转化设备坐标为窗口坐标,注意大转小,还是小转大。还注意double,
movePos.x = (movePos.x - 0)*((double)WINDOW_CX /cxClient ) + 0; //大转化小
movePos.y = (movePos.y - 0)*((double)WINDOW_CY /cyClient) + 0;
//
isMouseOver = PtInRect(&closeRect,movePos);
SendMessage(hWnd,WM_CLOSE,NULL,NULL);
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
cxWindow = GetSystemMetrics(SM_CXMAXIMIZED);
cyWindow = GetSystemMetrics(SM_CYMAXIMIZED);
cxBord = GetSystemMetrics(SM_CXSIZEFRAME);
cyBord = GetSystemMetrics(SM_CYSIZEFRAME);
sprintf(debugString,"cxClinet is % d cyClinet is %d \n",cxClient,cyClient);
OutputDebugStringA(debugString);
sprintf(debugString,"cxWindow is % d cyWindow is %d \n",cxWindow,cyWindow);
OutputDebugStringA(debugString);
sprintf(debugString,"cxBord is % d cyBord is %d \n",cxBord,cyBord);
OutputDebugStringA(debugString);
//===============================map===============================//
/*
hdcTemp = GetDC(hWnd);
SetMapMode(hdcTemp,MM_ANISOTROPIC);
SetWindowExtEx(hdcTemp,800,600,NULL);
SetViewportExtEx(hdcTemp,cxClient,cyClient,NULL);
SetViewportExtEx(hdcTemp,0,0,NULL);
/
ReleaseDC(hWnd,hdcTemp);
*/
break;
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_PAINT:
hdc = BeginPaint(hWnd, &ps);
///map
SetMapMode(hdc,MM_ANISOTROPIC);
SetWindowExtEx(hdc,WINDOW_CX ,WINDOW_CY,NULL);
SetViewportExtEx(hdc,cxClient,cyClient,NULL);
SetViewportExtEx(hdc,0,0,NULL);
// TODO: 在此添加任意绘图代码...
sprintf(debugString,"left is % d top is %d right is %d bottom is %d\n",ps.rcPaint.left,ps.rcPaint.top,ps.rcPaint.right,ps.rcPaint.bottom);
OutputDebugStringA(debugString);
if(isMouseOver)
{
StretchBlt(hdc,0,0,50,25,hdcClose,0,0,bitMapClose.bmWidth,bitMapClose.bmHeight,NOTSRCCOPY);
}
else
{
StretchBlt(hdc,0,0,50,25,hdcClose,0,0,bitMapClose.bmWidth,bitMapClose.bmHeight,SRCCOPY);
}
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;
}