Thread: simple noob question: HWND, HDC etc.

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    22

    simple noob question: HWND, HDC etc.

    I've been learning winprogramming for several days now but I still haven't found out what EXACTLY are HWND,HBITMAP,HDC etc. I understand that theyre some sort of handles, but what type? No description of them on MSDN.
    The problem arose when I wanted to find out information about a bitmap that I had attached to HBITMAP thingy.
    Eventually I will probably have to write code for saving BMPs from HBITMAPS, but first I would like to have clear idea whats the mechanism behind all this.

    To put it short: Can I access pixel and other data associated with my HBITMAP handle?

    Most of you are probably laughing on the floor reading this but I can't help being a noob :P

    Thanks for your time.
    Last edited by freedik; 08-18-2003 at 06:25 AM.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    handles are not like a pointer to a struct that you can access members on. All you need to know is that they are values that windows can use to associate your API call to a particular entity (bitmap, window, etc.) So if you want to get a destroy a window, you need an HWND to that window. DestroyWindow(hwnd);. Somewhere inside that DestroyWindow() call, windows knows which window you're talking about because of that handle.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Don't put yourself down, it's not going to get you more replies.

    Basically, a handle is a unique identifier that allows you to access various bits of data. So, all your HWNDs allow you to uniquely refer to your windows, all the HDCs allow you to uniquely refer to your device contexts (used for graphics and other stuff) and HBITMAPs refer to a bitmap stored somewhere in ram. Note that these are not pointers, that is, they don't contain the address of your object. These handles are most likely stored in a lookup table somewhere so that when you refer to them, all the API functions you use can find the data they refer to.

    Using the HBITMAP handle, you sure can retrieve that data. That stuff SHOULD be on MSDN. Look up GetObject(), that should get you started.

    EDIT: A good analogy of handles is the P.O. Box. I don't know if they have them in America, but in Australia, you can rent out a P.O. Box where mail is sent. That means that people won't know your home address. So, you send data using the handle, but you can't find out it's actual address, because it's famous or something, or in the witness protection program. Kapeesh?
    Last edited by bennyandthejets; 08-18-2003 at 06:53 AM.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    22
    That's more or less what I thought about it... In my own fuzzy noob way
    But this handle is still some kind of variable, such as int. Right?

    I guess it means that I can't get bitmap data from HBITMAP and save it to file and vice versa. Unless windows provides functions for doing so (it probably doesn't have at least one of them).
    Argh, why does every little step have to be so painful with win programming...

    Don't put yourself down, it's not going to get you more replies.
    It's embarassing to be totally incompetent. now you made me feel being embarassed for being embarassed. But I'm definitely not trying to get more replies this way :S

    Anyway, thanks for clearing this thing up for me.
    Last edited by freedik; 08-18-2003 at 06:56 AM.

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    But this handle is still some kind of variable, such as int. Right?
    Yep. More technically, try to decipher this line:

    Code:
    #define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
    I can't actually figure it out, but basically, a handle is a 32 bit struct of some sort.

    I guess it means that I can't get bitmap data from HBITMAP and save it to file and vice versa
    Didn't you read my post?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I guess it means that I can't get bitmap data from HBITMAP and save it to file and vice versa. Unless windows provides functions for doing so
    that's correct. But there are lots of functions for getting info about the object.
    But this handle is still some kind of variable, such as int. Right?
    yes, they are usually, but not always void *. A 32 bit pointer value that you can't really treat as a pointer.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Parallelism is ruining this thread. We should all stop posting for at least a second.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    we're posting the same thing, maybe we should wait a minute to post.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>I guess it means that I can't get bitmap data from HBITMAP and save it to file and vice versa.

    You can convert a HBITMAP to a bmp file (harder as you have to convert from screen display depths or colour depth) and also convert from a bmp file to a HBITMAP (this is very easy as there are functions available).

    How to convert a HBITMAP to a .bmp file

    PS all of these data types are HANDLE's. Think of them as just a way to hold your windows objects.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Bitmap objects (with HBITMAP handles) are device-dependent in that its data can vary with the display device, such as the image's layout. Lately, the information in the bitmap object is highly variable, and GDI queries the driver a lot to interpret the information.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here is what my book says:

    A handle is a 32-bit integer value which indentifies an object of some kind.

    The variable hWnd of type HWND is a 32-bit integer handle to a window.

    The type HDC defines what is called a display context, or more generally a device context. A device context provides the link between the device-independent Windows API functions for outputting information to the screen or a printer, and the device drivers which support writing to the specific devices attached to your PC. You can also regard a device context as a token of authority which is handed to you on request from Windows and grants you permission to output some information. Without a device context, you simply can't generate any output.

    (Ivor Horton's Beginning Visual C++ 6)


    From MSDN:

    HBITMAP Handle to a bitmap.
    Last edited by 7stud; 08-19-2003 at 01:30 AM.

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    22
    Thanks novacain for pointing out this example, but yesterday I already managed to create a function that saves HBITMAPs to BMPs. Now I'm trying to figure out how to do opposite action

    Dang, I wish I could still have access to physical pixel array. Using GetPixel and SetPixel seems extremely inefficient for most bitmap manipulations. Currently I'm trying to compare 2 bitmaps to see how many points they have same color, using direct memory access instead of GetPixel would increase speed around 100 times i think, lol. But since I'm still a noob, I better stick to traditional ways of programming for now.

    Still one question puzzles me... Let's say I create a BITMAP, which holds some data. Since it is a bitmap after all, doesn't it have its handle somewhere in Windows? Just curious....

    *edit*
    The variable hWnd of type HWND is a 32-bit integer handle to a window.
    That's the answer I was looking for at first place 3 days ago when I started winprogramming, I tried to print HWND handles, but didn't know what type they are, nor if they are regular variables at all. Why did I want to do that? Because I didn't know that theres a program called SPY

    Thanks all, this thread has been quite enlightening.
    Last edited by freedik; 08-19-2003 at 01:45 AM.

  13. #13
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    BMP -> HBITMAP

    Code:
    HBITMAP     hImage=NULL;
    char            sImage[256]={0};
    
    sprintf(sImage,".\\Images\\MyBitmap.bmp");
    hImage=(HBITMAP)LoadImage(hInstance, sImage, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); //load from file
    
    //or if included in the exe as a resource
    
    hImage=(HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(ID_MYBITMAP) , IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR ); //load resource
    
    if(!hImage)//error
              iError=GetLastError();
    >>Dang, I wish I could still have access to physical pixel array.

    Looked at GetDIBits() ?

    or load with LR_CREATEDIBSECTION
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to fill an ellipse
    By Ducky in forum Windows Programming
    Replies: 4
    Last Post: 08-30-2008, 09:05 AM
  2. sprite
    By lilhawk2892 in forum Game Programming
    Replies: 6
    Last Post: 10-18-2005, 07:36 PM
  3. My first "real" windows app
    By JoshR in forum Windows Programming
    Replies: 2
    Last Post: 07-28-2005, 07:40 AM
  4. simple noob question
    By NiVaG in forum Linux Programming
    Replies: 5
    Last Post: 09-28-2004, 06:56 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM