- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 C 构建一个 Sudoku Win32 应用程序。我猜我对 GetWindowText() 有一些问题,它在未定义的次数内正常工作,但随后应用程序卡住,没有挂起(进程的状态是正常,没有“app.exe 停止工作”),不能在编辑框中插入更多值,菜单不可点击,只有最小化和关闭按钮响应。我已经插入了一些 MessageBoxes,但在问题意外出现之前,一切似乎都正常。我已经尝试使用多个调试器、CLion(我正在使用的 IDE)、Codeblocks 甚至 Windows 集成调试器(Visual Studio)进行调试,但没有出现任何相关问题。我吓坏了。请帮助我,并在必要时向我询问更多详细信息,但我认为仅此而已。谢谢!
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <winuser.h>
#include <windowsx.h>
#include <scrnsave.h>
#define N 9
#define SRN 3
#define K 64 //Celle nascoste all'inizio
#define ID_STUFF_GO 9000
#define ID_MODE_NOHINT 4200
#define ID_MODE_HINT 4201
typedef struct{
int correctVal;
int insertedVal;
bool visible;
}cell;
cell puzzle[N][N];
HWND grid[N][N];
bool hint=true;
bool ready=false;
int count=0;
int CDECL MessageBoxPrintf(unsigned int , const char *, const char *, ...);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void sudokuInit(void);
void diagonalInit(void);
void boxInit(int, int);
bool fillMissing(int, int);
bool notInBox(int, int, int);
bool notInRow(int, int);
bool notInColumn(int, int);
bool isLegalValue(int, int, int);
void setVisibleValues(void);
void createFile(void);
bool checkValue(int, int);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
static char appName[]= TEXT("Sudoku");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
//------------------- Definizione finestra -------------------//
wndclass.style = CS_HREDRAW | CS_VREDRAW; //Definizione stili della finestra, in questo caso il contenuto viene centrato sia verticalmente che orizontalmente all'interno della finestra
wndclass.lpfnWndProc = WndProc; //Collega la procedura che gestira i messaggi provenienti dalla finestra
wndclass.cbClsExtra = 0; //Spazio extra per specifici scopi del programma
wndclass.cbWndExtra = 0; //Spazio extra per specifici scopi del programma
wndclass.hInstance = hInstance; //Imposta l'istanza che gestisce la finestra
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION); //Carica l'icona del programma
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW); //Carica il cursore del mouse da usare nel programma
wndclass.hbrBackground = CreateSolidBrush(RGB(63,81,181)); //Recupera un oggetto grafico, in questo caso un pennelo che colora lo sfondo
wndclass.lpszMenuName = appName; //Configura il menu della finestra
wndclass.lpszClassName = appName; //Definisce il nome della classe della finestra
//------------------------------------------------------------//
if(!RegisterClass (&wndclass)){
MessageBoxPrintf(MB_OK|MB_ICONERROR,appName,"Questo programma richiede Windows NT!");
return 0;
}
//------------------ Creazione finestra ----------------------//
hwnd = CreateWindow (appName, //nome della classe della finestra
TEXT("Sudoku Game"), //Titolo della finestra
WS_OVERLAPPED|WS_MINIMIZEBOX|WS_SYSMENU, //Stile della finestra
CW_USEDEFAULT, //Posizione orizontale iniziale
CW_USEDEFAULT, //Posizione verticale iniziale
525, //Larghezza iniziale
565, //ALtezza iniziale
NULL, //Gestore della finestra madre
NULL, //Gestore del menu della finestra
hInstance, //Gestore dell'istanza del programma
NULL); //Parametri di creazione
//------------------------------------------------------------//
ShowWindow(hwnd, nCmdShow); //Mostra la finestra sullo schermo
sudokuInit();
UpdateWindow(hwnd); //Ordina alla finestra di riempire(dipingere) se stessa
while(GetMessage(&msg, NULL, 0, 0)){ //Recupera i messaggi dalla coda dei messaggi
/*if(count==K){
MessageBoxPrintf(MB_ICONINFORMATION,appName,"Gioco terminato!");
SendMessage(hwnd, WM_DESTROY, MAKEWPARAM(FALSE, 0), MAKELPARAM(FALSE,0));
}*/
TranslateMessage(&msg); //Traduce gli eventi di input (tastiera/mouse) in messaggi
DispatchMessage(&msg); //Invia i messaggi alla procedura della finestra
}
return msg.wParam;
}
int CDECL MessageBoxPrintf(unsigned int type, const char *title, const char *content, ...){
char buffer[1024];
va_list pArgList;
va_start(pArgList, content);
_vsnprintf(buffer, sizeof(buffer)/ sizeof(char), content, pArgList);
va_end(pArgList);
return MessageBox(NULL, buffer, title, type);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
HFONT hfont = CreateFont(50,0,0,0,FW_SEMIBOLD,0,0,0,0,0,0,0,0,"Century Gothic");
HMENU hSubMenu, hMenu;
int i, j, k=10, m=10, n, id;
char out[2];
switch(message){
case WM_CREATE:
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_MODE_NOHINT, "Senza suggerimenti");
AppendMenu(hSubMenu, MF_STRING, ID_MODE_HINT, "Con suggerimenti");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "Modalita'");
ModifyMenu(hMenu, ID_MODE_HINT, MF_BYCOMMAND | MF_CHECKED, ID_MODE_HINT, "Con suggerimenti");
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_STUFF_GO, "Nope");
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "Difficolta'");
SetMenu(hwnd, hMenu);
for (i=0; i<N ; i++){
for (j=0; j<N; j++) {
id=i*10+j+1;
grid[i][j]= CreateWindow("EDIT",
"",
ES_CENTER | ES_NUMBER | WS_BORDER | WS_CHILD | WS_VISIBLE,
k, m, 50, 50,
hwnd, (HMENU)id, NULL, NULL);
SendMessage(grid[i][j], WM_SETFONT, (WPARAM)hfont, MAKELPARAM(FALSE, 0));
if((j+1)%3==0)
k+=60;
else
k+=55;
}
k=10;
if((i+1)%3==0)
m+=60;
else
m+=55;
}
return 0;
case WM_CTLCOLOREDIT:
hdc = (HDC) wParam;
return SetTextColor(hdc, RGB(63,81,181));
case WM_COMMAND:
switch(LOWORD(wParam)){
case ID_MODE_HINT:
hint=true;
ModifyMenu(hMenu, ID_MODE_HINT, MF_BYCOMMAND | MF_CHECKED, ID_MODE_HINT, "Con suggerimenti");
ModifyMenu(hMenu, ID_MODE_NOHINT, MF_BYCOMMAND | MF_UNCHECKED, ID_MODE_NOHINT, "Senza suggerimenti");
break;
case ID_MODE_NOHINT:
hint=false;
ModifyMenu(hMenu, ID_MODE_HINT, MF_BYCOMMAND | MF_UNCHECKED, ID_MODE_HINT, "Con suggerimenti");
ModifyMenu(hMenu, ID_MODE_NOHINT, MF_BYCOMMAND | MF_CHECKED, ID_MODE_NOHINT, "Senza suggerimenti");
break;
case ID_STUFF_GO:
MessageBoxPrintf(MB_ICONINFORMATION,"Test", "NOPE!");
break;
}
switch(HIWORD(wParam)){
case EN_CHANGE:
if(ready){
MessageBoxPrintf(MB_ICONEXCLAMATION,"Test", "Acquiring");
GetWindowText((HWND)lParam, out, 2);
UINT err= GetLastError();
n=atoi(out);
id=(int)GetMenu((HWND)lParam);
MessageBoxPrintf(MB_ICONEXCLAMATION,"Test", "Ready");
if(checkValue(n,id)){
if(hint)
MessageBoxPrintf(MB_ICONINFORMATION,"Test", "Giusto");
//SetTextColor(hdc, RGB(76,175,80));
count++;
}else
if(hint)
MessageBoxPrintf(MB_ICONSTOP,"Test", "Sbagliato");
//SetTextColor(hdc,RGB(244,67,54));
}
break;
}
return 0;
case WM_PAINT:
BeginPaint(hwnd, &ps); //Inizia a riempire(dipingere) la finestra
GetClientRect(hwnd, &rect); //Ottiene le dimensioni sullo schermo della finestra
for (i=0; i<N; i++){
for (j=0; j<N; j++){
if(puzzle[i][j].visible){
itoa(puzzle[i][j].correctVal, out,10);
Edit_SetText(grid[i][j], out);
SendMessage(grid[i][j],EM_SETREADONLY,(WPARAM)TRUE, MAKELPARAM(FALSE, 0));
}
SetTextColor(GetDC(hwnd), RGB(63,81,181));
}
}
ready=true;
EndPaint(hwnd, &ps); //Finisce di riempire(dipingere) la finestra
return 0;
case WM_DESTROY:
PostQuitMessage(0); //Inserisce un messaggio di uscita alla coda dei messaggi
return 0;
default:
return DefWindowProc (hwnd, message, wParam, lParam); //Esegue il processo predefinito per i messaggi
}
}
void sudokuInit(void){
srand(time(NULL));
diagonalInit();
fillMissing(0, SRN);
setVisibleValues();
createFile();
}
void diagonalInit(void){
int i;
for (i=0; i<N; i+=SRN)
boxInit(i, i);
}
void boxInit(int r, int c){
int i, j, num;
for (i=0; i<SRN; i++) {
for (j=0; j<SRN; j++) {
do{
num=rand()%N+1;
}while(!notInBox(r, c, num));
puzzle[r+i][c+j].correctVal=num;
puzzle[r+i][c+j].visible=true;
}
}
}
bool fillMissing(int i, int j){
int num;
if (j>=N && i<N-1){
i++;
j=0;
}
if (i>=N && j>=N)
return true;
if (i<SRN){
if (j<SRN)
j=SRN;
}
else if (i<N-SRN){
if (j==(i/SRN)*SRN)
j+=SRN;
}
else{
if (j==N-SRN){
i++;
j=0;
if (i>=N)
return true;
}
}
for (num=1; num<=N; num++){
if (isLegalValue(i, j, num)){
puzzle[i][j].correctVal=num;
puzzle[i][j].visible=true;
if (fillMissing(i, j+1))
return true;
puzzle[i][j].correctVal=0;
puzzle[i][j].visible=true;
}
}
return false;
}
bool isLegalValue(int i, int j, int num){
return notInRow(i, num) && notInColumn(j, num) && notInBox(i-i%SRN, j-j%SRN, num);
}
bool notInRow(int i, int num){
int j;
for (j=0; j<N; j++)
if (puzzle[i][j].correctVal==num)
return false;
return true;
}
bool notInColumn(int j, int num){
int i;
for (i=0; i<N; i++)
if (puzzle[i][j].correctVal==num)
return false;
return true;
}
bool notInBox(int rS, int cS, int num){
int i, j;
for (i=0; i<SRN; i++)
for (j=0; j<SRN; j++)
if (puzzle[rS+i][cS+j].correctVal==num)
return false;
return true;
}
void setVisibleValues(void){
int i, j, cellId, count=K;
while(count!=0){
cellId=rand()%(N*N)+1;
i=cellId/N;
j=cellId%N;
j=j-1;
if(puzzle[i][j].visible){
count--;
puzzle[i][j].visible=false;
}
}
}
void createFile(void){
FILE *file;
char fileName[MAX_PATH], prefix[]="sudoku";
time_t id = time(NULL);
int i, j;
sprintf(fileName, "%s-%ld.txt", prefix, id);
file=fopen(fileName,"w");
for (i=0; i<N; i++)
for (j=0; j<N; j++){
if(i!=0 || j!=0)
fprintf(file,",");
fprintf(file,"%d", puzzle[i][j].correctVal);
}
fclose(file);
}
bool checkValue(int n, int cell){
MessageBoxPrintf(MB_APPLMODAL,"Checking value", "Hey there!");
int i=0, j=0, op=0;
op=(int)cell/10;
switch(op){
case 0:
i=0;
break;
case 1:
i=1;
break;
case 2:
i=2;
break;
case 3:
i=3;
break;
case 4:
i=4;
break;
case 5:
i=5;
break;
case 6:
i=6;
break;
case 7:
i=7;
break;
case 8:
i=8;
break;
default:
MessageBoxPrintf(MB_ICONERROR, "Errore", "Impossibile determinare la riga");
break;
}
op=(int)(cell-1)%10;
switch(op){
case 0:
j=0;
break;
case 1:
j=1;
break;
case 2:
j=2;
break;
case 3:
j=3;
break;
case 4:
j=4;
break;
case 5:
j=5;
break;
case 6:
j=6;
break;
case 7:
j=7;
break;
case 8:
j=8;
break;
default:
MessageBoxPrintf(MB_ICONERROR, "Errore", "Impossibile determinare la colonna");
break;
}
puzzle[i][j].insertedVal=n;
MessageBoxPrintf(MB_APPLMODAL,"Checking value", "Detected!");
if(puzzle[i][j].insertedVal == puzzle[i][j].correctVal)
return true;
else
return false;
}
这是启动时的样子:
最佳答案
您的程序占用了所有可用资源。此行获取新的 DC
但从未将其发布到系统。
SetTextColor(GetDC(hwnd), RGB(63,81,181));
你应该删除这一行。设置颜色并立即忘记 HDC 没有任何意义。
我还会考虑将读取或修改编辑控件的代码从 WM_PAINT
移动到更好的位置。 WM_PAINT
应该只绘制主窗口内容。在您的情况下(背景是 ny 类画笔绘制的)它可以完全忽略。
关于c - Win32 (C) 应用程序在一些 GetWindowText 后卡住?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52245539/
我尝试了以下代码,但它没有从前景窗口检索文本! procedure TForm1.Button1Click(Sender: TObject); var title : pansichar;
我正在编写一个小应用程序,我希望从 Chrome 浏览器中获取 URL。 为了首先检查 Chrome 浏览器是否打开,我使用以下代码: IntPtr WindowTitleTextPtr = Get
我正在尝试学习如何使用 visual studio 为 C++ 制作 GUI。但是我在使用 GetWindowText() 函数时遇到了一些问题。它不会将 LPTSTR 标题更改为文本框中的文本,并且
我想显示对话框的标题: HWND hWnd = ::GetActiveWindow(); char cc[101]; ::GetWindowText(hWnd,cc,100); MessageBox(
我在全局 CBT Hook 过程中使用以下代码: TCHAR title[256]; int getT = GetWindowText(hWnd, title, 256); if (getT == 0
我想弄清楚 GetWindowText 背后的系统调用是什么。我编写了一个简单的程序来调用 GetWindowText,并使用不同进程中的窗口句柄。 int CALLBACK WinMain(HINS
HWND ForeWindow = GetForegroundWindow(); wchar_t WindowTxt[1024] = { L'\0' }; GetWindowTextW(ForeWin
我不太明白 GetWindowText 如何在与调用者相同的线程上为窗口工作。如果 GetWindowText 阻塞直到消息被处理,线程如何调用 DispatchMessage?它已在 GetWind
我试图在我的 Win32 C++ 应用程序的 dialog 中获取 textedit 控件的文本。我正在使用以下代码块来获取它,并对其进行测试。 HWND hCarRegNo = GetDlgItem
我正在尝试使用以下代码获取当前事件窗口的名称: HWND winHandle = GetActiveWindow(); wchar_t buffer[512] = L""; i
如何通过GetWindowText获取不同语言的文本?我修改了一个白板项目。当用户在白板上输入时,onchange 函数将被调用并将文本显示在白板上。但是,当我键入日语文本(非系统默认语言)时,它显示
主窗口上有两个项目和按钮的组合框。组合框: HWND hCombo; hCombo = CreateWindow(L"COMBOBOX", L"combobox", WS_CHILD |
我试图在 C++/Java 中从外部窗口列出并获取编辑控件的内容,不幸的是没有成功。 当我调用 GetWindowText 或 GetWindowTextA 时,它在编辑控件上返回一个空值,我知道 G
我是 Windows 编程的新手。我正在尝试检索窗口的名称。 char NewName[128]; GetWindowText(hwnd, NewName, 128); 我需要使用 char[] 但它
我有这样的代码: IntPtr hwndGetValue = new IntPtr(67904); List windows = GetChildWindows(hwndGetValue); int
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 8 年前。 Improve t
GetWindowText 函数的返回值为 documented as follows : If the function succeeds, the return value is the leng
我正在用 C 构建一个 Sudoku Win32 应用程序。我猜我对 GetWindowText() 有一些问题,它在未定义的次数内正常工作,但随后应用程序卡住,没有挂起(进程的状态是正常,没有“ap
我正在为我的“正在播放”插件获取 Spotify 的窗口标题,其功能为: GetWindowText(spotify_window_handle, title, title_length) 但输出包含
使用下面的代码 [DllImport("user32.dll", EntryPoint = "GetWindowText", ExactSpelling = false, CharSet =
我是一名优秀的程序员,十分优秀!