Showing posts with label MFC. Show all posts
Showing posts with label MFC. Show all posts

Thursday, January 8, 2015

Fixing "Fatal Error LNK1123 : Failure During Conversion to COFF, File Invalid or Corrupt" in VS 2010

Seperti terlihat pada judul entrinya, disini akan diperlihatkan cara untuk menangani error pada visual studio 2010 untuk lnk1123 tersebut. Sebelumnya error tersebut terjadi karena ada beberapa hal. Kalau saya sendiri, error tersebut terjadi karena microsoft net framework yang ada di PC diupgrade ke 4.5. Sedangkan visual studio 2010 sendiri menggunakan versi yang lebih lama. Nah pada saat kita akan menjalankan program C++(MFC) di visual studio tersebut, maka error tersebut akan terjadi. Tapi untuk program yang buat C# normal-normal saja. Hal lain yang dapat menyebabkan error seperti ini adalah apabila kita mempunyai visual studio 2010 dan visual studio yang lebih baru misalnya 2012 atw 2013 di PC. Kemudian kita akan menjalankan program C++ di vs 2010, nah kondisi seperti ini pun dapat menghasilkan error link1123.

Sebenarnya error tersebut terjadi karena linker visual studio berusaha menjalankan file cvtres.exe. Ketika net framework terupgrade, maka cvtres.exe terjadi ada 2 biji, satu dari VS 2010 dan satunya lagi dari net framework yang baru. Kemudian karena cvtres.exe ada yang terupdgrade maka cvtres.exe yang lama tidak dapat digunakan lagi padahal path visual studio 2010 itu masih terhubung ke cvtres.exe yang versi lama.

Jadi solusinya adalah atur kembali path visual studio ke cvtres.exe yang baru. Solusi lain yang dapat digunakan adalah mengubah nama cvtres.exe tersebut, misalnya ke cvtres-old.exe. Jika telah diubah, maka visual studio secara otomatis akan mencari cvtres.exe versi yang terbaru sehingga pengaturan pathnya tidak usah kita handle sendiri. Ngomong-ngomong, disini digunakan teknik yang kedua, yaitu mengubah nama cvtres.exe menjadi cvtres-old.exe. Jadi silahkan buka lokasi file tersebut, kalo di PC yang saya gunakan lokasinya ada di C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin, seperti terlihat di bawah :


Nah silahkan ubah file cvtres.exe tersebut. Click continu aja jika harus memperoleh administrator permission :
Setelah diubah :

Sampai disini, ketika program C++ (MFC) kita jalankan kembali, maka error lnk1123 tersebut sudah tidak ada dan program dapat kembali berjalan normal.



Sekian... semoga bermanfaat.... :-)

Sunday, June 2, 2013

Bitmaps Intro

A bitmap is a series of points (bits) arranged like a map so that, when put together, they produce a picture that can be written to, copied from, re-arranged, changed, manipulated, or stored as a computer file. Bitmaps are used to display pictures on graphical applications, word processors, database files, or audience presentations. To display its product on a device such as a monitor or a printer, a bitmap holds some properties and follows a set of rules.

There are various types of bitmap, based on the number of colors that the bitmap can display. First of all, a bitmap can monochrome in which case each pixel corresponds to 1 bit. A bit can also be colored. The number of colors that a bitmap can display is equal to 2 raised to the number of pits/pixel. For example, a simple bitmap uses only 4 pits/pixel or 4 bpp can handle only 2^4 = 16 colors. A more enhanced bitmap that requires 8 bpp can handle 2^8 = 256 colors. Bitmap are divided in two categories that control their availability to display on a device, namely :

1)    A device-independent bitmap (DIB) is a bitmap that is designed to be loaded on any application or display on any device and produce the same visual effect. To make this possible, such a bitmap contains a table of colors that describes how the colors of the bitmap should be used on pixels when displaying it. The characteristics of a DIB are defined by the BITMAPINFO structure.

2)    A device-dependent bitmap(DDB) is a bitmap created from the BITMAP structure the dimension of the bitmap.

Unlike the other GDI tools, creating a bitmap usually involves more steps. For example, we may want to create a bitmap to display on a window. We may create another bitmap to paint a geometric area, in which case the bitmap would be used as a brush.
Before creating a bitmap as a GDI object, we should first have a bitmap. We can do this by defining an array of unsigned hexadecimal numbers. Such a bitmap can be used for a brush.
To create and manipulate bitmaps, the MFC library provides the CBitmap class. The use of this class depends on the type of bitmap we want to create and how we want to use that bitmap. One way we can use a bitmap is to display a picture on a window. To do this, we must first have a picture resource. Although the Image Editor built -in Microsoft Visual C++ is meant to help with regular application resources, it has a problem handling a bitmap that displays more than 16 colors. The remedy used is to import the bitmap we want to use. Once our bitmap is ready, call the CBitmap::LoadBitmap() method. Its syntaxes :

       BOOL LoadBitmap(UINT nIDResource);
       BOOL LoadBitmap(LPCTSTR lpszResourceName);

The first version takes, as argument, the identifier of the bitmap we want to use. If the bitmap is recognized by its name, we can use the second version of this method and provide the lpszResourceName argument.
Before selecting the newly created bitmap object, allocate a block of computer memory that would hold the bitmap and can then copy it to the actual device. This job can be taken care of by the CDC::CreateCompatibleDC() method. Its syntax is :

      virtual BOOL CreateCompatibleDC(CDC *pDC);

This method takes a pointer to a device context. If it is successful, it returns TRUE or a non-zero value. If it is not, it returns FALSE or 0.

Practical Learning :
1. Start a new project and name it Bitmap1
2. Create is as a Single Document application based on CView
3. In the class View, expand everything and access the CMainFrame::PreCreateWindow() method
4. Change its code as follows:
      BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
      {
         if( !CFrameWnd::PreCreateWindow(cs)) return FALSE;
     
         //The new width of the window's frame
        cs.cx = 480;
     
        //The new height of the window's frame
       cs.cy = 490;

       //Remove the untitled thing
      cs.style & = ~FWS_ADDTOTITLE;
      return TRUE;
      }

5. In the Resource view, display the string table and change the IDR_MAINFRAME caption to Poipo on Phone\n\nBitmap\n\nBitmap1.Document\nBitmap Document

6. Right-click any folder and click import...
7. In the import Resource dialog box, change the Files of Type to All files and, in the Look In Combo Box, change the folder to the one that holds the accompanying exercise for this example.
8. select the bitmap
9. Click import. After the bitmap has been imported, we may receive a message box. Then just click OK
10. Right-click the new IDB_BITMAP1 resource and click Properties
11. Change its ID to whatever name we want
12. Add a message handler of the WM_PAINT message for the CBitmap1 View class and implement it as follows :

 void CBitmap1View::OnPaint()
 {
      CPaintDC dc(this); //device context for painting
   
      //TODO: Add your message handler code here
      CBitmap BmpPoipo;
      CDC MemDCPoipo;

     //Load the bitmap from the resource
     BmpPoipo.LoadBitmap(IDB_POIPO);
     //Create a memory device compatible with the above CPaintDC variable
     MemDCPoipo.CreateCompatibleDC(&dc);
     //Select the new bitmap
     CBitmap *BmpPrevious = MemDCPoipo.SelectObject(&BmpPoipo);

     //Copy the bits from the memory DC into the current dc
     dc.BitBlt(20,10,436,364,&MemDCPoipo,0,0,SRCCOPY);

     //Restore the old bitmap
     dc.SelectObject(BmpPrevious);

    //Note : Do not call CView::OnPaint() for painting messages
 }

13. Run the applications... :D

Drawing Text in MFC

By default, the CDC class is able to draw text using a font pre-selected, known as the System Font. To draw text, we can use the CDC::TextOut() method. Its syntax is :

 virtual BOOL TesxtOut(int x, int y, LPCTSTR lpszString, int nCount);

To use this method, we must specify where the text would start. This location is determined from the (0,0) origin to the right (x) and to the bottom (y). The text to display is the lpszString. The nCount value is the length of text. Here is an example :

void CExoView::OnDraw(CDC *pDC)
{
      CExoDoc *pDoc = GetDocument();
      ASSERT_VALID(pDoc);
   
     pDC->TextOut(50,42,"Hellow Poipo",12);
}

If we want to control the color used to draw the text, use the CDC::SetTextColor() method whose syntax is :

virtual COLORREF SetTextColor(COLORREF crColor);

The argument can be provided as a COLORREF variable or by calling the RGB macro. Here the example :

void CExoView::OnDraw(CDC *pDC)
{
      CExoDoc *pDoc = GetDocument();
      ASSERT_VALID(pDoc);

     pDC->SetTextColor(RGB(255,25,2));
    pDC->TextOut(50,42,"Hellow Poipo",12);
}

Sunday, April 21, 2013

CStatic Members

Jika ingin membuat CStatic object maka operasi(method) yang bisa dilakukan pada object tersebut. Namun sebelum melangkah lebih jauh, object tersebut harus di create dulu dengan fungsi :
virtual BOOL Create(
                                   LPCTSTR lpszText,
                                   DWORD dwStyle,
                                   const RECT& rect,
                                   CWnd* pParentWnd,
                                  UINT nID = 0xFFFF);
Pada method tersebut, lpszText berfungsi untuk menspesifikasikan text yang akan disimpan pada control, jika tidak ada maka tidak akan ada text yang muncul hehe... :D
dwStyle berguna untuk menspesifikasikan kontrol statik pada window style. Kontrol statik di sini contohnya SS_BITMAP, SS_CENTER, SS_ICON, SS_RIGHT, dsb, keterangannya lengkapnya bisa dilihat di msdn.
kemudian variabel rect menspesifikasikan posisi dan ukuran dari kontrol statik. rect ini dapat berupa RECT structure ataupun CRECT object.
pParentWnd menunjukkan CStatic parent window yang biasanya adalah CDialog. pParentWnd ini biasanya bernilai NULL.
terakhir adalah nID yang menunjukkan ID pada kontrol statiknya.

Nah setelah objectnya di create, maka method yang bisa digunakan yaitu :

  • HBITMAP GetBitmap() const; Method ini mengembalikan handle bitmap yang sebelumnya diset oleh SetBitmap method. Contohnya, misalkan dbuat program berikut,

CStatic myStatic;
/*kita create dulu*/
myStatic.Create(_T("my Static Bitmap"),WS_CHILD|WS_VISIBLE|SS_BITMAP|SS_CENTERIMAGE, CRect(10,10,150,50),NULL);
/*nah jika tidak ada bitmap yang didefinisikan untuk kontrol statik, maka buat bitmap ke system kemudian close bitmapnya*/
if(myStatic.GetBitmap() == NULL)
{ myStatic.SetBitmap(::LoadBitmap(NULL,MAKEINTRESOURCE(OBM_CLOSE)));
}

  • HCURSOR GetCursor(); Method ini menghandle current cursor yang sebelumnya diset oleh SetCursor method. Jadi method ini akan bernilai NULL jika sebelumnya belum ada cursor yang di set, perlu diketahui bahwa method ini butuh header afxwin.h. Contoh :
