📄 填充.cpp
字号:
#include<windows.h>
#include<math.h>
#include<stack>
using namespace std;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("LINE") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
memset(&wndclass, 0, sizeof(WNDCLASS));//clear the wndclass
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION ) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1) ;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("填充 sample"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
void Line(HDC hdc,int lxB,int lyB,int lxE,int lyE,COLORREF cr)
{
int x, y, dx, dy, i, Length;
if ( abs(lxE - lxB) >= abs(lyE - lyB) )
Length = abs(lxE - lxB);
else
Length = abs(lyE - lyB);
dx = (lxE - lxB) / Length;
dy = (lyE - lyB) / Length;
x = lxB;
y = lyB;
i = 1;
while (i <= Length)
{
SetPixel(hdc, x, y, cr);
x = x + dx;
y = y + dy;
i = i + 1;
}
}
void SimpleSeedFill( HDC hdc, int ix, int iy,
COLORREF boundary_color, COLORREF fill_color )
{
stack<POINT> IntStack;
int Savex, Xright, Xleft, Pflag, Xenter;
POINT tmpPoint;
tmpPoint.x = ix;
tmpPoint.y = iy;
#define X tmpPoint.x
#define Y tmpPoint.y
IntStack.push( tmpPoint );
while ( IntStack.size() != 0 )
{
tmpPoint = IntStack.top();
IntStack.pop();
SetPixel( hdc, X, Y, fill_color );
Savex = X;
++X;
while ( GetPixel( hdc, X, Y ) != boundary_color )
{
SetPixel( hdc, X, Y, fill_color );
++X;
}
Xright = X - 1;
X = Savex;
X = X - 1;
while ( GetPixel( hdc, X, Y ) != boundary_color )
{
SetPixel( hdc, X, Y, fill_color );
--X;
}
Xleft = X + 1;
X = Savex;
X = Xleft;
++Y;
while ( X <= Xright )
{
Pflag = 0;
while ( ( GetPixel( hdc, X, Y ) != boundary_color ) &&
( GetPixel( hdc, X, Y ) != fill_color ) &&
( X < Xright ) )
{
if ( 0 == Pflag )
{
Pflag = 1;
}
++X;
}
if ( Pflag == 1 )
{
if ( ( X == Xright ) &&
( GetPixel( hdc, X, Y ) != boundary_color ) &&
( GetPixel( hdc, X, Y ) != boundary_color ) )
{
POINT point;
point.x = X;
point.y = Y;
IntStack.push( point );
}
else
{
POINT point;
point.x = X;
point.y = Y;
IntStack.push( point );
}
Pflag = 0;
}
Xenter = X;
while ( ( GetPixel( hdc, X, Y ) == boundary_color ||
GetPixel( hdc, X, Y ) == fill_color ) &&
X < Xright )
{
++X;
}
if ( X = Xenter )
{
++X;
}
}
X = Xleft;
Y -= 2;
while ( X <= Xright )
{
Pflag = 0;
while ( ( GetPixel( hdc, X, Y ) != boundary_color ) &&
( GetPixel( hdc, X, Y ) != fill_color ) &&
( X < Xright ) )
{
if ( 0 == Pflag )
{
Pflag = 1;
}
++X;
}
if ( Pflag == 1 )
{
if ( ( X == Xright ) &&
( GetPixel( hdc, X, Y ) != boundary_color ) &&
( GetPixel( hdc, X, Y ) != boundary_color ) )
{
POINT point;
point.x = X;
point.y = Y;
IntStack.push( point );
}
else
{
POINT point;
point.x = X;
point.y = Y;
IntStack.push( point );
}
Pflag = 0;
}
Xenter = X;
while ( ( GetPixel( hdc, X, Y ) == boundary_color ||
GetPixel( hdc, X, Y ) == fill_color ) &&
X < Xright )
{
++X;
}
if ( X = Xenter )
{
++X;
}
}
}
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
static BOOL bDraw = FALSE;
switch(message)
{
case WM_CREATE:
return 0;
case WM_LBUTTONDOWN:
bDraw = TRUE;
InvalidateRect( hwnd, NULL, TRUE );
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
if ( bDraw )
{
//Line drawing
Line(hdc, 150, 150, 300, 150, RGB(150, 125, 55) );
Line(hdc, 150, 150, 150, 200, RGB(150, 125, 55) );
Line(hdc, 150, 200, 300, 200, RGB(150, 125, 55) );
Line(hdc, 300, 150, 300, 200, RGB(150, 125, 55) );
//填充
SimpleSeedFill( hdc, 175, 175, RGB(150, 125,55), RGB(0,255,0) );
bDraw = FALSE;
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -