gpt4 book ai didi

c - GLFW 如何拖动未修饰的窗口?

转载 作者:行者123 更新时间:2023-12-04 02:17:31 30 4
gpt4 key购买 nike

我一直在试图让我的窗口可拖动。

这是我的代码,

#include <stdio.h>
#include <math.h>
#include <GL/gl.h>
#include "include/GLFW/glfw3.h"

void cursor_position_callback(GLFWwindow* window, double x, double y);
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);

int cp_x;
int cp_y;
int wrel_cpx;
int wrel_cpy;
int w_posx;
int w_posy;
int buttonEvent;

int main(){
glfwInit();
glfwWindowHint(GLFW_DECORATED, 0);
GLFWwindow *window = glfwCreateWindow(640, 480, "Undecorated Resizable", 0, 0);

int w_width;
int w_height;
int ccp_x;
int ccp_y;

glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);

glfwMakeContextCurrent(window);


while(!glfwWindowShouldClose(window)){
if(buttonEvent == 1){
glfwSetWindowPos(window, wrel_cpx - cp_x, wrel_cpy - cp_y);
}

glfwSwapBuffers(window);

glfwWaitEvents();
}

return 0;
}

void cursor_position_callback(GLFWwindow* window, double x, double y){
glfwGetWindowPos(window, &w_posx, &w_posy);
wrel_cpx = cp_x + w_posx;
wrel_cpy = cp_y + w_posy;
cp_x = floor(x);
cp_y = floor(y);
}

void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
buttonEvent = 1;
}
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE){
buttonEvent = 0;
}
}

当我运行我的程序并尝试拖动窗口时,我的窗口的位置将保持其位置并且完全不执行任何操作。

如果我改变 wrel_cpx - cp_x类似于 wrel_cpx + cp_x ,我的 window 疯狂地移动。

有人可以帮帮我吗?

最佳答案

也许答案对某人有用。
拖动未修饰窗口的正确代码:

#include <stdio.h>
#include <math.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>

void cursor_position_callback(GLFWwindow* window, double x, double y);
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);

int cp_x;
int cp_y;
int offset_cpx;
int offset_cpy;
int w_posx;
int w_posy;
int buttonEvent;

int main(){
glfwInit();
glfwWindowHint(GLFW_DECORATED, 0);
GLFWwindow *window = glfwCreateWindow(640, 480, "Undecorated Resizable", 0, 0);

int w_width;
int w_height;
int ccp_x;
int ccp_y;

glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);

glfwMakeContextCurrent(window);


while(!glfwWindowShouldClose(window)){
if(buttonEvent == 1){
glfwGetWindowPos(window, &w_posx, &w_posy);
glfwSetWindowPos(window, w_posx + offset_cpx, w_posy + offset_cpy);
offset_cpx = 0;
offset_cpy = 0;
cp_x += offset_cpx;
cp_y += offset_cpy;

}

glfwSwapBuffers(window);

glfwWaitEvents();
}

return 0;
}

void cursor_position_callback(GLFWwindow* window, double x, double y){
if (buttonEvent == 1) {
offset_cpx = x - cp_x;
offset_cpy = y - cp_y;
}
}

void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
buttonEvent = 1;
double x, y;
glfwGetCursorPos(window, &x, &y);
cp_x = floor(x);
cp_y = floor(y);
}
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE){
buttonEvent = 0;
cp_x = 0;
cp_y = 0;
}
}

关于c - GLFW 如何拖动未修饰的窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33052651/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com