#include "Drawer.h"
static HPEN hPen[6];
static HBRUSH hbrush[6];
static COLORREF col[6];
static int width = 0;
static int psstyle;
#define WM_STATUSBAR 60
static OPENFILENAME ofn;
typedef struct
{
POINT begin;
POINT end;
int width;
COLORREF color;
int psstyle;
int Kind;
}shape;
static vector<shape> shapes;
static list<shape> stacks;
static TCHAR szStatus[] = TEXT("就绪");
static TCHAR szFileName[MAX_PATH], szTitleName[MAX_PATH] ;
static TCHAR szFilter[] = TEXT ("Text Files (*.00)\0*.00\0")\
TEXT ("All Files (*.*)\0*.*\0\0") ;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hInstance = NULL;
HINSTANCE hInst;
//HWND hStatusWnd;
int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR IpCmdLine,int nCmdShow)
{
static TCHAR szAppName[] = TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
g_hInstance = hInstance;
hInst = hInstance;
HACCEL hAccel;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (hInstance,(LPCTSTR)IDI_HEART);
wndclass.hCursor = LoadCursor (hInstance,(LPCTSTR)IDC_HAND);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = (LPCTSTR)IDR_MENU;
wndclass.lpszClassName =szAppName;
if (!RegisterClass (&wndclass))
{
MessageBox(NULL,TEXT("Wrong"),TEXT("WARNING"),MB_ICONERROR);
return 0;
}
hwnd = CreateWindow (szAppName,
TEXT ("简易小画板---高文涛"),
WS_OVERLAPPEDWINDOW,
200,
110,
650,
500,
NULL,
NULL,
hInstance,
NULL);
hAccel = LoadAccelerators(hInstance,MAKEINTRESOURCE(IDR_ACCELERATOR));
ShowWindow (hwnd,nCmdShow);
UpdateWindow (hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccel, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
void DrawBitmap(HDC hdc,int xStart,int yStart,HBITMAP hBitmap)
{
BITMAP bm;
HDC hMemDC;
POINT pt;
hMemDC=CreateCompatibleDC(hdc);
SelectObject(hMemDC,hBitmap);
GetObject(hBitmap,sizeof(BITMAP),&bm);
pt.x=bm.bmWidth;
pt.y=bm.bmHeight;
BitBlt(hdc,xStart,yStart,pt.x,pt.y,hMemDC,0,0,SRCCOPY);
DeleteDC(hMemDC);
}
double Distance(POINT pt,POINT line1,POINT line2)
{
double s1,s2,s3,s,d;
s1 = sqrt((double) (line1.x - pt.x)*(line1.x - pt.x)
+(line1.y - pt.y)*(line1.y - pt.y));
s2 = sqrt((double) (line2.x - pt.x)*(line2.y - pt.x)
+(line2.y- pt.y)*(line2.y - pt.y));
s3 = sqrt((double) (line1.x - line2.x)*(line1.x - line2.x)
+(line1.y - line2.y)*(line1.y - line2.y));
s = (s1 + s2 + s3) / 2;
return d = sqrt( s*(s-s1)*(s-s2)*(s-s3) / s3 );
}
LRESULT CALLBACK WndProc (HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
HPEN hpen;
HBRUSH hBr;
PAINTSTRUCT ps;
HWND hToolsWindow;
HBITMAP hBitmap;
RECT rect;
GetClientRect(hwnd,&rect);
LPNMTTDISPINFO TTtext;
static HWND hwndEdit;
static HINSTANCE hInstance;
static int cxChar,cxCaps,cyChar,cxClient, cyClient;
static int count=0,st = 0;
static int flag;
static bPress = 0;
static BOOL FD = false,SX = false,SAVED = TRUE,bKEY = FALSE,choose = FALSE;
static POINT begin,end,pchoose;
static DWORD dwCharSet = DEFAULT_CHARSET ;
static BOOL curf = false;
static HDC hdcmem;
static HBITMAP hmap;
static HPEN Hpen;
static CHOOSECOLOR cc;
static COLORREF crCustColors[16];
static shape pb,pstack;
static double dis;
static int i,j;
int iReturn;
cc.lStructSize = sizeof(CHOOSECOLOR);
cc.hwndOwner = hwnd;
cc.hInstance = NULL;
cc.rgbResult = RGB(0x80,0x80,0x80);
cc.lpCustColors = crCustColors;
cc.Flags = CC_RGBINIT |CC_FULLOPEN;
cc.lCustData = 0;
cc.lpfnHook = NULL;
cc.lpTemplateName = NULL;
ofn.lStructSize = sizeof (OPENFILENAME) ;
ofn.hwndOwner = hwnd ;
ofn.hInstance = NULL ;
ofn.lpstrFilter = szFilter ;
ofn.lpstrCustomFilter = NULL ;
ofn.nMaxCustFilter = 0 ;
ofn.nFilterIndex = 0 ;
ofn.lpstrFile = szFileName ; // Set in Open and Close functions
ofn.nMaxFile = MAX_PATH ;
ofn.lpstrFileTitle = szTitleName ; // Set in Open and Close functions
ofn.nMaxFileTitle = MAX_PATH ;
ofn.lpstrInitialDir = NULL ;
ofn.lpstrTitle = NULL ;
ofn.Flags = 0 ; // Set in Open and Close functions
ofn.nFileOffset = 0 ;
ofn.nFileExtension = 0 ;
ofn.lpstrDefExt = TEXT ("00") ;
ofn.lCustData = 0L ;
ofn.lpfnHook = NULL ;
ofn.lpTemplateName = NULL ;
switch (message)
{
//////////////////////////// case WM_CREATE: ////////////////////////////////////////
case WM_CREATE:
{
////////////////////////////////// 起始屏 /////////////////////////////////////////
hInstance=(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE);
hdc=CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
hBitmap=LoadBitmap(hInstance,MAKEINTRESOURCE(IDB_BITMAP1));
DrawBitmap(hdc,320,230,hBitmap);
DeleteDC(hdc);
Sleep(1500);
///////////////////////////// 创建工具条 ///////////////////////////////////
TBBUTTON ColorToolBar[21];
for (i = 0; i < 21; i ++)
{
ColorToolBar[i].iBitmap = i;
ColorToolBar[i].idCommand = i;
ColorToolBar[i].fsState = TBSTATE_ENABLED;
ColorToolBar[i].fsStyle = TBSTYLE_CHECKGROUP;
ColorToolBar[i].dwData = 0;
ColorToolBar[i].iString = 0;
}
ColorToolBar[0].fsStyle = TBSTYLE_BUTTON;
ColorToolBar[1].fsStyle = TBSTYLE_BUTTON;
ColorToolBar[2].fsStyle = TBSTYLE_BUTTON;
ColorToolBar[3].fsStyle = TBSTYLE_BUTTON;
ColorToolBar[4].fsStyle = TBSTYLE_BUTTON;
ColorToolBar[5].fsStyle = TBSTYLE_BUTTON;
ColorToolBar[6].fsStyle = TBSTYLE_BUTTON;
ColorToolBar[7].fsStyle = TBSTYLE_BUTTON;
ColorToolBar[13].fsStyle = TBSTYLE_BUTTON;
ColorToolBar[14].fsStyle = TBSTYLE_BUTTON;
ColorToolBar[20].fsStyle = TBSTYLE_BUTTON;
hToolsWindow=::CreateToolbarEx( hwnd,
WS_VISIBLE|CCS_ADJUSTABLE|TBSTYLE_WRAPABLE |TBSTYLE_TOOLTIPS,
IDR_TOOLBAR,
22,
g_hInstance,
IDR_COLORTOOLBAR,
ColorToolBar,
21,
16,
16,
16,
16,
sizeof(TBBUTTON));
TBADDBITMAP ToolBarBitmap;
ToolBarBitmap.hInst = g_hInstance;
ToolBarBitmap.nID = IDR_COLORTOOLBAR;
::SendMessage(hToolsWindow,TB_ADDBITMAP,(WPARAM)3,(LPARAM)&ToolBarBitmap);
//////////////////////////////// 创建状态栏////////////////////////////////////////////
/*hStatusWnd = CreateStatusWindow(
WS_CHILD | WS_VISIBLE,
szStatus,
hwnd,
WM_STATUSBAR
);*/
}
return 0;
///////////////////////////// case WM_COMMAND: ////////////////////////////////
case WM_COMMAND:
bPress = wParam;
hdc = GetDC(hwnd);
switch(wParam)
{
case IDM_FILE_NEW:
case 0:
if(SAVED == FALSE)
iReturn = MessageBox(hwnd,TEXT("文档没有保存,保存吗?"),TEXT("提示"),
MB_YESNOCANCEL|MB_APPLMODAL|MB_ICONQUESTION);
if(iReturn == IDYES)
{
SendMessage (hwnd, WM_COMMAND, 2, 0);
return 0;
}
if(iReturn == IDNO)
{
shapes.clear();
InvalidateRect(hwnd,NULL,true);
SAVED = TRUE;
}
if(iReturn == IDCANCEL)
return 0;
break;
case IDM_FILE_OPEN:
case 1:
{
DWORD dwByteRead;
HANDLE hFile;
shape temp;
ofn.Flags = OFN_HIDEREADONLY | OFN_CREATEPROMPT ;
iReturn = GetOpenFileName (&ofn) ;
if(iReturn == TRUE)
{
hFile=CreateFile(ofn.lpstrFile,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
shapes.clear();
while(ReadFile