📄 polyline.c
字号:
/*
* POLYLINE.C
*
* Window procedure for the polyline drawing window and support functions.
* This window is not complicated. On creation it allocates a block of
* memory for a POLYLINE structure that contains 20 POINTs. We do not
* attempt to reallocate this array at all just to maintain simplicity.
* This sample is to demonstrate OLE, not LocalReAlloc.
*
* Copyright(c) Microsoft Corp. 1992-1994 All Rights Reserved
* Win32 version, January 1994
*/
#include <windows.h>
#include "cosmo.h"
/*
* HACK: To fix some Invalid hDC rips, we need to supress certain
* operations on metafile DC's: GetMapMode, DPtoLP, LPtoDP.
* We use this flag to indicate supression.
*/
BOOL fMetaDC=FALSE;
/*
* HPolylineWindowCreate
*
* Purpose:
* Creates a Polyline window within the client area of hWndParent.
*
* Parameters:
* hWndParent HWND of the parent window.
* hInstance HINSTANCE of the application instance.
*
* Return Value:
* HWND Result of the CreateWindowEx call.
*
*/
HWND WINAPI HPolylineWindowCreate(HWND hWndParent, HINSTANCE hInstance)
{
RECT rc;
HWND hWndT;
/*
* Create the secondary window for this application in a
* shrunk client area.
*/
GetClientRect(hWndParent, &rc);
InflateRect(&rc, -8, -8);
//Create the editor window.
hWndT=CreateWindowEx(WS_EX_NOPARENTNOTIFY, rgpsz[IDS_CLASSPOLYLINE]
, rgpsz[IDS_CLASSPOLYLINE], WS_CHILD | WS_VISIBLE, rc.left
, rc.top, rc.right-rc.left, rc.bottom-rc.top
, hWndParent, (HMENU)ID_POLYLINE, hInstance, NULL);
return hWndT;
}
/*
* PolylineWndProc
*
* Purpose:
* Window procedure for the polyline drawing window.
*
* Parameters:
* The standard.
*
* Return Value:
* Standard.
*/
LRESULT WINAPI PolylineWndProc(HWND hWnd, UINT iMsg
, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hDC;
HWND hWndParent;
HLOCAL hMem;
LPPOLYLINE ppl;
RECT rc;
DWORD dwRet=0L;
#ifdef WIN32
ppl=(LPPOLYLINE)(PSTR)GetWindowLong(hWnd, 0);
#else
ppl=(LPPOLYLINE)(PSTR)GetWindowWord(hWnd, 0);
#endif
if (WM_USER <= iMsg)
return LPolylineUserMessage(hWnd, iMsg, wParam, lParam, ppl);
switch (iMsg)
{
case WM_NCCREATE:
hMem=LocalAlloc(LPTR, CBPOLYLINE);
if (NULL==hMem)
return 0L;
#ifdef WIN32
SetWindowLong(hWnd, 0, (LONG)hMem);
#else
SetWindowWord(hWnd, 0, (WORD)hMem);
#endif
return DefWindowProc(hWnd, iMsg, wParam, lParam);
case WM_NCDESTROY:
#ifdef WIN32
hMem=(HLOCAL)GetWindowLong(hWnd, 0);
#else
hMem=(HLOCAL)GetWindowWord(hWnd, 0);
#endif
LocalFree(hMem);
return DefWindowProc(hWnd, iMsg, wParam, lParam);
case WM_CREATE:
//Stash away the current window rectangle.
GetClientRect(hWnd, &rc);
RECTTORECTS(rc, ppl->rc);
ppl->wVerMaj=VERSIONMAJOR;
ppl->wVerMin=VERSIONMINOR;
ppl->cPoints=0;
break;
case WM_PAINT:
hDC=BeginPaint(hWnd, &ps);
if (0!=ppl->cPoints)
{
ppl->fDrawEntire=TRUE;
PolylineDraw(hWnd, hDC, ppl);
}
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONDOWN:
//Stop if we are already at the limit.
if (CPOLYLINEPOINTS==ppl->cPoints)
{
MessageBeep(0);
break;
}
//Stuff the new point in the array.
ppl->rgpt[ppl->cPoints].x=LOWORD(lParam);
ppl->rgpt[ppl->cPoints].y=HIWORD(lParam);
ppl->cPoints++;
//Draw the lines to this new point only.
hDC=GetDC(hWnd);
ppl->fDrawEntire=FALSE;
PolylineDraw(hWnd, hDC, ppl);
ReleaseDC(hWnd, hDC);
hWndParent=GetParent(hWnd);
#ifdef WIN32
SendMessage(hWndParent, WM_COMMAND
, MAKELONG(ID_POLYLINE, PLN_POINTCHANGE), (LPARAM)hWnd);
#else
SendMessage(hWndParent, WM_COMMAND
, ID_POLYLINE, MAKELONG(hWnd, PLN_POINTCHANGE));
#endif
break;
default:
dwRet=DefWindowProc(hWnd, iMsg, wParam, lParam);
break;
}
return dwRet;
}
/*
* LPolylineUserMessage
*
* Purpose:
* Handles all window-specific messages WM_USER and greater,
* for the Polyline window:
*
* PLM_RECTSET: Changes the size of the window and scales the
* data points.
*
* PLM_POLYLINESET: Sets the current data points and the rectangle
* used to generate the figure.
*
* PLM_POLYLINEGET: Retrieves the current data points and rectangle
* used to generate the figure.
*
* PLM_POLYLINENEW: Resets the data points to defaults, meaning
* a blank figure.
*
* PLM_BACKUPUNDO: Backs the figure up one point.
*
* PLM_BITMAPGET: Retrieves a bitmap (DDB) of the current image.
*
* PLM_METAFILEGET: Retrieves a metafile for the current image.
*
* PLM_METAFILEPICTGET:Retrieves a METAFILEPICT structure of the image for
* use in clipboard I/O.
*
* Parameters:
* hWnd HWND of the Polyline window.
* iMsg UINT message to process.
* wParam WPARAM parameter of the message.
* lParam LPARAM pameter of the message.
* ppl LPPOLYLINE to the window's extra data structure.
*
* Return Value:
* DWORD Value to return from the window procedure
* that recieved the message.
*/
DWORD PASCAL LPolylineUserMessage(HWND hWnd, UINT iMsg, WPARAM wParam
, LPARAM lParam, LPPOLYLINE ppl)
{
DWORD dwRet=0L;
HWND hWndParent;
HBITMAP hBmp, hBmpT;
HDC hDC, hMemDC;
LPPOLYLINE pplT;
LPMETAFILEPICT pMF;
HGLOBAL hMem;
HMETAFILE hMF;
RECT rc;
LPRECT pRect;
UINT i;
LONG l, cx, cy, cxT, cyT;
hWndParent=GetParent(hWnd);
switch (iMsg)
{
case PLM_RECTSET:
/*
* Resize the window to the given size, letting WM_SIZE handlers
* take care of the rest.
*/
pRect=(LPRECT)lParam;
/*
* Scale all the current points to new dimensions. ppl->rc
* has the old dimensions, pRect points to the new. We
* force each of cx and cy to 1 if they are zero to prevent
* exceptions.
*/
RECTSTORECT(ppl->rc, rc);
cxT=rc.right - rc.left;
cyT=rc.bottom - rc.top;
RECTTORECTS(ppl->rc, *pRect);
cx=pRect->right - pRect->left;
cy=pRect->bottom - pRect->top;
//Prevent crashes
if (0L==cxT)
cxT=1;
if (0L==cyT)
cyT=1;
//Loop through each point, scaling if necessary.
for (i=0; i< ppl->cPoints; i++)
{
//Must use DWORD to insure proper scaling.
if (cx!=cxT)
{
l=((LONG)ppl->rgpt[i].x*cx);
ppl->rgpt[i].x=(short)(l/cxT);
}
if (cy!=cyT)
{
l=((LONG)ppl->rgpt[i].y*cy);
ppl->rgpt[i].y=(short)(l/cyT);
}
}
SetWindowPos(hWnd, NULL, pRect->left, pRect->top, (int)cx
, (int)cy, SWP_NOMOVE | SWP_NOZORDER);
//Check if we need to notify the parent.
if (0!=wParam)
{
#ifdef WIN32
SendMessage(hWndParent, WM_COMMAND
, MAKELONG(ID_POLYLINE, PLN_SIZECHANGE), (LPARAM)hWnd);
#else
SendMessage(hWndParent, WM_COMMAND
, ID_POLYLINE, MAKELONG(hWnd, PLN_SIZECHANGE));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -