模仿迅雷小窗口的小程序 好些年前写的 发着玩玩~~
程序代码:
#include <windows.h> #define WS_EX_LAYERED 0x00080000 #define LWA_COLORKEY 0x00000001 #define LWA_ALPHA 0x00000002 #define WIN_START_X 100 #define WIN_START_Y 100 #define WIN_WIDTH 36 #define WIN_HEIGHT 36 POINT winPos; tagPOINT mouPos ,Pos; HWND hwnd; MSG msg; TCHAR szAppName[] = TEXT("XunLei"); typedef BOOL (FAR WINAPI *LAYERFUNC)(HWND,COLORREF,BYTE,DWORD); int setWinClass(HINSTANCE hInstance); int createWindow(HINSTANCE hInstance); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); BOOL SetLayeredWindowAttributes(HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags); /******************************************************************************/ int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { MSG msg; setWinClass(hInstance); createWindow(hInstance); winPos.x = WIN_START_X; winPos.y = WIN_START_Y; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; } /*----------------------------------------------------------------------------*/ //设置窗口的WNDCLASS int setWinClass(HINSTANCE hInstance) { WNDCLASS 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) GetStockObject (LTGRAY_BRUSH); wndclass.lpszMenuName = NULL; wndclass.lpszClassName = szAppName; RegisterClass (&wndclass); return 1; } //创建窗口 int createWindow(HINSTANCE hInstance) { hwnd = CreateWindow (szAppName, TEXT ("迅雷") , WS_POPUP , WIN_START_X, WIN_START_Y , WIN_WIDTH, WIN_WIDTH , NULL, NULL, hInstance, NULL); SetWindowPos(hwnd , HWND_TOPMOST , WIN_START_X, WIN_START_Y , WIN_WIDTH, WIN_WIDTH ,SWP_SHOWWINDOW); //最顶端显示 // 设置窗口透明 SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE)^WS_EX_LAYERED); SetLayeredWindowAttributes(hwnd, RGB(0, 0, 255), 200, LWA_ALPHA); ShowWindow(hwnd, SW_SHOWNA); UpdateWindow (hwnd); return 1; } //窗口消息处理过程 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; HBRUSH hBrush; switch (message) { case WM_CREATE: return 0; case WM_PAINT: hdc = BeginPaint (hwnd, &ps); Rectangle(hdc, 0, 0, 36, 36); hBrush = CreateSolidBrush (RGB(30, 114, 171)); SelectObject (hdc, hBrush); Rectangle(hdc, 1, 1, 35, 35); DeleteObject (hBrush); EndPaint (hwnd, &ps); return 0; case WM_KEYUP: switch(wParam) { case 27: //按ESC退出 PostQuitMessage(0); return 0; } return 0; case WM_LBUTTONDOWN: GetCursorPos(&mouPos); return 0; case WM_MOUSEMOVE: while(GetAsyncKeyState(VK_LBUTTON)) { GetCursorPos(&Pos); winPos.x += Pos.x-mouPos.x; winPos.y += Pos.y-mouPos.y; mouPos.x = Pos.x; mouPos.y = Pos.y; MoveWindow(hwnd , winPos.x, winPos.y , WIN_WIDTH, WIN_HEIGHT , 1); } return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message, wParam, lParam); } //窗口透明函数 BOOL SetLayeredWindowAttributes(HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags) { LAYERFUNC SetLayer; HMODULE hmod = LoadLibrary("user32.dll"); SetLayer = (LAYERFUNC)GetProcAddress(hmod, "SetLayeredWindowAttributes"); BOOL bReturn = SetLayer(hwnd, crKey, bAlpha, dwFlags); FreeLibrary(hmod); return bReturn; }好几年前写的东西 没写注释 大家对付看一下吧
不算是纯C 但是有C语言基础的应该就能看懂
在VC CFree下都能运行