Error with displaying an .bmp image using mfc dialog - c++

I am trying to display a bitmap image using MFC application.
I am using a browse button to select file which is working properly. But when I try to load an image by double clicking on the file, the application is launched, but the image is not displayed.
Here is my code for browse button and function to open a double clicked image.
void COpenImageDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CString path;
CFileDialog dlg(TRUE);
int result=dlg.DoModal();
if(result==IDOK)
{
path=dlg.GetPathName();
UpdateData(FALSE);
}
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap bmp;
bmp.Attach(hBmp);
CClientDC dc(this);
CDC bmDC;
bmDC.CreateCompatibleDC(&dc);
CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
BITMAP bi;
bmp.GetBitmap(&bi);
dc.BitBlt(0,0,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
bmDC.SelectObject(pOldbmp);
}
void COpenImageDlg::OpenImage1(CString path)
{
//CString path;
CFileDialog dlg(TRUE);
int result=dlg.DoModal();
if(result==IDOK)
{
path=dlg.GetPathName();
UpdateData(FALSE);
}
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap bmp;
bmp.Attach(hBmp);
CClientDC dc(this);
CDC bmDC;
bmDC.CreateCompatibleDC(&dc);
CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
BITMAP bi;
bmp.GetBitmap(&bi);
dc.BitBlt(0,0,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
}
Init class :
`BOOL COpenImageApp::InitInstance()
{
// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles. Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
AfxEnableControlContainer();
// Create the shell manager, in case the dialog contains
// any shell tree view or shell list view controls.
CShellManager *pShellManager = new CShellManager;
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need
// Change the registry key under which our settings are stored
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
COpenImageDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
char* buff;
char* command_line = GetCommandLine();
buff = strchr(command_line, ' ');
buff++;
buff = strchr(buff, ' ');
buff++;
buff = strchr(buff, ' ');
buff++;
if (buff != NULL)
{
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, "C:\Users\Raguvaran\Desktop\tiger.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap bmp;
bmp.Attach(hBmp);
dlg.RedrawWindow();
CClientDC dc(m_pMainWnd);
CDC bmDC;
bmDC.CreateCompatibleDC(&dc);
CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
BITMAP bi;
bmp.GetBitmap(&bi);
dc.BitBlt(0,0,bi.bmWidth,bi.bmHeight,&bmDC,0,0,SRCCOPY);
}
//RedrawWindow(dlg, NULL, NULL, RDW_INVALIDATE);
//UpdateWindow(dlg);
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
// Delete the shell manager created above.
if (pShellManager != NULL)
{
delete pShellManager;
}
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}`
I used the same code for browse button and it displays the image. But when I double click the file, the image is not displayed. Please tell me what I am doing wrong.

If you have associated your application with a particular file extension, it will be launched automatically when you double-click such a file (as you have said).
When this happens, your application is launched with the file name (actually the full path) supplied as a command line argument to your application.
In SDI MFC applications, this is handled automatically by the framework as long as you haven't overridden the default File/Open handling mechanism, but if you have a dialog-based application you will need to add code for this yourself.

Your dialog COpenImageDlg is created and displayed inside the call to DoModal before the command line has a chance to be processed. When the DoModal returns, the dialog is already destroyed, so there is no dialog for the code to draw upon.

I understand that when you double click the file to choose a image on file dialog, the image doesn't show. I just tried your code of function OnBnClickedButton1 and OpenImage1. And it turns out that the image is displayed when double click to choose a image. I use VS2010 on win7. I hope this will help you though i donot find the error of your code.

I found the answer to my question.
It was actually a very stupid mistake.
When I read the file address using Commandline, the address has single slash, whereas I need to pass the address using double slash.
Such a silly bug. Sorry to waste your time.

Related

Win32 Use a resource font inside the application

I have an application, and I imported some fonts into the resource.
Now, I want to use those resourced fonts inside the application, without installing them to the computer that runs it.
The way I want to use the font resources is that I want to set a label's font to the resourced font by sending WM_SETFONT message to it.
Generally, if the font is already installed on the computer I would use the following code:
HDC hdc = GetDC(hwnd);
//here hwnd is the handle to the window.
const TCHAR* fontName = TEXT("/* THE FONT NAME */");
//this is where I'd enter the font name, but it only works when the font is already installed on the computer.
const long nFontSize = NFONTSIZE(7);
//this is where I set the font size.
LOGFONT logFont = {0};
logFont.lfHeight = -MulDiv(nFontSize, GetDeviceCaps(hdc, LOGPIXELSY), 72);
logFont.lfWeight = FW_SEMIBOLD;
_tcscpy_s(logFont.lfFaceName, fontName);
HFONT font = CreateFontIndirect(&logFont); //get the font handle
as soon as I get the HFONT handle, it's no difficulty to send the WM_SETFONT message to the label with:
SendMessage(hwnd, WM_SETFONT, (WPARAM)font, static_cast<LPARAM>(MAKELONG(TRUE, 0)));
//here hwnd is the handle of the static label.
But now, I don't want to set the font by this way because this only works when the specified font is already installed on the computer. I have MY OWN font file with the .ttf format imported as resource. I want to set the label's font to THIS .ttf font.
Assuming you have a token IDF_MYFONT defined for the resource ID, then you can embed your font in the executable with a line like this in your .rc (or .rc2) script:
IDF_MYFONT BINARY "..\\MyFont.ttf" // Or whatever the path to your font file is.
You can load and lock the font resource using code like the following:
HANDLE hMyFont = INVALID_HANDLE_VALUE; // Here, we will (hopefully) get our font handle
HINSTANCE hInstance = ::GetModuleHandle(nullptr); // Or could even be a DLL's HINSTANCE
HRSRC hFntRes = FindResource(hInstance, MAKEINTRESOURCE(IDF_MYFONT), L"BINARY");
if (hFntRes) { // If we have found the resource ...
HGLOBAL hFntMem = LoadResource(hInstance, hFntRes); // Load it
if (hFntMem != nullptr) {
void* FntData = LockResource(hFntMem); // Lock it into accessible memory
DWORD nFonts = 0, len = SizeofResource(hInstance, ares);
hMyFont = AddFontMemResourceEx(FntData, len, nullptr, &nFonts); // Fake install font!
}
}
Then, when you've finished with the font, you can release it from memory like this:
RemoveFontMemResourceEx(hMyFont);
I've included some checks on the return values for the system calls, but you can add others. And you will need to be able to handle the cases where any of these fail (e.g. providing a default font).
While the font is loaded/locked in memory, you can use it as though it were installed on the system: for example, using its name in a LOGFONT structure:
LOGFONT MyLogFont = { -8, 0, 0, 0, 400, FALSE, FALSE, FALSE, ANSI_CHARSET,
OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY,
VARIABLE_PITCH | FF_SWISS, L"MyFontName" };
Feel free to ask for further clarification and/or explanation.
Funny thing, I was just dealing with this problem yesterday night...
To solve it, the values passed into FindResourceW must match the resource type:
const auto resourceHandle{ FindResourceW(nullptr, MAKEINTRESOURCE(IDR_FONT1), RT_FONT) };
const auto resourceLoad{ LoadResource(nullptr, resourceHandle) };
const auto pResource{ LockResource(resourceLoad) };
//do something with the resource pointer here...
FreeResource(resourceLoad);
This should give you a pointer to the resource, and you can then extract the font by creating a new file and write to it using WriteFile. To get the size of the resource, use SizeofResource.
Or you can create the font by passing in the resource pointer into AddFontMemResourceEx.

Why can't i set a thumbnail on the Taskbar using DwmSetIconicThumbnail?

I need your help for a project at my job.
The main software, using 4D language, works as a MDI, it creates many windows included in the main window. The main issue is that we have a lot of windows and we need an easy way to switch from a window to another.
We decided to create a little c++ plugin which is a dll to resolve this issue.
This plugin will create a tab on the taskbar for each window opened like Windows Explorer. The creation and the deletion of tabs already works.
But the current problem is that no thumbnail is set on the Tab.
The parameter given is the ID of the window from the main software. The method called PA_GetHWND is the method given by 4D to obtain the handle of the window using the windowID.
I have already check where is the problem. The bitmap created from the window already exists and is good. For this test, i put the bitmap in the clipboard and paste it on Paint and the Bitmap was good.
Here is the code introducing the method to refresh the bitmap on the tab.
bool CManageTaskBar::UpdateWindow(long WindowID)
{
HRESULT res;
HDC hdcScreen;
HDC hdcWindow;
HDC hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
//Get the Handle from 4D
HWND nHandle = (HWND)(PA_GetHWND((PA_WindowRef)(WindowID)));
// Retrieve the handle to a display device context for the client
// area of the window.
hdcScreen = GetDC(NULL);
hdcWindow = GetDC(nHandle);
// Create a compatible DC which is used in a BitBlt from the window DC
hdcMemDC = CreateCompatibleDC(hdcWindow);
// Get the client area for size calculation
RECT rcClient;
GetClientRect(nHandle, &rcClient);
// Create a compatible bitmap from the Window DC
hbmScreen = CreateCompatibleBitmap(hdcWindow,rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
// Select the compatible bitmap into the compatible memory DC.
SelectObject(hdcMemDC, hbmScreen);
// Bit block transfer into our compatible memory DC.
if (!BitBlt(hdcMemDC,
0, 0,
rcClient.right - rcClient.left, rcClient.bottom - rcClient.top,
hdcWindow,
0, 0,
SRCCOPY))
{
MessageBox(nHandle, L"BitBlt has failed", L"Failed", MB_OK);
//goto done;
}
ITaskbarList3* ptbl = NULL;
HRESULT hr = CoCreateInstance(my_CLSID_TaskbarList, NULL, CLSCTX_ALL, my_IID_ITaskbarList3, (LPVOID*)&ptbl);
BOOL fForceIconic = TRUE;
BOOL fHasIconic = TRUE;
res = DwmSetWindowAttribute(nHandle, DWMWA_FORCE_ICONIC_REPRESENTATION, &fForceIconic, sizeof(fForceIconic));
res = DwmSetWindowAttribute(nHandle, DWMWA_HAS_ICONIC_BITMAP, &fHasIconic, sizeof(fHasIconic));
if (hbmScreen)
{
res = DwmSetIconicThumbnail(nHandle, hbmScreen,0);//DWM_SIT_DISPLAYFRAME);
}
DeleteObject(hbmScreen);
DeleteObject(hdcMemDC);
ReleaseDC(NULL, hdcScreen);
ReleaseDC(nHandle, hdcWindow);
return true;
}
The calls to DwmSetWindowAttribute return Invalid Handle. This handle works to get a bitmap but not to set an attribute.
And the call to DwmSetIconicThumbnail returns E_INVALIDARG maybe because the handle given is wrong.
Why cannot I set an attribute to this handle and why the call to set the thumbnail returns E_INVALIDARG ?
Thanks to everyone who will take care of my problem.
It's my first question, be friendly please :)

Why did I fail to find window use parent specified in Spy++?

Imagine there is a web page with a <input type="file" />. I open this page in Firefox, and click on the file button. Then a dialog will popup.
I want to set the file name edit of the popup dialog programmatically in C++:
First I use Spy++ to check window class, the Firefox window and popup dialog properties in Spy++ look like:
Firefox window:
Handle: 001E013E
Caption: Table with objects - Mozilla Firefox
Class: MozillaWindowClass
Popup dialog:
Handle: 004508BE
Caption: File Upload
Class: #32770 (Dialog)
Spy++ also shows browser window is the parent of popup dialog.
My code looks like:
#include <Windows.h>
#include <stdio.h>
int main()
{
HWND hBrowser = FindWindow(L"MozillaWindowClass", NULL);
printf("Browser hwnd=%X\n", hBrowser);
HWND hDialog = FindWindowEx(hBrowser, NULL, L"#32770 (Dialog)", NULL);
printf("Dialog hwnd=%X\n", hDialog);
system("pause");
}
But the value of hBrowser does not equal the value in Spy++ dialog, and value of hDialog is NULL. I have only one Firefox window opened, with only one tab.
Then I tried to change my code to:
// 0x001E013E is the handle in Spy++
HWND hDialog = FindWindowEx((HWND)0x001E013E, NULL, L"#32770 (Dialog)", NULL);
hDialog still outputs as NULL.
Questions:
Why the handle in Spy++ and the one I get in program is not the same?
Does the "parent window" in Spy++ have the same meaning with the parent parameter in FindWindowEx?
Note: I can't use window title to do the find, due to localization issue (Firefox may be installed in languages other than English).
MozillaWindowClass is the owner of the open dialog, it is not a parent you can use with FindWindowEx. There can also be more than one MozillaWindowClass window so it is better to look for the dialog first:
BOOL CALLBACK FindMozillaOpenFilenameDialogEnumProc(HWND hWnd, LPARAM param)
{
HWND*pData = (HWND*) param;
if (GetClassLongPtr(hWnd, GCW_ATOM) == 32770) // Found a dialog?
{
HWND hOwner = GetWindow(hWnd, GW_OWNER);
if (hOwner)
{
WCHAR buf[100];
GetClassName(hOwner, buf, 100);
if (0 == lstrcmp(buf, TEXT("MozillaWindowClass"))) // The dialog is owned by a Mozilla window?
{
HWND hCombo = GetDlgItem(hWnd, 0x047c); // cmb13
GetClassName(hCombo, buf, 100);
buf[8] = '\0'; // Some Windows versions use ComboBox and some use ComboBoxEx32, we only care if it is some type of combobox
if (0 == lstrcmp(buf, TEXT("ComboBox"))) // The dialog contains a ComboBox with the expected ID?
{
*pData = hWnd;
return false;
}
}
}
}
return true;
}
int main()
{
HWND hDialog = NULL;
EnumWindows(FindMozillaOpenFilenameDialogEnumProc, (LPARAM) &hDialog);
printf("Dialog hwnd=%X\n", hDialog);
if (hDialog)
{
HWND hCombo = GetDlgItem(hDialog, 0x047c);
SendMessage(hCombo, WM_SETTEXT, 0, (LPARAM) TEXT("c:\\foo\\bar.exe")); // Could also use CDM_SETCONTROLTEXT?
}
return 0;
}
This code relies on undocumented and internal names and window relationships, it could break at any time.
Keep in mind that "MozillaWindowClass" is a internal Mozilla name and could change at any time. The documented cmb13 id of the filename control is only documented for GetOpenFileName and GetSaveFileName but not for IFileDialog based dialogs. You really should use UI Automation when you interact with the open dialog in another application!

Displaying a CBitmap on a CHtmlView

I am trying to draw on top of a CHtmlView. I tried overriding OnDraw() but I end up with either some Unhandled exception or nothing being drawn. Any tips or recommendations? Our documents contain a .bmp zipped inside the document and I am trying to display those images on to the CHtmlView.
Here's what I started on (reference: http://forums.codeguru.com/showthread.php?255739-Bitmaps-in-CHtmlView), just trying to use the pen to see if anything comes up:
void CMyHtmlView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
ASSERT_VALID(pDoc);
IOleWindow* InterfacePtr;
HWND hwnd;
// long pointer to Idispatch interface
LPDISPATCH DispatchPtr;
// receives pointer to Idispatch
DispatchPtr = GetHtmlDocument();
// InterfacePtr now contains IOleWindow*
DispatchPtr->QueryInterface(IID_IOleWindow, (void**)&InterfacePtr);
// get the window handle
InterfacePtr->GetWindow(&hwnd);
// get dc from hwnd
CClientDC dc((CWnd*)hwnd);
CPen* Pen = new CPen;
Pen->CreatePen(PS_SOLID, 3 ,RGB(0, 0, 0));
dc.SelectObject(Pen);
dc.MoveTo(100,100);
dc.LineTo(300,300);
}
Setting the CClientDC may be part of the issue

how to load image from file using MFC

My browse button code is
void CFileOpenDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(TRUE);
int result=dlg.DoModal();
if(result==IDOK)
{
path=dlg.GetPathName();
UpdateData(FALSE);
}
}
and this is the code for loading an image from resource but that does not work for loading an image from file. i know LoadImage(); is used for this but how? how can i edit this code to load image from file. Plzz help.....
void CFileOpenDlg::OnBnClickedButton2()
{
// TODO: Add your control notification handler code here
CRect r;
CBitmap* m_bitmap;
CDC dc, *pDC;
BITMAP bmp;
m_bitmap = new CBitmap();
m_bitmap->LoadBitmapW(IDB_BITMAP1);
m_bitmap->GetBitmap(&bmp);
pDC = this->GetDC();
dc.CreateCompatibleDC(pDC);
dc.SelectObject(m_bitmap);
pDC->BitBlt(200, 200, bmp.bmWidth, bmp.bmHeight, &dc,0 , 0, SRCCOPY);
m_bitmap->DeleteObject();
m_bitmap->Detach();
}
MSDN LoadImage
HANDLE hBitMap = ::LoadImage(0, "c:\\mybmp.bmp",IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
CBitmap bmp;
bmp.Attach((HBITMAP)hBitMap);
If you want to open .JPG, .PNG ... eventually you can use CImage (is a shared class between MFC and ATL)
CImage image;
image.Load ( "picture.jpg" );
image.Draw ( pDC , 200, 200 );