#include <windows.h>
#include <windowsx.h>
#include"resource.h"
#include "hFuncVar.h"
MYTEXT mytexts[100];
MYLINE mylines[100];
MYRECT myrects[100];
MYELLIPSE myellipses[100];
RECT rect;
POINT pointclik;
int iCountl=0,iCountr=0,iCountw=0,iCounte=0,i=0;
LOGFONT lpFt;
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
MSG msg;
if (!InitWindow (hInstance, iCmdShow))
return FALSE;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
static BOOL InitWindow (HINSTANCE hInstance, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Draw");
HWND hwnd;
WNDCLASS wcMainWnd;
wcMainWnd.style = CS_VREDRAW | CS_HREDRAW|CS_DBLCLKS;
wcMainWnd.lpfnWndProc = WinProc;
wcMainWnd.cbClsExtra = 0;
wcMainWnd.cbWndExtra = 0;
wcMainWnd.hInstance = hInstance;
wcMainWnd.hIcon = LoadIcon (hInstance, IDI_APPLICATION);
wcMainWnd.hCursor = LoadCursor (NULL, IDC_ARROW);
wcMainWnd.hbrBackground =(HBRUSH) GetStockObject (WHITE_BRUSH);
wcMainWnd.lpszMenuName = "IDR_MENU1";
wcMainWnd.lpszClassName = szAppName;
if (!RegisterClass (&wcMainWnd))
{
MessageBox (NULL, TEXT ("注册窗口类失败!"), szAppName,
MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName,
TEXT ("简易绘图--张杰"),
WS_OVERLAPPEDWINDOW,
100,
100,
700,
500,
NULL,
NULL,
hInstance,
NULL);
if( !hwnd ) return FALSE;
ShowWindow( hwnd, iCmdShow );
UpdateWindow( hwnd );
return TRUE;
}
LRESULT CALLBACK WinProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
static HDC hdc;
HMENU hMenu;
static CHOOSECOLOR cc;
static COLORREF crCustomColors[16];
static HINSTANCE hInstance;
static int iSelectionFile=0;
static int iSelectionWord=0;
static int iSelectionColorc=0;
static int iSelectionGraphics=0;
static int iSelectionColorb=0;
switch (message)
{
case WM_CREATE:
hInstance=(HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE);
cc.lStructSize = sizeof (CHOOSECOLOR);
cc.hwndOwner = hWnd;
cc.hInstance = NULL;
cc.rgbResult = RGB (0, 255, 0);
cc.lpCustColors = crCustomColors;
cc.Flags = CC_RGBINIT | CC_FULLOPEN;
cc.lCustData = 0;
cc.lpfnHook = NULL;
cc.lpTemplateName = NULL;
return 0;
case WM_LBUTTONDOWN:
switch(iSelectionGraphics)
{
case IDT_LINE:
mylines[iCountl].PosD.x=GET_X_LPARAM(lParam);
mylines[iCountl].PosD.y=GET_Y_LPARAM(lParam);
break;
case IDT_TANGLE:
myrects[iCountr].myrect.left=GET_X_LPARAM(lParam);
myrects[iCountr].myrect.top=GET_Y_LPARAM(lParam);
break;
case IDT_ELLIPSE:
myellipses[iCounte].myellipse.left=GET_X_LPARAM(lParam);
myellipses[iCounte].myellipse.top=GET_Y_LPARAM(lParam);
break;
}
switch(iSelectionWord)
{
case IDW_WORD:
mytexts[iCountw].myword.left=GET_X_LPARAM(lParam);
mytexts[iCountw].myword.top=GET_Y_LPARAM(lParam);
break;
}
return 0;
case WM_LBUTTONUP:
switch(iSelectionGraphics)
{
case IDT_LINE:
mylines[iCountl].PosU.x=GET_X_LPARAM(lParam);
mylines[iCountl].PosU.y=GET_Y_LPARAM(lParam);
mylines[iCountl].CLline=crCustomColors[0];
iCountl++;
break;
case IDT_TANGLE:
myrects[iCountr].myrect.right=GET_X_LPARAM(lParam);
myrects[iCountr].myrect.bottom=GET_Y_LPARAM(lParam);
if (iSelectionColorb==IDC_BKGD)
myrects[iCountr].CRect=crCustomColors[0];
iCountr++;
break;
case IDT_ELLIPSE:
myellipses[iCounte].myellipse.right=GET_X_LPARAM(lParam);
myellipses[iCounte].myellipse.bottom=GET_Y_LPARAM(lParam);
if (iSelectionColorb==IDC_BKGD)
myellipses[iCounte].CEllipse=crCustomColors[0];
iCounte++;
break;
}
switch(iSelectionWord)
{
case IDW_WORD:
mytexts[iCountw].myword.right=GET_X_LPARAM(lParam);
mytexts[iCountw].myword.bottom=GET_Y_LPARAM(lParam);
DialogBox(hInstance,"IDD_DIALOG1",hWnd,DialogFunc);
mytexts[iCountw].CText=crCustomColors[0];
mytexts[iCountw].lpFont=lpFt;
iCountw++;
break;
case IDW_FONT:
break;
}
if(iCountl>100)
MessageBox(hWnd,"画线缓冲器已满","提示",MB_OK|MB_ICONERROR);
if(iCountr>100)
MessageBox(hWnd,"矩形缓冲器已满","提示",MB_OK|MB_ICONERROR);
if(iCountw>100)
MessageBox(hWnd,"文本缓冲器已满","提示",MB_OK|MB_ICONERROR);
if(iCounte>100)
MessageBox(hWnd,"椭圆缓冲器已满","提示",MB_OK|MB_ICONERROR);
InvalidateRect(hWnd,NULL,TRUE);
return 0;
case WM_LBUTTONDBLCLK:
pointclik.x =GET_X_LPARAM(lParam);
pointclik.y =GET_Y_LPARAM(lParam);
for (i=0;i<iCountr;i++)
{
if ((myrects[i].myrect.left<pointclik.x)&&(pointclik.x<myrects[i].myrect.right) && (myrects[i].myrect.top<pointclik.y)&&(pointclik.y<myrects[i].myrect.bottom))
{
DialogBox(hInstance,"IDD_DIALOG2",hWnd,DialogPro);
InvalidateRect(hWnd,NULL,TRUE);
}
}
return 0;
case WM_COMMAND:
hMenu=GetMenu(hWnd);
switch(LOWORD (wParam))
{
case IDF_EXIT:
SendMessage(hWnd,WM_CLOSE,0,0);
return 0;
case IDF_OPEN:
case IDF_SAVE:
case IDF_SAVEAS:
CheckMenuItem (hMenu, iSelectionFile, MF_UNCHECKED) ;
iSelectionFile = LOWORD (wParam) ;
CheckMenuItem (hMenu, iSelectionFile, MF_CHECKED) ;
FileFunc(hWnd,iSelectionFile);
return 0;
case IDW_WORD:
case IDW_FONT:
CheckMenuItem (hMenu, iSelectionGraphics, MF_UNCHECKED) ;
CheckMenuItem (hMenu, iSelectionWord, MF_UNCHECKED) ;
iSelectionWord = LOWORD (wParam) ;
CheckMenuItem (hMenu, iSelectionWord, MF_CHECKED) ;
iSelectionGraphics=0;
if(iSelectionWord==IDW_FONT)
{
FontFunc( hWnd);
CheckMenuItem (hMenu, iSelectionWord, MF_UNCHECKED) ;
iSelectionWord=IDW_WORD;
CheckMenuItem (hMenu, iSelectionWord, MF_CHECKED) ;
}
return 0;
case IDC_BKGD:
CheckMenuItem (hMenu, iSelectionColorc, MF_UNCHECKED) ;
iSelectionColorb = LOWORD (wParam) ;
CheckMenuItem (hMenu, iSelectionColorb, MF_CHECKED) ;
if (!ChooseColor (&cc))
MessageBox(hWnd,"获取调色板颜色失败","提示",MB_OK|MB_ICONERROR);
return 0;
case IDC_COLOR:
CheckMenuItem (hMenu, iSelectionColorb, MF_UNCHECKED) ;
iSelectionColorc = LOWORD (wParam) ;
CheckMenuItem (hMenu, iSelectionColorc, MF_CHECKED) ;
if (!ChooseColor (&cc))
MessageBox(hWnd,"获取调色板颜色失败","提示",MB_OK|MB_ICONERROR);
return 0;
case IDT_LINE:
case IDT_TANGLE:
case IDT_ELLIPSE:
CheckMenuItem (hMenu, iSelectionWord, MF_UNCHECKED) ;
CheckMenuItem (hMenu, iSelectionGraphics, MF_UNCHECKED) ;
iSelectionGraphics = LOWORD (wParam) ;
CheckMenuItem (hMenu, iSelectionGraphics, MF_CHECKED) ;
iSelectionWord=0;
return 0;
case IDH_ABOVE:
MessageBox(hWnd,"简易绘图工具--第一版--作者:张杰","绘图",MB_OK|MB_ICONQUESTION);
return 0;
}
return 0;
case WM_PAINT:
hdc=BeginPaint (hWnd, &ps);
GetClientRect (hWnd, &rect);
DrawMyLine(hdc,mylines,iCountl);
MyRectangle(hdc,myrects,iCountr);
DrawMyEllipse(hdc,myellipses,iCounte);
DrawMyText(hdc,mytexts,iCountw);
EndPaint (hWnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}