windows API(VC++编程 中IsRectEmpty(&llpRect)的问题

myselfdragon 2009-08-30 08:26:52
int xPos, yPos;

int xp,yp;
POINT pt;

HBITMAP hBitmap;
LPCSTR lpszFileName;
int saxp;
int sayp;

int flag=0;//用于鼠标弹起
int tag=0;

//int savexpos;
//int saveypos;
RECT llpRect;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;


//////////////////////////////////////

case WM_LBUTTONDOWN:
{

if(!tag)
{

xPos = LOWORD(lParam);
yPos = HIWORD(lParam);


tag=1;
llpRect.left=xPos;
llpRect.top=yPos;

}



//flag=1;
}
break;


//框没的时候,有的时候就是不操作即可
case WM_LBUTTONUP:
{
saxp=xp;
sayp=yp;
flag=1;
InvalidateRect(hWnd,NULL,true);
//Rectangle(hdc,xPos,yPos,xp,yp);

}
break;



case WM_MOUSEMOVE:
{


{
if(flag)
{
xp=saxp;
yp=sayp;
llpRect.right=xp;
llpRect.bottom=yp;

}

else
{

xp=LOWORD(lParam);
yp=HIWORD(lParam);
InvalidateRect(hWnd,NULL,true);

}

}


//InvalidateRect(hWnd,NULL,true);

}

break;

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);





if(xPos&&yPos)

Rectangle(hdc,xPos,yPos,xp,yp);

DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);


EndPaint(hWnd, &ps);
break;

case WM_LBUTTONDBLCLK:
{

if (IsRectEmpty(&llpRect))
return NULL;

请注意问题我在执行if (IsRectEmpty(&llpRect)),怎么走了return NULL;不应该走啊,请高手帮调试一下

int a=5;
a=a+5;
printf("%d",a);


}

break;





//////////////////////////////////////////////////////////////////

case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
...全文
319 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2009-08-31
  • 打赏
  • 举报
回复
已改好,请告诉如何给你?
schlafenhamster 2009-08-31
  • 打赏
  • 举报
回复
问题:
1.不要用“dblclick”,用right button down,否则选择的rect会变。
2.“dblclick”,用做选择区清除。
3.HBITMAP CopyScreenToBitmap(CRect &Rect,HWND hwnd)中要坐标变换://去const
MapWindowPoints(hwnd,NULL,(POINT *)&Rect,2);

myselfdragon 2009-08-31
  • 打赏
  • 举报
回复
RECT llpRect;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;


//////////////////////////////////////

case WM_LBUTTONDOWN:
{

if(!tag)
{

xPos = LOWORD(lParam);
yPos = HIWORD(lParam);
// savexpos=

tag=1;
//llpRect->left=xPos;
//llpRect->top=yPos;

llpRect.left=xPos;
//llpRect.right=xPos;
llpRect.top=yPos;

}



//flag=1;
}
break;


//框没的时候,有的时候就是不操作即可
case WM_LBUTTONUP:
{
//xPos=xp=0;
//yPos=yp=0;
saxp=xp;
sayp=yp;
flag=1;
InvalidateRect(hWnd,NULL,true);
//Rectangle(hdc,xPos,yPos,xp,yp);

}
break;



case WM_MOUSEMOVE:
{


{
if(flag)
{
xp=saxp;
yp=sayp;
// llpRect->right=xp;
// llpRect->bottom=yp;
llpRect.right=xp;
// llpRect.left=xp;
llpRect.bottom=yp;
/*
int saveleft;
int savetop;

if(llpRect.left>llpRect.right)
{
saveleft=llpRect.left;
llpRect.left=llpRect.right;

llpRect.right=saveleft;
}


if(llpRect.top>llpRect.bottom)
{
savetop=llpRect.top;
llpRect.top=llpRect.bottom;

llpRect.bottom=savetop;
}*/







}

else
{

xp=LOWORD(lParam);
yp=HIWORD(lParam);
}

}


InvalidateRect(hWnd,NULL,true);

}

break;

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);





if(xPos&&yPos)

Rectangle(hdc,xPos,yPos,xp,yp);

DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);


EndPaint(hWnd, &ps);
break;

