Daros911
Daros911

Reputation: 435

Creating a windows with WS_POPUP style

I'm implementing a simple program displaying an image on the screen using GDI+ WinAPI.

Here is the code I have so far:

#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>

using namespace Gdiplus;
#pragma comment (lib, "Gdiplus.lib")

void Example_DrawImage9(HDC hdc) {
  Graphics graphics(hdc);
  Image image(L"C:/test.bmp");
  graphics.DrawImage(&image, 0, 0);
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow) {
  ULONG_PTR token;
  GdiplusStartupInput input = { 0 };
  input.GdiplusVersion = 1;
  GdiplusStartup(&token, &input, NULL);
  const wchar_t CLASS_NAME[] = L"Sample Window Class";
  WNDCLASS wc = {};
  wc.lpfnWndProc = &WindowProc;
  wc.hInstance = hInstance; 
  wc.lpszClassName = CLASS_NAME;
  RegisterClass(&wc);

  HWND hwnd = CreateWindowEx(0, CLASS_NAME, L"Learn to Program Windows", WS_POPUP, 0, 0, 190, 110, NULL, NULL, hInstance, NULL);

  if (hwnd != NULL) {
    ShowWindow(hwnd, nCmdShow);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0) > 0)
    {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
  }

  GdiplusShutdown(token);
  return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  switch (uMsg)
  {
  case WM_DESTROY:
    PostQuitMessage(0);
    return 0;

  case WM_PAINT: {
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hwnd, &ps);
    FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));
    Example_DrawImage9(hdc);
    EndPaint(hwnd, &ps);
    return 0;
  }
  }

  return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

Because I'd like to show only the image itself (no Non-Client-Area) i'm using the WS_POPUP style for the created window. But there is apparently sth. wrong with the window - when i move the mouse over the app window i get "loading/busy" (animating blue circle). Moreover i cant' move the window. When i change the style to for example WS_CAPTION all is OK - the mouse cursor is "normal" (just arrow as usually) and i can move/drag the window. How can i use the WS_POPUP style without this bizarre side effects I've described?

Upvotes: 1

Views: 1080

Answers (1)

Dmytro Ovdiienko
Dmytro Ovdiienko

Reputation: 1156

Try set WNDCLASS::hCursor. That fixes the cursor issue.

    WNDCLASS wc = {};
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);  // <<<< HERE
    wc.lpfnWndProc = &WindowProc;
    wc.hInstance = hInstance; 
    //...

Found here.

Moreover i cant' move the window

To solve this problem, you have to implement the WM_NCHITTEST handler. See the link above.

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)  
{  
    HDC hdc;  
    PAINTSTRUCT ps;  
    HWND minimize_button, close_button, demo_button;  
    static int X, Y;  
    LRESULT move = NULL;  
    LPDRAWITEMSTRUCT pdis;  
    HICON hIcon;  
    RECT rc;  
    POINT pt;  

    switch (message)  
    {
    case WM_NCHITTEST:  
        GetCursorPos(&pt);  
        GetWindowRect(hwnd, &rc);  
        rc.bottom = rc.bottom - 466;  

        //if cursor position is within top layered drawn rectangle then  
        //set move to HTCAPTION for moving the window from its client  
        if (pt.x <= rc.right && pt.x >= rc.left 
                             && pt.y <= rc.bottom 
                             && pt.y >= rc.top)  
        {  
            move = DefWindowProc(hwnd, message, wParam, lParam);  
            if (move == HTCLIENT)  
            {  
                move = HTCAPTION;  
            }  
        }  

        return move;  
// ... 

Upvotes: 1

Related Questions