Вы находитесь на странице: 1из 74

. ..

WINDOWS:
WIN32 API
..

, 2006.

1. Win32 API
Windows
.
Windows
.
Windows
. , Win32 API,
API Application Program Interface.
Windows
, system32
Windows.
API: Win16 Win32. Win32
32- , Win16 16 1.
GUI (Graphical User Interface)
Windows- 2:
1) : Win32 Consol Application;
2)

Win32 GUI Applications;
3) Win32 Application.
, ,
.
( , .)
Windows- ,
, . Microsoft Visual C++6.0.

.

.
Win32 API
. , , 3,
- . ,
, .. Win32
API4.
.. . ,
Microsoft Visual C++6.0. ,
Win32 API MSDN
(Microsoft Developer Network Library)5.

1
2

. . .: , 2002. 704.
.. Windows C/C++. 1. .: -, 2004.
528.
3 . Microsoft Windows API. . .: , 2004.
1216.
4 .. Win32 API. .: -, 2005. 551.
5 http://msdn.microsoft.com/subscriptions/

.
,
Win32 API. .1

. ,
, ,
, Win32 API.

.1.

Microsoft Visual C++6.0. : File


New Project: Win32 Application Project name: Expl1
Win32 Application Step 1 of 1 An empty project Finish
OK.
Workspace FileView
Source Files,
Add Files to Folder ,
Expl1.cpp .
Expl1.cpp .
1
// 1.
////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
HINSTANCE hin;//
//
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM);
//
ATOM winclass();
HWND makeexamplewin();//
//

void messageprocess();
WINAPI WinMain // Windows
(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
//
hin=hInstance;
//
winclass();
//
HWND winhandler=makeexamplewin();
//
ShowWindow(winhandler,SW_SHOW);
//
messageprocess();
return 0;
}
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
//msg -
switch(msg)
{
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
//
ATOM winclass()
{
// WNDCLASSEX
WNDCLASSEX winexam1;
//
winexam1.cbSize=sizeof(WNDCLASSEX);
//
winexam1.style=CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
//
winexam1.lpfnWndProc=WinProc;
//
winexam1.cbClsExtra=0;
//
winexam1.cbWndExtra=0;
// ,
winexam1.hInstance=hin;
//
winexam1.hIcon=LoadIcon(hin,IDI_APPLICATION);

//
winexam1.hCursor=LoadCursor(hin,IDC_APPSTARTING);
//
winexam1.hbrBackground=HBRUSH(COLOR_WINDOW+1);
//
winexam1.lpszMenuName=NULL;
//
winexam1.lpszClassName="examplewinclass";
//
winexam1.hIconSm=LoadCursor(hin,IDC_APPSTARTING);
//
return RegisterClassEx(&winexam1);
}
//
//
HWND makeexamplewin()
{
return CreateWindowEx
(WS_EX_OVERLAPPEDWINDOW,"examplewinclass",
" ",WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,
hin,NULL);
}
void messageprocess()
{
MSG msg;
//
while(GetMessage(&msg,(HWND) NULL,0,0))
{
// -
DispatchMessage(&msg);
}
}

Expl1 (Ctrl+F7) (Ctrl+F5)


, .2. .2 ,
Expl1
.
1 Windows .
1 .
.3 .
Windows . ,

.
. .
1 LRESULT
CALLBACK WinProc. ,
,
DefWindowProc.
5

.2. 1

Win32 API windows.h,


.

( HWND). 1
winhandler.
Windows API
, , ,
.
2 ( Expl2) ,

SetParent. .
2 ,
. Expl2.cpp,
.

Windows

WINAPI WinMain

winclass()
makeexamplewin()

ShowWindow(,SW_SHOW)

messageprocess()

.3. - 1

WINAPI WinMain // Windows


(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
//
hin=hInstance;
// ,
//
winclass();
//
HWND winhandler1=makeexamplewin();
//
HWND winhandler2=makeexamplewin();
//
HWND winhandler3=makeexamplewin();
//
ShowWindow(winhandler1,SW_SHOW);
//
ShowWindow(winhandler2,SW_SHOW);
//
ShowWindow(winhandler3,SW_SHOW);
//
SetParent(winhandler3,winhandler2);
//
messageprocess();
return 0;
}

.4 Expl2. 2,
makeexamplewin ,
.
,
:

CreateWindow ;
DefWindowProc ;

DestroyWindow ;
GetWindow ;
GetWindowLong ;
SetWindowLong .

:
()
, , , ,
.

.4. ,

, Win32 API,
.
.
.

:

(foreground window) ,
;

(background window) .
Windows ,
Z,
. Z ,
, .
:

(overlapped window) ,
;

(pop-up window)
8

, .;

(child window)
;

(layered window)
;

(message-only window)
, ,
.
:

(taskbar),
;

, ;
;

,
.
3, 3,
: , ,
.
3
//
LRESULT CALLBACK CWinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{return DefWindowProc(hwnd,msg,wParam,lParam);}
LRESULT CALLBACK WinProc //
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
//msg -
switch(msg)
{
//
case WM_CREATE:
//
CreateWindow("child","popup window",
WS_POPUP|WS_CAPTION|WS_VISIBLE,0,0,200,200,
hwnd,NULL,hin,NULL);
//
CreateWindow("child","child window",
WS_CHILD|WS_CAPTION|WS_VISIBLE,0,0,200,200,
hwnd,NULL,hin,NULL);
break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);

}
return 0;
}
//
ATOM winclass(HINSTANCE k)
{
// WNDCLASSEX
WNDCLASSEX winexam1;
//
winexam1.cbSize=sizeof(WNDCLASSEX);
//
winexam1.style=CS_OWNDC;
//
winexam1.lpfnWndProc=WinProc;
//
winexam1.cbClsExtra=0;
//
winexam1.cbWndExtra=0;
// ,
winexam1.hInstance=k;
//
winexam1.hIcon=LoadIcon(k,IDI_APPLICATION);
//
winexam1.hCursor=LoadCursor(k,IDC_APPSTARTING);
//
winexam1.hbrBackground=HBRUSH(COLOR_WINDOW+1);
//
winexam1.lpszMenuName=NULL;
//
winexam1.lpszClassName="examplewinclass";
//
winexam1.hIconSm=LoadCursor(k,IDC_APPSTARTING);
//
RegisterClassEx(&winexam1);
//
winexam1.lpszClassName="child";
winexam1.lpfnWndProc=CWinProc;
//
return RegisterClassEx(&winexam1);
}

3 Expl3.cpp.
Win32 API Microsoft Visual C++6.0
. ,
.5.

10

.5. 3 ,

ArrangeIconicWindows ;
BringWindowToTop ;
CascadeWindows ;
CloseWindow , ;
GetClientRect ;
GetWindowRect ;
GetWindowText ;
IsWindowVisible ;
MoveWindow ;
SetWindowText ;
ShowWindow ;
TileWindow .


, Microsoft Visual C++6.0
,
, MSDN.

11

2. .
64,
.
, , ,
.