CStatic myStatic;
myStatic.Create(_T("My Static lagi hehe",WS_CHILD|WS_VISIBLE|SS_ICON|SS_CENTERIMAGE, Crect(10,10,150,50),NULL);
/*jika blm ada image yang didefinisikan untuk static control, maka buat image ke system arrow dan question mark cursor*/
if(myStatic.GetCursor()==NULL)
{ myStatic.SetCursor(::LoadCursor(NULL,IDC_HELP));
}
  • HENMETAFILE GetEnhMetaFile() const; Method ini mengembalikan handle dari enhanced metafile yang sebelumnya diset oleh SetEnhMetaFile method. Method ini mengembalikan nilai NULL jika blm ada metafile yang telah diset. Sama seperti method sebelumnya, method ini juga butuh header afxwin.h. Contoh pemakaiannya :
CStatic myPoipo;
myPoipo.Create(_T("Poipo Clan"),WC_CHILD|WS_VISIBLE|SS_ENHMETAFILE|SS_CENTERIMAGE, CRect(10,10,150,50), NULL);
/*jika blm ada image maka definisikan image ke poipo.emf*/
if(myPoipo.GetEnhMetaFile() ==NULL)
    myPoipo.SetEnhMetaFile(::GetEnhMetafile(_T("poipo.emf")));

  • HICON GetIcon() const; Method ini mengembalikan nilai handle dari icon yang sebelumnya diset oleh SetIcon. Requirementnya afxwin.h. Ex:
CStatic poipo;
poipo.Create(_T("poipo GetIcon"),WS_CHILD|WS_VISIBLE|SS_ICON|SS_CENTERIMAGE, CRect(10,10,150,50), NULL);
if(poipo.GetIcon() ==  NULL)
   poipo.SetIcon(::LoadIcon(NULL,IDI_ERROR));

  • HBITMAP SetBitmap(HBITMAP hBitmap);  Method ini mengembalikan handle bitmap yang sebelumnya berhubungan dengan static control. Parameter hBitmap  pada method ini menunjukkan handle dari bitmap yang akan digambar pada static control. Bitmap akan secara otomatis digambar dari static control. Secara defult, bitmap tersebut akan ditampilkan dalam upper-left corner dan static control akan menyesuaikan ke ukuran dari bitmap. Kita dapat menggunakan berbagai macam ukuran window dan styles control static, tapi harus selalu menggunakan style SS_BITMAP dan SS_CENTERIMAGE. Ex:
CStatic hyosoka;
hyosoka.Create(_T("Hyosoka"),WS_CHILD|WS_VISIBLE|SS_BITMAP|SS_CENTERIMAGE, CRect(10,10,150,50),NULL);
if(hyosoka.GetBitmap == NULL)
   hyosoka.SetBitmap(::LoadBitmap(NULL,MAKEINTRESOURCE(OBM_CLOSE)));

  • HCURSOR SetCursor(HCURSOR hCursor); Method ini menghandle cursor yg akan digambar pada static control yang ditandai oleh parameter hCursor. Cursor akan secara otomatis digambar pada static control. Secara default, akan berbentuk upper-left corner dan akan disesuaikan dengan ukuran cursor. Kita dapat menggunakan beberapa style yang berbeda, namun harus menggunakan style SS_ICON & SS_CENTERIMAGE. Ex:
CStatic myCursor;
myCursor.Create(_T("My Poipo Cursor"),WS_CHILD|WS_VISIBLE|SS_ICON|SS_CENTERIMAGE, CRect(10,10,150,50),NULL);
if(myCursor.GetCursor() == NULL)
   myCursor.SetCursor(::LoadCursor(NULL,IDC_HELP));

  • HENMETAFILE SetEnhMetaFile(HENMETAFILE hMetaFile); Method ini menspesifikasikan metafile yang akan ditampilkan pada static control. Style wajib yang harus digunakan dalam method ini yaitu SS_ENHMETAFILE. Ex:
CStatic myMetaFile;
myMetaFile.Create(_T("myMetaFile"),WS_CHILD|WS_VISIBLE|SS_ENHMETAFILE|SS_CENTERIMAGE, CRect(10,10,150,50), NULL);
if (myMetaFile.GetEnhMetaFile() == NULL)
  myMetaFile.SetEnhMetaFile(::GetEnhMetaFile(_T("myemf.emf"));

  • HICON SetIcon(HICON hIcon); Method ini menunjukkan icon yang akan ditampilkan pada static control. Style window yang harus digunakan yaitu SS_ICON dan SS_CENTERIMAGE. Ex:
CStatic myIcon;
myIcon.Create(_T("My Static Icon"), WS_CHILD|WS_VISIBLE|SS_ICON|SS_CENTERIMAGE, CRect(10,10,150,50), NULL);
if(myIcon.GetIcon() == NULL)
  myIcon.SetIcon(::LoadIcon(NULL,IDI_ERROR));

  • virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); Method ini bersifat overridables (ada fitur virtual). Parameter lpDrawItemStruct berguna sebagai pointer ke DRAWITEMSTRUCT structure. Structure tersebut berisi informasi tentang item yang akan digambar dan tipe penggambaran yang dibutuhkan. Style window yang digunakan yaitu SS_OWNERDRAW.

Saturday, April 20, 2013

Adding JPG and GIF Images to MFC Project

1. Open the resource editor for a CDialog Window and add an MFC Picture Control
2. Change the Picture Control's "Type" property from the default "Frame" to "Bitmap"
3. Create a global pointer to a CStatic class object. Ex: CStatic * pPCView;
4. Associate the CStatic pointer with the Picture Control using the function GetDigItem() and casting to the correct data type in OnInitDialog(). We must do this before we want to try to call any methods with the pointer. Ex: pPCView = (CStatic *) GetDigItem(PC_VIEW);
5. Include the file in our globals to provide access to CImage class.
6. In our code where we want to display the image, create a CImage object and a CBitmap object. Ex:
      CImage ViewImage;
      CBitmap ViewBitmap;
7. Load the JPG or GIF file into the CImage object. Ex:
                                          ViewImage.Load(_T("View/Hyosoka.jpg"));
8. Attach the CImage object to the CBitmap Object by passing it as an argument to CBimap's Attach() method and calling detach() on the CImage. Ex: ViewBitmap.Attach(ViewImage.Detach());
9. Call the method SetBitmap() on the pointer to the picture control and pass in the CBitmap as an argument, casting it to HBITMAP. Ex: pPCView->SetBitmap((HBITMAP)ViewBitmap);
10. We will need to add ON_WM_PAINT to our MFC Message Map tags.
11. We should override the MFC OnPaint() method.
12. Call the CPaintDC method, passing in the this pointer.