C语言写的桌面画笔
写了一个早上....code如下
程序代码:
#include <windows.h>
#pragma comment(linker, "/subsystem:windows")
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static int iScrWidth, iScrHeight;
static HDC hScrDC;
static HDC hSaveDC;
static HBITMAP hSaveBmp;
static BOOL bIsDraw;
static HPEN hPen;
switch(uMsg)
{
case WM_CREATE:
MessageBox(hWnd, TEXT("按下鼠标中间退出程序"), TEXT("提示"), MB_OK | MB_ICONINFORMATION);
iScrWidth = GetSystemMetrics(SM_CXSCREEN);
iScrHeight = GetSystemMetrics(SM_CYSCREEN);
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
hSaveDC = CreateCompatibleDC(hScrDC);
hSaveBmp = CreateCompatibleBitmap(hScrDC, iScrWidth, iScrHeight);
SelectObject(hSaveDC, hSaveBmp);
BitBlt(hSaveDC, 0, 0, iScrWidth, iScrHeight, hScrDC, 0, 0, SRCCOPY);
bIsDraw = FALSE;
hPen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
SelectObject(hScrDC, hPen);
SetTimer(hWnd, 1, 10, NULL);
break;
case WM_TIMER:
KillTimer(hWnd, 1);
BitBlt(hScrDC, 0, 0, iScrWidth, iScrHeight, hSaveDC, 0, 0, SRCCOPY);
break;
case WM_MOUSEMOVE:
if(bIsDraw)
LineTo(hScrDC, LOWORD(lParam), HIWORD(lParam));
else
MoveToEx(hScrDC, LOWORD(lParam), HIWORD(lParam), NULL);
break;
case WM_RBUTTONDOWN:
BitBlt(hScrDC, 0, 0, iScrWidth, iScrHeight, hSaveDC, 0, 0, SRCCOPY);
break;
case WM_LBUTTONUP:
bIsDraw = FALSE;
break;
case WM_LBUTTONDOWN:
bIsDraw = TRUE;
break;
case WM_MBUTTONDOWN:
SendMessage(hWnd, WM_CLOSE, 0, 0);
break;
case WM_DESTROY:
DeleteObject(hSaveBmp);
DeleteDC(hSaveDC);
DeleteDC(hScrDC);
InvalidateRect(NULL, NULL, TRUE);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
int Run(HINSTANCE hInstance)
{
WNDCLASSEX wex;
ZeroMemory(&wex, sizeof(WNDCLASSEX));
wex.cbSize = sizeof(WNDCLASSEX);
wex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wex.hCursor = LoadCursor(hInstance, IDC_ARROW);
wex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wex.hIconSm = LoadIcon(hInstance, IDI_APPLICATION);
wex.hInstance = hInstance;
wex.lpfnWndProc = WndProc;
wex.lpszClassName = TEXT("WndDesktopPen");
wex.style = CS_HREDRAW | CS_VREDRAW;
if( !RegisterClassEx(&wex) )
{
MessageBox(NULL, TEXT("RegisterClassEx Error"), TEXT("Error"), MB_OK | MB_ICONINFORMATION);
return -1;
}
HWND hWnd = CreateWindowEx(NULL, TEXT("WndDesktopPen"), TEXT(""), WS_DLGFRAME | WS_THICKFRAME | WS_POPUP | WS_VISIBLE, 0, 0, 100, 100, NULL, NULL, hInstance, NULL);
if(NULL == hWnd)
{
MessageBox(NULL, TEXT("CreateWindowEx Error"), TEXT("Error"), MB_OK | MB_ICONINFORMATION);
return -2;
}
ShowWindow(hWnd, SW_MAXIMIZE);
UpdateWindow(hWnd);
MSG msg;
while( GetMessage(&msg, NULL, 0, 0) > 0 )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
return Run(hInstance);
}
vc6环境编译
写给新手的, 大牛勿喷
[ 本帖最后由 马甲1号 于 2011-8-28 13:21 编辑 ]









