Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include "resource.h"
- #include "CommCtrl.h"
- #include "CommDlg.h"
- #include <vector>
- #define ID_STATUS 300
- LRESULT CALLBACK WndProc(HWND hwnd, UINT messg, WPARAM wParam, LPARAM lParam);
- BOOL CALLBACK SettingProc(HWND hdlg, UINT messg, WPARAM wParam, LPARAM lParam);
- void Draw_all_Ellipses(int x,int y, int n);
- void Draw_Ellipses_and_Find_Points(int x,int y, int n);
- bool first = true,step = true,fst = true;
- int count = 1;
- std::vector<POINT> vec;
- std::vector<POINT>::iterator it;
- HPEN hpen;
- HBRUSH hbrush;
- HDC hdc;
- DWORD dColors[3]={0};
- char file[80] = "";
- COLORREF color=RGB(0,0,0);
- PAINTSTRUCT ps;
- static HINSTANCE hInst;
- HWND hWnd, hStatus;
- int be = 41;
- int r = 0;
- int n = 0;
- int x = 400;
- int y = 380;
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
- {
- MSG lpMsg;
- WNDCLASS w;
- hInst = hInstance;
- w.lpszClassName = "Моё окно";
- w.hInstance = hInstance;
- w.lpfnWndProc = WndProc;
- w.hCursor = LoadCursor(NULL, IDC_ARROW);
- w.hIcon = LoadIcon(NULL,"IDI_ICON1");
- w.lpszMenuName = "IDR_MENU1";
- w.hbrBackground = CreateSolidBrush(RGB(250,250,250));
- w.style = CS_HREDRAW|CS_VREDRAW;
- w.cbClsExtra = 0;
- w.cbWndExtra = 0;
- if(!RegisterClass(&w))
- return 0;
- hWnd=CreateWindow("Моё окно",
- "Треугольный секундомер",
- WS_OVERLAPPEDWINDOW,
- 250,
- 100,
- 800,
- 500,
- (HWND)NULL,
- (HMENU)NULL,
- (HINSTANCE)hInstance,
- (HINSTANCE)NULL);
- ShowWindow(hWnd,nCmdShow);
- UpdateWindow(hWnd);
- hStatus = CreateStatusWindow(WS_CHILD|WS_VISIBLE, "Количество строк: 0, Радиус: 0, Цвет: 0,0,0\n", hWnd, ID_STATUS);
- ShowWindow(hStatus,nCmdShow);
- UpdateWindow(hStatus);
- while(GetMessage(&lpMsg, NULL, 0, 0))
- {
- TranslateMessage(&lpMsg);
- DispatchMessage(&lpMsg);
- }
- return(lpMsg.wParam);
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT messg, WPARAM wParam, LPARAM lParam)
- {
- OPENFILENAME of;
- of.lStructSize = sizeof(OPENFILENAME);
- of.hwndOwner = hWnd;
- of.lpstrFilter = "txt\000 *.txt\000\000";
- of.lpstrFile = file;
- of.nMaxFile = MAX_PATH;
- of.lpstrFileTitle = NULL;
- of.lpstrInitialDir = "D:\\";
- of.lpstrTitle = "Открытие файла настроек";
- of.Flags = OFN_HIDEREADONLY;
- of.hInstance = hInst;
- of.lpTemplateName = NULL;
- of.lpstrDefExt = NULL;
- of.lpstrCustomFilter = NULL;
- CHOOSECOLOR cc;
- cc.Flags=CC_RGBINIT|CC_FULLOPEN;
- cc.hInstance=NULL;
- cc.hwndOwner=hWnd;
- cc.lCustData=0L;
- cc.lpCustColors=dColors;
- cc.lpfnHook=NULL;
- cc.lpTemplateName=(LPSTR)NULL;
- cc.lStructSize=sizeof(cc);
- cc.rgbResult=RGB(255,0,0);
- PAINTSTRUCT ps;
- char Buf[256];
- switch(messg)
- {
- case WM_SIZE:
- MoveWindow(hStatus, 0,0,0,0, TRUE);
- break;
- case WM_PAINT:
- if(!n)
- {
- ValidateRect(hwnd,NULL);
- break;
- }
- ShowWindow(hStatus,SW_NORMAL);
- UpdateWindow(hStatus);
- hdc = BeginPaint(hwnd,&ps);
- hpen = CreatePen(1,2,RGB(200,0,0));
- SelectObject(hdc,hpen);
- if(first)
- {
- Draw_Ellipses_and_Find_Points(x,y,n);
- it = vec.begin();
- first = false;
- }
- else
- {
- Draw_all_Ellipses(x,y,n);
- if(step)
- {
- hbrush = CreateSolidBrush(color);
- SelectObject(hdc,hbrush);
- Ellipse(hdc,(it -> x) - 2*r,(it -> y) - 2*r,(it -> x) + 2*r,(it -> y) + 2*r);
- DeleteObject(hbrush);
- }
- else
- {
- hbrush = CreateSolidBrush(RGB(255,255,255));
- SelectObject(hdc,hbrush);
- Ellipse(hdc,(it -> x) - r,(it -> y) - r,(it -> x) + r,(it -> y) + r);
- DeleteObject(hbrush);
- it++;
- if(fst)
- {
- it--;
- fst = false;
- }
- if(it == vec.end())
- {
- it = vec.begin();
- KillTimer(hWnd,1);
- }
- }
- }
- DeleteObject(hpen);
- ValidateRect(hwnd,NULL);
- EndPaint(hwnd,&ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- case WM_TIMER:
- if(count%2)
- step = true;
- else
- step = false;
- count++;
- InvalidateRect(hwnd,NULL,true);
- break;
- case WM_COMMAND:
- switch(wParam)
- {
- case ID_40004:
- MessageBox(hwnd,"Здесь могла быть ваша реклама!","Место для рекламы",MB_ICONINFORMATION|MB_OK);
- break;
- case ID_40002:
- DialogBox(hInst,"IDD_DIALOG",hwnd,SettingProc);
- break;
- case ID_40001:
- GetOpenFileName(&of);
- break;
- case ID_40003:
- PostQuitMessage(0);
- break;
- case ID_40005:
- if(ChooseColor(&cc))
- {
- color=(COLORREF)cc.rgbResult;
- std::sprintf(Buf, "Количество строк: %d, Радиус: %d, Цвет: %d,%d,%d\n",n,r,GetRValue(color),GetGValue(color),GetBValue(color));
- SendMessage(hStatus, SB_SETTEXT, 0, (LONG)Buf);
- }
- break;
- default:
- return DefWindowProc(hwnd,messg,wParam,lParam);
- }
- break;
- default:
- return DefWindowProc(hwnd,messg,wParam,lParam);
- }
- return 0;
- }
- BOOL CALLBACK SettingProc(HWND hdlg, UINT messg, WPARAM wParam, LPARAM lParam)
- {
- char Buf[256];
- int c1,c2,c3,red,green,blue,i = 0;
- switch(messg)
- {
- case WM_INITDIALOG:
- return true;
- case WM_COMMAND:
- switch(wParam)
- {
- case IDNO: case WM_CLOSE:
- EndDialog(hdlg,LOWORD(wParam));
- break;
- case IDOK:
- if(SendDlgItemMessage(hdlg,IDC_RADIO2,BM_GETCHECK,0,0))
- {
- _lread(_lopen(file,OF_READ),Buf,30);
- n = atoi(&Buf[i]);
- while(Buf[i++] != ' ');
- r = atoi(&Buf[i++]);
- while(Buf[i++] != ' ');
- red = atoi(&Buf[i++]);
- while(Buf[i++] != ' ');
- green = atoi(&Buf[i++]);
- while(Buf[i++] != ' ');
- blue = atoi(&Buf[i++]);
- color = RGB(red,green,blue);
- }
- if(SendDlgItemMessage(hdlg,IDC_RADIO1,BM_GETCHECK,0,0))
- {
- char tempn[5],tempr[5];
- GetDlgItemText(hdlg,IDC_EDIT1,tempn,4);
- n = atoi(tempn);
- GetDlgItemText(hdlg,IDC_EDIT2,tempr,4);
- r = atoi(tempr);
- c1 = SendDlgItemMessage(hdlg,IDC_CHECK1,BM_GETCHECK,0,0);
- c3 = SendDlgItemMessage(hdlg,IDC_CHECK2,BM_GETCHECK,0,0);
- c2 = SendDlgItemMessage(hdlg,IDC_CHECK3,BM_GETCHECK,0,0);
- if(c1&&c2&&c3)
- color = RGB(255,255,255);
- if(c1&&c2&&(!c3))
- color = RGB(255,255,0);
- if(c1&&(!c2)&&c3)
- color = RGB(255,0,255);
- if(c1&&(!c2)&&(!c3))
- color = RGB(255,0,0);
- if((!c1)&&c2&&c3)
- color = RGB(0,255,255);
- if((!c1)&&(!c2)&&c3)
- color = RGB(0,0,255);
- if((!c1)&&c2&&(!c3))
- color = RGB(0,255,0);
- }
- first = true;
- std::sprintf(Buf, "Количество строк: %d, Радиус: %d, Цвет: %d,%d,%d\n",n,r,GetRValue(color),GetGValue(color),GetBValue(color));
- SendMessage(hStatus, SB_SETTEXT, 0, (LONG)Buf);
- EndDialog(hdlg,LOWORD(wParam));
- SetTimer(hWnd,1,500,NULL);
- break;
- default:
- return DefWindowProc(hdlg,messg,wParam,lParam);
- }
- break;
- default:
- return DefWindowProc(hdlg,messg,wParam,lParam);
- }
- return 0;
- }
- void Draw_all_Ellipses(int a,int b, int n)
- {
- a += (n-1)*(2*r+be)/1.41;
- for(int i = 0; i < n; i++)
- {
- Ellipse(hdc,a-r,b-r,a+r,b+r);
- a -= (2*r+be)*1.41;
- }
- POINT prevline[20];
- for (int i = n-1, j = 0; i <= 2*n-2;)
- prevline[j++] = vec[i++];
- for(int j = n; j > 1;)
- {
- POINT curline[20];
- int i = 1;
- for(i; i < j;)
- {
- POINT p;
- p.x = (prevline[i-1].x + prevline[i].x)/2;
- p.y = prevline[i-1].y - (prevline[i-1].x - prevline[i].x)/2;
- curline[i-1] = p;
- Ellipse(hdc,p.x - r, p.y - r, p.x + r, p.y + r);
- i++;
- }
- for(int k = 0; k < j; k++)
- prevline[k] = curline[k];
- j--;
- }
- }
- void Draw_Ellipses_and_Find_Points(int a,int b, int n)
- {
- vec.erase(vec.begin(),vec.end());
- a += (n-1)*(2*r+be)/1.41;
- for(int i = 0; i < n; i++)
- {
- Ellipse(hdc,a-r,b-r,a+r,b+r);
- POINT p;
- p.x = a;
- p.y = b;
- vec.push_back(p);
- a -= (2*r+be)*1.41;
- }
- POINT prevline[20];
- for (int i = 0; i < n; i++)
- prevline[i] = vec[i];
- for(int j = n; j > 1;)
- {
- POINT curline[20];
- int i = 1;
- for(i; i < j;)
- {
- POINT p;
- p.x = (prevline[i-1].x + prevline[i].x)/2;
- p.y = prevline[i-1].y - (prevline[i-1].x - prevline[i].x)/2;
- if( i == 1 )
- vec.insert(vec.begin(),p);
- else
- {
- if( i == j-1 )
- vec.push_back(p);
- }
- curline[i-1] = p;
- Ellipse(hdc,p.x - r, p.y - r, p.x + r, p.y + r);
- i++;
- }
- for(int k = 0; k < j; k++)
- prevline[k] = curline[k];
- j--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement