IT Community - Software Programming, Web Development and Technical Support

Visual C++ Tips & Tricks

This is a discussion on Visual C++ Tips & Tricks within the C and C++ Programming forums, part of the Software Development category; The following macros make it easy to add reminders which are displayed when code is compiled. You can double click ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #21 (permalink)  
Old 04-06-2007, 05:08 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Thumbs up Re: VC++ Tips & Tricks

The following macros make it easy to add reminders which are displayed when code is compiled. You can double click on a reminder in the Output Window and jump to the line. Useful for marking TODOs.

// Statements like:
// #pragma message(Reminder "Fix this problem!")
// Which will cause messages like:
// C:\Source\Project\main.cpp(47): Reminder: Fix this problem!
// to show up during compiles. Note that you can NOT use the
// words "error" or "warning" in your reminders, since it will
// make the IDE think it should abort execution. You can double
// click on these messages and jump to the line in question.
#define Stringize( L ) #L
#define MakeString( M, L ) M(L)
#define $Line \
MakeString( Stringize, __LINE__ )
#define Reminder \
__FILE__ "(" $Line ") : Reminder: "
Once defined, use like so:

#pragma message(Reminder "Fix this problem!")
This will create output like:

C:\Source\Project\main.cpp(47): Reminder: Fix this problem!


thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #22 (permalink)  
Old 04-06-2007, 05:14 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Thumbs up Re: VC++ Tips & Tricks

A bug with namespaces causes Visual C++ 6.0 to move a namespace to root in cases where it should only be in another namespace. Here is a small

example:
#include <string>

namespace ipl{ // use namespace ipl
using namespace std;
}

namespace ipl{ // use namespace ipl
}

int main()
{
string str;
return 0;
}

The code above should not compile since string in the main function is unknown, i.e. it should still be in the std namespace. This bug caused me to redo some code in IPL98, instead of writing using namespace std inside the ipl namespace, I am forced to explecitely write what functions/classes I need from the std library, for instance using std::string. This works.

thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #23 (permalink)  
Old 04-13-2007, 04:58 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Thumbs up Re: VC++ Tips & Tricks

Dealing with Palettes
I have written about palettes , I will say only that the application has a single palette that is created as a simple color cube to give a rough variety of colors. The application needs to handle two palette messages, and these are taken care of by these two functions:

void CPaintWnd::OnPaletteChanged(CWnd* pFocusWnd)
{
// See if the change was caused by us and ignore it if not.
if (pFocusWnd != this) {
OnQueryNewPalette();
}
}

BOOL CPaintWnd::OnQueryNewPalette()
{
// We are going active, so realize our palette.
if (m_pPal) {
CDC* pdc = GetDC();
CPalette *poldpal = pdc->SelectPalette(m_pPal, FALSE);
UINT u = pdc->RealizePalette();
ReleaseDC(pdc);
if (u != 0) {
// Some colors changed, so we need to repaint.
InvalidateRect(NULL, TRUE); // Repaint the lot.
return TRUE; // Say we did something.
}
}
return FALSE; // Say we did nothing.
}


That's more or less all there is to it, except to know that each time a new CDIB or CSprite object image is loaded, it must be mapped to this common palette by calling its MapColorsToPalette function. The CCrayon and CEraser classes are both derived from CDrawingTool, which does this for you in its Load function:

void CDrawingTool::Load(UINT uiID, CPalette* pPal)
{
ASSERT(uiID);
ASSERT(pPal);
BOOL b = CSprite::Load(uiID);
ASSERT(b);
MapColorsToPalette(pPal);
}


thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Karpagarajan : 04-13-2007 at 05:01 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #24 (permalink)  
Old 07-16-2007, 06:46 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Smile Re: VC++ Tips & Tricks

Hi

Here is the tips for avoiding the build errors in Visual C++.

To avoid build errors, when upgrading from one version of the compiler to another, make sure you have deleted your old PDB, PCH files.

thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Booom : 08-09-2007 at 05:41 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #25 (permalink)  
Old 07-30-2007, 02:12 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Post Re: VC++ Tips & Tricks

Here is the simple way to call URL...
________________________________________

Hi.. All
Here i give a simple way to call the URL using visual c++ code "ShellExecute"

ShellExecute(NULL,"open", "www.google.com", "", "c:\\", SW_SHOW);

thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Booom : 08-09-2007 at 05:41 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #26 (permalink)  
Old 07-30-2007, 02:16 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Default Re: VC++ Tips & Tricks

Hi.
Here is another tips.

When i tried to create a dll in c using visual c++ compiler i received these errors.

LNK2001: unresolved external symbol _main
Debug/windll.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.


Can you people tell why this error was occured?
....

Yeah..
The problem is I am creating a executable file
hence the compiler is looking for main method..

Inorder to create a DLL file we have to some manipulation in the project properties. i.e open the project properties window and select the link tab.
change the output file from exe to DLL and the "Project Options" text box change the "subsytem console" to "dll"

Thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Booom : 08-09-2007 at 05:42 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #27 (permalink)  
Old 07-30-2007, 02:20 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Default Re: VC++ Tips & Tricks

Hi All...

I developed one application which contains the listviews. In that
I gave the backgournd image for the listview.
In Some situations I need that listview should be blank(e.i without the background).

I dont know how to do this. I spent lot time to search in that.
Its simple. But that time i didnt know the code.
After some R&D i found it.

Do u know whats that?
Yeah this is what i had written in my code.

m_cListCtrl.SetBkImage( HBITMAP(0));

(HBITMAP(0) will return the NULL HBITMAP.)

bye
..........
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #28 (permalink)  
Old 07-30-2007, 02:22 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Post Re: VC++ Tips & Tricks

Hi friends...

Today I am going to give the tips and tricks for the GDI+.

When I was new to the GDI+, I was not able to include the GDI+ library files into my project.

Here We will see how to link the GDI+ into our project.

1.) Go to Project setting by pressing Alt+F7.
2.) Go to Link tab in the Project Property, In that select Input from the category
combo list.
3.) Include the GDIplus.dll file path
4.) Then go to Application class header file(e.g App.h). In that Add the following code to link the GDI+ library class and functions.

#include <vector>
#include "myGdiPlus.h"
using namespace Gdiplus;

//Image Thread Handle
ULONG_PTR gdiplusToken;

//And Start the GDIplus in Application InitInstance()
//begin: initialize GDI+
GdiplusStartupInput gdiplusStartupInput;
VERIFY(GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, NULL ) == Ok );
//end: initialize GDI+

//And Shutdown the GDIplus in Application ExitInstance()
//begin: shutdown GDI+
GdiplusShutdown(gdiplusToken);
//end: shutdown GDI+

Now your application will support the GDI+ class and functions

...........
bye
Thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #29 (permalink)  
Old 07-30-2007, 02:25 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Post Re: VC++ Tips & Tricks

Hi friends...
Here another tips.

Using GDI+, You can get the image from HBITMAP.
This is why i gave as a tips, some time we will need to load a picture in Static control or listview item. For that we need HBITMAP.
//The following code will help for this.
HBITMAP hBmp;
CString pszPath=_T("C:\\test.jpg");
WCHAR wpath[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, pszPath, -1, wpath, MAX_PATH);

Bitmap image(wpath);
bmPhoto = new Bitmap( IMGWIDTH, IMGHEIGHT, PixelFormat24bppRGB );
bmPhoto->SetResolution( image.GetHorizontalResolution(), image.GetVerticalResolution() );

Graphics *grPhoto = Graphics::FromImage( bmPhoto );
Color colorW(255, 255, 255, 255);
grPhoto->Clear( colorW );
grPhoto->SetInterpolationMode( InterpolationModeHighQualityBicubic );
grPhoto->DrawImage( &image, Rect(destX, destY, destWidth, destHeight) );

bmPhoto->GetHBITMAP( colorW, &hBmp );
//End of the code
Hope this will help you
..............
bye
Thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #30 (permalink)  
Old 07-30-2007, 03:20 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Default Re: VC++ Tips & Tricks

Hi...

Today's tips

1. You must initialize references and non-static const members in the member-initialization list of constructor

2. To get some new powerful and useful source editor commands, click Macros on the Tools menu and load the sample macrofile

bye
......
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #31 (permalink)  
Old 07-30-2007, 05:11 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Post Re: VC++ Tips & Tricks

Rotating the Image in GDI
Hi...

Here is the tricks for rotating the Image in GDI+.
This code is taken from the Window SDK GDI+ help document.
This code is simple and very useful.
//------------------- code ------------------
Graphics graphics(hdc);
Image image(L"Crayons.jpg");

graphics.DrawImage(&image, 10, 10, image.GetWidth(), image.GetHeight());
image.RotateFlip(Rotate90FlipY);
graphics.DrawImage(&image, 160, 10, image.GetWidth(), image.GetHeight());
//------------------- code ------------------
.....

bye
Thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #32 (permalink)  
Old 07-30-2007, 05:54 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Default Re: VC++ Tips & Tricks

Hi Visual C++ friends,

Here is the another tips for getting the x,y cordination while mouse move

This is why i gave as tips, it will help to get the point structure from the lparam.

When i used ATL control, The mouse move message handler gave the function called
LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
return 0;
}
in that i couldnt know how to get the (x,y) mouse point

the paramters in this function called LPARAM is used to hold the mouse (x,y) coordination.

To typecast the LPARAM structure into point structure, use the following code
CPoint pint(lParam);
hope the above information will help u.

bye
.....
Thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Booom : 08-09-2007 at 05:42 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #33 (permalink)  
Old 07-30-2007, 05:56 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Default Re: VC++ Tips & Tricks

Hi VC++ friends

Anybody tell me how to assign a string value to the string variable which contains double quotes.

Say for example
CString mText=_T("My name is "Kabil" . ");?

Here is the tips for that.

For that you have to use escape sequence( \ )
CString mText=_T("My name is \"Kabil\" . ");
bye
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #34 (permalink)  
Old 07-30-2007, 05:59 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Default Re: VC++ Tips & Tricks

Hi ...
this tips is good one...

