Добавил:
sergun
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:I курс / Задача с физическим маятником / fy03
.cpp#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "fy03.h"
#define NUMBUTTONS 5
#define NUMPARTS 5
void InitToolbar (); // Initialize toolbar
LRESULT CALLBACK WindowFunc (HWND, UINT, WPARAM, LPARAM);
BOOL CALLBACK DialogFunc (HWND, UINT, WPARAM, LPARAM);
char szWinName [] = "MainWindow"; /* Window class name */
HINSTANCE hInst;
int nMaxX, nMaxY; // Window size
HDC memDC; // Virtual window DC
HBITMAP hBit; // Bitmap (in memory)
HBRUSH hBrush;
HPEN hOldPen;
HPEN hRedPen, hGreenPen, hBluePen, hDotPen;
int nTimer = !0, nCount = 0, nOverheads = !0;
TBBUTTON tbButtons [NUMBUTTONS]; // Buttons on toolbar
HWND hToolWnd; // Toolbar handle
RECT tbRect;
int nYtb;
HWND hStatusWnd;
RECT stRect;
RECT rc;
int nYst;
int parts [NUMPARTS];
char str [81];
HWND hMainWnd;
//
float nStr2Int (char *lpszStr,float nDef ,float nMax , float nMin);
void vClearMainWindow (void);
int nGetDelta (int nDelta, DWORD dwInit, DWORD *lpdwResult);
float nDelta1 = DELTA1_DEF;
int nT;
double t,Z01,x01,m1,n1;
double a,x1=0.0,L=5,dt=0.02,z1=0.0;
RECT Rect1, Rect2, Rect3;
int WINAPI WinMain (HINSTANCE hThisInst, /* дескриптор текущего экземпляра */
HINSTANCE hPrevInst, /* то же предыдущего (всегда NULL) */
LPSTR lpszArgs, /* аргументы командной строки */
int nWinMode) /* способ визуализации окна */
{
HWND hwnd; // Main window handle
MSG msg; // Messages buffer
WNDCLASS wcl; // Window class definition
HACCEL hAccel; // Accelerators table
// Window class is defined
wcl.hInstance = hThisInst; // Tihs application handle
wcl.lpszClassName = szWinName; // Window class name
wcl.lpfnWndProc = WindowFunc; // Window function
wcl.style = 0; // Window style (0 - by default)
wcl.hIcon = LoadIcon (NULL, IDI_APPLICATION); // Standard icon
wcl.hCursor = LoadCursor (NULL, IDC_ARROW); // Stabdard cursor
wcl.lpszMenuName = "FY03"; // Main menu name (NULL - without)
wcl.cbClsExtra = 0; // Extra data for class (0 - without)
wcl.cbWndExtra = 0; // Extra data for window (0 - wihtout)
wcl.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); // Window background
// Window class is registered
if (!RegisterClass (&wcl)) return 0;
// Window is created
hwnd = CreateWindow (szWinName, // Window class name
"Шарик - Последний отсчёт", // Window title
WS_OVERLAPPEDWINDOW, // Window style
CW_USEDEFAULT, CW_USEDEFAULT, // Upper left corner
CW_USEDEFAULT, CW_USEDEFAULT, // Width, height
HWND_DESKTOP, // Parent window handle (may be NULL)
NULL, // Main menu handle
hThisInst, // Application handle
NULL); // Extra info pointer
hInst = hThisInst;
hMainWnd = hwnd;
hAccel = LoadAccelerators (hThisInst, "FY03"); // Load accelerators
InitToolbar (); // Initialize toolbar
InitCommonControls (); // Initialize common controls
// Create toolbar window
hToolWnd = CreateToolbarEx (hwnd, // Parent window handle
WS_VISIBLE | WS_CHILD |
WS_BORDER | TBSTYLE_TOOLTIPS, // Window style
ID_TOOLBAR, // Toolbar ID
NUMBUTTONS, // Buttons number
hThisInst, // Running application handle
IDTB_BMP, // Bitmap ID
tbButtons, // Buttons info (array of TBUTTON struct)
NUMBUTTONS, // Buttons number
0, 0, // Buttons width & height (0 --- auto)
16, 16, // Bitmap width & height
sizeof (TBBUTTON)); // Structure TBUTTON size
ShowWindow (hwnd, nWinMode); // Show window
GetWindowRect (hToolWnd, &tbRect);
nYtb = tbRect.bottom - tbRect.top;
//vSetTimers ();
UpdateWindow (hwnd); // Update window
//vCalcFuncs ();
// Message cycle is run
while (GetMessage (&msg, // Message
NULL, // Window handle (NULL - all windows)
0, // First message
0)) // Last message (0, 0 - all messages)
{
if (!TranslateAccelerator (hwnd, hAccel, &msg)) {
TranslateMessage (&msg); // Keyboard enable
DispatchMessage (&msg); // Return to windows
}
}
//vKillTimers ();
return msg.wParam;
}
LRESULT CALLBACK WindowFunc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc; // Device context
PAINTSTRUCT paintstruct; // Information for an application
LPTOOLTIPTEXT TTtext;
int i;
switch (message) {
case WM_CREATE :
break;
case WM_NOTIFY : // Request from tooltips
TTtext = (LPTOOLTIPTEXT) lParam;
if (TTtext->hdr.code == TTN_NEEDTEXT) {
switch (TTtext->hdr.idFrom) {
case ID_SETPARMS :
TTtext->lpszText = "Установка значений";
break;
case ID_RUN :
TTtext->lpszText = "Запуск";
break;
case ID_STOP :
TTtext->lpszText = "Остановить";
break;
}
}
break;
case WM_COMMAND : // Menu command
switch (LOWORD (wParam)) {
case ID_SETPARMS :
DialogBox (hInst, "FY03", hwnd, DialogFunc);
break;
case ID_RUN :
SetTimer (hwnd , 0 ,1 , NULL);
break;
case ID_STOP :
KillTimer (hwnd , 0);
break;
case ID_EXIT :
SendMessage (hwnd, WM_DESTROY, 0, 0);
break;
case ID_HELP :
MessageBox (hwnd, "Допустимая величина вводимого угла - 6 градусов , так как колебания - малы.", "Помощь", MB_OK);
break;
case ID_AUTHOR :
MessageBox (hwnd, "Автор данного проекта - Алексей Сукачёв , студент 110 группы физического факультета ! ", "Визитка", MB_OK);
break;
}
break;
case WM_TIMER :
t+=dt;
x1=L*sin(a)*cos(sqrt(9.81/L)*t);
z1=L-sqrt(L*L-x1*x1);
InvalidateRect (hwnd , NULL, TRUE);
break;
case WM_PAINT :
hdc = BeginPaint (hwnd, &paintstruct); // Get device context (screen)
GetClientRect (hwnd, &rc);
x01=rc.left+(x1+L)*(rc.right-10-rc.left)/(2*L);
Z01=rc.bottom-100-z1*(rc.bottom-rc.top)/L;
Ellipse (hdc,(int)(x01-10),(int)(Z01-10),(int)(x01+10),(int)(Z01+10));
m1=rc.left+(rc.right-10-rc.left)/2;
n1=rc.bottom-300;
Arc (hdc,(int)(m1-210),(int)(n1-210),(int)(m1+210),(int)(n1+210),(int)m1 ,(int)n1 ,(int)m1 ,(int)n1 );
EndPaint (hwnd, &paintstruct); // Release device context
break;
case WM_SIZE :
SendMessage (hToolWnd, WM_SIZE, wParam, lParam);
// Set parts number
GetClientRect (hwnd, &stRect);
for (i = 0; i < NUMPARTS; i++)
parts [i] = stRect.right / NUMPARTS * (i + 1);
SendMessage (hStatusWnd, SB_SETPARTS, (WPARAM) NUMPARTS, (LPARAM) parts);
SendMessage (hStatusWnd, WM_SIZE, wParam, lParam);
break;
case WM_DESTROY : // Quit
KillTimer (hwnd , 0);
PostQuitMessage (0);
break;
default : // Default (to windows)
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
BOOL CALLBACK DialogFunc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int nTimerTmp, nCountTmp;
switch (message) {
case WM_INITDIALOG : // Initialize listbox
sprintf (str, "%f", nDelta1);
SetDlgItemText (hwnd, ID_ANGLE, str);
return 1;
case WM_CLOSE :
return 1;
case WM_DESTROY :
return 1;
case WM_COMMAND : // Command
switch (LOWORD (wParam)) {
case ID_CANCEL : EndDialog (hwnd, 0); return 1;
case ID_OK : // Enter end
nTimer = nTimerTmp; nCount = nCountTmp;
if (nTimer){
GetDlgItemText (hwnd, ID_ANGLE, str, 80);
nDelta1=nStr2Int (str ,DELTA1_DEF, DELTA1_MIN, DELTA1_MAX);
a=(nDelta1)*PI_NUM/180;
EndDialog (hwnd, 1);}
else
{GetDlgItemText (hwnd, ID_ANGLE, str, 80);
nDelta1=nStr2Int (str ,DELTA2_DEF, DELTA2_MIN, DELTA2_MAX);
a=(nDelta1);
EndDialog (hwnd, 1);}
return 1;
case ID_RAD1 :
nTimerTmp = !0; nCountTmp = 0;
return 1;
case ID_RAD2 :
nTimerTmp = 0; nCountTmp = !0;
return 1;
}
}
return 0;
}
float nStr2Int (char *lpszStr, float nDef, float nMin, float nMax)
{
float nValue;
if (lpszStr [0] == '\x0') return (nDef);
if (sscanf (lpszStr, "%f", &nValue) == 0) nValue = nDef;
if (nValue < nMin) nValue = nMin;
if (nValue > nMax) nValue = nMax;
return (nValue);
}
void InitToolbar ()
{
tbButtons [0].iBitmap = 0; // Bitmap index (offset in bitmap)
tbButtons [0].idCommand = ID_SETPARMS; // Command, associated with button
tbButtons [0].fsState = TBSTATE_ENABLED; // Button start state
tbButtons [0].fsStyle = TBSTYLE_BUTTON; // Button style
tbButtons [0].dwData = 0L; // Additional info
tbButtons [0].iString = 0; // String, associated with button
//
// Separator 1
tbButtons [1].iBitmap = 0;
tbButtons [1].idCommand = 0;
tbButtons [1].fsState = TBSTATE_ENABLED;
tbButtons [1].fsStyle = TBSTYLE_SEP;
tbButtons [1].dwData = 0L;
tbButtons [1].iString = 0;
//
tbButtons [2].iBitmap = 1;
tbButtons [2].idCommand = ID_RUN;
tbButtons [2].fsState = TBSTATE_ENABLED;
tbButtons [2].fsStyle = TBSTYLE_BUTTON;
tbButtons [2].dwData = 0L;
tbButtons [2].iString = 0;
//
// Separator 2
tbButtons [3].iBitmap = 0;
tbButtons [3].idCommand = 0;
tbButtons [3].fsState = TBSTATE_ENABLED;
tbButtons [3].fsStyle = TBSTYLE_SEP;
tbButtons [3].dwData = 0L;
tbButtons [3].iString = 0;
//
tbButtons [4].iBitmap = 2;
tbButtons [4].idCommand = ID_STOP;
tbButtons [4].fsState = TBSTATE_ENABLED;
tbButtons [4].fsStyle = TBSTYLE_BUTTON;
tbButtons [4].dwData = 0L;
tbButtons [4].iString = 0;
}
void vClearMainWindow (void)
{
MoveToEx (memDC, 0, 0, NULL);
PatBlt (memDC, 0, 0, nMaxX, nMaxY, PATCOPY); // Fills selected region
InvalidateRect (hMainWnd, NULL, TRUE);
}