更新时间:2015-12-20 19:27:21浏览次数:1+次
这些都是很实用的提示。我们这里实现鼠标移动时实时提示当前光标所在的客户区坐标值。效果图如下:
GetClientRect(hwnd,&rect);
SelectObject(hdc,CreatePen(PS_SOLID,1,RGB(120,120,120)));
for (int i=0;i<rect.right;i++)
{
MoveToEx(hdc,i*10,0,NULL);
LineTo(hdc,i*10,rect.bottom);
}
for (int i=0;i<rect.bottom;i++)
{
MoveToEx(hdc,0,i*10,NULL);
LineTo(hdc,rect.right,i*10);
}
#include "windows.h"
#include <Windowsx.h>//GET_X_LPARAM,GET_Y_LPARAM宏需要这个头文件
#include <tchar.h>
// - 项目是Unicode字符集
HINSTANCE g_hInstance=NULL;
LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
TCHAR Info[100]=_T("C++技术网http://www.cjjjs.com");
RECT rect;
static HWND hTip=NULL;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
SelectObject(hdc,CreatePen(PS_SOLID,1,RGB(120,120,120)));
for (int i=0;i<rect.right;i++)
{
MoveToEx(hdc,i*10,0,NULL);
LineTo(hdc,i*10,rect.bottom);
}
for (int i=0;i<rect.bottom;i++)
{
MoveToEx(hdc,0,i*10,NULL);
LineTo(hdc,rect.right,i*10);
}
EndPaint(hwnd,&ps);
return 0;
case WM_KILLFOCUS:
SetFocus(hwnd);
return 0;
case WM_MOUSEMOVE:
if (!hTip)
{
hTip = CreateWindow(_T("Button"),_T("测试"),BS_FLAT|WS_POPUP,GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam)+50,100,40,hwnd,NULL,g_hInstance,NULL);
ShowWindow(hTip,SW_SHOWNORMAL);
}
else
{
MoveWindow(hTip,GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam)+50,100,40,TRUE);
TCHAR msg[100]=_T("");
wsprintf(msg,_T("(%d ,%d)"),GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam));
SetWindowText(hTip,msg);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrev,LPSTR lpCmd,int iShow)
{
TCHAR ClassName[] = _T("MyClass");
TCHAR title1[] = _T("C++技术网http://www.cjjjs.com");
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_APPLICATION);
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc = WinProc;
wndClass.lpszClassName = ClassName;
wndClass.lpszMenuName=NULL;
wndClass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndClass))return 0;
g_hInstance = hInstance;
HWND hwnd = CreateWindow(ClassName,title1,WS_OVERLAPPEDWINDOW,0,0,440,400,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
MSG msg;
while (GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
相关资讯