:
;
;
;
.
.
4
, ,
. 4 .
Expl4.cpp.
4
WINAPI WinMain // Windows
(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
//
hin=hInstance;
//
winclass();
//
HWND winhandler=makeexamplewin();
//
ShowWindow(winhandler,SW_SHOW);
// GetDC()
// .
TextOut(GetDC(winhandler),120,120,
"! !",24);
//
messageprocess();
return 0;
}

.1.
:

CreateCompatibleDC , ;
CreateDC ;
DeleteDC ;
GetDC ;
GetWindowDC ;
ResetDC ;
ReleaseDC ;
SaveDC .

.1. 4


. ,
WM_PAINT.
BeginPaint, WM_PAINT
EndPaint.
5
, .
5.
Expl5.cpp.
5
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
//
HDC dc; PAINTSTRUCT p;
//msg -
switch(msg)
{
//
case WM_PAINT:
dc=BeginPaint(hwnd,&p);
//
TextOut(dc,120,120," ",17);
//
LineTo(dc,440,440);
EndPaint(hwnd,&p);
break;
case WM_DESTROY: //

//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

.2 Visual C++
Expl5.cpp.

.2. 5

BeginPaint ;
EndPaint ;
GetUpdateRect ;
GetUpdateRgn ;
LockWindowUpdate ;
RedrawWindow ;
UpdateWindow , WM_PAINT.

6
. .
6.
Expl6.cpp.
6

//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)

{
//
HDC dc; PAINTSTRUCT p;
//
RECT rect;
//msg -
switch(msg)
{
//
case WM_PAINT:
dc=BeginPaint(hwnd,&p);
//
GetClientRect(hwnd,&rect);
//
DrawText(dc,", ",
37,&rect,DT_CENTER);
EndPaint(hwnd,&p);
break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

Expl6 Expl6.cpp.
,
.3.
:

DrawText ;
GetTextAlign ;
GetTextCharacterExtra ;
GetTextFace ;
SetTextAlign ;
TextOut .

Win32 API
. 7 .
7 .
Expl7.cpp.

.3. 6

//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
//
HDC dc; PAINTSTRUCT p;
//
RECT rect;
//msg -
switch(msg)
{
//
case WM_PAINT:
dc=BeginPaint(hwnd,&p);
//
RoundRect(dc,200,200,60,60,105,105);
//
Ellipse(dc,70,0,130,60);
//
SetBkColor(dc,0x34ffdd);
TextOut(dc,0,70,"TEXT",4);
EndPaint(hwnd,&p);
break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;

,
, .4.

.4. 7
Win32 API

Chord ;
DrawFocusRect ;
Ellips ;
Pie , ;
Polygon ;
PolyPolygon ;
Rectangle ;
RoundRect .

AngleArc ;
Arc, ArcTo ;
GetArcDirection ;
LineTo ;
MoveToEx ;
PolyBezier, PolyBezierTo ;
Polyline, PolylineTo ;
SetArcDirection .

GetBkColor ;
GetStretchBitMode ;
GetTextColor ;
SetBkColor ;
SetTextColor .

GDI (Graphical User Interface)


6

( 120,
)
. ,
.
Matlab.
.
32- ( COLORREF).
, 0x00 bb gg rr, b , g , r
.
RGB(R,G,B),
GetRValue, GetGValue,
GetBValue.
:

GetObject ;
GetObjectType GDI;
GetStockObject GDI;
SelectObject ;
DeleteObject .

3. , ,

, , ,
Windows :

(DDB Device Dependent Bitmap),


,
;

(DIB Device Independent Bitmap),


.

*.bmp.
8
.
8,
Expl8.cpp.
8
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
//
HDC dc; PAINTSTRUCT ps;
//
RECT r;
static HBITMAP bmdesktop;
static HDC dcdesktop;
static int Ox, Oy;
//msg -
switch(msg)
{
case WM_CREATE:
//
dc=GetDC(NULL);
//
dcdesktop=CreateCompatibleDC(dc);
// X
Ox=GetDeviceCaps(dc,HORZRES);
// Y
Oy=GetDeviceCaps(dc,VERTRES);
// , ( )
bmdesktop=CreateCompatibleBitmap(dc,Ox,Oy);
//
SelectObject(dcdesktop,bmdesktop);
//
BitBlt(dcdesktop,0,0,Ox,Oy,dc,0,0,SRCCOPY);
//
ReleaseDC(NULL,dc);
//
dc=GetDC(hwnd);
//
//
SetStretchBltMode(dc,HALFTONE);

break;
//
case WM_PAINT:
dc=BeginPaint(hwnd,&ps);
//
GetClientRect(hwnd,&r);
// ,
//
StretchBlt(dc,0,0,r.right,r.bottom,
dcdesktop,0,0,Ox,Oy,SRCCOPY);
EndPaint(hwnd,&ps);
break;
case WM_DESTROY: //
//
DeleteDC(dcdesktop);
//
DeleteObject(bmdesktop);
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

.1.

Microsoft Visual C++ Expl8.cpp,


,
. .1 .
:

CreateBitmap ;
CreateCompatibleBitmap ;

CreateDIBitmap DDB DIB;


CreateDIBSection DIB;
GetBitmapDimensionEx ;
GetDIBits ;
GetPixel RGB- ;
SetBitmapDimensionEx ;
SetPixel ;
StretchBlt .


.
: , .
9 .
9.
Expl9.cpp.
9
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
//
HDC dc; PAINTSTRUCT ps;
//
static HBRUSH br1=CreateSolidBrush(0xff0000);
//
static HBRUSH br2=CreateHatchBrush(HS_DIAGCROSS,0x00ff00);
//msg -
switch(msg)
{
//
case WM_PAINT:
dc=BeginPaint(hwnd,&ps);
//
SelectObject(dc,br1);
//
PatBlt(dc,0,0,150,150,PATCOPY);
//
SelectObject(dc,br2);
//
PatBlt(dc,151,0,150,150,PATCOPY);
EndPaint(hwnd,&ps);
break;
case WM_DESTROY: //
//
dc=GetDC(hwnd);
//
SelectObject(dc,GetStockObject(WHITE_BRUSH));
//
DeleteObject(br1);
//
DeleteObject(br2);
//
PostQuitMessage(0);
break;

//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

Expl9.cpp .2.

.2. 9

CreateBrushIndirect , ;
CreateHatchBrush ;
CreatePatternBrush ;
CreateSolidBrush ;
GetBrushOrgEx ;
GetDCBrushColor ;
PatBit ;
SetBrushOrgEx ;
SetDCBrushColor .

, .
, , Times, Arial
. Windows (
), ( True Type,
Open Type, PostScript).
.
: , .
, , ,
, .
, Word.
10 ,
4

Arial, CreateFont.
10.
Expl10.cpp.
10
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static HFONT f; static HFONT oldf;
//
HDC dc; PAINTSTRUCT ps;
//msg -
switch(msg)
{
//
case WM_CREATE:
//
f=CreateFont(25,0,0,0,700,1,0,0,ANSI_CHARSET,
OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY,34,"Arial");
//
dc=GetDC(hwnd);
//
oldf=(HFONT)SelectObject(dc,f);
break;
//
case WM_PAINT:
dc=BeginPaint(hwnd,&ps);
//
TextOut(dc,20,20,"CURSIV ARIAL TEXT",17);
EndPaint(hwnd,&ps);
break;
case WM_DESTROY: //
//
dc=GetDC(hwnd);
// ,
SelectObject(dc,oldf);
DeleteObject(f);//
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

Expl10.cpp.
,
.3.

.3. 10

AddFontResource ;
CreateFont ;
CreateScalableFontResource ;
GetFontData ;
GetGlyphOutline ;
RemoveFontResource .

CreatePen ;
CreatPenIndirect LOGPEN;
GetDCPenColor ;
SetDCPenColor .

.
:


.
:

,
, .. 11
. 11.
Expl11.cpp.
11
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
int ix; HICON ic;
//

HDC dc; PAINTSTRUCT ps;


//msg -
switch(msg)
{
//
case WM_PAINT:
dc=BeginPaint(hwnd,&ps);
ix=0;
//
ic=LoadIcon(NULL,IDI_APPLICATION);
DrawIcon(dc,ix,0,ic);//
ic=LoadIcon(NULL,IDI_ASTERISK);
DrawIcon(dc,ix+=32,0,ic);
ic=LoadIcon(NULL,IDI_EXCLAMATION);
DrawIcon(dc,ix+=32,0,ic);
ic=LoadIcon(NULL,IDI_HAND);
DrawIcon(dc,ix+=32,0,ic);
ic=LoadIcon(NULL,IDI_QUESTION);
DrawIcon(dc,ix+=32,0,ic);
ic=LoadIcon(NULL,IDI_WINLOGO);
DrawIcon(dc,ix+=32,0,ic);
EndPaint(hwnd,&ps);
break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}


Expl11.cpp, , .4.

.4. , 11

CopyIcon ;
CreateIcon ;
CreateIconFromResource ;
DestroyIcon ;
DrawIconEx ;
GetIconInfo ;
LoadIcon .

,
, .
, (hot spot).
.
*.cur *.ani,
.
:

ClipCursor ;
CreateCursor ;
DestroyCursor ;
GetClipCursor ,
;
GetCursor ;
GetCursorInfo ;
GetCursorPos ;
LoadCursor ;
LoadCursorFromFile :
SetCursor ;
SetCursorPos ;
ShowCursor .

4. , , (),

, .
, ,
. 12

. ,
, 12.
Expl12.cpp.
12
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
HRGN rg,rg1; RECT r;
//msg -
switch(msg)
{
case WM_CREATE:
//
GetWindowRect(hwnd,&r);
r.right=r.right-r.left;
r.left=0;
r.bottom=r.bottom-r.top;
r.top=0;
//
rg=CreateRectRgnIndirect(&r);
//
rg1=(HRGN)CreateEllipticRgn(40,40,300,150);
//
CombineRgn(rg,rg,rg1,RGN_XOR);
//
DeleteObject(rg1);
//
rg1=(HRGN)CreateRectRgn(200,200,600,400);
//
CombineRgn(rg,rg,rg1,RGN_XOR);
//
DeleteObject(rg1);
//
SetWindowRgn(hwnd,rg,true);
break;
case WM_DESTROY: //
//
GetWindowRgn(hwnd,rg);
//
DeleteObject(rg);
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}

return 0;
}

Win32 API
Expl12.cpp, , .1.

.1. 12

CombineRgn , ;
CreateEllipticRgn ;
CreatePolygonGrn ;
CreatePolyPolygonRgn ;
CreateRectRgn ;
CreateRoundRectRgn ;
EqualRgn ;
FillRgn ;
FrameRgn ;
GetRegionData ;
GetRgnBox ;
InvertRgn ;
OffsetRgn ;
PaintRgn ;
PtlnRegion , ;
RectlnRegion , ;
SetRectRgn .

,
.
. 13
.
13 .
Expl13.cpp.
2

13
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
HRGN r;
HDC dc; PAINTSTRUCT p;
//msg -
switch(msg)
{
case WM_PAINT:
dc=BeginPaint(hwnd,&p);
//
r=CreateRectRgn(0,0,260,260);
//
SelectClipRgn(dc,r);
//
RoundRect(dc,150,0,280,150,15,15);
//
Ellipse(dc,0,0,150,450);
// , r
DeleteObject(r);
break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

Visual C++ Expl13.cpp,


, ,
.2.
:

ExcludeClipRect ,
;
ExtSelectClipRgn ;
GetClipBox ;
GetClipRgn ;
GetRandomRgn ;
IntersectClipRect ,
;
OffsetClipRgn ;
PtVisible , ;
RectVisible , ;
SelectClipPath ;
SelectClipRgn ;

.2. 13

,
, GDI Win32 API.
14 .
14. Expl14.cpp.
14
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
HDC dc; PAINTSTRUCT ps;
//msg -
switch(msg)
{
case WM_PAINT:
dc=BeginPaint(hwnd,&ps);
BeginPath(dc); //
//
MoveToEx(dc,10,10,NULL);
LineTo(dc,220,30); //
LineTo(dc,160,190); //
LineTo(dc,115,150); //
CloseFigure(dc);//
EndPath(dc);// ,
StrokePath(dc); //
//
EndPaint(hwnd,&ps);
break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//

default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

.3 ,
Expl14.cpp.

.3. 14

BeginPath ;
CloseFigure ;
EndPath ;
FillPath ;
FlattenPath ;
GetPath ;
PathToRegion ;
StrokePath .

Win32 API
RECT. .
, :

CopyRect ;
EqualRect ;
InflateRect ;
IntersectRect ;
IsRectEmpty , ;
OffsetRect ;
PtlnRect ;
SetRect ;
SetRectEmpty ;
SubtractRect ;

UnionRect .

.
, ,
. 15 ,

.
15. Expl15.cpp.
15

//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
POINT ptCursor;
HDC hDC;
char szMsg[128];
//msg -
switch(msg)
{
case WM_MOUSEMOVE:
hDC=GetDC(hwnd);
//
// RGB(255,255,255)
SetTextColor(hDC, RGB(255,255,255));
TextOut(hDC,0,0,szMsg,lstrlen(szMsg));
//
GetCursorPos(&ptCursor);
//
//
ScreenToClient(hwnd,&ptCursor);
wsprintf(szMsg,"Movement of the cursor of mouse X=%d, Y=%d",
ptCursor.x,ptCursor.y);
// RGB(255,0,0)
SetTextColor(hDC, RGB(255,0,0));
TextOut(hDC,0,0,szMsg,lstrlen(szMsg));
ReleaseDC(hwnd,hDC);
break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

Expl15.cpp,
, .4,
.

.4. 15

GetCapture , ;
GetDoubleClickTime ;
GetMouseMovePointsEx ;
ReleaseCapture ;
SetCapture ;
SetDoubleClickTime ;
SwapMouseButton ;
TrackMouseEvent , .

DisspatchMessage ;
GetMessage ;
PostMessage ;
PostQuitMessage ;
RegisterWindowMessage ;
ReplyMessage ;
SendMessage ;
SendNitifyMessage .

Windows,
.
:

- ;

- .
:

5. .

Windows , ,
. .
.
:

BUTTON ;

EDIT ;

LISTBOX ;

COMBOBOX ;

MDICLIENT MDI (
);

SCROLLBAR ();

STATIC .
16 ,
. 16
. Expl16.cpp.
16

//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
HDC dc;
//msg -
switch(msg)
{
case WM_CREATE:
//
CreateWindow("BUTTON"," ",
WS_CHILD|BS_PUSHBUTTON|WS_VISIBLE,
0,0,160,20,hwnd,0,hin,NULL);
CreateWindow("BUTTON"," ",
WS_CHILD|BS_PUSHBUTTON|WS_VISIBLE,
0,40,160,20,hwnd,(HMENU)1,hin,NULL);
break;
// -
case WM_COMMAND:
//
dc=GetDC(hwnd);
switch(LOWORD(wParam))
{
case 0://
if (HIWORD(wParam)==BN_CLICKED)
TextOut(dc,170,20," ",20);
break;
case 1://
if (HIWORD(wParam)==BN_CLICKED)
TextOut(dc,170,20," ",20);
break;
};

break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

,
CreateWindow HMENU.
wParam WM_COMMAND.
.1
.

.1. 16

17
. 17.
Expl17.cpp.
17
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
//msg -
switch(msg)
{
case WM_CREATE:

//
CreateWindow("edit"," 1",
WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_BORDER|
WS_VSCROLL|WS_HSCROLL,50,50,160,80,
hwnd,0,hin,NULL);
CreateWindow("edit"," 2",
WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_BORDER|
WS_VSCROLL|WS_HSCROLL,220,50,160,80,
hwnd,0,hin,NULL);
break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
void messageprocess()
{
MSG msg;
//
while(GetMessage(&msg,(HWND) NULL,0,0))
{
//
TranslateMessage(&msg);
// -
DispatchMessage(&msg);
}
}

, TranslateMessage
,
.
.2
.
18 .
, .

EnumWindows,
EnumW. 18.
Expl18.cpp.

.2. 17

18
//
// hwnd , lParam
BOOL CALLBACK EnumW(HWND hwnd, LPARAM lParam)
{
int len=0; //
//
char*str=new char [84];
// str hwnd
len=GetClassName(hwnd,str,40);
str[len++]=' ';
str[len++]='-';
str[len++]=' ';
// str hwnd
len+=GetWindowText(hwnd,str+len,40);
str[len]=0; // 0
// str lParam
SendMessage((HWND)lParam,LB_ADDSTRING,0,(long)str);
delete [] str; //
// 1
return 1;
}
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
HWND hw;
//msg -
switch(msg)
{
case WM_CREATE:
//
hw=CreateWindow("LISTBOX","",
WS_CHILD|WS_VISIBLE|LBS_STANDARD|

LBS_DISABLENOSCROLL,0,0,480,200,
hwnd,0,hin,NULL);
//
EnumWindows((WNDENUMPROC)EnumW,(long)hw);
break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

.3
Expl18.cpp.

.3. 18

19 , .
,
commctrl.h.
,
. 19.
Expl19.cpp.
19
#include <commctrl.h>
WINAPI WinMain // Windows
(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)

{
//
// :
//Project -> Settings -> Link ->
//Object/library modules
// comctl32.lib
InitCommonControls();
//
hin=hInstance;
//
winclass();
//
HWND winhandler=makeexamplewin();
//
ShowWindow(winhandler,SW_SHOW);
//
messageprocess();
return 0;
}
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
//,
TCITEMA tabitem;
//
static HWND hw;
// ,
static HWND hwtab[3];
int stab; //
//msg -
switch(msg)
{
case WM_CREATE:
//
hw=CreateWindow(WC_TABCONTROL,"",
WS_CHILD|WS_VISIBLE,0,0,150,150,
hwnd,(HMENU)10,hin,NULL);
//
tabitem.mask=TCIF_TEXT;
//
tabitem.pszText="tab1";
//
SendMessage(hw,TCM_INSERTITEM,1,LPARAM(&tabitem));
//
tabitem.pszText="tab2";
//
SendMessage(hw,TCM_INSERTITEM,2,LPARAM(&tabitem));
//
tabitem.pszText="tab3";
//
SendMessage(hw,TCM_INSERTITEM,3,LPARAM(&tabitem));
//

hwtab[0]=CreateWindow("edit"," ",
WS_CHILD|ES_MULTILINE,0,30,149,119,hw,0,hin,NULL);
hwtab[1]=CreateWindow("edit"," ",
WS_CHILD|ES_MULTILINE,0,30,149,119,hw,0,hin,NULL);
hwtab[2]=CreateWindow("button"," ",
WS_CHILD|BS_PUSHBUTTON,5,60,120,30,hw,0,hin,NULL);
break;
//
case WM_NOTIFY:
//
if (((LPNMHDR)lParam)->code==TCN_SELCHANGE )
{
stab=SendMessage(hw,TCM_GETCURSEL,0,0);
//
for (int i=0;i<3;i++)
ShowWindow(hwtab[i],SW_HIDE);
//
ShowWindow(hwtab[stab],SW_SHOW);
}
break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

.4. 19

.4 .
:

CheckDlgButton -;

CheckRadioButton ;
IsDlgButtonChecked
;
DlgDirList ;
DlgDirListComboBox ;
DlgDirSelectEx ;
GetListBoxInfo ;
EnableScrollBar ;
ScrollWindowEx .

Windows ,
.
.


.
:

,
;

,
.
,
DefWindowProc.
.
20
: .
. 20 .
Expl20.cpp.
20
WINAPI WinMain // Windows
(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
//
hin=hInstance;
//
winclass();
//
HWND winhandler=makeexamplewin();
//
ShowWindow(winhandler,SW_SHOW);
//
COLORREF dop[16]; //
//
CHOOSECOLOR ch;
//
ch.lStructSize=sizeof(CHOOSECOLOR);
//
ch.hwndOwner=winhandler;
ch.Flags=CC_ANYCOLOR|CF_SCREENFONTS; //
// ,

//
ch.lpCustColors=dop;
ChooseColor(&ch); //
//
CHOOSEFONT cf;
//
LOGFONT f;
//
cf.lStructSize=sizeof(CHOOSEFONT);
//
cf.hwndOwner=winhandler;
//
cf.Flags=CF_APPLY|CF_SCREENFONTS|CF_EFFECTS;
//,
cf.lpLogFont=&f;
ChooseFont(&cf); //
//
messageprocess();
return 0;
}

.5
20.

.5. 20

CreateDialog, CreateDialogIndirect, CreateDialogIndirectParam, CreateDialogParam


;
DefDlgProc ;
DialogBox ;
EndDialog ;
MessageBox .

6. , ,


.
,
. .

.
21 .

. 15-
. 21.
Expl21.cpp.
21
//
void timerproc(HWND hwnd,UINT msg,
UINT_PTR idevent,DWORD dwtime);
WINAPI WinMain // Windows
(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
//
hin=hInstance;
//
winclass();
//
HWND winhandler=makeexamplewin();
//
ShowWindow(winhandler,SW_SHOW);
//
SetTimer(winhandler,1,2000,(TIMERPROC)timerproc);
//
messageprocess();
return 0;
}
//
void timerproc(HWND hwnd,UINT msg,
UINT_PTR idevent,DWORD dwtime)
{
static int i=0;
//
static RECT rect={0,20,15,150};
if (i<15 && i>0)
{
//
DrawFocusRect(GetDC(hwnd),&rect);
rect.left+=5; //
rect.right+=15; //
//
DrawFocusRect(GetDC(hwnd),&rect);
i++;

}
else
{
if (i==0)
{
//
DrawFocusRect(GetDC(hwnd),&rect);
i++;
}
if (i==15)
KillTimer(hwnd,idevent); //
}
}

.1 ,
30 ,
.

.1. 21

GetSystemTime ;
GetTickCount Windows;
KillTimer ;
QueryPerformanceCounter ;
QueryPerformanceFrequency ;
SetSystemTime ;
SetTimer ;
TimeProc WM_TIMER.

,
.
. Windows
,

. 22 ,
.
22. Expl22.cpp.
22
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
//msg -
switch(msg)
{
case WM_CREATE:
//
CreateCaret(hwnd,NULL,50,25);
ShowCaret(hwnd); //
SetCaretPos(10,10); //
break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

Visual C++ Expl22.cpp,


.
.
. .2 ,
.
:

CreateCaret ;
DestroyCaret ;
GetCaretBlinkTime ;
GetCaretPos ;
HideCaret ;
SetCaretBlinkTime ;
SetCaretPos ;
ShowCaret .

.2. 22:

, ,
.
,
.
WM_COMMAND. 23
.
23. Expl23.cpp.
23
HWND makeexamplewin()
{
MENUITEMINFO item; //
//
item.cbSize=sizeof(MENUITEMINFO);
// ( )
item.fType=MFT_STRING;
// dwTypeData
// MIIM_STRING
//winuser.h 0x00000040
item.fMask=0x00000040;
//
HMENU menu=CreateMenu();
// "OPTIONS"
item.dwTypeData="OPTIONS";
InsertMenuItem(menu,1,0,&item);
// "HELP"
item.dwTypeData="HELP";
InsertMenuItem(menu,2,0,&item);
//
HMENU submenu=CreatePopupMenu();
//
item.dwTypeData="OPEN";

InsertMenuItem(submenu,11,0,&item);
// \t
//
item.dwTypeData="SAVE\tCtrl+S";
InsertMenuItem(submenu,12,0,&item);
//,
item.fMask=MIIM_SUBMENU|0x00000040;
//
item.hSubMenu=submenu;
//
//(& - )
item.dwTypeData="&FILE";
//
InsertMenuItem(menu,1,1,&item);
HWND hw=CreateWindowEx
(WS_EX_OVERLAPPEDWINDOW,"examplewinclass",
" ",WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,
hin,NULL);
//
SetMenu(hw,menu);
//
return hw;
}

.3. 23

Visual C++,
, , .3.
:

CreateMenu ;
CreatePopupMenu ;
DeleteMenu ;
DestroyMenu ;

DrawMenuBar ;
EnableMenuItem ;
EndMenu ;
GetMenu ;
GetMenuBarInfo ;
GetMenuItemCount ;
GetMenuItemID ;
GetMenuItemInfo ;
GetMenuItemRect ;
HiliteMenuItem ;
InsertMenuItem ;
IsMenu , ;
RemoveMenu ;
SetMenu .

CreateAcceleratorTable ;
DestroyAcceleratorTable ;
LoadAccelerators ;
TranslateAccelerator WM_COMMAND
WM_SYSCOMMAND, .


,
. :

7. , ,

.
Windows
.
. , ,
, Far.
DOS. 24 .
24 .
Expl24.cpp.
24
/*

getkey.
:
scan - -,
ascii - ,
vkey - .
*/
unsigned long getkey(char* scan, char* ascii, char* vkey)
{
unsigned long s;
INPUT_RECORD key;
//
HANDLE hinput=GetStdHandle(STD_INPUT_HANDLE);
while (1) //
{
if (ReadConsoleInput(hinput,&key,1,&s) &&
(key.Event.KeyEvent.bKeyDown))
if (key.EventType==KEY_EVENT)
break;
// ,
if (scan)
*scan=(char)key.Event.KeyEvent.wVirtualScanCode;
if (ascii)
*ascii=(char)key.Event.KeyEvent.uChar.AsciiChar;
if (vkey)
*vkey=(char)key.Event.KeyEvent.wVirtualKeyCode;
}
// , Alt
return key.Event.KeyEvent.dwControlKeyState;
}
WINAPI WinMain // Windows
(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
//
hin=hInstance;
unsigned long wi;
COORD coord;
coord.X=0; coord.Y=0;

//
winclass();
//
HWND winhandler=makeexamplewin();
//
ShowWindow(winhandler,SW_SHOW);
AllocConsole(); //
//
SetConsoleTitle("My first console window");
//
HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
//
FillConsoleOutputAttribute(hout,BACKGROUND_BLUE|
BACKGROUND_GREEN|BACKGROUND_RED,80*25,coord,&wi);
//
WriteConsole(hout,"Hello world\n",12,&wi,0);
//
SetConsoleTextAttribute(hout,FOREGROUND_RED|
BACKGROUND_BLUE|BACKGROUND_GREEN|
BACKGROUND_RED);
WriteConsole(hout,"Press any key\n",14,&wi,0);
getkey(NULL,NULL,NULL); //
// ,
FreeConsole();
//
messageprocess();
return 0;
}

Expl24.cpp, .1
: .

.1. 24

AllocConsole ;

CreateConsoleScreenBuffer ;
FlushConsoleInputBuffer ;
FreeConsole ;
GetConsoleCursorInfo ;
GetConsoleScreenBufferInfo ;
GetConsoleTitle ;
ReadConsole ;
ScrollConsoleScreenBuffer ;
SetConsoleActiveScreenBuffer ;
SetConsoleCursorPosition ;
SetConsoleTextAttribute ;
SetConsoleTitle ;
SetConsoleWindowInfo ;
WriteConsole .

Windows
, . 25
, ,
,
. 25 Expl25.cpp.
25
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//
vector <string> disks;
void get_disks(); // disks
//
void get_disks_info();
//
struct DISK_info
{
string name; // , "c:\\"
string type; //
string type_fs; //
unsigned long fnlen; //
unsigned __int64 totalb; //
unsigned __int64 freeb; //
//
void init(string);
void out(); //
};
void DISK_info::init(string _name)
{
char tmp[100];
name=_name;
//
switch (GetDriveType(name.begin()))
{

case DRIVE_UNKNOWN:
type="type cannot be determined"; break;
case DRIVE_REMOVABLE: type="removable"; break;
case DRIVE_FIXED: type="fixed"; break;
case DRIVE_REMOTE: type="remote drive"; break;
case DRIVE_CDROM: type="CD-ROM"; break;
case DRIVE_RAMDISK: type="RAM disk"; break;
default: type="error";
}
//
//
SetErrorMode(SEM_FAILCRITICALERRORS);
//
//
if (GetVolumeInformation(name.begin(),0,0,0,
&fnlen,0,tmp,100))
type_fs=tmp;
else
{
type_fs="disk not inserted";
fnlen=0;
}
//
GetDiskFreeSpaceEx(name.begin(),0,
(PULARGE_INTEGER) &totalb,(PULARGE_INTEGER) &freeb);
}
void DISK_info::out()
{
// long
// Mb
long tot,fre;
cout << "\nDisk name: " << name << "\n";
cout << "Drive type: " << type << "\n";
cout << "Current file system: " << type_fs << "\n";
cout << "Maximum length of file name: " << fnlen << "\n";
if (type_fs != "disk not inserted")
{
tot=totalb/1024; tot=tot/1024;
cout << "Total size of disk: " << tot << " Mb\n";
fre=freeb/1024; fre=fre/1024;
cout << "Free size of disk: " << fre << " Mb\n";
}
else
{
cout << "Total size of disk: " << 0 << " Mb\n";
cout << "Free size of disk: " << 0 << " Mb\n";
}
}
int main(int argc, char* argv[])
{
get_disks();
get_disks_info();
return 0;
}

// disks
void get_disks()
{
//
unsigned long d=GetLogicalDrives();
unsigned long mask=1;
char dname='A';
string tmp;
if (d==0)
{
cout << "some error occur";
return;
}
// disks
for (int i=0; i<='Z'; i++)
{
if (d&mask)
{
tmp="";
tmp+=dname;
tmp=tmp+":\\";
disks.push_back(tmp);
}
mask<<=1;
dname++;
}
}
//
void get_disks_info()
{
DISK_info disk;
int n; //
for (;;)
{
cout << "\nYou have " << disks.size() <<
" logical disks\n";
cout << "Select disk for view more information: ";
cin >> n; //
if (n<disks.size())
{
//
disk.init(disks[n]);
//
disk.out();
}
else break;
}
}

.2 25, ,
, C:\
.

.2. C:\ 25

CopyFileEx ;
CreateDirectoryEx ;
CreateFile ;
DeleteFile ;
FindClose ;
FindFirstFile ;
FindNextFile ;
GetBinaryType ;
GetCurrentDirectory ;
GetDiskFreeSpaceEx ;
GetFileAttributes ;
GetFileSizeEx ;
GetFileType ;
MoveFileEx ;
ReadFile ;
RemoveDirectory ;
ReplaceFile ;
SetEndOfFile ;
SetFileAttributes ;
SetFilePointerEx ;
WriteFile .

*.rc , ,
, , ,
.
, .
RC.EXE,
RES. *.exe
RES .
, 1 65535 (
MAKEINTRESOURSE,
).
26

. 26
. Expl26\Expl26.cpp.
26
//
ATOM winclass()
{
// WNDCLASSEX
WNDCLASSEX winexam1;
//
winexam1.cbSize=sizeof(WNDCLASSEX);
//
winexam1.style=CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
//
winexam1.lpfnWndProc=WinProc;
//
winexam1.cbClsExtra=0;
//
winexam1.cbWndExtra=0;
// ,
winexam1.hInstance=hin;
//
winexam1.hIcon=LoadIcon(hin,IDI_APPLICATION);
//
winexam1.hCursor=LoadCursor(hin,IDC_APPSTARTING);
//
winexam1.hbrBackground=HBRUSH(COLOR_WINDOW+1);
//
winexam1.lpszMenuName="sample";
//
winexam1.lpszClassName="examplewinclass";
//
winexam1.hIconSm=LoadCursor(hin,IDC_APPSTARTING);
//
return RegisterClassEx(&winexam1);
}


Expl26\sample.rc,

Visual C++ . 27
.
27
// sample.rc
sample MENU
{
POPUP "&FILE"
{
MENUITEM "SAVE" 120
MENUITEM "LOAD" 121
MENUITEM "SAVE AS" 122
MENUITEM "EXIT" 123
}

POPUP "S&etup"
{
MENUITEM "&autosave", 124
MENUITEM "&make backup", 125, CHECKED
MENUITEM "", 126
POPUP "&Display option"
{
MENUITEM "color", 127
MENUITEM "resolution", 128
}
}
}

.3. 26

.3 ,
Expl26.cpp.
27 ,
. 28
.
Expl27\Expl27.cpp.
28
//
INT_PTR CALLBACK DlgProc
(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
//
case WM_COMMAND:
//
return EndDialog(hwnd,1);
//

default: return 0;
}
}
//
LRESULT CALLBACK WinProc
(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
//msg -
switch(msg)
{
case WM_COMMAND:
// "about"
if (LOWORD(wParam)==10)
//
DialogBox(hin,"about_dlg",hwnd, (DLGPROC) DlgProc);
break;
case WM_DESTROY: //
//
PostQuitMessage(0);
break;
//
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
//
ATOM winclass()
{
// WNDCLASSEX
WNDCLASSEX winexam1;
//
winexam1.cbSize=sizeof(WNDCLASSEX);
//
winexam1.style=CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
//
winexam1.lpfnWndProc=WinProc;
//
winexam1.cbClsExtra=0;
//
winexam1.cbWndExtra=0;
// ,
winexam1.hInstance=hin;
//
winexam1.hIcon=LoadIcon(hin,IDI_APPLICATION);
//
winexam1.hCursor=LoadCursor(hin,IDC_APPSTARTING);
//
winexam1.hbrBackground=HBRUSH(COLOR_WINDOW+1);
//
winexam1.lpszMenuName="sample";
//
winexam1.lpszClassName="examplewinclass";
//

winexam1.hIconSm=LoadCursor(hin,IDC_APPSTARTING);
//
return RegisterClassEx(&winexam1);
}

27
29. sample.rc Expl27.
29
sample MENU
{
MENUITEM "ABOUT", 10
}
about_dlg DIALOGEX 10, 10, 150, 100
CAPTION "about"
{
LTEXT " , ",
2, 0, 10, 150, 40
CTEXT " OK ",
2, 0, 40, 150, 12
PUSHBUTTON "&OK", 3, 60, 80, 30, 12
}


Expl27\Expl27.cpp Expl27\sample.rc,
ABOUT,
.4.

.4. ,
27

10

8.



Win32 API.

,

. , 6
.
.1
. : P , N , T .
MT .
(0 / 2).

MT

O
N
T

x
.1. ,

T + P sin = 0,

N P cos = 0,
M rP sin = 0.
T

(1)

, :
T f N, MT N,
(2)
f , .
(1), (2), : tg f, tg / r.
f = arctg f
6

.., .., .. . .: , 2002.

= arctg( / r),
.
(2),
:
1) : min( f , );
2) : f ;
3) : f ;
4) , : max( f , ).

.
( x = x( t ), t )
( = ( t )).
:

sin( f )

, f;
mx = P
cos(

J = rP sin( ) , .

cos( )

(3)

(3) ,
f. (3)
, . (3) m, r J ,
. ,
2
, J = 14 mr . (3),

sin( f ) gt 2
, f;
x(t ) =
cos(

)
2
f

4 sin( ) gt 2

(t ) = r cos( ) 2 , ,

(4)

g .

. .2
,
. L.
, ;
1) , ;
2) , f;
3) , ;
4) , r;
2

, L.

, .
.2, x
y : r (1sin) + L cos, r (1+cos) + L sin.
s , ..
s = max{ r (1sin) + L cos, r (1+cos) + L sin}.

r (r / s)min{Lx,Ly}, L (L / s)min{Lx,Ly}
Lx, Ly x y
.
5)

dy
dx

r(1+cos)

Lsin

Lcos

r(1 sin)
2.

A, B, C, D, M :
A = (dx + r (1sin),dy + r (1 + cos) + L sin),
B = (dx + r (1sin),dy + r (1 + cos)),
C = (dx + r,dy + r),
D = (dx + r (1sin) + L cos,dy + r (1 + cos) + L sin),
M = (dx + r + 0.5 r sin,dy + r + 0.5 r cos),
M , .

. 30
. InclPlane.cpp,
Expl29.

30
// 28. ///////////////
////////////////////////// //////////////////
#include <windows.h>
// ,
// InclPlane
#include "resource.h"
#include <stdlib.h>
//
#include <math.h>
//
BOOL CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM);
// Windows
WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
int ret=DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),
NULL,(DLGPROC)DlgProc);
return ret;
}
//
//
#define BufSize 32
char Buf[BufSize];
//
HWND hEdit1,hEdit2,hEdit3,hEdit4,hEdit5;
//
HWND hStatic;
//
HWND hButParam,hButStart,hButClose;
// ,
double angle;
// , f
double fric;
// ,
double fric_s;
// , r
double radius;
// , L
double length;
// , s
double scale;
//
double xCyl,yCyl,xMark,yMark;
// ,
double fi;
//
BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,
WPARAM wParam,LPARAM lParam)
{
//msg -

switch(msg)
{
//
case WM_INITDIALOG:
{
// :
//hEdit1 - , ;
hEdit1=GetDlgItem(hwnd,IDC_EDIT1);
//hEdit2 - , ;
hEdit2=GetDlgItem(hwnd,IDC_EDIT2);
//hEdit3 - , ;
hEdit3=GetDlgItem(hwnd,IDC_EDIT3);
//hEdit4 - , .
hEdit4=GetDlgItem(hwnd,IDC_EDIT4);
//hEdit5 - ,
hEdit5=GetDlgItem(hwnd,IDC_EDIT5);
//
hStatic=GetDlgItem(hwnd,IDC_STATIC6);
//
SetWindowText(hEdit1,"30");
SetWindowText(hEdit2,"0.1");
SetWindowText(hEdit3,"0.01");
SetWindowText(hEdit4,"2.5");
SetWindowText(hEdit5,"20");
//
hButParam=GetDlgItem(hwnd,IDC_Param);
hButStart=GetDlgItem(hwnd,IDC_St);
hButClose=GetDlgItem(hwnd,IDC_Close);
//
EnableWindow(hButStart,FALSE);
return FALSE;
}
//
//
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_EDIT1:
GetWindowText(hEdit1,Buf,BufSize);
// atof()
// double
angle=atof(Buf);
return TRUE;
case IDC_EDIT2:
GetWindowText(hEdit2,Buf,BufSize);
fric=atof(Buf);
return TRUE;
case IDC_EDIT3:
GetWindowText(hEdit3,Buf,BufSize);
fric_s=atof(Buf);
return TRUE;
case IDC_EDIT4:
GetWindowText(hEdit4,Buf,BufSize);
radius=atof(Buf);
return TRUE;

case IDC_EDIT5:
GetWindowText(hEdit5,Buf,BufSize);
length=atof(Buf);
return TRUE;
//
//
//
case IDC_Param:
//
GetWindowText(hEdit1,Buf,BufSize);
angle=atof(Buf);
GetWindowText(hEdit2,Buf,BufSize);
fric=atof(Buf);
GetWindowText(hEdit3,Buf,BufSize);
fric_s=atof(Buf);
GetWindowText(hEdit4,Buf,BufSize);
radius=atof(Buf);
GetWindowText(hEdit5,Buf,BufSize);
length=atof(Buf);
if ((angle<0) || (angle>90))
{
MessageBox(hwnd,
" . \n [0,90]!","",MB_OK);
return TRUE;
}
if (fric<0)
{
MessageBox(hwnd,
" \n !","",MB_OK);
return TRUE;
}
if (fric_s<0)
{
MessageBox(hwnd,
" \n !","",MB_OK);
return TRUE;
}
if (radius<0)
{
MessageBox(hwnd,
" \n !","",MB_OK);
return TRUE;
}
//
//
if (radius>(length/(2*3.14159)))
{
MessageBox(hwnd," !","",MB_OK);
return TRUE;
}
//
//10
if (radius<(length/(20*3.14159)))
{
MessageBox(hwnd, " !","",MB_OK);
return TRUE;

}
if (length<0)
{
MessageBox(hwnd,
" \n !","",MB_OK);
return TRUE;
}
//
EnableWindow(hEdit1,FALSE);
EnableWindow(hEdit2,FALSE);
EnableWindow(hEdit3,FALSE);
EnableWindow(hEdit4,FALSE);
EnableWindow(hEdit5,FALSE);
// Start
EnableWindow(hButParam,FALSE);
EnableWindow(hButStart,TRUE);
return TRUE;
//
case IDC_St:
//
//
int dx,dy,Lx,Ly;
dx=20; dy=40; Lx=300, Ly=230;
HDC hDC;
HRGN Cyl,Marker;
LOGBRUSH lb;
HBRUSH hBrCyl,hBrMark,hBrWh;
hDC=GetDC(hwnd);
//
Rectangle(hDC,dx,dy,dx+Lx,dy+Ly);
// ()
angle=3.14159*(angle/180);
scale=(((radius*(1-sin(angle))+length*cos(angle))>
(radius*(1+cos(angle))+length*sin(angle)))?
(radius*(1-sin(angle))+length*cos(angle)):
(radius*(1+cos(angle))+length*sin(angle)))/
((Lx<Ly)?Lx:Ly);
radius=radius/scale;
length=length/scale;
//
// A
MoveToEx(hDC,(int)(dx+radius*(1-sin(angle))),
(int)(dy+radius*(1+cos(angle))+length*sin(angle)),NULL);
// A B
LineTo(hDC,(int)(dx+radius*(1-sin(angle))),
(int)(dy+radius*(1+cos(angle))));
// B D
LineTo(hDC,(int)(dx+radius*(1-sin(angle))+length*cos(angle)),
(int)(dy+radius*(1+cos(angle))+length*sin(angle)));
// D A
LineTo(hDC,(int)(dx+radius*(1-sin(angle))),
(int)(dy+radius*(1+cos(angle))+length*sin(angle)));
//
lb.lbStyle = BS_SOLID;
lb.lbColor = RGB(255,255,255);
hBrWh = CreateBrushIndirect( &lb );

//
xCyl=dx+radius; yCyl=dy+radius;
Cyl=CreateEllipticRgn((int)(xCyl-radius),(int)(yCyl-radius),
(int)(xCyl+radius),(int)(yCyl+radius));
// -
lb.lbColor = RGB(255,0,0);
hBrCyl = CreateBrushIndirect( &lb );
FillRgn(hDC,Cyl,hBrCyl);
// M,
Marker=CreateEllipticRgn(
(int)(dx+radius+0.5*radius*sin(angle)-4),
(int)(dy+radius-0.5*radius*cos(angle)-4),
(int)(dx+radius+0.5*radius*sin(angle)+4),
(int)(dy+radius-0.5*radius*cos(angle)+4));
// -
lb.lbColor = RGB(0,0,0);
hBrMark = CreateBrushIndirect( &lb );
FillRgn(hDC,Marker,hBrMark);
/////////////// - /////////////
// , :
//angle<min(arctg(fric),arctg(fric_s/radius))
if (angle<=((atan(fric)<atan(fric_s/radius))?atan(fric):
atan(fric_s/radius)))
{
SetWindowText(hStatic," ");
}
////////// - /////////////
// , :
//atan(fric)<angle<arctg(fric_s/radius)
if ((atan(fric)<=atan(fric_s/radius))&&
(angle>atan(fric))&&(angle<=atan(fric_s/radius)))
{
SetWindowText(hStatic," ");
for (int i=1;i<=150;i++)
{
// 150
//
int t=GetTickCount();
while ((GetTickCount()-t)<150){};
xCyl=xCyl+0.01*sin(angle-atan(fric))*cos(angle)*i*i;
yCyl=yCyl+0.01*sin(angle-atan(fric))*sin(angle)*i*i;
if(((xCyl+radius)>(dx+Lx))||
((yCyl+radius)>(dy+radius*(1+cos(angle))+
length*sin(angle)))) break;
FillRgn(hDC,Cyl,hBrWh);
DeleteObject(Cyl);
Cyl=CreateEllipticRgn((int)(xCyl-radius),(int)(yCyl-radius),
(int)(xCyl+radius),(int)(yCyl+radius));
FillRgn(hDC,Cyl,hBrCyl);
Marker=CreateEllipticRgn( (int)(xCyl+0.5*radius*sin(angle)-4),
(int)(yCyl-0.5*radius*cos(angle)-4),
(int)(xCyl+0.5*radius*sin(angle)+4),
(int)(yCyl-0.5*radius*cos(angle)+4));
FillRgn(hDC,Marker,hBrMark);
}
}

///////////// - //////////////////
// , :
//atan(fric_s/radius)<angle<atan(fric)
if ((atan(fric_s/radius)<atan(fric))&&
(angle>atan(fric_s/radius))&&(angle<atan(fric)))
{
SetWindowText(hStatic," ");
for (int i=1;i<150;i++)
{
// 150
//
int t=GetTickCount();
while ((GetTickCount()-t)<150){};
fi=0.01*sin(angle-atan(fric_s/radius))*i*i;
xCyl=xCyl+2*3.14159*fi*cos(angle);
yCyl=yCyl+2*3.14159*fi*sin(angle);
if(((xCyl+radius)>(dx+Lx))||
((yCyl+radius)>(dy+radius*(1+cos(angle))+
length*sin(angle)))) break;
FillRgn(hDC,Cyl,hBrWh);
DeleteObject(Cyl);
Cyl=CreateEllipticRgn((int)(xCyl-radius),(int)(yCyl-radius),
(int)(xCyl+radius),(int)(yCyl+radius));
FillRgn(hDC,Cyl,hBrCyl);
Marker=CreateEllipticRgn(
(int)(xCyl+0.5*radius*sin(angle+fi)-4),
(int)(yCyl-0.5*radius*cos(angle+fi)-4),
(int)(xCyl+0.5*radius*sin(angle+fi)+4),
(int)(yCyl-0.5*radius*cos(angle+fi)+4));
FillRgn(hDC,Marker,hBrMark);
}
}
////////// +//////////
// , :
//angle>max(atan(fric),atan(fric_s))
if (angle>=((atan(fric)>atan(fric_s/radius))?
atan(fric):atan(fric_s/radius)))
{
SetWindowText(hStatic," ");
for (int i=1;i<150;i++)
{
// 150
//
int t=GetTickCount();
while ((GetTickCount()-t)<150){};
fi=0.01*(4/radius)*(sin(angle-atan(fric_s/radius))/
cos(atan(fric_s/radius)))*i*i;
xCyl=xCyl+0.01*(sin(angle-atan(fric))/
cos(atan(fric)))*cos(angle)*i*i;
yCyl=yCyl+0.01*(sin(angle-atan(fric))/
cos(atan(fric)))*sin(angle)*i*i;
if(((xCyl+radius)>(dx+Lx))||
((yCyl+radius)>(dy+radius*(1+cos(angle))+
length*sin(angle)))) break;
FillRgn(hDC,Cyl,hBrWh);
DeleteObject(Cyl);

Cyl=CreateEllipticRgn((int)(xCyl-radius),
(int)(yCyl-radius),(int)(xCyl+radius),
(int)(yCyl+radius));
FillRgn(hDC,Cyl,hBrCyl);
Marker=CreateEllipticRgn(
(int)(xCyl+0.5*radius*sin(angle+fi)-4),
(int)(yCyl-0.5*radius*cos(angle+fi)-4),
(int)(xCyl+0.5*radius*sin(angle+fi)+4),
(int)(yCyl-0.5*radius*cos(angle+fi)+4));
FillRgn(hDC,Marker,hBrMark);
}
}
/////////////////////////////////////////////////////////////
DeleteObject(Cyl);
DeleteObject(Marker);
DeleteObject(hBrCyl);
DeleteObject(hBrMark);
DeleteObject(hBrWh);
ReleaseDC(hwnd,hDC);
//
//
EnableWindow(hEdit1,TRUE);
EnableWindow(hEdit2,TRUE);
EnableWindow(hEdit3,TRUE);
EnableWindow(hEdit4,TRUE);
EnableWindow(hEdit5,TRUE);
// Start
EnableWindow(hButParam,TRUE);
EnableWindow(hButStart,FALSE);
return TRUE;
}
}
case IDC_Close:
{
EndDialog(hwnd,0);
return TRUE;
}
//
case WM_CLOSE:
{
EndDialog(hwnd,0);
return TRUE;
}
}
return FALSE;
}

,
InclPlane Microsoft Visual C++
InclPlane.cpp, resource.h Script.rc.
resource.h Header Files ,
Script.rc Resource Files
. ,
, .3.
10

.3.


.
: , ;
, f; , ; , r;
, L.
, .3.

.4.

,
, .
,
Start, .4.
Start ,
.
, .5
11

.5. : ) ; ) ;
) ; )

12

Вам также может понравиться