Jump to content
  • Advertisement
Sign in to follow this  
ClaudeFleming

Windows API Programming: program compiles but mouse icon is laggy

This topic is 2549 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I'm pretty desperate here. I have created a halfway decent program that is short but useful. It loads a text file and displays the file contents line-by-line to a window. The problem is that my mouse icon, when in the window, after the window is displayed, displays a hollow circle that keeps spinning, as if the program is not finished performing a task yet. And when I hover over the edges of the window, so that I can resize them, and then move the mouse into the white area of the window where you should not have the horizontal or vertical resize mouse icons, they still appear there. I'm very sorry for posting this long code, but I really am getting into windows programming. I'm tired of not knowing the proper way to do things, :(

#ifndef UNICODE
#define UNICODE
#endif

#include <windows.h>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
// Register the window class.
const wchar_t CLASS_NAME[] = L"Sample Window Class";

WNDCLASS wc = {};

wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;

RegisterClass(&wc);

// Create the window.
HWND hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
L"Learn to Program Windows", // Window text
WS_OVERLAPPEDWINDOW, // Window style

// Size and position
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);

if (hwnd == NULL)
{
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

// Run the message loop.
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static vector <wstring> strings;
static int cxChar, cxCaps, cyChar;
HDC hdc;
TEXTMETRIC tm;
PAINTSTRUCT ps;
RECT rect;


switch (msg)
{
case WM_CREATE:
{
wifstream junkFile("C:\\Users\\Lee\\Documents\\junk.txt");
if (!junkFile)
{
MessageBox(NULL, L"Couldn't open file", L"Error", MB_OK);
return 1;
}

std::wstring myString;

getline(junkFile, myString);
strings.push_back(myString);

while (!junkFile.eof())
{
getline(junkFile, myString);
strings.push_back(myString);
}
junkFile.close();

hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
cxChar = tm.tmAveCharWidth;
cxCaps = (tm.tmPitchAndFamily & 1? 3: 2) * cxChar /2;
cyChar = tm.tmHeight + tm.tmExternalLeading;
ReleaseDC(hwnd, hdc);

return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
FillRect(hdc, &rect, (HBRUSH) (COLOR_WINDOW + 1));

wstring temp;

for (int i = 0; i < (int)strings.size(); i++)
{
temp = strings;

TextOut(hdc, 0, cyChar*i, temp.c_str(), temp.size());

}
EndPaint(hwnd, &ps);
return 0;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}

Share this post


Link to post
Share on other sites
Advertisement

Did you try setting the hCursor member of your WNDCLASS?

SiCrane you are a god in my eyes. I don't know if that's the problem, but you are an extremely helpful member on these forums!

Thank you so, so, much.

In truth, I just initialized the window class the short and sloppy way because I was being lazy and was following the guide on MSDN.

Share this post


Link to post
Share on other sites
From MSDN:

hCursor
Type: HCURSOR

A handle to the class cursor. This member must be a handle to a cursor resource. If this member is NULL, an application must explicitly set the cursor shape whenever the mouse moves into the application's window.

There's no end to learning, I tell you. I'll be a beginner forever :0)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!