C++ Dll Win32 Console Project

1. สร้างโปรเจ็กส์ C++ แบบ Dll ชื่อ CDll เป็นแบบ Win32 Console Application > Dll

2. สร้างโปรเจ็กส์ C++ เพื่อใช้งาน Dll ชื่อ WinConsole เป็นแบบ Win32 Console Application > Console Application

เพิ่ม Code

เพิ่ม Code

ให้โปรเจ็กส์ WinConsole ทำการ Add reference project ไปที่โปรเจ็กส์ CDLL

ที่มา: Creation of a Simple DLLWalkthrough: Creating and Using a Dynamic Link Library

How to mix C and C++ ใน VC2005

เมื่อนำ code ภาษา c (ไฟล์ .c) มารัน อยู่ใน Project เดียวกันกับ .cpp แล้วเกิด Error ดังนี้
/Ycstdafx.h command-line option was not found in the source file
ให้แก้ไขด้วยการเพิ่ม #include “stdafx.h” ในไฟล์ .c
จากนั้นไปที่ Project > Properties > Configuration Properties > C/C++ > Precomplied Headers > Create/Use Precomplied Header
ให้เลือก Use Precomplied Header (/Yu)
เป็นอันเสร็จ

.NET Image Scanner

TWAIN

Windows Image Acquisition (WIA)

Convert Image to Icon

แปลงไฟล์ภาพ ไปเป็น icon มี 2 ตัวอย่าง ดังนี้

