How to add Images to CListCtrl in MFC - visual-c++

How do you add Images to a ClistCtrl in MFC? I have tried and found that it's quite difficult.
I used CImageList to add images and then passed it to the CListCtrl. Can you provide some samples?
m_sentToCListCtrl.InsertColumn(0, _T("Item Name"), LVCFMT_LEFT,nColInterval*3);
m_sentToCListCtrl.InsertColumn(1, _T("Value"),LVCFMT_LEFT, nColInterval);
m_sentToCListCtrl.InsertColumn(2, _T("Time"), LVCFMT_LEFT, rect.Width()-4*nColInterval);
ListView_SetExtendedListViewStyle(m_sentToCListCtrl.m_hWnd,LVS_EX_CHECKBOXES );
// Create 256 color image lists
HIMAGELIST hSentToList =ImageList_Create(84,71, ILC_COLOR8 |ILC_MASK , 8, 1);
m_sentToImageList.Attach(hSentToList);

You need to add some bitmaps to your CImageList after you have created it. Something like this:
m_myImageList.Create(84,71, ILC_COLOR8 |ILC_MASK , 8, 1);
CBitmap bm;
bm.LoadBitmap(IDB_BITMAP1);
m_myImageList.Add(&bm, RGB(0, 0, 0));
bm.LoadBitmap(IDB_BITMAP2);
m_myImageList.Add(&bm, RGB(0, 0, 0));
Then, attach it to the CListCtrl:
m_sentToCListCtrl.SetImageList(&m_imageList, LVSIL_SMALL);
Finally, you add items to your CListCtrl by using the InsertItem method:
LVITEM lvItem;
lvItem.iItem = 0;
lvItem.iImage = 0; // image index that refers to your image list
lvItem.pszText = L"Item 1";
lvItem.mask = LVIF_TEXT;
m_sentToCListCtrl.InsertItem(&lvItem);
For more info refer to CListCtrl documentation. There are examples too.

Related

How to measure ImageView before creating bitmap into it?

My ImageView is matching screen size on x-axis and is using remaining space on y-axis in my layout. I want to create bitmap into this ImageView with exactly the same size as the ImageView is. How to make it please? Can it be done by some automatic setting, should I call some measure function?
I tried SetAdjustViewBounds() but it didn't work for me.
Creating Bitmap big enough (I don't like much such a memory wasting) and setting SetScaleType(ImageView.ScaleType.Matrix) works, but still when I'm making drawing operations on canvas, I don't know real size of area I should paint into, both canvas and bitmap height are equal to yScreen while imgWeekView height is pretending to be 0, even though it paints whole desired area with gray color.
imgWeekView = new ImageView(context);
//imgWeekView.SetAdjustViewBounds(true);
imgWeekView.SetScaleType(ImageView.ScaleType.Matrix);
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent,1f);
layoutParams.Height = 0;
imgWeekView.LayoutParameters = layoutParams;
Bitmap bitmap = Bitmap.CreateBitmap((int)xScreen, (int)yScreen, Bitmap.Config.Argb8888);
cnvWeekView = new Canvas(bitmap);
imgWeekView.SetImageBitmap(bitmap);
linearLayout.AddView(imgWeekView); //whole activity layout
//Test
cnvWeekView.DrawColor(new Color(128, 128, 128));
Paint paint = new Paint(PaintFlags.AntiAlias);
paint.Color = new Color(255, 255,0);
cnvWeekView.DrawCircle(50, 50, 40, paint);
Finally I found a way how to measure my ImageView and here I will post my answer.
I believed that there should be much easier solution, but maybe there isn't. From this question I took most of the important data:
How to get the width and height of an android.widget.ImageView?
Things look however a little different in my android application and I'm not experienced enough to tell why. I had to change things a little. I had to learn a bit about interfaces and this question helped too.
Implementing the View.IOnTouchListener interface
Here is how I combined things. First I created class that will do the measure.
public class MyPredrawListener : Java.Lang.Object, ViewTreeObserver.IOnPreDrawListener
{
ImageView imageView;
public MyPredrawListener(ImageView img)
{
imageView = img;
}
public bool OnPreDraw()
{
imageView.ViewTreeObserver.RemoveOnPreDrawListener(this);
int finalHeight = imageView.MeasuredHeight;
int finalWidth = imageView.MeasuredWidth;
Bitmap bitmap = Bitmap.CreateBitmap(finalWidth, finalHeight, Bitmap.Config.Argb8888);
imageView.SetImageBitmap(bitmap);
//Test to see result
Canvas cnv = new Canvas(bitmap);
Paint paint = new Paint();
paint.Color = new Color(255, 255, 0);
cnv.DrawColor(new Color(128, 128, 128));
cnv.DrawCircle(finalWidth-50, finalHeight-50, 50, paint);
return true;
}
}
And in code where I create my imageView I set the listener like this.
imgWeekView = new ImageView(context);
MyPredrawListener listener=new MyPredrawListener(imgWeekView);
imgWeekView.ViewTreeObserver.AddOnPreDrawListener(listener);
In OnPreDraw function I put test code to see the result graphically, clearing bitmap to gray color and painting yellow circle to bottom right of a view.

Fl_Scroll initial scroll values

I'm using FLTK for a project (the version distributed with Debian/sid 1.3.2-6+b1) and I'm having some trouble initializing Fl_Scroll's content scroll values.
I create a Fl_Double_Window and on the right side a vertical panel using Fl_Scroll, it is positioned at left x 600 and top y 24.
Then I set Fl_Scroll's type to Fl_Scroll::VERTICAL and place a Fl_Button inside, everything works fine.
The problem? Fl_Scroll initialize already scrolled with xposition() = 600 and yposition() = 24 (X and Y of the Fl_Scroll's constructor?? Is supposed to work so?), instead I want it to initialize with content scrolled at left and top 0, 0, so I tried scroll_to() in different places, before and after window's show(), on FL_SHOW of the subclassed Fl_Scroll (I didn't really need to subclass Fl_Scroll, just trying to get scroll_to() working), also on overridden draw() after
the parent draw(), even creating Fl_Scroll at 0, 0 then position() at 600, 24, but nothing.
The only way I got scroll_to() working is if I call it from an async event after the application initialize (FL_KEYUP).
Here's the overridden Fl_Double_Window window constructor:
group = new Fl_Group(0, 0, WIN_W, WIN_H);
{
menu = new Fl_Menu_Bar(0, 0, WIN_W, MENU_H);
menu->add("File/Quit", FL_CTRL+'q', cbMenuQuit_i);
glview = new CGLView(0, MENU_H, WIN_W-PROPS_W+1, WIN_H-MENU_H);
group->resizable(glview);
scroll = new CScroll(WIN_W-PROPS_W, MENU_H, PROPS_W-1, WIN_H-MENU_H);
scroll->type(Fl_Scroll::VERTICAL);
{
Fl_Pack *pack = new Fl_Pack(0, 0, PROPS_W-1, WIN_H-1);
{
btnAddLayer = new Fl_Button(0, 0, PROPS_W, 32, "##+ Add Layer");
btnAddLayer->callback(btnAddLayerCb_i, (void *)this);
}
pack->end();
}
scroll->end();
}
group->end();
end();
size_range(WIN_MIN_W, WIN_MIN_H); /* Make the window resizable */
show();
Never mind, got it, two altenative:
create Fl_Scroll at 0, 0 then position() it after scroll->end()
create Fl_Scroll at the wanted position then after scroll->end() use negative values scroll_to(-X, -Y)
In both cases is important that the widgets contained are already added (scroll_to() alter cotained widgets coords)
Isn't clear to me why X and Y of Fl_Scroll's constructor are used.

How to Convert Emgu.Cv.Image<Gray,byte> to System.Image

I was new to Emgu Cv and i was wondering if someone could let me know how i change Emgu.Cv.Image to System.Image?If there is a need for further explanation, let me know and i will do it.The language i am using is C#.
You can just use the ToImage() method to get a System.Drawing.Bitmap (which is a derived class of System.Drawing.Image), so something like this
// create an Emgu image of 400x200 filled with Blue color
Image<Bgr, Byte> img = new Image<Bgr, byte>(400, 200, new Bgr(255, 0, 0));
// copy to a .NET image
System.Drawing.Image pMyImage = img.ToBitmap();
Is that what you mean?

Toolbar - Depth Color for Icons

I wanna make a large toolbar with support of icons with more colors depth than default in Visual Studio. I am using Visual Studio 2005 and the Toolbar is on a CDialog.
I used the Code found : here
but did not work.
int CSalariesForm::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
HBITMAP hBitmap = (HBITMAP) ::LoadImage(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDR_MAINFRAME1), IMAGE_BITMAP,
0,0, LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS);
CBitmap bm;
bm.Attach(hBitmap);
CImageList m_imagelist;
m_imagelist.Create(20, 20, ILC_COLOR8, 4, 4);
m_imagelist.Add(&bm, (CBitmap*) NULL);
cToolBar.Create(this);
cToolBar.GetToolBarCtrl().SetImageList(&m_imagelist);
cToolBar.ShowWindow(SW_SHOW);
cToolBar.SetBarStyle(CBRS_TOOLTIPS | CBRS_FLOATING | CBRS_ALIGN_TOP | CBRS_FLYBY);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
return 0;
}
And when I call the dialog there is no Toolbar shown. What is wong with the code?
Thank You
Assigning an image list to a toolbar does not create any toolbar buttons. Also your image list should be a class member, not a local variable.
// header file
private:
CImageList m_imagelist;
CToolBar m_toolbar;
// source file
enum { width = 20, height = 20 }; // width and height of one button image
m_toolbar.Create(this);
// create the image list
m_imagelist.Attach(
ImageList_LoadImage(
AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1), width, 4,
CLR_DEFAULT, IMAGE_BITMAP, LR_CREATEDIBSECTION | LR_LOADTRANSPARENT )
);
// set button and image sizes (copied from CToolBar::LoadToolBar)
m_toolbar.SetSizes(CSize(width+7,height+7), CSize(width,height));
// set image list
m_toolbar.GetToolBarCtrl().SetImageList(&m_imagelist);
// define command ids for each button
const UINT cmdIds[] = {
IDOK,
0, // separator
IDCANCEL,
};
// assign ids to the toolbar
m_toolbar.SetButtons(cmdIds, sizeof(cmdIds)/sizeof(UINT));
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
IDB_BITMAP1 is a 40x20 24bit color bitmap (two 20x20 buttons). If you need more control over the creation of the buttons, you can use CToolBarCtrl::SetButtons() instead. Refer to ImageList_LoadImage for more details on loading the image list.

Padding in UITextField

Is it possible to set padding inside a UITextField, if so how?
I was also wondering, here you are:
UIView paddingView = new UIView(new CGRect(0, 0, 5, 20));
YourTextField.LeftView = paddingView;
YourTextField.LeftViewMode = UITextFieldViewMode.Always;
You can modify padding size of course by changing values above.
Hope this will help.

Resources