case WM_LBUTTONDBLCLK:
{
// int i=1;//CopyScreenToBitmap(lpRect);
int saveleft;
int savetop;

if(llpRect.left>llpRect.right)
{
saveleft=llpRect.left;
llpRect.left=llpRect.right;

llpRect.right=saveleft;
}


if(llpRect.top>llpRect.bottom)
{
savetop=llpRect.top;
llpRect.top=llpRect.bottom;

llpRect.bottom=savetop;
}




/*int savebottom;

if(llpRect.bottom>llpRect.top)
{
savebottom=llpRect.bottom;
llpRect.bottom=llpRect.top;
llpRect.top=savebottom;

}*/
////////////////////////////////////////

//llpRect.top=llpRect.bottom;

// llpRect.bottom=llpRect.bottom-(savetop-llpRect.bottom);
////////////////////////////////////////





hbitmap=CopyScreenToBitmap(llpRect,hWnd);

// SaveBitmapToFile(CopyScreenToBitmap(llpRect,hWnd),"temp.bmp");
SaveBitmapToFile(hbitmap,"temp.bmp");
CloseWindow(hWnd);

}



break;

case WM_RBUTTONDOWN:
CloseWindow(hWnd);
break;



//////////////////////////////////////////////////////////////////

case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
myselfdragon 2009-08-31
  • 打赏
  • 举报
回复
HBITMAP CopyScreenToBitmap(const RECT lpRect,HWND hWnd)
{
//HWND hWnd;
HDC hScrDC, hMemDC;
HBITMAP hOldBitmap,hBitmap;
int nX, nY, nX2, nY2;
int nWidth, nHeight;
int xScrn, yScrn;
if (IsRectEmpty(&lpRect))
return NULL;
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hScrDC);
nX = lpRect.left;
nY = lpRect.top;
nX2 = lpRect.right;
nY2 = lpRect.bottom;
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
if (nX < 0) nX = 0;
if (nY < 0) nY = 0;
if (nX2 > xScrn) nX2 = xScrn;
if (nY2 > yScrn) nY2 = yScrn;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
ShowWindow(hWnd,SW_HIDE);
//BitBlt(hMemDC, 0, 0, nWidth, nHeight,hScrDC, nX, nY, SRCCOPY);
BitBlt(hMemDC, 0,0, nWidth, nHeight,hScrDC,nX, nY, SRCCOPY);
hBitmap =(HBITMAP)SelectObject(hMemDC, hOldBitmap);
DeleteDC(hScrDC);
DeleteDC(hMemDC);
ShowWindow(hWnd,SW_SHOW);
return hBitmap;
}



///////

BOOL SaveBitmapToFile(HBITMAP hBitmap, LPCSTR lpszFileName)
{
HDC hDC;
//设备描述表
int iBits;
//当前显示分辨率下每个像素所占字节数
WORD wBitCount;
//位图中每个像素所占字节数
//定义调色板大小, 位图中像素字节大小 , 位图文件大小 , 写入文件字节数
DWORD dwPaletteSize=0,dwBmBitsSize,dwDIBSize, dwWritten;
BITMAP Bitmap;
//位图属性结构
BITMAPFILEHEADER bmfHdr;
//位图文件头结构
BITMAPINFOHEADER bi;
//位图信息头结构
//LPBITMAPINFOHEADER lpbi;
BITMAPINFOHEADER *lpbi;

//指向位图信息头结构
HANDLE fh, hDib, hPal;
HPALETTE hOldPal=NULL;
//定义文件,分配内存句柄,调色板句柄

//计算位图文件每个像素所占字节数
hDC = CreateDC("DISPLAY",NULL,NULL,NULL);
iBits = GetDeviceCaps(hDC, BITSPIXEL) *
GetDeviceCaps(hDC, PLANES);
DeleteDC(hDC);
if (iBits <= 1)
wBitCount = 1;
else if (iBits <= 4)
wBitCount = 4;
else if (iBits <= 8)
wBitCount = 8;
else if (iBits <= 24)
wBitCount = 24;
else
wBitCount = 32;
//计算调色板大小
if (wBitCount <= 8)
dwPaletteSize=(1<<wBitCount)*sizeof(RGBQUAD);

//设置位图信息头结构
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);

//hBitmap=CreateBitmapIndirect(&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;

dwBmBitsSize = ((Bitmap.bmWidth*wBitCount+31)/32)*4*Bitmap.bmHeight;
//为位图内容分配内存

/*xxxxxxxx计算位图大小分解一下(解释一下上面的语句)xxxxxxxxxxxxxxxxxxxx
//每个扫描行所占的字节数应该为4的整数倍,具体算法为:
int biWidth = (Bitmap.bmWidth*wBitCount) / 32;
if((Bitmap.bmWidth*wBitCount) % 32)
biWidth++; //不是整数倍的加1
biWidth *= 4;//到这里,计算得到的为每个扫描行的字节数。
dwBmBitsSize = biWidth * Bitmap.bmHeight;//得到大小
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/


hDib = GlobalAlloc(GHND,dwBmBitsSize+dwPaletteSize+sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
//先不要
//有问题*lpbi = bi;
//先不写,因为上面的那就话已经对其赋值了lpbi=new( BITMAPINFOHEADER);
*lpbi = bi;
//lpbi=&bi;
// 处理调色板
hPal = GetStockObject(DEFAULT_PALETTE);
if (hPal)
{
hDC = ::GetDC(NULL);
hOldPal=SelectPalette(hDC,(HPALETTE)hPal,FALSE);
RealizePalette(hDC);
}
// 获取该调色板下新的像素值
//有问题GetDIBits(hDC,hBitmap,0,(UINT)Bitmap.bmHeight,(LPSTR)lpbi+sizeof(BITMAPINFOHEADER)+dwPaletteSize, (BITMAPINFO *)lpbi,DIB_RGB_COLORS);
GetDIBits(hDC,hBitmap,0,(UINT)Bitmap.bmHeight,(LPSTR)lpbi+sizeof(BITMAPINFOHEADER)+dwPaletteSize, (BITMAPINFO *)lpbi,DIB_RGB_COLORS);
//恢复调色板
if (hOldPal)
{
SelectPalette(hDC, hOldPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL, hDC);
}
//创建位图文件
fh=CreateFile(lpszFileName, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh==INVALID_HANDLE_VALUE)
return FALSE;
// 设置位图文件头
bmfHdr.bfType = 0x4D42; // "BM"
dwDIBSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER)+(DWORD)sizeof(BITMAPINFOHEADER)+dwPaletteSize;
// 写入位图文件头
WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
// 写入位图文件其余内容
WriteFile(fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize , &dwWritten, NULL);
//清除
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(fh);
return TRUE;

}




///////






//1 ////////////////////

int xPos, yPos;

int xp,yp;
POINT pt;
//LPRECT lpRect;
HBITMAP hbitmap;

LPCSTR lpszFileName;
int saxp;
int sayp;

int flag=0;//用于鼠标弹起
int tag=0;
myselfdragon 2009-08-31
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include "resource.h"
#include<stdio.h>
#include<WINGDI.h>
#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MOUSEMOVE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MOUSEMOVE);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

// wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MOUSEMOVE);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_MOUSEMOVE;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);



ShowWindow(hWnd,SW_SHOWMAXIMIZED);
SetWindowLong(hWnd,GWL_EXSTYLE,GetWindowLong(hWnd,GWL_EXSTYLE)^0x80000);
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
//取得SetLayeredWindowAttributes函数指针
fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if(fun)fun(hWnd,0,30,2); //参数1 窗体句柄 参数2 颜色值 参数3透明度 参数4 透明方式 一般用2
FreeLibrary(hInst);
}





if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}
myselfdragon 2009-08-30
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 conry 的回复:]
你把llpRect的值打出来就知道了
你在down的时候llpRect.left=xPos; llpRect.top=yPos;并且设置了标志,就是以后llpRect的左上角坐标不变了,
而在mousemove的时候并且鼠标弹起状态 才给llpRect,right,bottom赋值,那么以后你只要移动鼠标
llpRect,right,bottom就会改变,你双击的时候正好right <=left或者bottom <=top,IsRectEmpty就返回true了
[/Quote]

请问程序中应该怎样改
schlafenhamster 2009-08-30
  • 打赏
  • 举报
回复
这是鼠标移动的方法不一样造成的,当鼠标从右下移到左上时,就会有这个问题,一般可以用NormalizeRect 把它转换过来。
你也可以不用IsRectEmpty,直接判断高和宽。只要高和宽不为0就可以了。
Conry 2009-08-30
  • 打赏
  • 举报
回复
你把llpRect的值打出来就知道了
你在down的时候llpRect.left=xPos; llpRect.top=yPos;并且设置了标志,就是以后llpRect的左上角坐标不变了,
而在mousemove的时候并且鼠标弹起状态 才给llpRect,right,bottom赋值,那么以后你只要移动鼠标
llpRect,right,bottom就会改变,你双击的时候正好right<=left或者bottom<=top,IsRectEmpty就返回true了
myselfdragon 2009-08-30
  • 打赏
  • 举报
回复
我感觉是IsRectEmpty既然返回了Return NULL,则程序中存在问题,要是不写了,也依然有问题啊
schlafenhamster 2009-08-30
  • 打赏
  • 举报
回复
你的llpRect不是正常的RECT是反向的即bottom < top,right<left?如果是CRect可以使用:
NormalizeRect
你也可以不用IsRectEmpty,直接判断高和宽。
myselfdragon 2009-08-30
  • 打赏
  • 举报
回复
up
myselfdragon 2009-08-30
  • 打赏
  • 举报
回复
哦,多谢帮忙
schlafenhamster 2009-08-30
  • 打赏
  • 举报
回复
代码复制了。
myselfdragon 2009-08-30
  • 打赏
  • 举报
回复
UP
MoXiaoRab 2009-08-30
  • 打赏
  • 举报
回复
LZ你贴代码累不累啊
myselfdragon 2009-08-30
  • 打赏
  • 举报
回复
int xPos, yPos;

int xp,yp;
POINT pt;
//LPRECT lpRect;
HBITMAP hBitmap;
LPCSTR lpszFileName;
int saxp;
int sayp;

int flag=0;//用于鼠标弹起
int tag=0;

//int savexpos;
//int saveypos;
//RECT llpRect;

//LPRECT llpRect;
RECT llpRect;
//llpRect=new LPRECT;
//llpRect=new RECT;
//llpRect=malloc(sizeof(RECT));
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;


//////////////////////////////////////

case WM_LBUTTONDOWN:
{

if(!tag)
{

xPos = LOWORD(lParam);
yPos = HIWORD(lParam);
// savexpos=

tag=1;
llpRect.left=xPos;
llpRect.top=yPos;

/*llpRect->left=xPos;
llpRect->top=yPos;*/

}



