[散300技术分]C++Builder下UI的酷炫效果代码
最近刚升三星,貌似水分没什么意思,这里再散一次技术分。
当然,技术分是不能乱散D,那是会被妖哥给BS地,so,只要大家能给出任何使VCL原生组件变得更炫(界面或动画或功能补全之类的)的代码就给分!
如果给的代码实在是帅,偶另开帖加分~~嘻嘻
[解决办法]
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
name="XP style manifest"
processorArchitecture="x86"
version="1.0.0.0"
type="win32"/>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="x86"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
把上面的代码保存为XPStyle.manifest,然后建个rc文件 ,内容为1 24 "XPStyle.Manifest"
然后把rc文件加入工程,即可使bcb程序在xp系统以上时,可以拥有xp样式。。
这也是在论坛上学的。。。
[解决办法]
// 渐变填充,以下代码我自己都很少用,汗。都是直接用的皮肤组件// .h//---------------------------------------#ifndef HmyGdiH#define HmyGdiH//---------------------------------------#include <Classes.hpp>#include <Graphics.hpp>//---------------------------------------namespace hmy{enum TGradientStyle { hmygsH = GRADIENT_FILL_RECT_H, hmygsV = GRADIENT_FILL_RECT_V };extern PACKAGE void __fastcall CopyRects(TRect *rects, int size, const TRect &src);extern PACKAGE void __fastcall SetTriVertex(TTriVertex &vert, Word x, Word y, TColor color, Word alpha);// 渐变填充extern PACKAGE void __fastcall DrawGradient(TCanvas *canvas, const TRect &rect, TColor startColor, TColor endColor, hmy::TGradientStyle gstyle);// 2次渐变填充,colors必须是4个extern PACKAGE void __fastcall DrawGradient2(TCanvas *canvas, const TRect &rect, TColor *colors, hmy::TGradientStyle gstyle);}//---------------------------------------#endif// .cpp//---------------------------------------#include <vcl.h>#pragma hdrstop#include "HmyGdi.h"#include <boost/scoped_array.hpp>//---------------------------------------#pragma package(smart_init)#pragma comment(lib, "msimg32.lib") //---------------------------------------namespace hmy{//---------------------------------------void __fastcall CopyRects(TRect *rects, int size, const TRect &src){ for (int i=0; i<size; i++) rects[i] = src;}//---------------------------------------void __fastcall SetTriVertex(TTriVertex &vert, Word x, Word y, TColor color, Word alpha){ vert.x = x; vert.y = y; vert.Red = GetRValue(color)<<8; vert.Green = GetGValue(color)<<8; vert.Blue = GetBValue(color)<<8; vert.Alpha = alpha;}//---------------------------------------void __fastcall DrawGradient(TCanvas *canvas, const TRect &rect, TColor startColor, TColor endColor, hmy::TGradientStyle gstyle){ const int max_size = 2; TTriVertex vert[max_size]; TGradientRect gRect; hmy::SetTriVertex(vert[0], rect.Left, rect.Top, startColor, 0); hmy::SetTriVertex(vert[1], rect.Right, rect.Bottom, endColor, 0); gRect.UpperLeft = 0; gRect.LowerRight = 1; ::GradientFill(canvas->Handle, &vert[0], max_size, &gRect, 1, (DWord)gstyle);}//---------------------------------------// 2次渐变填充,colors必须是4个void __fastcall DrawGradient2(TCanvas *canvas, const TRect &rect, TColor *colors, hmy::TGradientStyle gstyle){ int times = 2; boost::scoped_array<TRect> rects(new TRect[2]); CopyRects(rects.get(), times, rect); if (gstyle == hmy::hmygsV) { rects[0].Bottom = rect.Height() / 2 - 1; rects[1].Top = rects[0].Bottom; } else if (gstyle == hmy::hmygsH) { rects[0].Right = rect.Width() / 2 - 1; rects[1].Left = rects[0].Right; } DrawGradient(canvas, rects[0], colors[0], colors[1], gstyle); DrawGradient(canvas, rects[1], colors[2], colors[3], gstyle);}}
[解决办法]
//窗口的美化是使用无模式窗口。然后映射消息 BEGIN_MESSAGE_MAP VCL_MESSAGE_HANDLER(WM_NCHITTEST, TWMNCHitTest, wmnchittest) VCL_MESSAGE_HANDLER(WM_NCMOUSEMOVE, TWMNCMouseMove, wmncmousemove) END_MESSAGE_MAP(TForm)//下面做消息的一些改变void __fastcall TButForm::wmnchittest(TWMNCHitTest & msg){ //TODO: Add your source code here TRect *cprect ; //定义标题栏的RECT(也就是一个正方形的区域) TRect *minRect ; //最小化按纽的RECT TRect *MaxRect ; //最大化按纽的RECT TRect *CloseRect ; //关闭 按纽的RECT TPoint pt ;//鼠标指针 cprect = &Rect(43, 1, Width-112, 29); //设置RECT minRect = &Rect((Width - 81),5, (Width - 57), 20); MaxRect = &Rect((Width - 56),1, (Width - 32), 20); CloseRect = &Rect((Width - 31),1, (Width - 7), 20); pt.x = ScreenToClient(Mouse->CursorPos).x;//鼠标X pt.y = ScreenToClient(Mouse->CursorPos).y;// Y值 //准备欺骗系统了 ,因为BSNONE样式的窗体是没有这些返回消息的 if ( PtInRect(minRect,pt) ) msg.Result = HTMINBUTTON; //告诉它在最小化按纽这里 else if ( PtInRect(MaxRect,pt) ) msg.Result = HTMAXBUTTON; //告诉它在最大化按纽这里 else if ( PtInRect(CloseRect,pt) ) msg.Result = HTCLOSE; //告诉它在关闭 按纽这里 else if ( PtInRect(cprect,pt) ) msg.Result = HTCAPTION; //告诉它在标题栏这里 else if ( (pt.x<5) && (pt.y<5) ) msg.Result=HTTOPLEFT; //告诉它在左上角 else if ( (pt.x>Width-5) && (pt.y<5) ) msg.Result=HTTOPRIGHT; //告诉它在右上角 else if ( (pt.x >Width-5) && (pt.y>Height-5) ) msg.Result=HTBOTTOMRIGHT; //告诉它在右下角 else if ( (pt.x<5) && (pt.y>Height-5) ) msg.Result=HTBOTTOMLEFT; //告诉它在左下角 else if (pt.x<5) msg.Result=HTLEFT; else if (pt.y<5) msg.Result=HTTOP; else if (pt.x>Width-5) msg.Result=HTRIGHT; else if (pt.y>Height-5) msg.Result=HTBOTTOM; // inherited ; TForm::Dispatch(&msg);}void __fastcall TButForm::wmncmousemove(TWMNCMouseMove & msg){ //TODO: Add your source code here HDC dc ; TCanvas *cv ; dc = GetWindowDC(Handle); cv = new TCanvas(); try { cv->Handle = dc; if ( m_bDraw == true ) { if ( msg.HitTest == HTMINBUTTON ) { BitBlt(dc, (Width - 81), 5, 24, 20, imgmin->Canvas->Handle, 48, 0, SRCCOPY); } else { BitBlt(dc, (Width - 81), 5, 24, 20, imgmin->Canvas->Handle, 0, 0, SRCCOPY); } if ( msg.HitTest == HTMAXBUTTON ) { if ( WindowState == wsNormal ) { BitBlt(dc, (Width - 56), 5, 24, 20, imgMax->Canvas->Handle, 48, 0, SRCCOPY); } else if ( WindowState == wsMaximized ) { BitBlt(dc, (Width - 56), 5, 24, 20, imgrestore->Canvas->Handle, 48, 0, SRCCOPY); } } else { if ( WindowState == wsNormal ) { BitBlt(dc, (Width - 56), 5, 24, 20, imgMax->Canvas->Handle, 0, 0, SRCCOPY); } else if ( WindowState == wsMaximized ) { BitBlt(dc, (Width - 56), 5, 24, 20, imgrestore->Canvas->Handle,0, 0, SRCCOPY); } } //close if ( msg.HitTest == HTCLOSE ) { BitBlt(dc, (Width - 31), 5, 24, 20, imgclose->Canvas->Handle, 48, 0, SRCCOPY); } else { BitBlt(dc, (Width - 31), 5, 24, 20, imgclose->Canvas->Handle, 0, 0, SRCCOPY); } } } __finally { ReleaseDC(Handle, cv->Handle); delete cv; } TForm::Dispatch(msg);}
[解决办法]
//创建不规则窗体void __fastcall TForm1::BmpToRgn(TImage *m_Image){ m_Image->AutoSize=true; Form1->AutoSize=true; Form1->BorderStyle=bsNone; //将Form的标题栏去掉 //以这个点的颜色为透明基准色 TColor ColorKey=m_Image->Canvas->Pixels[0][0]; int x,y; int l,r; POINT *a; bool lb,rb; HRGN wndrgn,temprgn; if((a=(POINT *)malloc(Width*2*(sizeof(POINT))))==NULL) { ShowMessage("申请内存失败!"); exit(0); } l=0; r=m_Image->Height*2-1; wndrgn=CreateRectRgn(0,0,m_Image->Width,m_Image->Height); for(y=0;y<m_Image->Height;y++) { lb=true; for(x=0;x<m_Image->Width+1;x++) if(m_Image->Canvas->Pixels[x][y]!=ColorKey) { a[l].x=x; a[l].y=y; lb=false; break; } if(lb) a[l]=a[l-1]; l++; rb=true; for(x=m_Image->Width;x>=0;x--) if(m_Image->Canvas->Pixels[x][y]!=ColorKey) { a[r].x=x; a[r].y=y; rb=false; break; } if(rb) a[r]=a[r+1]; r--; } r=m_Image->Height*2-1; for(y=0;y<m_Image->Height-1;y++) { for(x=a[y].x;x<=a[r].x;x++) if(m_Image->Canvas->Pixels[x][y]==ColorKey) { temprgn=CreateRectRgn(x,y,x+1,y+1); CombineRgn(wndrgn,wndrgn,temprgn,RGN_XOR); DeleteObject(temprgn); } r--; } //temprgn=CreatePolygonRgn(a,m_Image->Height*2,ALTERNATE); temprgn=CreatePolygonRgn(a,m_Image->Height*2,WINDING); CombineRgn(wndrgn,wndrgn,temprgn,RGN_AND); DeleteObject(temprgn); delete a; SetWindowRgn(Handle,wndrgn,true);}
[解决办法]
(小胖猫^_^笨猫先飞) :画菜单的我随后再补充,现在再贴一篇如何在listview里显示缩略图的代码。
用到了gdiplus,请按网上的说明配置。
//.h//---------------------------------------#ifndef Unit1H#define Unit1H//---------------------------------------#include <Classes.hpp>#include <Controls.hpp>#include <StdCtrls.hpp>#include <Forms.hpp>#include <ComCtrls.hpp>#include <StrUtils.hpp>#include <ImgList.hpp>#include <vector>#include <algorithm>#define THUMBNAIL_WIDTH 100#define THUMBNAIL_HEIGHT 75using std::min;using std::max;#include <gdiPlus.h>using namespace Gdiplus;//---------------------------------------class TForm1 : public TForm{__published: // IDE-managed Components TListView *m_ListThumbnail; TButton *Button1; TEdit *m_strImageDir; TImageList *m_ImageListThumb; TButton *Button2; void __fastcall Button1Click(TObject *Sender); void __fastcall Button2Click(TObject *Sender);private: // User declarations BOOL __fastcall GetImageFileNames(); void __fastcall DrawThumbnails(); ULONG_PTR m_GdiplusToken; GdiplusStartupInput m_GdiplusStartupInput;public: // User declarations __fastcall TForm1(TComponent* Owner); __fastcall ~TForm1(void);protected: std::vector<String> m_VectorImageNames; // vector holding the image names};#pragma warn -bei //关闭枚举变量赋值的警告 class TWaitCursor{//自动处理等待光标,当函数调用栈解体时自动还原光标private:TCursor oldc;public:TWaitCursor(void) : oldc(Screen->Cursor){ Screen->Cursor = crHourGlass; }~TWaitCursor(void){ Screen->Cursor = oldc; }};//---------------------------------------extern PACKAGE TForm1 *Form1;//---------------------------------------#endif//.cpp//---------------------------------------#define NO_WIN32_LEAN_AND_MEAN#include "shlobj.h"#include <vcl.h>#pragma hdrstop#include "Unit1.h"//---------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;//---------------------------------------__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){ // init GDI+ GdiplusStartup(&m_GdiplusToken, &m_GdiplusStartupInput, NULL);}//---------------------------------------__fastcall TForm1::~TForm1(void){ GdiplusShutdown(m_GdiplusToken); // Close GDI+}//---------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){ String picFolder = ""; char szDir[MAX_PATH]="\0"; char pszDisplayName[MAX_PATH]="\0"; BROWSEINFO bi; ITEMIDLIST *pidl; bi.hwndOwner = Handle; bi.pidlRoot = NULL; bi.pszDisplayName = pszDisplayName; bi.lpszTitle = "请选择文件夹:"; bi.ulFlags = BIF_RETURNONLYFSDIRS; bi.lpfn = NULL; bi.lParam = 0; bi.iImage = 0; pidl = SHBrowseForFolder(&bi); if(pidl != NULL) if(SHGetPathFromIDList(pidl, szDir)) picFolder = szDir; m_strImageDir->Text = picFolder;}//---------------------------------------BOOL __fastcall TForm1::GetImageFileNames(){ String strExt; String strName; String strPattern; BOOL bRC = TRUE; HANDLE hFind = NULL; WIN32_FIND_DATA FindFileData; std::vector<String> VectorImageNames; if ( m_strImageDir->Text[m_strImageDir->Text.Length() - 1] == char('\\') ) strPattern.sprintf( "%s*.*", m_strImageDir->Text ); else strPattern.sprintf( "%s\\*.*", m_strImageDir->Text ); hFind = ::FindFirstFile(strPattern.c_str(), &FindFileData); // strat search if (hFind == INVALID_HANDLE_VALUE) { LPVOID msg; ::FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&msg, 0, NULL ); MessageBox(Handle,(LPTSTR)msg, "Thumbnail", MB_OK|MB_ICONSTOP); ::LocalFree(msg); return FALSE; } // filter off the system files and directories if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)) { // test file extension strName = FindFileData.cFileName; strExt = RightStr(strName,3); if ( AnsiSameText(strExt,"bmp") || AnsiSameText(strExt,"jpg") || AnsiSameText(strExt,"gif") || AnsiSameText(strExt,"tif") || AnsiSameText(strExt,"png") ) { // save the image file name VectorImageNames.push_back(strName); } } // loop through to add all of them to our vector while (bRC) { bRC = ::FindNextFile(hFind, &FindFileData); if (bRC) { // filter off the system files and directories if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)) { // test file extension strName = FindFileData.cFileName; strExt = RightStr(strName,3); if ( AnsiSameText(strExt,"bmp") || AnsiSameText(strExt,"jpg") || AnsiSameText(strExt,"gif") || AnsiSameText(strExt,"tif") || AnsiSameText(strExt,"png") ) { // save the image file name VectorImageNames.push_back(strName); } } } else { DWORD err = ::GetLastError(); if (err != ERROR_NO_MORE_FILES) { LPVOID msg; ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&msg, 0, NULL); MessageBox(Handle,(LPTSTR)msg, "Thumbnail", MB_OK|MB_ICONSTOP); ::LocalFree(msg); ::FindClose(hFind); return FALSE; } } } // end of while loop // close the search handle ::FindClose(hFind); // update the names, if any if ( !VectorImageNames.empty() ) { // reset the image name vector m_VectorImageNames.clear(); m_VectorImageNames = VectorImageNames; return TRUE; } return FALSE;}//---------------------------------------void __fastcall TForm1::DrawThumbnails(){ ::Graphics::TBitmap* pImage = NULL; HBITMAP hBmp = NULL; POINT pt; String strPath; int i; TListItem *ListItem; // no images if (m_VectorImageNames.empty()) return; int nGap = 6; m_ListThumbnail->DoubleBuffered = true; // reset our image list for (i = 0; i<m_ImageListThumb->Count; i++) m_ImageListThumb->Delete(i); // remove all items from list view if (m_ListThumbnail->Items->Count != 0) m_ListThumbnail->Items->Clear(); // set the size of the image list for (int i =0; i<m_VectorImageNames.size();i++) { m_ImageListThumb->Add(NULL,NULL); } i = 0; std::vector<String>::iterator iter; for (iter = m_VectorImageNames.begin(); iter != m_VectorImageNames.end(); iter++) { // load the bitmap strPath.sprintf( "%s\\%s", m_strImageDir->Text, *iter ); Bitmap *img; img = Bitmap::FromFile(WideString(strPath)); Bitmap *pThumbnail = static_cast<Bitmap*>(img->GetThumbnailImage(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, NULL, NULL)); pThumbnail->GetHBITMAP(NULL, &hBmp); pImage = new ::Graphics::TBitmap(); pImage->Handle = hBmp; m_ImageListThumb->Replace(i, pImage, NULL); ListItem = m_ListThumbnail->Items->Insert(i); ListItem->Caption = m_VectorImageNames[i]; ListItem->ImageIndex = i; pt = m_ListThumbnail->Items->Item[i]->GetPosition(); pt.x = nGap + i*(THUMBNAIL_WIDTH + nGap); m_ListThumbnail->Items->Item[i]->SetPosition(pt); i++; delete img; delete pImage; delete pThumbnail; }}void __fastcall TForm1::Button2Click(TObject *Sender){ // validate image directory if (m_strImageDir->Text.IsEmpty()) { MessageBox(Handle,"Invalid image directory", "Thumbnail", MB_OK|MB_ICONSTOP); m_strImageDir->SetFocus(); return; } TWaitCursor waitit; //变为等待光标 if ( !GetImageFileNames() ) { return; } DrawThumbnails(); m_ListThumbnail->SetFocus(); m_ListThumbnail->ItemIndex = 0; return; //直接返回,光标会自动还原}//---------------------------------------
[解决办法]
BUTTON按钮不能改颜色
于是我用LABEL控件当按钮来用
可以改变颜色 还可以改变按下的幅度
小菜鸟一只 就这点能耐了
void __fastcall TForm1::Label1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y){ Label1-> Top = Label1-> Top + 3; Label1-> Left = Label1-> Left +3;}//---------------------------------------void __fastcall TForm1::Label1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y){ Label1-> Top = Label1-> Top - 3; Label1-> Left = Label1-> Left - 3;}//---------------------------------------