|
Okay then send me some code, an example, something I can work with. I think by now we know that simply admonishing me for not using a Paint handler is not helping me.
Thanks,
Jeff
|
|
|
|
|
C# example (not tested):
private Panel panel;
private bool paintRectFlag=true;
private Rectangle rect=new Rectangle(20, 20, 300, 200);
private Pen rectPen=Pens.Black;
…
public Form1() {
InitializeComponents();
panel=new Panel();
panel.Paint+=panelPaintHandler;
Controls.Add(panel);
}
…
protected void panelPaintHandler(object sender, PaintEventArgs e) {
Graphics g=e.Graphics;
if (paintRectFlag) g.DrawRectangle(rectPen, rect);
}
protected void buttonClickHandler(object sender, EventArgs e) {
paintRectFlag=!paintRectFlag;
panel.Invalidate();
}
|
|
|
|
|
Thanks for the code.
As I understand it, the actual drawing occurs here:
protected void panelPaintHandler(object sender, PaintEventArgs e) {
Graphics g=e.Graphics;
if (paintRectFlag) g.DrawRectangle(rectPen, rect);
Which draws to a panel, which I'm assuming is a sort of generalized/abstract screen object. Presumably this would be rendered according to the needs of some Form (or other) object when invoked?
So if you (were to) run this, would it draw a rectangle? Or do you need additional code to make it "work"?
Thanks,
Jeff
|
|
|
|
|
Hello All,
I am trying to convert a list of Strings in LPWSTR* array. I am using the following way to convert it. But here the problem occurs in the returning array items. It contains all the elements same in the array. I found the problme is due to memset() which updates the memory address everytime same as earlier. Please help me out----
for(DWORD i = 0; i < itemIds->Count; i++)
{
ATL::CComBSTR itemWChar;
itemWChar = ::GetAtlBstr(itemIds[i]);
WCHAR _itemWChar[_MAX_PATH];
memset(_itemWChar, 0, sizeof(_itemWChar) / sizeof(_itemWChar[0]));
memcpy(_itemWChar, static_cast(itemWChar), itemWChar.ByteLength());
items[i] = _itemWChar;
}
ANURAG VISHNOI,
Sr. Software Engineer,
|
|
|
|
|
The memset() should have no effect here (nor is it necessary).
Plus, you've calculated the number of bytes to set incorrectly...
it should be sizeof(_itemWChar), not sizeof(_itemWChar) / sizeof(_itemWChar[0])
What are "itemIds" and "items" (what are their declared types)?
This is the part that looks fishy to me:
memcpy(_itemWChar, static_cast(itemWChar), itemWChar.ByteLength());
items[i] = _itemWChar;
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I'm writing a diagnostic tool in Visual C++. It's a GUI. I need to be able to have the program determine whether or not it needs to be loaded with admin privileges on Vista. The diagnostic tool works flawlessly in XP 32 bit, and it would work flawlessly on 64 bit if I could determine how to verify whether the system is 32 bit or 64 bit by redirecting the directory the batch files are automatically written to. Could anyone help me with figuring out the code used to first verify the OS and whether it's 32 or 64 bit? After that, possibly how to bypass UAC on Vista so it has the administrator rights when running batch files?
|
|
|
|
|
|
Thanks for that first link, that will help tremendously. What I meant by bypass UAC on Vista is to allow the program to execute batch files as the administrator. Some commands, such as resetting tcp/ip, require the "elevated" command prompt.
|
|
|
|
|
Something like this?
System::Diagnostics::Process ^process = gcnew System::Diagnostics::Process();
process->StartInfo->FileName = "cmd.exe";
process->StartInfo->Arguments = "/c c:\\mybatchfile.bat";
process->StartInfo->Verb = "runas";
process->Start();
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I'll have to try this, would this be Vista only or would it work on XP as well? With the runas verb, would that prompt the user or have it automatically do it?
|
|
|
|
|
turkmeistr1 wrote: With the runas verb, would that prompt the user
The way I showed it, yes, it will prompt.
System::Diagnostics::Process::StartInfo, as well as another System::Diagnostics::Process::Start() overload,
also have user name and password properties if you know an administrative user/password in advance.
turkmeistr1 wrote: I'll have to try this, would this be Vista only or would it work on XP as well?
As far as I know it should work on XP...try it - you can always just test with notepad or
something...
System::Diagnostics::Process ^process = gcnew System::Diagnostics::Process();
process->StartInfo->FileName = "c:\\windows\\system32\\notepad.exe";
process->StartInfo->Verb = "runas";
process->Start();
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Awesome, that works beautifully, final question, is there a way to verify if it's running on XP or Vista? I only want it to pull up the Run As prompt if it's Vista, otherwise it's not entirely necessary.
|
|
|
|
|
turkmeistr1 wrote: is there a way to verify if it's running on XP or Vista?
I use something like this:
static public bool OsIsAtLeastVista
{
get
{
bool ret = false;
System.OperatingSystem osInfo = System.Environment.OSVersion;
if (osInfo.Platform == System.PlatformID.Win32NT)
{
if (osInfo.Version.Major > 5)
{
ret = true;
}
break;
}
return ret;
}
}
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I get a lot of errors when using System.OperatingSystem, if I change it to System::OperatingSystem it works up unto the point of System::Environment.OSVersion, what should I use for that last dot there? The :: doesn't work for that, Platform, or Win32NT or Version.Major.
|
|
|
|
|
The code I posted was C#, sorry.
Use :: and -> where necessary to convert to C++/CLI.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I ripped the code from a larger property implementation and left some
artifacts. Here's fixed, in C++/CLI:
bool ret = false;
System::OperatingSystem ^osInfo = System::Environment::OSVersion;
if (osInfo->Platform == System::PlatformID::Win32NT)
{
if (osInfo->Version->Major > 5)
{
ret = true;
}
}
return ret;
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
YES! You are the greatest. Thank you so much for all of your help!
|
|
|
|
|
Grr, I keep hitting speed bumps! How would I be able to do file stream from within the app to write the batch file to a directory within the program files? It's saying that access was denied on Vista, it works perfectly fine in XP. I can force the program to run as administrator, but it still doesn't work when trying to do file stream to write to a file.
|
|
|
|
|
Hi,
My current project is to make wrapper classes of legacy mfc codes. I could easily marshal the String^ to CString like this:
bool LoadFile(String^ strPath)
{
// from managed to unmanaged using C++ Interop marshaling
pin_ptr<const wchar_t> pstrPath = PtrToStringChars(strPath);
// unmanaged declaration: bool LoadFile(CString path);
return m_pLegacyClass->LoadFile(pstrPath);
}
But string array is not a simple case. I marshaled all array members then copied the members to unmanaged memory. Here is the code.
bool LoadFiles(array<String^>^ arrayAviFilePath, int nAviFile)
{
// Array to marshal String^
array<IntPtr>^ arrayMarshalString = gcnew array<IntPtr>(nAviFile);
// Initialize unmanaged array
IntPtr arrayUnmanagedString = Marshal::AllocHGlobal(IntPtr::Size * nAviFile);
for (int i = 0; i < nAviFile; i++)
{
// Marshaling String^ to IntPtr
arrayMarshalString[i] = Marshal::StringToHGlobalUni(arrayAviFilePath[i]);
// Write to unmanaged memory
Marshal::WriteIntPtr(arrayUnmanagedString, i * IntPtr::Size, arrayMarshalString[i]);
}
// Use unmanaged array here
CString *pArrayAvi = static_cast<CString *>(arrayUnmanagedString.ToPointer());
bool bReturn = m_pLegacyClass->LoadFiles(pArrayAvi, nAviFile);
// Free unmanaged memory now
for (int i = 0; i < nAviFile; i++)
Marshal::FreeHGlobal(arrayMarshalString[i]);
Marshal::FreeHGlobal(arrayUnmanagedString);
return bReturn;
}
// legacy declaration
bool LoadFiles(CString arrayAviFilePath[], int nAviFile);
It works well anyway but is this best way?
|
|
|
|
|
Oldboy2 wrote: It works well
I don't see how, unless you're just getting lucky.
For example, casting arrayUnmanagedString.ToPointer() to a CString* is not valid.
It seems overly complex to me. Why not just create an array of CStrings and marshal
each System::String to a CString in the array like in your first simple example
(using PtrToStringChars)? There's no need to do all the managed allocation of native
memory - just use the native heap.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Mark Salsbery wrote: I don't see how, unless you're just getting lucky.
Didn't you get the memo? That's how it's done[^]!
|
|
|
|
|
Thank you for reply. I got a simplest example from googling. I did not even know CString has a constructor that takes a String. That's so simple!
bool LoadFiles(array<String^>^ arrayAviFilePath, int nAviFile)
{
CString *pArrayAvi = new CString[nAviFile];
for (int i = 0; i < nAviFile; i++)
{
pArrayAvi[i] = CString(arrayAviFilePath[i]);
}
bool bReturn = m_pLegacyClass->LoadFiles(pArrayAvi, nAviFile);
delete [] pArrayAvi;
return bReturn;
}
|
|
|
|
|
Oldboy2 wrote: I did not even know CString has a constructor that takes a String.
How could you know when they hide that information in the documentation[^]
|
|
|
|
|
Hey guys, thanks for looking at my questions!
First off, I am making a program that changes words into pig-latin form, here is an explanation of that: If a words starts with a vowel (including y), simply add -way to the end of the word (ex. "alphabet" would be alphabet-way). If a word starts with a consonant, move the section of the word up till the first vowel and put it at the end with "ay" attatched to it (ex. "lame" would be "ame-lay" and glucose would be "ucose-glay")
On to the questions:
I'm having some trouble figuring out how to determine the length of a word to a vowel. An example would be that the length of the word "Chair" up to the first vowel is 2.
Also, I am having trouble using the String::Compare method to determine if a word even starts with a vowel (see code block at the bottom). I believe the syntax is String::Compare(string1, subscript1, string2, subscript2, count) and it returns a value of 0 if string1 is equivalent to string2, please correct me if I am wrong. I used an array containing all of the vowels and used it as string2 and had the word to compare it it to as string1 with the subscripts and count set up accordingly but it does not seem to return a value as I expected.
Ok, I realize this probably made no sense without any code, so here is what I have so far (problem areas bolded):
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
String ^transformy = "";
String ^transformed = "";
String ^beginning = "";
String ^end = "";
array<String ^>^vowels = {"a", "e", "i", "o", "u", "y"};
int VowelOrNot = 0;
int toVowel = 0;
int toEnd = 0;
transformy = Convert::ToString(uIn->Text);
VowelOrNot = String::Compare(transformy, 0, Convert::ToString(vowels), 0, 1);;
if (VowelOrNot == 0)
{
transformed = String::Concat(transformy, "-way");
pOut->Text = Convert::ToString(transformed);
}
else
{
toVowel = Something goes here;
toEnd = Something also goes here;
beginning = transformy->Substring(toVowel, toEnd);
end = String::Concat(transformy->(0, toVowel), "ay");
transformed = String::Concat(String::Concat(begining, "-"), end);
pOut->Text = Convert::ToString(transformed);
}
}
Thank you very much for your time, and any help you can offer!
|
|
|
|
|
TabascoSauce wrote: //Determine if the first letter is a vowel by comaring the first letter
bool startsWithVowel = (Array::IndexOf(vowels, transformy->Substring(0,1), 0) == -1) ? false : true;
if(startsWithVowel)
{
.....
}
TabascoSauce wrote: //Determine how long a word is up to first vowel
transformy->IndexOfAny(vowels); You need to make vowels as array<Char>^
modified on Tuesday, April 21, 2009 5:37 AM
|
|
|
|