/// <summary>
/// Converts an image into an icon.
/// </summary>
/// <param name=”img”>The image that shall become an icon</param>
/// <param name=”size”>The width and height of the icon. Standard
/// sizes are 16×16, 32×32, 48×48, 64×64.</param>
/// <param name=”keepAspectRatio”>Whether the image should be squashed into a
/// square or whether whitespace should be put around it.</param>
/// <returns>An icon!!</returns>
private Icon MakeIcon(Image img, int size, bool keepAspectRatio) {
Bitmap square = new Bitmap(size, size); // create new bitmap
Graphics g = Graphics.FromImage(square); // allow drawing to it

int x, y, w, h; // dimensions for new image

if(!keepAspectRatio || img.Height == img.Width) {
// just fill the square
x = y = 0; // set x and y to 0
w = h = size; // set width and height to size
} else {
// work out the aspect ratio
float r = (float)img.Width / (float)img.Height;

// set dimensions accordingly to fit inside size^2 square
if(r > 1) { // w is bigger, so divide h by r
w = size;
h = (int)((float)size / r);
x = 0; y = (size – h) / 2; // center the image
} else { // h is bigger, so multiply w by r
w = (int)((float)size * r);
h = size;
y = 0; x = (size – w) / 2; // center the image
}
}

// make the image shrink nicely by using HighQualityBicubic mode
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(img, x, y, w, h); // draw image with specified dimensions
g.Flush(); // make sure all drawing operations complete before we get the icon

// following line would work directly on any image, but then
// it wouldn’t look as nice.
return Icon.FromHandle(square.GetHicon());

private void cmdConvert_Click(object sender, EventArgs e)
{
string source = @”d:\image.bmp”;
string target = @”d:\image.ico”;

BmpToIcon(source, target);
}

public static void BmpToIcon(string sourcePath, string
targetPath)
{

// Retrieve the bitmap from the file.
Bitmap bmp = (Bitmap)Image.FromFile(sourcePath);

// Convert the bitmap to an icon.
Icon ico = Icon.FromHandle(bmp.GetHicon());

// Create a file stream to save the icon stream.
Stream st = new FileStream(targetPath, FileMode.Create);

// Create a stream writer to physical write the data to
the disk.
BinaryWriter wr = new BinaryWriter(st);

// Write the binary icon data to the file stream.
ico.Save(st);

// Close the file to write the stream to the disk.
wr.Close();

ที่มา: dreamincode.net, bytes.com/topic/c-sharp

C# Visual Studio: Change the default icon of your .exe file.

To change the default icon of your .exe file click on :

  1. ไปที่ Project
  2. คลิ๊กขวาที่ Project เลือก Properties
  3. เลือก Application
  4. ที่ Icon ทำการเลือก icon ที่ต้องการ

เรียบร้อย

หรือจะใช้คำสั่ง ดังนี้
this.Icon = new System.Drawing.Icon(“shuttle.ico”);

ที่มา: normankosmal.comen.csharp-online.net

MDIList property missing?

การสร้างโปรเจ็กส์แบบ MDI
สิ่งสำคัญอย่างนึงคือการแสดงรายชื่อหน้าต่างย่อยบนเมนู Window ให้เลือกใช้ได้ง่ายๆ โดยการเขียน MDI ด้วย VS2003 ก็ใช้ MainMenu แล้วกำหนดคุณสมบัติ MDIList
แต่ตั้งแต่ VS2005 ไม่มี MainMenu ให้เลือกใช้ (แต่ถ้า Upgrade มาจาก VS2003 ก็ยังใช้ได้) ต้องเลือกใช้ MenuStrip แทน
แต่ MenuStrip ดันไม่มี MDIList property ให้ใช้
ทางแก้ก็ง่ายๆครับ

Me.MenuStrip1.MdiWindowListItem = WindowMenuToolStripItem

โดย WindowMenuToolStripItem คือ เมนูที่เราตั้งชื่อไว้ว่า Window ที่จะให้แสดงชื่อของหน้าต่างลูกนั่นเอง

Using OpenCV-2.1.0 with MS Visual C++ 2010 on Windows 7

ที่มา: Using OpenCV-2.1.0 with MS Visual C++ 2010 on Windows 7

 

หลังจากติดตั้งเสร็จ พอรัน helloworld เจอ error ดังนี้

The application was unable to start correctly (0*0150002). Click Ok to close the application.

วิธีแก้

ไปที่ Project -> Proporties -> Linker-> Input -> Additional Dependencies

จากนั้นเปลี่ยนจาก cv210d.lib; cvaux210d.lib; cxcore210d.lib; highgui210d.lib; ml210d.lib;

เป็น cv210.lib; cvaux210.lib; cxcore210.lib; highgui210.lib; ml210.lib; opencv_ffmpeg210.lib;

ก็จะไม่ error อีก

 

นอกจาก error แล้ว ยังมีคำเตือนคำเตือนเหล่านี้อีก

‘C:\Windows\SysWOW64\ntdll.dll’, Cannot find or open the PDB file
‘C:\Windows\SysWOW64\kernel32.dll’, Cannot find or open the PDB file
‘C:\Windows\SysWOW64\kernellbase.dll’, Cannot find or open the PDB file

วิธีแก้

Go to Tools->Options->Debugging->Symbols and select checkbox “Microsoft Symbol Servers”

http://stackoverflow.com/questions/4813975/why-is-visual-studio-2010-not-able-to-find-open-pdb-files

แต่หลังจากแก้ไข้แล้ว เจอ คำเตือนอันใหม่ดังนี้

‘OpenCV_helloworld.exe’: Loaded ‘C:\OpenCV2.2\bin\opencv_imgproc220d.dll’, Cannot find or open the PDB file
‘OpenCV_helloworld.exe’: Loaded ‘C:\WINDOWS\system32\imm32.dll’, Symbols loaded (source information stripped).

ยังไม่รู้ว่าแก้ยังไง

 

VS2010: IntelliSense: #error directive: Please use the /MD switch for _AFXDLL builds

สร้าง Project ใหม่แบบ MFC และเลือก Shared Library พอสั่งรันแล้วเกิด Error ดังนี้

IntelliSense: #error directive: Please use the /MD switch for _AFXDLL builds

ให้แก้ไขโดยคลิกขวาที่ Project เลือก properties > Configuration Properties > C/C++ > Code Generation

จากนั้นให้ดูที่ Runtime Libraryจะมีค่า default เป็น Multi-threaded Debug DLL (/MDd) แต่ไม่ใช่ตัวหนา

จากนั้นให้เปลี่ยนเป็นค่าอื่นก่อน แล้วเลือกกลับมาเป็น Runtime Library ให้เป็น Multi-threaded Debug DLL (/MDd) อีกครั้ง จะสังเกตุเห็นได้ว่า คำว่า Multi-threaded Debug DLL (/MDd) กลายเป็นตัวหนาแล้ว เป็นอันเสร็จ

อันนี้เข้าใจว่าเป็น Bug ของ VC 2010

vs2010

ที่มา: connect.microsoft.combasharatspace.blogspot.com