none
C++ PictureBox issues... Mobile OS 5 and 6

    Question

  • Working on one C++ project that contains common code for Windows Mobile OS 5 and Windows Mobile OS 6.

    Currently can place a PictureBox on a form and can add an Image to that PictureBox at design time.
    - Much easier to do the same things in C# and VB!

    I am using VIsual Studio 2005 C++ and have some questions regarding the PictureBox Control when running the program on the Mobile OS Device
    - Can't seem to find what I am looking to do... perhaps not looking in the correct places or the examples are very clear that have been found.

    Question 1. How can I resize a PictureBox on a Form?
    When adding the PictureBox on the form, there are NO resize options.

    Question 2. How can I load an image from a File into the PictureBox?

    Question 3. How to Load an "in memory" image into the PictureBox?

    Odd that of the Samples that I have seen in Mobile OS 5 or Mobile OS 6 none of them seem to address anything related to Pictures on a Form or Loading Images from a file. Perhaps I missed one that has the information that I'm looking to find.

    C# and Visual Basic are not options for this project! C# and VB have better functionality when it comes to design time and run time. There are a LOT of examples for them to do exactly what I need.

    Many of the Microsoft Pages that I have looked on show general information but no details when details are needed.

    Thanks for any helpful hints and links!
    Wednesday, September 02, 2009 9:33 PM

Answers

  • Hi Bill,

     

    Sorry about that.

    In a win32 smartdevice project,

    1)       Add

    #include <windows.h>

    2)

    In LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

    Function add two declarations

    HWND myBox;

         HBITMAP bitmap;

    3)       I modified WM_CREATE as below, you need to modify the picture path to yours

    case WM_CREATE:

                SHMENUBARINFO mbi;

                memset(&mbi, 0, sizeof (SHMENUBARINFO));

                mbi.cbSize     = sizeof (SHMENUBARINFO);

                mbi.hwndParent = hWnd;

                mbi.nToolBarId = IDR_MENU;

                mbi.hInstRes   = g_hInst;

                if (!SHCreateMenuBar(&mbi))

                {

                    g_hWndMenuBar = NULL;

                }

                else

                 {

                    g_hWndMenuBar = mbi.hwndMB;

                }

                // Initialize the shell activate info structure

                memset(&s_sai, 0, sizeof (s_sai));

                s_sai.cbSize = sizeof (s_sai);

             // Create Picturebox

             myBox = CreateWindowEx(0, L"STATIC" , L"Picture Box" ,

               SS_BITMAP | WS_CHILD | WS_VISIBLE,

             10,10,100,100,hWnd,(HMENU)-1,NULL,NULL

         );

         //load from file first, you need to place your bmp file under temp directory

         bitmap = SHLoadImageFile(L"\\Temp\\aaa.bmp" );

         //put bitmap on static control

         SendMessage(myBox,STM_SETIMAGE, (WPARAM)IMAGE_BITMAP,(LPARAM)bitmap);

              break ;

    Let me know if there is further issue.

     

    Cheers,

    Zhe Zhao

    • Marked as answer by ZHE ZHAO Wednesday, September 09, 2009 7:39 AM
    Wednesday, September 09, 2009 2:18 AM

All replies

  • Let me clarify this slightly. Need to do the 3 steps above at Runtime.
    Thursday, September 03, 2009 4:15 PM
  • Hmmm... Doesn't anyone have any ideas?

    Even tried to use a "Custom Control" to see how to get this done.

    Did find this article:
    http://www.codeproject.com/KB/mobile/LightweightImageViewer.aspx?msg=3185884#xx3185884xx
    - Seems to have something close to what I need except it puts the image on the whole form not on a PictureBox.

    Still fighting to get the code I need.

    Sadly this could have done this extremely easy for Mobile OS 5 or Mobile OS 6 using Visual Studio 2005 for C# or Visual Basic.

    It is sad that at the beginning you have to fight for every line of code in C++. This makes it very hard for those learning or relearning C++.

    Oh well... The search continues...

    * Suggestion for Windows Mobile OS 6.5 SDK and beyond: Add at least 1 real world example of doing things like loading pictures from a file and allowing Picture Boxes to be resized on a C++ application.
    Friday, September 04, 2009 7:26 PM
  • Hi Bill,

     

    You can create a static control with SS_BITMAP style

    HWND hWndPictureBox = CreateWindowEx( 0,

                                          L"Static",

                                          L"Picture Box",

                                          WS_CHILD | WS_VISIBLE | SS_BITMAP,

                                          0,0, 100,100,

                                          hWndParent,

                                          (HMENU) 10001, // control ID

                                          hInstance,

                                          NULL ); 

    // Use the SHLoadImage function from shell to get a bitmap handle

    // or use any function that gets you a bitmap handle 

    HBITMAP hBmp = SHLoadImage(L"Image.bmp"); 

    // set the image to the static control

    SendMessage( hWndPictureBox, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hBmp);

    You may be interested in control style:

    http://msdn.microsoft.com/en-us/library/aa923260.aspx

    Regards,

    Zhe Zhao

     

    Monday, September 07, 2009 2:44 AM

  • Zhe,

    When I compile in Visual Studio 2005 with the code from your post in a sample program, these errors show up:

    • .\PictureBox-TestDlg.cpp(107) : error C2065: 'hWndParent' : undeclared identifier
    • .\PictureBox-TestDlg.cpp(109) : error C2065: 'hInstance' : undeclared identifier
    • .\PictureBox-TestDlg.cpp(116) : error C3861: 'SHLoadImage': identifier not found
    • .\PictureBox-TestDlg.cpp(119) : error C2660: 'CWnd::SendMessageW' : function does not take 4 arguments

    If "SHLoadImageFile" is used in place of "SHLoadImage" then the error on line 115 goes away.

    If I add these above the code sample you supplied:

    HWND hWndParent = NULL; // Not sure if this is correct!
    
    HINSTANCE hInstance = GetModuleHandle(NULL); // Not sure if this is correct!
    
    

    Then we reduce it to only this error:
    • .\PictureBox-TestDlg.cpp(119) : error C2660: 'CWnd::SendMessageW' : function does not take 4 arguments

    Intellisense shows only 3 Parameters needed for "SendMessage" Function.

    What if any additional Header Files are needed?


    Currently using:
    #include "stdafx.h"

    This almost addresses Question 2 in the original post IF we can get the image into the New PictureBox (at runtime) that will be placed on the form.

    Thanks for the code sample... At least it seems like a step in the right direction.
    • Edited by BillNew Tuesday, September 08, 2009 7:46 PM syntax corrected...
    Tuesday, September 08, 2009 7:44 PM
  • Hi Bill,

     

    Sorry about that.

    In a win32 smartdevice project,

    1)       Add

    #include <windows.h>

    2)

    In LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

    Function add two declarations

    HWND myBox;

         HBITMAP bitmap;

    3)       I modified WM_CREATE as below, you need to modify the picture path to yours

    case WM_CREATE:

                SHMENUBARINFO mbi;

                memset(&mbi, 0, sizeof (SHMENUBARINFO));

                mbi.cbSize     = sizeof (SHMENUBARINFO);

                mbi.hwndParent = hWnd;

                mbi.nToolBarId = IDR_MENU;

                mbi.hInstRes   = g_hInst;

                if (!SHCreateMenuBar(&mbi))

                {

                    g_hWndMenuBar = NULL;

                }

                else

                 {

                    g_hWndMenuBar = mbi.hwndMB;

                }

                // Initialize the shell activate info structure

                memset(&s_sai, 0, sizeof (s_sai));

                s_sai.cbSize = sizeof (s_sai);

             // Create Picturebox

             myBox = CreateWindowEx(0, L"STATIC" , L"Picture Box" ,

               SS_BITMAP | WS_CHILD | WS_VISIBLE,

             10,10,100,100,hWnd,(HMENU)-1,NULL,NULL

         );

         //load from file first, you need to place your bmp file under temp directory

         bitmap = SHLoadImageFile(L"\\Temp\\aaa.bmp" );

         //put bitmap on static control

         SendMessage(myBox,STM_SETIMAGE, (WPARAM)IMAGE_BITMAP,(LPARAM)bitmap);

              break ;

    Let me know if there is further issue.

     

    Cheers,

    Zhe Zhao

    • Marked as answer by ZHE ZHAO Wednesday, September 09, 2009 7:39 AM
    Wednesday, September 09, 2009 2:18 AM
  • Zhe,

    Will try the Project type you suggested to get the test done and prove that it can be done.

    In the long run I need a MAIN Form on the project!

    The existing project that needs this PictureBox has a Main Form and seems to be using ATL. I'll let you know how things go after testing the sample code.

    Thanks.

    ---

    Test Results: Sample Code works.

    Picture that is being loaded into the PictureBox is 480 by 640 pixels.

    I set the size of the picturebox to something like 160 by 120 pixels for testing.

    The Picture gets loaded into the PictureBox and the PictureBox gets resized beyond the size of the form. I'll need to force the picture to stretch to the size of the PictureBox instead of automatically sizing the PictureBox to the picture size.
    - Does this require using BltBit?

    ...
    • Edited by BillNew Wednesday, September 09, 2009 5:30 PM Updated Results...
    Wednesday, September 09, 2009 4:38 PM