下面是一个很经典的程序,画图程序:可以画图,可以擦掉(来自Windows程序设计第五版)
#include<windows.h>
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // command line
int nCmdShow // show state
)
{
static TCHAR szAppName[]=TEXT("leidemingzi");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WindowProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("the program require the windows nt"),TEXT("tips"),MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(
szAppName, // registered class name
TEXT("this is title"), // window name
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // horizontal position of window
CW_USEDEFAULT, // vertical position of window
CW_USEDEFAULT, // window width
CW_USEDEFAULT, // window height
NULL, // handle to parent or owner window
NULL, // menu handle or child identifier
hInstance, // handle to application instance
NULL // window-creation data
);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void GetLargestDisplayMode(int *pcxBitmap,int *pcyBitmap)
{
DEVMODE devmode;
int iModeNum=0;
*pcxBitmap=*pcyBitmap=0;
ZeroMemory(&devmode,sizeof(DEVMODE));
devmode.dmSize=sizeof(DEVMODE);
while(EnumDisplaySettings(NULL,iModeNum++,&devmode))
{
*pcxBitmap=max(*pcxBitmap,(int)devmode.dmPelsHeight);
*pcyBitmap=max(*pcyBitmap,(int)devmode.dmPelsHeight);
}
}
LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
static BOOL fLeftButtonDown,fRightButtonDown;
static HBITMAP hBitmap;
static HDC hdcMem;
static int cxBitmap,cyBitmap,cxClient,cyClient,xMouse,yMouse;
HDC hdc;
PAINTSTRUCT ps;
switch(uMsg)
{
case WM_CREATE:
GetLargestDisplayMode(&cxBitmap,&cyClient);
hdc=GetDC(hwnd);
hBitmap=(HBITMAP)CreateCompatibleDC(hdc);
ReleaseDC(hwnd,hdc);
if(!hBitmap)
{
DeleteDC(hdcMem);
return -1;
}
SelectObject(hdcMem,hBitmap);
PatBlt(hdcMem,0,0,cxBitmap,cyBitmap,WHITENESS);
return 0;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;
case WM_LBUTTONDOWN:
if(!fRightButtonDown)
{
SetCapture(hwnd);
}
xMouse=LOWORD(lParam);
yMouse=HIWORD(lParam);
fLeftButtonDown=TRUE;
return 0;
case WM_LBUTTONUP:
if(fLeftButtonDown)
{
SetCapture(NULL);
}
fLeftButtonDown=FALSE;
return 0;
case WM_RBUTTONDOWN:
if(!fLeftButtonDown)
{
SetCapture(hwnd);
}
xMouse=LOWORD(lParam);
yMouse=HIWORD(lParam);
fRightButtonDown=TRUE;
return 0;
case WM_RBUTTONUP:
if(fRightButtonDown)
{
SetCapture(NULL);
}
fRightButtonDown=FALSE;
return 0;
case WM_MOUSEMOVE:
if(!fLeftButtonDown&&!fRightButtonDown)
{
return 0;
}
hdc=GetDC(hwnd);
SelectObject(hdc,GetStockObject(fLeftButtonDown?BLACK_PEN:WHITE_PEN));
SelectObject(hdcMem,GetStockObject(fLeftButtonDown?BLACK_PEN:WHITE_PEN));
MoveToEx(hdc,xMouse,yMouse,NULL);
MoveToEx(hdcMem,xMouse,yMouse,NULL);
xMouse=(short)LOWORD(lParam);
yMouse=(short)HIWORD(lParam);
LineTo(hdc,xMouse,yMouse);
LineTo(hdcMem,xMouse,yMouse);
ReleaseDC(hwnd,hdc);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
BitBlt(hdc,0,0,cxClient,cyClient,hdcMem,0,0,SRCCOPY);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
DeleteDC(hdcMem);
DeleteObject(hBitmap);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}

不做过多的解释,自己动手敲敲,有用的。要下载Windows程序设计第五版光盘代码的朋友,可以到我的资源下载。
这是一个源自《Windows程序设计》第五版的经典画图程序示例,能够实现在窗口中绘制和擦除线条的功能。程序通过响应鼠标点击事件来绘制黑色或白色的线条,并通过窗口过程函数处理各种消息。
2万+

被折叠的 条评论
为什么被折叠?



