📄 buildgrid.cpp
字号:
case WM_PAINT:
try{
hdc=BeginPaint(hwnd,&ps);
SetMapMode(hdc,MM_TEXT);
EndPaint(hwnd,&ps);
hdc=BeginPaint(hwndDrawBox,&ps);
SetMapMode(hdc,MM_TEXT);SelectObject(hdc,hP);
TextOut(hdc,10,10,"等边三角形",strlen("等边三角形"));
TextOut(hdc,10,30,"划分为四边形单元",strlen("划分为四边形单元"));
TextOut(hdc,10,60,"等边三角形",strlen("等边三角形"));
TextOut(hdc,10,80,"每二分之一边上有",strlen("每二分之一边上有"));
TextOut(hdc,10,140,"个平均划分的",strlen("个平均划分的"));
TextOut(hdc,10,160,"四边形单元",strlen("四边形单元"));
MoveToEx(hdc,x0+pt1.x,y0+pt1.y,NULL);
LineTo(hdc,x0+pt2.x,y0+pt2.y);
LineTo(hdc,x0+pt3.x,y0+pt3.y);
LineTo(hdc,x0+pt1.x,y0+pt1.y);
if(pEQ!=NULL)
for(i=0;i<3*N*N;i++)
{
MoveToEx(hdc,pEQ[i].nd[0].x,pEQ[i].nd[0].y,NULL);
LineTo(hdc,pEQ[i].nd[1].x,pEQ[i].nd[1].y);
LineTo(hdc,pEQ[i].nd[2].x,pEQ[i].nd[2].y);
LineTo(hdc,pEQ[i].nd[3].x,pEQ[i].nd[3].y);
LineTo(hdc,pEQ[i].nd[0].x,pEQ[i].nd[0].y);
}
EndPaint(hwndDrawBox,&ps);
}
catch(...)
{
}
break;
case WM_COMMAND:
switch (wParam)
{
case BTN_BUILD_GRID :
//----------------------------------------------------------
GetWindowText(hwndEditN,str,256);
N=atoi(str);
if(N<0)
{
MessageBox(hwnd,"划分单元数不能小于0!","",MB_OK|MB_ICONSTOP);
return 0;
}else if(N==0)
{
if(pEQ!=NULL)delete []pEQ;
pEQ=NULL;
SetWindowText(hwndEditOutput,"");
InvalidateRect (hwnd, NULL, TRUE) ;
return 0;
}
else
if(N>20)
if(MessageBox(hwnd,"划分单元过多,可能导致等待很长时间,是否继续?","",
MB_YESNO|MB_ICONQUESTION|MB_SYSTEMMODAL)==IDNO)
return 0;
StrMsg="";
//为所有四边形单元分配内存
if(pEQ!=NULL)delete []pEQ;
pEQ=new EQuad[3*N*N];
BuildGrid(pt1,pt2,pt3,pEQ);
for(i=0;i<3*N*N;i++)
{
sprintf(str,"第 %d 个四边形单元\r\n",i);
StrMsg+=str;
for(j=0;j<4;j++)
{
sprintf(str,"%9.2f %9.2f\r\n",pEQ[i].nd[j].x,pEQ[i].nd[j].y);
StrMsg+=str;
}
StrMsg+="\r\n";
}
SetWindowText(hwndEditOutput,StrMsg.data());
InvalidateRect (hwnd, NULL, TRUE) ;
//----------------------------------------------------------
break ;
case BTN_SAVEGRIDMSG :
//----------------------------------------------------------
if(pEQ==NULL)
{
MessageBox(hwnd,"没有网格数据可供保存!","",MB_OK|MB_ICONEXCLAMATION);
return 0;
}
if(GetSaveFileDlg (hwnd,szFileName,TEXT ("Text Files (*.TXT)\0*.txt\0")))
{
if((wfp=fopen(szFileName,"w+"))!=NULL)
{
for(i=0;i<3*N*N;i++)
{
fprintf(wfp,"单元编号 %d \n节点坐标: ",i);
for(j=0;j<4;j++)
fprintf(wfp,"%d#(%3.2f,%3.2f) ",
pEQ[i].nd[j].num,pEQ[i].nd[j].x,pEQ[i].nd[j].y);
/*for(j=0;j<4;j++)
fprintf(wfp,"%d#: 总体%d#(%3.2f %3.2f)\t",
j,pEQ[i].nd[j].num,pEQ[i].nd[j].x,pEQ[i].nd[j].y);
*/
fprintf(wfp,"\n");
}
str[0]='\0';
strcat(str,"网格数据已经写到文件");
strcat(str,szFileName);
MessageBox(hwnd,str,"",MB_OK|MB_ICONINFORMATION);
}
fclose(wfp);
}
//----------------------------------------------------------
break;
case BTN_SAVEBMP :
//----------------------------------------------------------
if(GetSaveFileDlg (hwnd,szFileName,
TEXT ("Bitmap Files (*.BMP)\0*.bmp\0")))
WriteWindowToDIB(szFileName, hwndDrawBox);
break;
case BTN_EXIT :
PostQuitMessage(0);
break;
default:
break;
}
break;
case WM_DESTROY:
if(pEQ!=NULL)delete []pEQ; //释放所有四边形单元占用的内存
DeleteObject(hP);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
}
return (0);
}
//============================================================================
//Functions for Writing Window To DIB file
HANDLE DDBToDIB(HBITMAP bitmap, DWORD dwCompression, HPALETTE pPal )
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDIB;
HANDLE handle;
HDC hDC;
HPALETTE hPal;
//ASSERT( bitmap.GetSafeHandle() );
// The function has no arg for bitfields
if( dwCompression == BI_BITFIELDS )
return NULL;
// If a palette has not been supplied use defaul palette
hPal = pPal;
if (hPal==NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE);
// Get bitmap information
GetObject(bitmap,sizeof(bm),(LPSTR)&bm);
// Initialize the bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// Compute the size of the infoheader and the color table
int nColors = (1 << bi.biBitCount);
if( nColors > 256 )
nColors = 0;
dwLen = bi.biSize + nColors * sizeof(RGBQUAD);
// We need a device context to get the DIB from
hDC = GetDC(NULL);
hPal = SelectPalette(hDC,hPal,FALSE);
RealizePalette(hDC);
// Allocate enough memory to hold bitmapinfoheader and color table
hDIB = GlobalAlloc(GMEM_FIXED,dwLen);
if (!hDIB){
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)hDIB;
*lpbi = bi;
// Call GetDIBits with a NULL lpBits param, so the device driver
// will calculate the biSizeImage field
GetDIBits(hDC, bitmap, 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS);
bi = *lpbi;
// If the driver did not fill in the biSizeImage field, then compute it
// Each scan line of the image is aligned on a DWORD (32bit) boundary
if (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight;
// If a compression scheme is used the result may infact be larger
// Increase the size to account for this.
if (dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
}
// Realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
if (handle = GlobalReAlloc(hDIB, dwLen, GMEM_MOVEABLE))
hDIB = handle;
else{
GlobalFree(hDIB);
// Reselect the original palette
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}
// Get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDIB;
// FINALLY get the DIB
BOOL bGotBits = GetDIBits( hDC, bitmap,
0L, // Start scan line
(DWORD)bi.biHeight, // # of scan lines
(LPBYTE)lpbi // address for bitmap bits
+ (bi.biSize + nColors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // address of bitmapinfo
(DWORD)DIB_RGB_COLORS); // Use RGB for color table
if( !bGotBits )
{
GlobalFree(hDIB);
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return NULL;
}
SelectPalette(hDC,hPal,FALSE);
ReleaseDC(NULL,hDC);
return hDIB;
}
HBITMAP DIBToDDB( HANDLE hDIB )
{
LPBITMAPINFOHEADER lpbi;
HBITMAP hbm;
HPALETTE pal;
HPALETTE pOldPal;
HDC dc;
dc=GetDC(NULL);
if (hDIB == NULL)
return NULL;
lpbi = (LPBITMAPINFOHEADER)hDIB;
int nColors = lpbi->biClrUsed ? lpbi->biClrUsed :
1 << lpbi->biBitCount;
BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
LPVOID lpDIBBits;
if( bmInfo.bmiHeader.biBitCount > 8 )
lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
bmInfo.bmiHeader.biClrUsed) +
((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
else
lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
// Create and select a logical palette if needed
if( nColors <= 256 && GetDeviceCaps(dc,RASTERCAPS) & RC_PALETTE)
{
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * nColors);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;
pLP->palNumEntries = nColors;
for( int i=0; i < nColors; i++)
{
pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
pLP->palPalEntry[i].peFlags = 0;
}
pal=CreatePalette( pLP );
delete[] pLP;
// Select and realize the palette
pOldPal =SelectPalette( dc, pal, FALSE );
RealizePalette(dc);
}
hbm = CreateDIBitmap(dc, // handle to device context
(LPBITMAPINFOHEADER)lpbi, // pointer to bitmap info header
(LONG)CBM_INIT, // initialization flag
lpDIBBits, // pointer to initialization data
(LPBITMAPINFO)lpbi, // pointer to bitmap info
DIB_RGB_COLORS ); // color-data usage
if (pal)//hjs
SelectPalette(dc,pOldPal,FALSE);
return hbm;
}
// WriteDIB - Writes a DIB to file
// Returns - TRUE on success
// szFile - Name of file to write to
// hDIB - Handle of the DIB
BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB)
{
BITMAPFILEHEADER hdr;
LPBITMAPINFOHEADER lpbi;
if (!hDIB)
return FALSE;
FILE *wfp;
if((wfp=fopen(szFile,"w"))==NULL)
MessageBox(NULL,"Cannot create file!","Error",MB_OK|MB_ICONERROR);
lpbi = (LPBITMAPINFOHEADER)hDIB;
int nColors = 1 << lpbi->biBitCount;
// Fill in the fields of the file header
hdr.bfType = ((WORD) ('M' << 8) | 'B'); // is always "BM"
hdr.bfSize = GlobalSize (hDIB) + sizeof( hdr );
hdr.bfReserved1 = 0;
hdr.bfReserved2 = 0;
hdr.bfOffBits = (DWORD) (sizeof( hdr ) + lpbi->biSize +
nColors * sizeof(RGBQUAD));
// Write the file header
fwrite(&hdr,sizeof(char),sizeof(hdr),wfp);
// Write the DIB header and the bits
fwrite(lpbi,sizeof(char),GlobalSize(hDIB),wfp);
fclose(wfp);
return TRUE;
}
BOOL WriteWindowToDIB(LPTSTR szFile, HWND hwnd)
{
HDC dc=GetDC(hwnd);
HDC memDC;
RECT rect;
memDC=CreateCompatibleDC(dc);
GetWindowRect(hwnd,&rect);//rect 没有分配内存
//用窗口区域大小定义一个内存DC ,并将窗口图像暂存其中
HBITMAP bitmap=CreateCompatibleBitmap(dc, rect.right-rect.left,rect.bottom-rect.top );
HBITMAP hOldBmp;
hOldBmp=(HBITMAP)SelectObject(memDC,bitmap);
BitBlt(memDC,0, 0, rect.right-rect.left,rect.bottom-rect.top, dc, 0, 0, SRCCOPY);
// 如果设备支持调色板,创建调色板
HPALETTE pal;
if( GetDeviceCaps(dc,RASTERCAPS) & RC_PALETTE )
{
UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
pLP->palVersion = 0x300;
pLP->palNumEntries =
GetSystemPaletteEntries( dc, 0, 255, pLP->palPalEntry );
// Create the palette
pal=CreatePalette( pLP );
delete[] pLP;
}
SelectObject(memDC,hOldBmp);
// 转换 bitmap 到 a DIB
HANDLE hDIB = DDBToDIB( bitmap, BI_RGB, pal );
if( hDIB == NULL )
return FALSE;
// Write it to file
WriteDIB( szFile, hDIB );
// Free the memory allocated by DDBToDIB for the DIB
GlobalFree( hDIB );
return TRUE;
}
///////////////////////////////////////////////////////////////////
//end of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -