How to Store Dynamically in the List Control of MFC in Visual Studio? - visual-studio-2010

I am working on the dialog based applciation using MFC in visual studio 2010. I used the list control as report type to display . I managed to display some hardcoded data on that output window. Here is the code. what's wrong in the code
void CuserspecificationDlg::OnAdd() // This function add file by clicking on Add button
{
// TODO: Add your control notification handler code here
CFileDialog ldFile(TRUE);
// Show the File Open dialog and capture the result
if (ldFile.DoModal() == IDOK)
{
CStdioFile fileName;
//TCHAR buf[100]; // it is declared in h file
while( fileName.ReadString(buf,99))
{}
fileName.Close();
}
void CuserspecificationDlg::InsertItems()
{
//
list.cx = 100;
list.pszText = "Project"; // this project is the column heading of the dialog
list.iSubItem = 2;
::SendMessage(hWnd ,LVM_INSERTCOLUMN,
(WPARAM)1,(WPARAM)&list);
SetCell(hWnd,"1",0,0);
SetCell(hWnd,buf,0,1); // these 1,G,X,X are the hardcoded entries.
SetCell(hWnd,"G ",0,2);
SetCell(hWnd," X",0,3);
//----- //
}
How to display that buf? It doesnt work . buf is not diplaying the content from file properly. As some characters 1,G and X are visible in the output window but the buf statement doesnt show the characters properly. .. What's wrong in the code.

To add items to a list control, you first need to create a column:
LVCOLUMN lvCol;
lvCol.mask = LVCF_TEXT | LVCF_WIDTH;
lvCol.pszText = L"Column Header Text";
m_CListCtrl.InsertColumn(0, &lvCol);
// ...
You then insert items into the list control of struct type LVITEM
LVITEM item;
item.mask = LVIF_TEXT;
item.pszText = "Column Text";
item.iItem = numItem; // Item number
item.iSubItem = 0; // Sub item number (column number)
m_CListCtrl.InsertItem(&item);

Related

Can I make a property sheet without resource script?

I'd like to add controls to a property sheet without resource script, rather using pure code.
The reason for this I'd like to create a property sheet(mimicking C#'s property grid), calling C routines/WINAPI, from another language, binary-compatible to C; but I'd like to define everything with code, without need of a resource script. Is this possible or the way to go is write my own property-sheet-like, with underlying CreateWindow*() calls? (different approaches to do this are welcome, I'm new to WINAPI) which I suppose property sheet use behind the scenes
Found the solution! I found this post from Raymond Chen where he shows how do that.
the main code goes like this:
BOOL FakeMessageBox(HWND hwnd, LPCWSTR pszMessage, LPCWSTR pszTitle)
{
BOOL fSuccess = FALSE;
HDC hdc = GetDC(NULL);
if (hdc) {
NONCLIENTMETRICSW ncm = { sizeof(ncm) };
if (SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, 0, &ncm, 0)) {
DialogTemplate tmp;
// Write out the extended dialog template header
tmp.Write<WORD>(1); // dialog version
tmp.Write<WORD>(0xFFFF); // extended dialog template
tmp.Write<DWORD>(0); // help ID
tmp.Write<DWORD>(0); // extended style
tmp.Write<DWORD>(WS_CAPTION | WS_SYSMENU | DS_SETFONT | DS_MODALFRAME);
tmp.Write<WORD>(2); // number of controls
tmp.Write<WORD>(32); // X
tmp.Write<WORD>(32); // Y
tmp.Write<WORD>(200); // width
tmp.Write<WORD>(80); // height
tmp.WriteString(L""); // no menu
tmp.WriteString(L""); // default dialog class
tmp.WriteString(pszTitle); // title
// Next comes the font description.
// See text for discussion of fancy formula.
if (ncm.lfMessageFont.lfHeight < 0) {
ncm.lfMessageFont.lfHeight = -MulDiv(ncm.lfMessageFont.lfHeight,
72, GetDeviceCaps(hdc, LOGPIXELSY));
}
tmp.Write<WORD>((WORD)ncm.lfMessageFont.lfHeight); // point
tmp.Write<WORD>((WORD)ncm.lfMessageFont.lfWeight); // weight
tmp.Write<BYTE>(ncm.lfMessageFont.lfItalic); // Italic
tmp.Write<BYTE>(ncm.lfMessageFont.lfCharSet); // CharSet
tmp.WriteString(ncm.lfMessageFont.lfFaceName);
// Then come the two controls. First is the static text.
tmp.AlignToDword();
tmp.Write<DWORD>(0); // help id
tmp.Write<DWORD>(0); // window extended style
tmp.Write<DWORD>(WS_CHILD | WS_VISIBLE); // style
tmp.Write<WORD>(7); // x
tmp.Write<WORD>(7); // y
tmp.Write<WORD>(200-14); // width
tmp.Write<WORD>(80-7-14-7); // height
tmp.Write<DWORD>(-1); // control ID
tmp.Write<DWORD>(0x0082FFFF); // static
tmp.WriteString(pszMessage); // text
tmp.Write<WORD>(0); // no extra data
// Second control is the OK button.
tmp.AlignToDword();
tmp.Write<DWORD>(0); // help id
tmp.Write<DWORD>(0); // window extended style
tmp.Write<DWORD>(WS_CHILD | WS_VISIBLE |
WS_GROUP | WS_TABSTOP | BS_DEFPUSHBUTTON); // style
tmp.Write<WORD>(75); // x
tmp.Write<WORD>(80-7-14); // y
tmp.Write<WORD>(50); // width
tmp.Write<WORD>(14); // height
tmp.Write<DWORD>(IDCANCEL); // control ID
tmp.Write<DWORD>(0x0080FFFF); // static
tmp.WriteString(L"OK"); // text
tmp.Write<WORD>(0); // no extra data
// Template is ready - go display it.
fSuccess = DialogBoxIndirect(g_hinst, tmp.Template(),
hwnd, DlgProc) >= 0;
}
ReleaseDC(NULL, hdc); // fixed 11 May
}
return fSuccess;
}

How to check if a button is checkbox on 64 bit windows?

I'm checking if a button is checkbox of 32bit process on 64bit windows10.
The problem is that I can not distingush checkbox from normal button.
The buttons are different in Window-Detective:
(After I restart the application, even Window-Detective shows it is a button now!)
But the checkbox can't be recognized as checkbox in Spy++
BS_CHECKBOX is not listed.
Code (compiled as 32bit):
TEST_METHOD(ShouldCheckStyle) {
auto styleOfButton = ::GetWindowLongPtr((HWND)0x003F06E8, GWL_STYLE);
auto styleOfCheckbox = ::GetWindowLongPtr((HWND)0x01101642, GWL_STYLE);
auto bsOfButton = styleOfButton & BS_TYPEMASK;
auto bsOfCheckbox = styleOfCheckbox & BS_TYPEMASK;
auto resultOfButton = (bsOfButton == BS_CHECKBOX);
auto resultOfCheckbox = (bsOfCheckbox == BS_CHECKBOX);
auto debugger = 0;
}
Debug output
The code indicates they both have BS_OWNERDRAW. The above behaves the same for the button and the checkbox.
The weird thing is Window-Detective can recognize the style of checkbox. The code is same as I used above. Here's a piece of code:
Window* WindowManager::createWindow(HWND handle) {
WindowClass* windowClass = getWindowClassFor(handle);
String className = windowClass->getName().toLower();
if (className == "button") {
LONG typeStyle = GetWindowLong(handle, GWL_STYLE) & BS_TYPEMASK;
switch (typeStyle) {
case BS_CHECKBOX:
case BS_AUTOCHECKBOX:
case BS_3STATE:
case BS_AUTO3STATE: {
return new CheckBox(handle, windowClass);
}
case BS_RADIOBUTTON:
case BS_AUTORADIOBUTTON: {
return new RadioButton(handle, windowClass);
}
case BS_GROUPBOX: {
return new GroupBox(handle, windowClass);
}
default: {
// If none of the above is true, then the control is just a Button
return new Button(handle, windowClass);
}
}
}
After some discussion, you can use GetWindowText to get the text from each control and compare the specific text.
BS_CHECKBOX cannot be detected from the properties of the "checkbox" control beacuse of BS_OWNERDRAW.
Creates an owner-drawn button. The owner window receives a WM_DRAWITEM
message when a visual aspect of the button has changed. Do not combine
the BS_OWNERDRAW style with any other button styles.
Try the below code:
WCHAR str1[20];
WCHAR str2[] = L"Agree me";
GetWindowText(hwnd_checkbox, str1, 256);
if (_tcscmp(str1, str2) == 0)
{
//it is checkbox
}
else
{
//it isn't checkbox
}
After you get the correct control handle of the checkbox, you can use SendDlgItemMessage or SendMessage to send BM_SETCHECK check message.
SendMessage(hwnd_checkbox, BM_SETCHECK, BST_CHECKED, 0);

Invalidate() and child_wnd_name.InvalidateRect (&rect) cause different results

I am writing a dialog based application which has one static control, one button and two edit controls. The static's type is class MyStatic : public CStatic which has an OnPaint() defined as follows:
void MyStatic::OnPaint()
{
CPaintDC dc(this);
int Row = pBGDlg->iRow; //pBGDlg is a pointer to the dialog
int Column = pBGDlg->iColumn; //iRow and iColumn are variables of the two edit controls
int RowHei = pBGDlg->iRowHei;
int ColumnWid = pBGDlg->iColumnWid;
if (bBuPressed)
{
for (int i = 0; i <= Row; ++i)
{
dc.MoveTo (0, i * RowHei);
dc.LineTo (Column * ColumnWid, i * RowHei);
}
}
Whenever the user input two integers and press the button, the application will draw corresponding lines on the static
void CProjDlg::OnClickedIdpreview()
{
UpdateData ();
mystatic.GetClientRect (&rtStatic); //mystatic is the name of the static
iRowHei = (rtStatic.Height () - 1) / iRow;
iColumnWid = (rtStatic.Width () - 1) / iColumn;
mystatic.bBuPressed = true;
Invalidate ();
UpdateWindow ();
}
For Example:Input 4 to the 1st edit control and then input 1 to the 1st edit control. However, if I substitute mystatic.InvalidateRect(&rtStatic); and mystatic.UpdateWindow(); for Invalidate(); and UpdateWindow(); respectively, the application fails to erase previous results. For example: Input 4 to the 1st edit control and then input 5 to the 1st edit control. I can not figure out why the second method fails. Could anyone explain for me? Thank you very much.

Highlighting code window of visual studio based on line number using VSPackage

I'm building a VSpackage extension to create "VisualStudio Tool Window".
I have a grid inside tool window, consisting of numbers. If a user selects a particular row of the grid. That particular line of code should be highlighted.
To be more clear,
Suppose my grid contains:
row 1 - 10,
row 2 - 15,
row 3 - 14,
if user selects row 1, then 10th line in the code window should get highlighted.
Is this feature possible using VisualStudio package. I have a strong feeling that this is possible.Because most of the search results work that way.
Any help on the same is greatly appreciated!!
I finally found the answer to my post based on lot of googling. Hope this helps for others.
You need to use "TextSelection" class to highlight the above code line.
foreach (CodeElement codeElement in projectItem.FileCodeModel.CodeElements)// search for the function to be opened
{
// get the namespace elements
if (codeElement.Kind == vsCMElement.vsCMElementNamespace)
{
foreach (CodeElement namespaceElement in codeElement.Children)
{
// get the class elements
if (namespaceElement.Kind == vsCMElement.vsCMElementClass || namespaceElement.Kind == vsCMElement.vsCMElementInterface)
{
foreach (CodeElement classElement in namespaceElement.Children)
{
try
{
// get the function elements to highlight methods in code window
if (classElement.Kind == vsCMElement.vsCMElementFunction)
{
if (!string.IsNullOrEmpty(highlightString))
{
if (classElement.Name.Equals(highlightString, StringComparison.Ordinal))
{
classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);
classElement.StartPoint.TryToShow(vsPaneShowHow.vsPaneShowTop, null);
// get the text of the document
EnvDTE.TextSelection textSelection = window.Document.Selection as EnvDTE.TextSelection;
// now set the cursor to the beginning of the function
textSelection.MoveToPoint(classElement.StartPoint);
textSelection.SelectLine();
}
}
}
}
catch
{
}
}
}
}
}
}
You can also use simpler solution; see below
int lineNo = 3;
if (!isProjectItemOpen)
{
Window win = projectItem.Open();
win.Visible = true;
Document doc = win.Document;
doc.Activate();
var ts = dte.ActiveDocument.Selection;
ts.GotoLine(lineNo, true);
}

Firefox extension, opening a local file in a new foreground tab from menu

I am learning how to program Firefox extensions. I have created a new menu and when the menu item is clicked, I want a new tab to be opened, in the foreground, containing a local file contained within the contents directory.
For example:
MENU -> Item1
When Item1 is selected, I want a new tab to open in the foreground containing what is located in /myextension/content/content.html.
Where can I find out how to do this?
For clarity, I can get the local file to open in a new tab, I just do not know how to get to open in a new focused tab.
I use the following function to open a tab, make sure it hasn't already been opened and switch focus to it:
function OpenAndReuseOneTabPerURL(url)
{
var wm = Components.classes["#mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
var browserEnumerator = wm.getEnumerator("navigator:browser");
// Check each browser instance for our URL
var found = false;
while (!found && browserEnumerator.hasMoreElements())
{
var browserWin = browserEnumerator.getNext();
var tabbrowser = browserWin.gBrowser;
// Check each tab of this browser instance
var numTabs = tabbrowser.browsers.length;
for (var index = 0; index < numTabs; index++)
{
var currentBrowser = tabbrowser.getBrowserAtIndex(index);
if (url == currentBrowser.currentURI.spec)
{
// The URL is already opened. Select this tab.
tabbrowser.selectedTab = tabbrowser.tabContainer.childNodes[index];
// Focus *this* browser-window
browserWin.focus();
found = true;
break;
}
}
}
// Our URL isn't open. Open it now.
if (!found)
{
var recentWindow = wm.getMostRecentWindow("navigator:browser");
if (recentWindow) {
// Use an existing browser window
recentWindow.delayedOpenTab(url, null, null, null, null);
} else {
// No browser windows are open, so open a new one.
window.open(url);
}
}
}
Use it like:
OpenAndReuseOneTabPerURL("http://yoururl.com");

Resources