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; Aligning Text... The following example draws text in a rectangle. Each line of text is centered (side to side), and ...


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
  #41 (permalink)  
Old 07-30-2007, 05:49 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

Aligning Text...

The following example draws text in a rectangle. Each line of text is centered (side to side), and the entire block of text is centered (top to bottom) in the rectangle.
WCHAR string[] = L"Use StringFormat and RectF objects to center text in a rectangle.";

FontFamily fontFamily(L"Arial");
Font font(&fontFamily, 12, FontStyleBold, UnitPoint);
RectF rectF(30.0f, 10.0f, 120.0f, 140.0f);
StringFormat stringFormat;
SolidBrush solidBrush(Color(255, 0, 0, 255));

// Center-justify each line of text.
stringFormat.SetAlignment(StringAlignmentCenter);

// Center the block of text (top to bottom) in the rectangle.
stringFormat.SetLineAlignment(StringAlignmentCente r);

graphics.DrawString(string, -1, &font, rectF, &stringFormat, &solidBrush);

Pen pen(Color(255, 0, 0, 0));
graphics.DrawRectangle(&pen, rectF);
bye
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
  #42 (permalink)  
Old 07-30-2007, 05:55 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

creating directory with user access persmission

Hi friends,

here I have given the code for creating directory with user access persmission. Very useful one. This code I got from Platform SDK -> Security section.
#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <sddl.h>
#include <stdio.h>

BOOL CreateMyDACL(SECURITY_ATTRIBUTES *);

void main()
{
SECURITY_ATTRIBUTES sa;

sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = FALSE;

// Call function to set the DACL. The DACL
// is set in the SECURITY_ATTRIBUTES
// lpSecurityDescriptor member.
if (!CreateMyDACL(&sa))
{
// Error encountered; generate message and exit.
printf("Failed CreateMyDACL\n");
exit(1);
}

// Use the updated SECURITY_ATTRIBUTES to specify
// security attributes for securable objects.
// This example uses security attributes during
// creation of a new directory.
if (0 == CreateDirectory(TEXT("D:\\Karpagarajan"), &sa))
{
// Error encountered; generate message and exit.
printf("Failed CreateDirectory\n");
exit(1);
}

// Free the memory allocated for the SECURITY_DESCRIPTOR.
if (NULL != LocalFree(sa.lpSecurityDescriptor))
{
// Error encountered; generate message and exit.
printf("Failed LocalFree\n");
exit(1);
}
}


// CreateMyDACL.
// Create a security descriptor that contains the DACL you want.
// This function uses SDDL to make Deny and Allow ACEs.
//
// Parameter:
// SECURITY_ATTRIBUTES * pSA
// Pointer to a SECURITY_ATTRIBUTES structure. It is the caller's
// responsibility to properly initialize the structure and to free
// the structure's lpSecurityDescriptor member when the caller has
// finished using it. To free the structure's lpSecurityDescriptor
// member, call the LocalFree function.
//
// Return value:
// FALSE if the address to the structure is NULL.
// Otherwise, this function returns the value from the
// ConvertStringSecurityDescriptorToSecurityDescripto r function.
BOOL CreateMyDACL(SECURITY_ATTRIBUTES * pSA)
{
// Define the SDDL for the DACL. This example sets
// the following access:
// Built-in guests are denied all access.
// Anonymous logon is denied all access.
// Authenticated users are allowed read/write/execute access.
// Administrators are allowed full control.
// Modify these values as needed to generate the proper
// DACL for your application.
TCHAR * szSD = TEXT("D:") // Discretionary ACL
TEXT("(D;OICI;GA;;;BG)") // Deny access to built-in guests
TEXT("(D;OICI;GA;;;AN)") // Deny access to anonymous logon
TEXT("(D;OICI;GRGWGX;;;BG)") // Allow read/write/execute to authenticated users
TEXT("(A;OICI;GRGWGX;;;BA)") // Allow read/write/execute to authenticated users
TEXT("(A;OICI;GA;;;BA)"); // Allow full control to administrators

if (NULL == pSA)
return FALSE;

return ConvertStringSecurityDescriptorToSecurityDescripto r(
szSD,
SDDL_REVISION_1,
&(pSA->lpSecurityDescriptor),
NULL);
}
bye
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #43 (permalink)  
Old 07-30-2007, 06:39 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

WM_MESSAGE => KILL_FOCUS

Hi Buddies,

here I am giving the solution for ActiveX control illegal closing of Internet explorer.

I had found this problem in my activex which was used in web development.
The IE was closed with the Illegal error closing message. When I try to find out the problem in debug mode, I had found that there was stack point in my variable assignment statement in VC++.

The error happens because of the memory variable not been destroyed after the control was destroyed. The memory was not flushed after the activex control unloaded. I just check the flag in activex WM_MESSAGE => KILL_FOCUS.

Hope it may help you in activex coding part.

...........
bye
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #44 (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

Hi buddies,

You can use the following steps to maintain the version in ATL activeX control.
1. Open your Visual C++ ATL project workspace in VC++ IDE
2. Go to resource view
3. In that select version folder
4. In that one node "VS_VERSION_INFO", open it.
5. A grid window will be displayed. In that change the "FILEVERSION" key and "PRODUCTVERSION" key.
by changing these two keys you can maintain your activex control versioning.
..............
bye
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Booom : 08-09-2007 at 04:44 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #45 (permalink)  
Old 07-30-2007, 08:31 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

ASSERTION

An assertion statement specifies a condition that you expect to hold true at some particular point in your program. If that condition does not hold true, the assertion fails, execution of your program is interrupted, and the Assertion Failed dialog box appears.

MFC and C Run-Time Library Assertions
When the debugger halts because of an MFC or C run-time library assertion, it navigates to the point in the source file where the assertion occurred (if the source is available). The assertion message appears in the Output window as well as the Assertion Failed dialog box. You can copy the assertion message from the Output window to a text window if you want to save it for future reference. The Output window may contain other error messages as well. Examine these messages carefully, because they provide clues to the cause of the assertion failure.

_DEBUG
Assertion statements compile only when _DEBUG is defined. When _DEBUG is not defined, the compiler treats assertions as null statements. Therefore, assertion statements have zero overhead in your final release program; you can use them liberally in your code without affecting the performance of your Release version and without having to use #ifdefs.

...............
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #46 (permalink)  
Old 08-02-2007, 03:01 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

To enable debugging of a release build, change the following release build options in VC++

1. On the Project menu, choose Settings.
2. Next to Settings for, select the configuration.
3. In the project tree, under Settings for, select a project.
4. Choose the C/C++ tab.
5. In the Category drop-down box, choose General.
6. In the Debug info drop-down box, choose Program Database.
7. In the Optimizations drop-down box, choose Disable (Debug).
8. On the Link tab, select the Generate Debug Info checkbox.

These selections correspond to the /Od and /Zi options, which turn off optimizations and place debug information in the build.

After doing this, rebuild and test your program. If the program works fine, it is still possible that you have a memory overwrite, but it is also possible that the optimizations are causing problems on a particular piece of code.

...........
thanks
bye
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #47 (permalink)  
Old 08-02-2007, 03:05 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

__uuidof Operator

The __uuidof keyword retrieves the GUID attached to the expression.

__uuidof ( expression )
The expression can be a type name, pointer, reference, or array of that type, a template specialized on these types, or a variable of these types. The argument is valid as long as the compiler can use it to find the attached GUID.

A special case of this intrinsic is when either 0 or NULL is supplied as the argument. In this case, __uuidof will return a GUID made up of zeros.

Use this keyword to extract the GUID attached to:

An object by the uuid extended attribute.
A library block created with the module attribute.
The following code (compiled with ole32.lib) will display the uuid of a library block created with the module attribute:
// expre_uuidof.cpp
// compile with: ole32.lib
#include "stdio.h"
#include "windows.h"

[emitidl];
[module(name="joe")];
[export]
struct stuff {
int i;
};

int main() {
LPOLESTR lpolestr;
StringFromCLSID(__uuidof(joe), &lpolestr);
wprintf(L"%s", lpolestr);
CoTaskMemFree(lpolestr);
}
In cases where the library name is no longer in scope, you can use __LIBID_ instead of __uuidof. For example:

StringFromCLSID(__LIBID_, &lpolestr);

.................
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #48 (permalink)  
Old 08-02-2007, 03:08 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

From CString to char conversion...

Void CStrtoChar()
{
CString str = " test ";
char *p = new char [ str.GetLength () +1 ];
strcpy(p ,str);
delete p;
}

...............
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #49 (permalink)  
Old 08-02-2007, 03: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

Check the image file as JPEG...
Hi buddies,
here is the code to check the given file format is JPEG. Useful one...

Bitmap bm(L"Picture.dat");
GUID guidFileFormat;

bm.GetRawFormat(&guidFileFormat);
if(guidFileFormat == ImageFormatJPEG)
MessageBox(0, " JPEG!", 0, MB_OK);
.............
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #50 (permalink)  
Old 08-02-2007, 03:13 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

Disable mouse wheel scrolling

Dear Visual C++ Friends,

here is the trick
To disable mouse wheel in comboBox (MFC)

Use it in place of your combobox control, capture the mousewheel messages WM_MOUSEWHEEL and then don't forward them to the base class...

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

Last edited by Booom : 08-09-2007 at 04:45 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #51 (permalink)  
Old 08-02-2007, 03:16 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,

using the following win32 API we can find Multiple VGA card in Display.

EnumDisplayDevices()

..........
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #52 (permalink)  
Old 08-02-2007, 03:18 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,
I need: Get Handle of Main Window from process handle.

Here is the answer....

1. - Use GetProcessId() to get process id from process handle.

Use EnumWindows() to look at each top-level window.
For each top-level window call GetWindowThreadProcessId() to get the thread id and process id that was used to create the window.
Break when you find a top-level window that was created by the specified process handle/id.
2. - Use GetWindowThreadProcessId() to get the thread id and process id that was used to create the window.

...........
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #53 (permalink)  
Old 08-02-2007, 03:20 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

Trapping Windows Shutdown and Cancel the shutdown...

Hi,

Use WM_QUERYENDSESSION for system shutdown in WndProc system function.

When that message is received, return 0, pass the WM_CANCELMODE message

to that window message and then shutdown on your own by using

ExitWindowEx Api .

................
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #54 (permalink)  
Old 08-02-2007, 03: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

To change attributes for a directory
if (! SetFileAttributes("c:\\ResOrg",FILE_ATTRIBUTE_READ ONLY))
{
DWORD dwError = GetLastError();
}
................
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #55 (permalink)  
Old 08-02-2007, 03: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

Using multiple resource...

Hi ,

Here I am giving the tips for using multiple resource (*.rc) in your Visual C++ project

steps

1) Goto menu view-> Resource Includes
2) Resource Includes Window will be open. In that add #include "yourRes.rc" in the Compile - time Directives: textbox
3) Edit the main resource file and add #include yourRes.h behind the
#include "afxres.h"
#include yourRes.h
now your resource file will be linked

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

Last edited by Booom : 08-09-2007 at 04:46 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #56 (permalink)  
Old 08-02-2007, 03:27 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

To get the Cursor (X,Y) position in PreTranslateMessage add the following code.
POINTS pts = MAKEPOINTS( pMsg->lParam );
POINT point;
point.x = pts.x;
point.y = pts.y;
in the mouse message trapping like WM_LBUTTONUP ,WM_MOUSELEAVE etc.


thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #57 (permalink)  
Old 08-02-2007, 04:26 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

Change the color of the VC++ IDE...

by changing the registry values, we can change the color of the Visual C++ IDE...

[HKEY_USERS\.Default\Software\Microsoft\Devstudio\6 .0\Format\Source Window]
"FontFace"="Comic Sans MS"
"FontSize"=dword:00000009
"Text"=hex:c0,80,c0,00,00,00,00,00,12,01,b2,00
"Text Selection"=hex:00,00,00,00,c0,80,c0,00,19,00,19,00
"Current Error/Tag"=hex:ff,ff,ff,00,00,00,80,00,d2,01,b2,01
"Bookmark"=hex:00,00,00,00,00,ff,ff,00,10,00,1 0,00
"Breakpoint"=hex:ff,ff,ff,00,80,00,00,00,10,00 ,10, 00
"Current Statement"=hex:00,00,00,00,ff,ff,00,00,70,00,70,00
"Selection Margin"=hex:d8,e0,f0,00,d8,e0,f0,00,13,00,13,00
"Keyword"=hex:00,ff,00,00,00,00,00,00,10,00,10 ,00
"Comment"=hex:ff,ff,00,00,00,00,00,00,10,00,11 ,00
"Number"=hex:ff,00,00,00,00,00,00,00,10,00,11, 00
"String"=hex:ff,00,00,00,00,00,00,00,10,00,11, 00
"Operator"=hex:00,ff,00,00,00,00,00,00,10,00,1 1,00
"Wizard IDL/ODL Code"=hex:80,80,80,00,00,00,00,00,14,00,15,00
"HTML Element Name"=hex:c0,80,c0,00,00,00,00,00,14,00,15,00
"HTML Attribute Name"=hex:00,ff,00,00,00,00,00,00,14,00,15,00
"HTML Attribute Value"=hex:ff,00,00,00,00,00,00,00,14,00,15,00
"HTML Comment"=hex:ff,ff,00,00,00,00,00,00,14,00,15,00
"HTML Entity"=hex:ff,c0,40,00,00,00,00,00,14,00,15,00
"HTML Tag Delimiter"=hex:00,ff,00,00,00,00,00,00,14,00,15,00
"HTML String"=hex:ff,00,00,00,00,00,00,00,14,00,15,00
"HTML Tag Text"=hex:ff,00,ff,00,00,00,00,00,14,00,15,00
"HTML Operator"=hex:00,ff,00,00,00,00,00,00,14,00,15,00
"HTML Server-Side Script"=hex:00,00,00,00,ff,ff,00,00,14,00,14,00
"Wizard Code"=hex:80,80,80,00,00,00,00,00,14,00,15,00
"User Defined Keywords"=hex:00,00,ff,00,00,00,00,00,14,00,15,00

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

Last edited by Booom : 08-09-2007 at 04:46 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #58 (permalink)  
Old 08-20-2007, 08:03 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 another tips,

When debugging, you can single step into functions by pressing F11.

To break the debugging, press Ctrl+F5(this is to run the same application for debugging)

thanks
__________________
Karpagarajan. R
Necessity is the mother of invention
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #59 (permalink)  
Old 08-20-2007, 09:20 AM
roboeinstein roboeinstein is offline
D-Web Trainee
 
Join Date: Aug 2007
Posts: 1
roboeinstein is on a distinguished road
Question Re: VC++ Tips & Tricks

Hi,


#include<windows.h>

int_stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdLine,int nCmdShow)

{
MessageBox(0,"HELLO","TITLE",0);
return 0;

}
This program is not running it produces an error

syntax error : missing ';' before identifier 'WinMain'
error C2501: 'int_stdcall' : missing storage-class or type specifiers
fatal error C1004: unexpected end of file found
Error executing cl.exe.

How to I solve it.

Last edited by roboeinstein : 08-20-2007 at 09:22 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #60 (permalink)  
Old 08-21-2007, 01:03 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,

I hope you have started VC application wrongly. Here is the steps to run the WinMain. It is a Win32 Application. Follow the steps

1. New Project -> In Project tab, select Win32 Application
2. In your source code file for en example, if you started the project as test, then edit "test.cpp"
3. Add your Message box.
#include "stdafx.h"

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MessageBox(0,"Hello","Title",0);

return 0;
}
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Karpagarajan : 08-21-2007 at 01:09 AM.
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 Date
Microsoft Visual C - encyclopedia article about Microsoft Visual C.