GetDC question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mark

    #1

    GetDC question

    I need to use GetDC but I don't know how to find a parameter to pas to the
    function. This is the GetDc from MSVC help:

    **********
    GetDC

    The GetDC function retrieves a handle to a display device context for the
    client area of a specified window or for the entire screen. You can use the
    returned handle in subsequent GDI functions to draw in the device context.

    The GetDCEx function is an extension to GetDC, which gives an application
    more control over how and whether clipping occurs in the client area.

    HDC GetDC(
    HWND hWnd // handle to a window
    );

    Parameters
    hWnd
    Handle to the window whose device context is to be retrieved. If this value
    is NULL, GetDC retrieves the device context for the entire screen.

    ***********

    HWND hWnd // handle to a window -- THIS IS MY PROBLEM.

    Where or how can I find it ?

    I'm working in a global function and I havent any hWnd of any type.


  • Tamas Demjen

    #2
    Re: GetDC question

    mark wrote:[color=blue]
    > HWND hWnd // handle to a window -- THIS IS MY PROBLEM.
    >
    > Where or how can I find it ?
    >
    > I'm working in a global function and I havent any hWnd of any type.[/color]

    It depends what you want to do:

    1. GetDC(0) gets a DC to the screen. Don't forget to call ReleaseDC.
    2. CreateCompatibl eDC(0) creates a new DC that's compatible with the
    screen's bit depth. This is what you usually do for rendering off-screen
    bitmaps or something like that. Don't forget to call DeleteDC.
    3. CreateDC to create a DC for printers. Use DeleteDC in the end.
    4. If you paint somewhere in a widget, the DC is usually provided by the
    GUI toolkit.

    Perhaps your function should be generic enough to work with any DC, and
    thus the DC should be a parameter. Be careful, because DCs may change
    during the lifetime of an application. Avoid storing a DC permanently,
    always obtain it right before painting.

    You have to provide more specific details if you need more help, and
    maybe post your question to a graphics or GDI related group.

    Tom

    Comment

    • mark

      #3
      Re: GetDC question



      "Tamas Demjen" wrote:
      [color=blue]
      > mark wrote:[color=green]
      > > HWND hWnd // handle to a window -- THIS IS MY PROBLEM.
      > >
      > > Where or how can I find it ?
      > >
      > > I'm working in a global function and I havent any hWnd of any type.[/color]
      >
      > It depends what you want to do:
      >
      > 1. GetDC(0) gets a DC to the screen. Don't forget to call ReleaseDC.
      > 2. CreateCompatibl eDC(0) creates a new DC that's compatible with the
      > screen's bit depth. This is what you usually do for rendering off-screen
      > bitmaps or something like that. Don't forget to call DeleteDC.
      > 3. CreateDC to create a DC for printers. Use DeleteDC in the end.
      > 4. If you paint somewhere in a widget, the DC is usually provided by the
      > GUI toolkit.
      >
      > Perhaps your function should be generic enough to work with any DC, and
      > thus the DC should be a parameter. Be careful, because DCs may change
      > during the lifetime of an application. Avoid storing a DC permanently,
      > always obtain it right before painting.
      >
      > You have to provide more specific details if you need more help, and
      > maybe post your question to a graphics or GDI related group.
      >
      > Tom
      >[/color]

      Thank you Tom

      Ok.
      I need to create a DIBSection and below there is a piece of my code ( Idon't
      know if it is right).

      ***
      HBITMAP Bitmap;
      int Width, Height;
      HDC WindowDC, MemDC;
      BITMAPINFO BitmapInfo;
      unsigned char *Data;

      HWND hwnd = NULL;
      CRect rect;
      WindowDC = GetDC(hwnd);

      Width = image_width;
      Height = image_height;

      memset(&BitmapI nfo.bmiHeader, 0, sizeof(BitmapIn fo.bmiHeader));
      BitmapInfo.bmiH eader.biWidth = Width;
      BitmapInfo.bmiH eader.biHeight = -Height;
      BitmapInfo.bmiH eader.biSize = sizeof(BITMAPIN FOHEADER);
      BitmapInfo.bmiH eader.biPlanes = 1;
      BitmapInfo.bmiH eader.biBitCoun t = 24;
      BitmapInfo.bmiH eader.biCompres sion = BI_RGB;

      MemDC = CreateCompatibl eDC(WindowDC);
      Bitmap = CreateDIBSectio n(WindowDC, &BitmapInfo, DIB_RGB_COLORS, (void
      **)&Data, 0, 0);

      ***

      how you can see I'm using "hwnd = NULL" that is the full screen. I need only
      the window region of the application. What I have to do to put to hwnd the
      right value to do that?

      Comment

      • Tamas Demjen

        #4
        Re: GetDC question

        mark wrote:
        [color=blue]
        > MemDC = CreateCompatibl eDC(WindowDC);
        > Bitmap = CreateDIBSectio n(WindowDC, &BitmapInfo, DIB_RGB_COLORS, (void
        > **)&Data, 0, 0);[/color]

        I simply use WindowDC = 0, and it works just fine. The first parameter
        for CreateDIBSectio n is not used anyway, unless the 3rd parameter is
        DIB_PAL_COLORS. You're passing DIB_RGB_COLORS, which means WindowDC is
        irrelevant. I think you'll be fine with WindowDC = 0. If you're
        compatible with the screen, you're compatible with the current window.
        If you have concerns, you can always pass the current window handle to
        your function.

        Tom

        Comment

        • mark

          #5
          Re: GetDC question



          "Tamas Demjen" wrote:
          [color=blue]
          >If you have concerns, you can always pass the current window handle to
          > your function.
          >
          > Tom
          >[/color]

          The question is that i don't know how to obtain the current window handle

          Comment

          Working...