VC 背景填充位圖的幾種方法
IDB_BG 是位圖資源的ID
方法一
OnEraseBkgnd(CDC* pDC)
{
// 設置畫刷爲希望的背景
CDC backBmp;
backBmp.CreateCompatibleDC(pDC);
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BG);
CBrush m_brushBackground;
m_brushBackground.CreatePatternBrush(&bitmap); ///創建位圖畫刷
CBrush* pOldMemDCBrush=pDC->SelectObject(&m_brushBackground);
pDC->PatBlt(0,0,m_ScrollRect.Width(),m_ScrollRect.Height(),PATCOPY);
pDC->SelectObject(pOldMemDCBrush);
return TRUE;
}
方法二
OnEraseBkgnd(CDC* pDC)
{
CBitmap m_pMap;
m_pMap.LoadBitmap(IDB_BG);
CRect rect;
GetClientRect(&rect);
BITMAP bm;
CDC dcMem;
m_pMap.GetObject(sizeof(BITMAP),&bm);
dcMem.CreateCompatibleDC(pDC);
CBitmap* pOldBitmap = dcMem.SelectObject(&m_pMap);
for (register int nX=0;nX<rect.right-rect.left;nX+=bm.bmWidth)
for(int nY=0;nY<rect.bottom-rect.top;nY+=bm.bmHeight)
pDC->BitBlt(nX,nY,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY);
dcMem.SelectObject(pOldBitmap);
return TRUE;
}
方法三
OnEraseBkgnd(CDC* pDC)
{
CBitmap bmp;
bmp.LoadBitmap(IDB_BG); ///加載位圖
CBrush m_brushBackground;
m_brushBackground.CreatePatternBrush(&bmp); ///創建位圖畫刷
CRect rect;
GetClientRect(&rect);
pDC->FillRect(rect,&m_brushBackground); //用背景畫填充區域
return TRUE;
}
MFC中位圖按鈕的方法
曾加兩個按鈕所需的按下、擡起、選中和禁止狀態對應的位圖及其ID標識。在本文中共增加了8個位圖,它們的ID標識分別爲” ImageDown”、”ImageUp”、”ImageFocus”、 ”ImgaeDis”、”CancelD”、”CancelU”、”CancelF”、“CancelX“。要注意這裏的ID標識都要包含雙引號,從而使得 Visual C++ 不爲它們生成標識符定義語句。
(1)利用LoadBitmaps函數繪製按鈕
① 向CBMTestDlg類中添加成員變量:
protected: CBitmapButton button1; //定義位圖按鈕
② 在CBMTestDlg類的構造函數CBMTestDlg::CBMTestDlg中添加如下代碼:
//調入按鈕位圖資源函數
if(!button1.LoadBitmaps(_T(“ImageUp", //按鈕一所需位圖
_T(“ImageDown",_T(“ImageFocus",
_T(“ImageDis”))) // 擡起、按下、選中和禁止
{
TRACE("Failed to load bitmap for button1\n";
AfxThrowResourceException();
}
③ 向類CBMTestDlg類的OnInitDialog成員函數中添加如下代碼:
//將對話框中按鈕標識與位圖連接
VERIFY(button1.SubclassDlgItem(IDOK, this));
button1.SizeToContent();//使按鈕大小適應位圖
(2)利用AutoLoad函數繪製按鈕
① 向CBMTestDlg類中添加成員變量:
protected:
CBitmapButton button2; //定義位圖按鈕
② 向類CBMTestDlg類的OnInitDialog成員函數中添加如下代碼:
VERIFY(button2.AutoLoad(IDCANCEL,this)); //調入位圖
///////////////////////////////////////////
選定要使用位圖的按鈕(以OK按鈕爲例,假設其標識符爲IDC_BTONOK)
選其屬性中的Owner draw選項,此時,在對話框編輯器中就可看到原先顯示在
按鈕上的字符都消失了。將按鈕的Caption改爲OK(必須爲大寫)。
打開Insert菜單,單擊其中的Resource選項,隨後選擇Bitmap。再按下Import(導入)按鈕,將所需位圖導入項目(Project)。可以在Resource View窗口中通過右鍵單擊剛纔導入的位圖,將其ID(標識符)改爲"OKU",注意:字符必須爲大寫,雙引號及字母U必不可少。字母U代表的按鈕爲按下狀態時所顯示的位圖。此外還可使用後綴D、F、X,分別表示按下、擁有輸入焦點時、按鈕處於無效狀態時所顯示的位圖。通過對同一個按鈕的不同狀態使用不同的位圖,很容易做出具有動態效果的按鈕。
當將按鈕所需使用的位圖導入到項目中後,就應該在使用位圖按鈕的那個對話框的類聲明文件中加入位圖按鈕變量的定義CBitmapButton m_BtonOK。同時,在適當的位置(一般是在對話框的OnInitDialog()函數中)加入如下語句:m_BtonOK.AutoLoad(IDC_BTONOK),使程序在運行時將位圖裝入內存並顯示。
先把這個位圖這個選項選上,然後在程序里加下面的代碼:
HBITMAP hBitmap;
hBitmap = ::LoadBitmap(::AfxGetResourceHandle(), MAKEINTRESOURCE(IDB_BITMAP_DRAW));
m_btn.SetBitmap(hBitmap);
其中m_btn爲按鈕變量,IDB_BITMAP_DRAW爲要加載的位圖資源ID.
QQ頭像的那種列表框
procedure TFrmPrint.UpdateImageList;
var
MyImage:TAllImage;
MyLabel:TLabel;
SPos,i, j:integer;
NumOfDICOMFrame:integer;
begin
for i := scbImageList.ComponentCount - 1 downto 0 do
scbImageList.Components[i].free;
//圖象列表
//FindFiles(sPath:string;var FileBuf:TStringList;var fileCount:integer); //返回目錄下所有文件名 ,數目
FindFiles( CurrentExam.ImagePath,FileBuf,ImageNum);
if ImageNum=0 then
begin
exit;//沒有圖象的情況
pictureNum:=1;
end;
ImageNum:=fileCount;//圖象數目
j:=1;
for i := 1 to (ImageNum) do
begin
//爲圖像預留空間
MyImage := TAllImage.Create(scbImageList);
MyImage.Parent := scbImageList;
imgPath:=FileBuf[i];
if imgPath[length(imgPath)]<>'\' then
imgPath:=imgPath+'\'; //得到圖像路徑
MyImage.Hint := FileBuf[i]; //圖像的提示性文字是該圖像的文件名
MyImage.Filename:=MyImage.Hint;
SPos := scbImageList.VertScrollBar.ScrollPos;
MyImage.TOP := (j - 1) * 75+25*j -sPos;;
//MyImage.TOP := (j - 1) * 100 + 5*j -sPos;;
MyImage.Left := 10;
MyImage.Height := 75;
MyImage.Width := 100;
MyImage.Stretch := true;
MyImage.Filename:=MyImage.Hint;
// MyImage.GetFirstFrame;
MyImage.ShowHint := true;
MyImage.OnClick := OnImgClick; {---加圖象到報表中---}
j:= j + 1;
end;
MyImage.Free;
procedure TFrmPrint.UpdateImageList;
var
MyImage:TAllImage;
MyLabel:TLabel;
SPos,i, j:integer;
NumOfDICOMFrame:integer;
begin
for i := scbImageList.ComponentCount - 1 downto 0 do
scbImageList.Components[i].free;
//圖象列表
//FindFiles(sPath:string;var FileBuf:TStringList;var fileCount:integer); //返回目錄下所有文件名 ,數目
FindFiles( CurrentExam.ImagePath,FileBuf,ImageNum);
if ImageNum=0 then
begin
exit;//沒有圖象的情況
pictureNum:=1;
end;
ImageNum:=fileCount;//圖象數目
j:=1;
for i := 1 to (ImageNum) do
begin
//爲圖像預留空間
MyImage := TAllImage.Create(scbImageList);
MyImage.Parent := scbImageList;
imgPath:=FileBuf[i];
if imgPath[length(imgPath)]<>'\' then
imgPath:=imgPath+'\'; //得到圖像路徑
MyImage.Hint := FileBuf[i]; //圖像的提示性文字是該圖像的文件名
MyImage.Filename:=MyImage.Hint;
SPos := scbImageList.VertScrollBar.ScrollPos;
MyImage.TOP := (j - 1) * 75+25*j -sPos;;
//MyImage.TOP := (j - 1) * 100 + 5*j -sPos;;
MyImage.Left := 10;
MyImage.Height := 75;
MyImage.Width := 100;
MyImage.Stretch := true;
MyImage.Filename:=MyImage.Hint;
// MyImage.GetFirstFrame;
MyImage.ShowHint := true;
MyImage.OnClick := OnImgClick; {---加圖象到報表中---}
j:= j + 1;
end;
MyImage.Free;
1, ComboBox.style := csOwnerDrawFixed;
2。ComboBox1.ItemHeight = 32;
3。ComboBox1.DropDownCount = 2;
在Form上放一個TImageList,把它的Height屬性設爲32,然後隨便加幾個OICQ的bmp文件;
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
ComboBox1.Canvas.Brush.Color := clWindow;
if odSelected in State then ComboBox1.Canvas.Brush.Color := clHighLight;
ComboBox1.Canvas.FillRect(Rect);
ImageList1.Draw(ComboBox1.Canvas, Rect.Left, Rect.Top, Index);
end;
procedure TForm1.ComboBox1DropDown(Sender: TObject);
begin
if ComboBox1.Items.Count <> ImageList1.Count then
ComboBox1.Items.Text := StringOfChar(#13, ImageList1.Count);
end;
//添加一個Header
var
myHeader:TmxHeader;
i:integer;
begin
myHeader:=mxOutlookBarPro1.AddHeader;
myHeader.Caption:='MyCaption';
//添加MyHeader的其他屬性
//刪除一個MyHeader
mxOutlookbarPro1.DeleteHeader(i);
//修改一個MyHeader
mxOutlookBarPro1.RenameHeaderByIndex(i);
end;
//添加一個Button
var
mybutton:TmxButton;
begin
myButton:=mxOutlookbarPro1.HeaderbyIndex(i).Addbutton;
myButton.Caption:='MyCaption';
//刪除一個myCaption
end