//flag=1;
}
break;


//框没的时候,有的时候就是不操作即可
case WM_LBUTTONUP:
{
//xPos=xp=0;
//yPos=yp=0;
saxp=xp;
sayp=yp;
flag=1;
InvalidateRect(hWnd,NULL,true);
//Rectangle(hdc,xPos,yPos,xp,yp);

}
break;



case WM_MOUSEMOVE:
{


{
if(flag)
{
xp=saxp;
yp=sayp;
llpRect.right=xp;
llpRect.bottom=yp;
/*
llpRect->right=xp;
llpRect->bottom=yp;*/
}

else
{

xp=LOWORD(lParam);
yp=HIWORD(lParam);
InvalidateRect(hWnd,NULL,true);

}

}


//InvalidateRect(hWnd,NULL,true);

}

break;

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);





if(xPos&&yPos)

Rectangle(hdc,xPos,yPos,xp,yp);

DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);


EndPaint(hWnd, &ps);
break;

case WM_LBUTTONDBLCLK:
{
// int i=1;//CopyScreenToBitmap(lpRect);
//if (IsRectEmpty(&llpRect))
if (IsRectEmpty(&llpRect)){

return NULL;
}

int a=5;
a=a+5;
printf("%d",&a);
// CopyScreenToBitmap(llpRect,hWnd);
SaveBitmapToFile(hBitmap,"lpszFileName");



}

break;





//////////////////////////////////////////////////////////////////

case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
myselfdragon 2009-08-30
  • 打赏
  • 举报
回复
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);



ShowWindow(hWnd,SW_SHOWMAXIMIZED);
SetWindowLong(hWnd,GWL_EXSTYLE,GetWindowLong(hWnd,GWL_EXSTYLE)^0x80000);
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);
MYFUNC fun = NULL;
//取得SetLayeredWindowAttributes函数指针
fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if(fun)fun(hWnd,0,30,2); //参数1 窗体句柄 参数2 颜色值 参数3透明度 参数4 透明方式 一般用2
FreeLibrary(hInst);
}





if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return

HBITMAP CopyScreenToBitmap( const RECT lpRect,HWND &hWnd1)
{
//HWND hWnd;
HDC hScrDC, hMemDC;
HBITMAP hOldBitmap,hBitmap;
int nX, nY, nX2, nY2;
int nWidth, nHeight;
int xScrn, yScrn;
if (IsRectEmpty(&lpRect))
return NULL;
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hScrDC);
nX = lpRect.left;
nY = lpRect.top;
nX2 = lpRect.right;
nY2 = lpRect.bottom;
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
if (nX < 0) nX = 0;
if (nY < 0) nY = 0;
if (nX2 > xScrn) nX2 = xScrn;
if (nY2 > yScrn) nY2 = yScrn;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
ShowWindow(hWnd1,SW_HIDE);
BitBlt(hMemDC, 0, 0, nWidth, nHeight,hScrDC, nX, nY, SRCCOPY);
hBitmap =(HBITMAP)SelectObject(hMemDC, hOldBitmap);
DeleteDC(hScrDC);
DeleteDC(hMemDC);
ShowWindow(hWnd1,SW_SHOW);
return hBitmap;
}

///////

BOOL SaveBitmapToFile(HBITMAP hBitmap, LPCSTR lpszFileName)
{
HDC hDC;
//设备描述表
int iBits;
//当前显示分辨率下每个像素所占字节数
WORD wBitCount;
//位图中每个像素所占字节数
//定义调色板大小, 位图中像素字节大小 , 位图文件大小 , 写入文件字节数
DWORD dwPaletteSize=0,dwBmBitsSize,dwDIBSize, dwWritten;
BITMAP Bitmap;
//位图属性结构
BITMAPFILEHEADER bmfHdr;
//位图文件头结构
BITMAPINFOHEADER bi;
//位图信息头结构
LPBITMAPINFOHEADER lpbi;
//指向位图信息头结构
HANDLE fh, hDib, hPal;
HPALETTE hOldPal=NULL;
//定义文件,分配内存句柄,调色板句柄

//计算位图文件每个像素所占字节数
hDC = CreateDC("DISPLAY",NULL,NULL,NULL);
iBits = GetDeviceCaps(hDC, BITSPIXEL) *
GetDeviceCaps(hDC, PLANES);
DeleteDC(hDC);
if (iBits <= 1)
wBitCount = 1;
else if (iBits <= 4)
wBitCount = 4;
else if (iBits <= 8)
wBitCount = 8;
else if (iBits <= 24)
wBitCount = 24;
else
wBitCount = 32;
//计算调色板大小
if (wBitCount <= 8)
dwPaletteSize=(1<<wBitCount)*sizeof(RGBQUAD);

//设置位图信息头结构
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;

dwBmBitsSize = ((Bitmap.bmWidth*wBitCount+31)/32)*4*Bitmap.bmHeight;
//为位图内容分配内存

/*xxxxxxxx计算位图大小分解一下(解释一下上面的语句)xxxxxxxxxxxxxxxxxxxx
//每个扫描行所占的字节数应该为4的整数倍,具体算法为:
int biWidth = (Bitmap.bmWidth*wBitCount) / 32;
if((Bitmap.bmWidth*wBitCount) % 32)
biWidth++; //不是整数倍的加1
biWidth *= 4;//到这里,计算得到的为每个扫描行的字节数。
dwBmBitsSize = biWidth * Bitmap.bmHeight;//得到大小
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/


hDib = GlobalAlloc(GHND,dwBmBitsSize+dwPaletteSize+sizeof(BITMAPINFOHEADER));
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
//先不要
lpbi=new BITMAPINFOHEADER;
*lpbi = bi;
// 处理调色板
hPal = GetStockObject(DEFAULT_PALETTE);
if (hPal)
{
hDC = ::GetDC(NULL);
hOldPal=SelectPalette(hDC,(HPALETTE)hPal,FALSE);
RealizePalette(hDC);
}
// 获取该调色板下新的像素值
GetDIBits(hDC,hBitmap,0,(UINT)Bitmap.bmHeight,(LPSTR)lpbi+sizeof(BITMAPINFOHEADER)+dwPaletteSize, (BITMAPINFO *)lpbi,DIB_RGB_COLORS);
//恢复调色板
if (hOldPal)
{
SelectPalette(hDC, hOldPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL, hDC);
}
//创建位图文件
fh=CreateFile(lpszFileName, GENERIC_WRITE,0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh==INVALID_HANDLE_VALUE)
return FALSE;
// 设置位图文件头
bmfHdr.bfType = 0x4D42; // "BM"
dwDIBSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER)+(DWORD)sizeof(BITMAPINFOHEADER)+dwPaletteSize;
// 写入位图文件头
WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
// 写入位图文件其余内容
WriteFile(fh, (LPSTR)lpbi, sizeof(BITMAPINFOHEADER)+dwPaletteSize+dwBmBitsSize , &dwWritten, NULL);
//清除
GlobalUnlock(hDib);
GlobalFree(hDib);
CloseHandle(fh);
return TRUE;

}

myselfdragon 2009-08-30
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include "resource.h"
#include<stdio.h>
#define MAX_LOADSTRING 100
#include<stdlib.h>
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_MOUSEMOVE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MOUSEMOVE);

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

// wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_MOUSEMOVE);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_MOUSEMOVE;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}
schlafenhamster 2009-08-30
  • 打赏
  • 举报
回复
Nonzero if CRect is empty; 0 if CRect is not empty
myselfdragon 2009-08-30
  • 打赏
  • 举报
回复
加载更多回复(4)

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