悬浮框的代码是参考freerdp实现的。
请指出错误或者提供解决代码,谢谢
//=============================
#include
#include "resource.h"
typedef struct wf_context wfContext;
typedef struct _Button Button;
typedef struct _FloatBar FloatBar;
/* Button Type */
#define BUTTON_MINIMIZE 1 //最小化
#define BUTTON_CLOSE 2 //关闭
#define BTN_MAX 2 //
/* bmp size */ //位置参数初始化
#define BACKGROUND_W 80
#define BACKGROUND_H 29
#define MINIMIZE_X (BACKGROUND_W - 64)
#define CLOSE_X (BACKGROUND_W - 37)
#define BUTTON_Y 2
#define BUTTON_WIDTH 24
#define BUTTON_HEIGHT 24
struct wf_context
{
HWND hwnd;
HINSTANCE hInstance;
FloatBar* floatbar;
};
struct _Button {
FloatBar* floatbar;
int type;
int x, y, h, w;
HBITMAP bmp;
};
struct _FloatBar {
HWND parent;
HWND hwnd;
RECT rect;
LONG widthh;
LONG heightt;
wfContext* wfc;
Button* buttons[BTN_MAX];
HDC hdcmem;
HBITMAP background;
};
static int button_paint(Button* button, HDC hdc)//浮动栏按钮实现代码
{
FloatBar* floatbar = button->floatbar;
SelectObject(floatbar->hdcmem,button->bmp);
StretchBlt(hdc, button->x, button->y, button->w, button->h, floatbar->hdcmem, 0, 0, button->w, button->h, SRCCOPY);
return 0;
}
static int floatbar_paint(FloatBar* floatbar, HDC hdc)//浮动栏的绘制实现代码,包括背景条和按钮的绘制
{
int i;
/* paint background */
SelectObject(floatbar->hdcmem, floatbar->background);
StretchBlt(hdc, 0, 0, BACKGROUND_W, BACKGROUND_H, floatbar->hdcmem, 0, 0, BACKGROUND_W, BACKGROUND_H, SRCCOPY);
/* paint buttons */
for (i = 0;i < BTN_MAX; i++)
button_paint(floatbar->buttons[i], hdc);
return 0;
}
static Button* floatbar_create_button(FloatBar* floatbar, int type, int resid, int x, int y, int h, int w)//浮动栏按钮位图加载实现代码
{
Button *button;
button = (Button *)malloc(sizeof(Button));
if (!button)
return NULL;
button->floatbar = floatbar;
button->type = type;
button->x = x;
button->y = y;
button->w = w;
button->h = h;
button->bmp = (HBITMAP)LoadImage(floatbar->wfc->hInstance, MAKEINTRESOURCE(resid), IMAGE_BITMAP, w, h, LR_DEFAULTCOLOR);
return button;
}
static FloatBar* floatbar_create(wfContext* wfc)///浮动栏背景位图和按钮位图加载
{
FloatBar* floatbar;
floatbar = (FloatBar *)malloc(sizeof(FloatBar));
if (!floatbar)
return NULL;
floatbar->hwnd = NULL;
floatbar->parent = wfc->hwnd;
floatbar->wfc = wfc;
floatbar->hdcmem = NULL;
//浮动栏按钮绘制 背景绘制、最小化、恢复、关闭,置顶 z2015-7-27 14:25:13
floatbar->background = (HBITMAP)LoadImage(wfc->hInstance, MAKEINTRESOURCE(IDB_BACKGROUND), IMAGE_BITMAP, BACKGROUND_W, BACKGROUND_H, LR_DEFAULTCOLOR);
floatbar->buttons[0] = floatbar_create_button(floatbar, BUTTON_MINIMIZE, IDB_MINIMIZE, MINIMIZE_X, BUTTON_Y, BUTTON_HEIGHT, BUTTON_WIDTH);
floatbar->buttons[1] = floatbar_create_button(floatbar, BUTTON_CLOSE, IDB_CLOSE, CLOSE_X, BUTTON_Y, BUTTON_HEIGHT, BUTTON_WIDTH);
return floatbar;
}
static Button* floatbar_get_button(FloatBar* floatbar, int x, int y)///获取鼠标横纵坐标,判断鼠标是否点到按钮
{
int i;
if (y > BUTTON_Y && y < BUTTON_Y + BUTTON_HEIGHT)
for (i = 0; i < BTN_MAX; i++)
if (x > floatbar->buttons[i]->x && x < floatbar->buttons[i]->x + floatbar->buttons[i]->w)
return floatbar->buttons[i];
return NULL;
}
LRESULT CALLBACK floatbar_proc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)//过程函数
{
static int dragging = FALSE;
static int lbtn_dwn = FALSE;
static int btn_dwn_x = 0;
static FloatBar* floatbar;
static TRACKMOUSEEVENT tme;
PAINTSTRUCT ps;
Button* button;
HDC hdc;
int pos_x;
int pos_y;
int xScreen = GetSystemMetrics(SM_CXSCREEN);
switch(Msg)
{
case WM_CREATE:
floatbar = (FloatBar *)((CREATESTRUCT *)lParam)->lpCreateParams;
floatbar->hwnd = hWnd;
floatbar->parent = GetParent(hWnd);
GetWindowRect(floatbar->hwnd, &floatbar->rect);
floatbar->widthh = floatbar->rect.right - floatbar->rect.left;
floatbar->heightt = floatbar->rect.bottom - floatbar->rect.top;
hdc = GetDC(hWnd);
floatbar->hdcmem = CreateCompatibleDC(hdc);
ReleaseDC(hWnd, hdc);
tme.cbSize = sizeof(TRACKMOUSEEVENT);
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hWnd;
tme.dwHoverTime = HOVER_DEFAULT;
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
floatbar_paint(floatbar, hdc);
EndPaint(hWnd, &ps);
break;
case WM_LBUTTONDOWN:
pos_x = lParam & 0xffff;
pos_y = (lParam >> 16) & 0xffff;
button = floatbar_get_button(floatbar, pos_x, pos_y);
if (!button)
{
SetCapture(hWnd);
dragging = TRUE;
btn_dwn_x = lParam & 0xffff;
}
else
lbtn_dwn = TRUE;
break;
case WM_LBUTTONUP:
pos_x = lParam & 0xffff;
pos_y = (lParam >> 16) & 0xffff;
ReleaseCapture();
dragging = FALSE;
if (lbtn_dwn)
{
button = floatbar_get_button(floatbar, pos_x, pos_y);
////=======================================
switch (button->type)
{
case BUTTON_MINIMIZE: //最小化 //浮动栏的按钮 功能实现 2015-7-27 09:00:07
ShowWindow(floatbar->parent, SW_MINIMIZE);
break;
case BUTTON_CLOSE: //关闭
SendMessage(floatbar->parent, WM_DESTROY, 0 , 0);
break;
default:
return 0;
}
////=======================================
lbtn_dwn = FALSE;
}
break;
case WM_DESTROY:
DeleteDC(floatbar->hdcmem);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
///=========================================================================
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wnd_cls; //这个类把floatbar所有的属性赋在窗口上
HWND barWnd;
MSG msg ;
int x = (GetSystemMetrics(SM_CXSCREEN) - BACKGROUND_W) / 2;
wnd_cls.cbSize = sizeof(WNDCLASSEX);
wnd_cls.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
//WNDCLASS函数
wnd_cls.lpfnWndProc = floatbar_proc;//进程?
wnd_cls.cbClsExtra = 0;
wnd_cls.cbWndExtra = 0;
wnd_cls.hIcon = LoadIcon(NULL, IDI_APPLICATION);//
wnd_cls.hCursor = LoadCursor(NULL, IDC_ARROW);
wnd_cls.hbrBackground = NULL;
wnd_cls.lpszMenuName = NULL;
wnd_cls.lpszClassName = "floatbar";
wnd_cls.hInstance = hInstance;
wnd_cls.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wnd_cls);
if (!RegisterClass (&wnd_cls)) {
MessageBox (NULL, "窗口注册失败!", "floatbar", 0) ;
return 0 ;
}
wfContext* wfc;//报错
wfcc->hInstance=hInstance;//报错
wfcc->floatbar = floatbar_create(wfcc); //报错
//生成浮动栏
barWnd =CreateWindowExW(WS_EX_TOPMOST, "floatbar", NULL, WS_CHILD, x, 0, BACKGROUND_W, BACKGROUND_H, NULL, NULL, hInstance, wfc->floatbar);
if (barWnd == NULL)
return;
ShowWindow(barWnd, SW_SHOWNORMAL);//
while (GetMessage (&msg, NULL, 0, 0)) {
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
////====================================================================================