Using ATL in visual c++

I have received one error while implementing the ATL Objectsafty script
"error C2065: 'm_dwCurrentSafety' : undeclared identifier"
I had struggled with clearing this error.

After some internet search, i have found that it was because of not including the ATL IObjectSafetyImpl statement in ATL class declaration. After adding the following code, i didnt get the error.

public IObjectSafetyImpl<CNewUpl,
INTERFACESAFE_FOR_UNTRUSTED_CALLER |
INTERFACESAFE_FOR_UNTRUSTED_DATA>,


Hope it will help u to develop the error free controls in vc++
.....
bye
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Booom : 08-09-2007 at 05:43 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #35 (permalink)  
Old 07-30-2007, 06:03 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Default Re: VC++ Tips & Tricks

Hi Guys,

Here this is a very useful tip.

This is to call the VB DLL function in Visual C++ module.

DO the following code
// Add the following code in the StdAfx.h

#import "TestVBdll.dll"
using namespace TestVBdll;
// Add the following code in where you want to call the VB dll function

HRESULT hresult;
CLSID clsid;

CoInitialize(NULL); //initialize COM library
hresult=CLSIDFromProgID(OLESTR("TestVBdll.clsdll") ,&clsid);

_clsdll *t;
hresult=CoCreateInstance(clsid,NULL,CLSCTX_INPROC_ SERVER,__uuidof(_clsdll),(LPVOID *) &t);
if(FAILED(hresult))
{
//AfxMessageBox("Creation Failed");
return "failed";
}

t->TestVBFunction();
t->Release(); //call method
CoUninitialize(); //Unintialize the COM library

// End of the code
.......
bye
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Booom : 08-09-2007 at 05:43 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #36 (permalink)  
Old 07-30-2007, 06:05 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Default Re: VC++ Tips & Tricks

Hi,
here is the tips for copying the screen designs in one project to another project in visual c++.

Steps
1. Open the destination project workspace
2. Open the dialog window from resource view
3. Go to Files view in project explorer
4. Select the workspace name and click the right mouse button. select "Insert project in the workspace" menu from the context menu
5. And Open the resource view of the source project
6. Open the dialog window which you would like to copy the design
7. Paste it to the destination dialog window.
Now you will have the screen design

bye
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Booom : 08-09-2007 at 05:43 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #37 (permalink)  
Old 07-30-2007, 06:08 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Post Re: VC++ Tips & Tricks

Hi friends,

How can I shutdown the system programmatically?

The appropriately named ExitWindows() and ExitWindowsEx() functions do the trick. On Win9x, using them is pretty straightforward. On NT, however, the calling process must enable the SE_SHUTDOWN_NAME privilege explicitly before calling either of these functions. How to do this? by using AdjustTokenPrivileges(). You'll find examples on both issues below.

Also, on NT you'll find a very handy function called InitiateSystemShutdown(). It can basically do the same as ExitWindows(), except it allows you to shutdown remote systems. Remember that to do this, you need to have the SE_REMOTE_SHUTDOWN privilege granted on the remote computer, though.
bye
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #38 (permalink)  
Old 07-30-2007, 06:41 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Post Re: VC++ Tips & Tricks

Visual C++ Modifiers
Modifier Meaning
\b backspace
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\" double quote
\' single quote
\<enter> line continuation
\nnn nnn = octal character value
\Oxnn nn = hexadecimal value (not all compilers)
thanks
bye
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Booom : 08-09-2007 at 05:43 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #39 (permalink)  
Old 07-30-2007, 06:44 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Post Re: VC++ Tips & Tricks

GDI Font in Visual C++

Hi buddies,

This tips is about fonts in GDI+(very useful).

The System.Drawing.Font class represents a font type.

For example,

Font fnt = new Font("Arial", 14);

Creates a font type verdana with size 14. You can also use a FontStyle as an argument when constructing a font.
Font redStyle = new Font("Tahoma", 20, FontStyle.Bold|FontStyle.Italic|FontStyle.Underlin e);

g.DrawString("Text on the Screen", greenSolid, new SolidBrush(Color.Green), 10,10);

g.DrawString("Red Text", redStyle, new HatchBrush(HatchStyle.DiagonalCross, Color.Chocolate, Color.Red), 50,40);
The FontStyle Enumeration defines these styles.

bye
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Booom : 08-09-2007 at 05:44 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #40 (permalink)  
Old 07-30-2007, 06:47 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 299
Karpagarajan is on a distinguished road
Post Re: VC++ Tips & Tricks

Using Region in GDI+ programming....

Hi friends here is the sample code for GDI+ region in VC++(Good one)
Pen myPen(Color(255, 0, 0, 0), 1);

Rect rcLeft=Rect(20,20,100,100);

Region region1(rcLeft);

graphics->DrawRectangle(&myPen, rcLeft);

myPen.SetColor(Color(255, 255, 0, 0));
if(region1.IsVisible(point.x,point.y, graphics))
{
graphics->DrawRectangle(&myPen, rcLeft);
}
bye
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/c-c-programming/531-visual-c-tips-tricks.html
Posted By For Type